wxPython and unittest - ConvertToBitmap seg fault

Y'all,

After upgrading to python 2.3.2, and wxPython 2.4.2.4 on a Redhat 8.x
system, I have run into a weird problem.

I had some unit tests that did nothing more than make sure that my gui
forms could be created. The form was created with wxDesigner, and all
the unit test does is make sure that the form can be instantiated. I
avoided showing the forms as I did not want to have to close the form
while running my test cases.

Now if you have ever created a dialog that uses bitmaps with wxDesigner,
you will end up with a section of code like the following...

def About( index ):
    if index == 0:
        return wxImage( "aboutbox_wdr/About_0.png", wxBITMAP_TYPE_PNG
).ConvertToBitmap()
    if index == 1:
        return wxImage( "aboutbox_wdr/About_1.png", wxBITMAP_TYPE_PNG
).ConvertToBitmap()
    return wxNullBitmap

This is the way in which wxDesigner loads the png and returns it to the
constructor for a static bitmap. The problem is that when running a
unit test such as the testCreateAboutBox test shown below. I get a
segmentation fault on the convertToBitmap function.

class AboutBox(unittest.TestCase):
    def Title(self):
        """Just here so we can get a title in the output
        makes it alot easier to see where any errors occurred."""
        print '\nAboutbox tests'
        
    def testCreateAboutbox(self):
        self.VERSION = 'TEST'
        self.BUILD_DATE = '111111'
        dlg = None
        dlg = modules.psm.aboutbox.AboutBox(None,
                                            -1,
                                            "About PGEF"
                                            ,wxPoint(20,20),
                                            wxSize(500,340))
        dlg.GetVersion().SetLabel(str(self.VERSION))
        dlg.GetBuildDate().SetLabel(str(self.BUILD_DATE))
        dlg.CentreOnParent()
        assert dlg != None, 'Couldnt create AboutBox'
    def suite():
        about_suite = unittest.TestSuite()
        about_suite.addTest(AboutBox("Title"))
        about_suite.addTest(AboutBox("testCreateAboutbox"))
        return about_suite
    def main():
        runner = unittest.TextTestRunner()
        runner.run(suite())

The interesting thing is that at the bottom of my dialog class, I also
have testing code built into the __main__ section. If I run the form
directly, all works as expected. It is only when the ConvertToBitmap is
run from within the unittest framework that the segmentation fault
occurs.

Has anyone else ran into this problem?

If anyone is interested, I can tar up my files and send them for
inspection.

Bryan Muir

Bryan Muir wrote:

The interesting thing is that at the bottom of my dialog class, I also
have testing code built into the __main__ section. If I run the form
directly, all works as expected. It is only when the ConvertToBitmap is
run from within the unittest framework that the segmentation fault
occurs.

You need to have an wxApp object created before you can create bitmaps, (and you probably should for other stuff too.) Change your test suite so it creates an app befor running the tests.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!