Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest-qt is not printing the cause of exceptions on python 3 #215

Closed
fabioz opened this issue May 29, 2018 · 2 comments
Closed

pytest-qt is not printing the cause of exceptions on python 3 #215

fabioz opened this issue May 29, 2018 · 2 comments
Labels

Comments

@fabioz
Copy link
Contributor

fabioz commented May 29, 2018

Consider the code below: The line Error 1 should appear in the stderr, but only Error 2 appears.

This happens because exceptions.format_captured_exceptions is a custom exception printer that doesn't take into account the exception cause.

from pytestqt.qt_compat import qt_api

class MyWidget(qt_api.QWidget):

    def method(self):
        raise RuntimeError('Error 1')

    def event(self, ev):
        called.append(1)
        try:
            self.method()
        finally:
            raise RuntimeError('Error 2')


weak_ref = None
called = []


def test_1(qapp):
    global weak_ref
    w = MyWidget()
    qapp.postEvent(w, qt_api.QEvent(qt_api.QEvent.User))
    qapp.processEvents()
    assert len(called) > 0
    w.deleteLater()
@fabioz
Copy link
Contributor Author

fabioz commented May 29, 2018

Changing format_captured_exceptions to the code below seems to fix it for me.

def format_captured_exceptions(exceptions):
    """
    Formats exceptions given as (type, value, traceback) into a string
    suitable to display as a test failure.
    """
    if sys.version_info[0] <= 2:
        message = 'Qt exceptions in virtual methods:\n'
        message += '_' * 80 + '\n'
        for (exc_type, value, tback) in exceptions:
            message += ''.join(traceback.format_tb(tback)) + '\n'
            message += '%s: %s\n' % (exc_type.__name__, value)
            message += '_' * 80 + '\n'
        return message
    else:
        from io import StringIO
        message = StringIO()
        message.write('Qt exceptions in virtual methods:\n')
        message.write('_' * 80 + '\n')
        for (exc_type, value, tback) in exceptions:
            traceback.print_exception(exc_type, value, tback, file=message)
            message.write('_' * 80 + '\n')
        return message.getvalue()

@nicoddemus
Copy link
Member

Thanks for the report and sample code @fabioz! I plan to tackle this soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants