Python: Can't see the exception that

I am running a unit test and I understand that an exception is being thrown. However, I'm just not sure what exactly is being thrown.

from pt_hil.utilities.PT_HIL_Interface_Utils.widgets import PathPicker
import unittest
import wx

class TestUM(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print 'setUpClass called'
        cls.path_picker = PathPicker()
        print 'path_picker has been declared'

    def test_PathPicker(self):
        self.assertRaises(NotImplementedError, wx.UIActionSimulator.MouseClick(self.path_picker.browse))

if __name__ == '__main__':
    unittest.main()

      

PathPicker class:

class PathPicker(Widget):

    def __init__(self, parent=None, name="PathPicker"):
        print 'hi1'
        try:
            Widget.__init__(self, name, parent)
        except Exception as e:
            print 'hello'
            return logging.error(traceback.format_exc())
        print 'hi2'

      

the output I get when running the unit test is:

setUpClass called
hi1

Process finished with exit code 1

      

So clearly something is going wrong: Widget.__init__(self, name, parent)

but I don't see what it is. Is there a way to get this to print out what kind of exception or error is getting?

edit: here is the Widget class that should be matched with it:

class Widget(QWidget):
    def __init__(self, name, parent=None):
        print 'hey2'
        try:
            super(Widget, self).__init__()
        except BaseException as e:
            print 'hello'
            return logging.error(traceback.format_exc())
        print 'hey3'

      

now it gives me:

setUpClass called
hi1
hey2

Process finished with exit code 1

      

+3


source to share


2 answers


I needed to add app = QApplication(sys.argv)

and sys.exit(app.exec_())

to the script usingclass TestUM(unittest.TestCase):

to make the script above look like this:



from pt_hil.utilities.PT_HIL_Interface_Utils.widgets import PathPicker
import unittest
import wx

class TestUM(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print 'setUpClass called'
        cls.path_picker = PathPicker()
        print 'path_picker has been declared'

    def test_PathPicker(self):
        self.assertRaises(NotImplementedError, wx.UIActionSimulator.MouseClick(self.path_picker.browse))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    unittest.main()
    sys.exit(app.exec_())

      

Please note that this does not solve the problem I ran into to spare me the need (since no exception was visible). But that solved the problem and the script would work. Thank!

+1


source


As you can see here , the main exceptions in python (2.x) are:

BaseException
 +-- SystemExit
 +-- KeyboardInterrupt
 +-- GeneratorExit
 +-- Exception
      +-- StopIteration
      +-- StandardError
      ....

      

So, in your case, catching Exception, you are missing some other exceptions (rare exceptions, but it probably happens in your case): SystemExit, KeyboardInterrupt and GeneratorExit. Try changing your except clause:

except BaseException as e:

      

this way you are sure to catch all exceptions and define your problem.



EDIT:

BUT PyQT can be fun inside. As mentioned here :

In PyQt v5.5, an unhandled Python exception will result in a call to the Qts qFatal () function. By default, this will call the abort () function and the application will terminate. Please note that the application installed by the exception hook will still take priority.

So an exceptional exception (which can happen for many reasons in C ++ code, bad parameters ...) can quietly stop your application. However, the last part sounds useful, if you set the exception hook it will be called before silently canceling. Try adding an exception:

sys._excepthook = sys.excepthook # always save before overriding

def application_exception_hook(exctype, value, traceback):
    # Let try to write the problem
    print "Exctype : %s, value : %s traceback : %s"%(exctype, value, traceback)
    # Call the normal Exception hook after (this will probably abort application)
    sys._excepthook(exctype, value, traceback)
    sys.exit(1)

# Do not forget to our exception hook
sys.excepthook = application_exception_hook

      

+2


source







All Articles