views:

152

answers:

4

I've just repackaged my program. Previously all modules lived under the "whyteboard" package, with a "fakewidgets" package containing a bunch of dummy GUI test objects.

Now, all my modules are in packages, e.g. whyteboard.gui, whyteboard.misc, whyteboard.test - which is where fakewidgets now lives.

Now, when running my tests, I get an exception,

  File "/home/steve/Documents/whyteboard/whyteboard/gui/canvas.py", line 77, in __init__
    wx.ScrolledWindow.__init__(self, tab, style=wx.NO_FULL_REPAINT_ON_RESIZE | wx.CLIP_CHILDREN)
TypeError: unbound method __init__() must be called with ScrolledWindow instance as first argument (got Canvas instance instead)

here's the class in question

class Canvas(wx.ScrolledWindow):
    def __init__(self, tab, gui, area):
        wx.ScrolledWindow.__init__(self, tab, style=wx.NO_FULL_REPAINT_ON_RESIZE | wx.CLIP_CHILDREN)

However, my program loads and runs correctly, except from unit tests. The code is the same, just the code for my tests' imports are different to pull in from the new packages.

Before:

import os
import wx

import fakewidgets
import gui
import lib.mock as mock

from canvas import Canvas, RIGHT, DIAGONAL, BOTTOM
from fakewidgets.core import Bitmap, Event, Colour

from lib.configobj import ConfigObj
from lib.pubsub import pub
from lib.validate import Validator

now:

import os
import wx

import whyteboard.test
import whyteboard.gui.frame as gui

from whyteboard.lib import ConfigObj, mock, pub, Validator
from whyteboard.gui.canvas import Canvas, RIGHT, DIAGONAL, BOTTOM
from whyteboard.test.fakewidgets.core import Bitmap, Event, Colour, PySimpleApp

It may be worth noting that the fakewidgets package does some trickery into make my program think it's using wxPython classes, even though they're mocks. This is from a module that's imported by whyteboard.test.fakewidgets' __init__

class Window(object):
    def __init__(self, parent, *args, **kwds):
        self.parent = parent
        self.Enabled = True
        self.calls = []
        self.size = (0, 0)
        self.captured = False

    def GetClientSizeTuple(self):
        return (0, 0)
        self.captured = True

    def GetId(self):
        pass

    def Fit(self):
        pass

    def SetFocus(self):
        pass

    def PrepareDC(self, dc):
        pass

    def Destroy(self):
        pass

...


class ScrolledWindow(Window):
    def SetVirtualSize(self, *size):
        pass

    def SetVirtualSizeHints(self, *size):
        pass

import wx
wx.__dict__.update(locals())
+1  A: 

The code is the same, just the code for my tests' imports are different to pull in from the new packages

That sounds as if your imports are importing something you did not expect. Once I named one my files the same as a system module. It took me hours to figure out what went wrong.

See what happens when you change sys.path.

nate c
None of my files conflict - (except "test" can be imported from any python, so I tried renaming it to "tests"). Still no difference. I played around with sys.path too, to no avail
Steven Sproat
+1  A: 

Please print wx and wx.ScrolledWindow both before the definition of class Canvas and as the first line of Canvas.__init__. I strongly suspect that these will be different.

Are you doing any trickery with __new__ or metaclasses or such?

bukzor
I did try to do so but it seems that "nosetests" doesn't print out any output that's not in a test case.
Steven Sproat
+3  A: 

when you import whyteboard.test, does that automatically run whyteboard.test.fakewidgets.core? I think the problem is that Canvas is being created before the mocking code runs. This explains the switchup.

>>> import wx
>>> class Test1(wx.Window):
...    pass
... 
>>> wx.Window = object
>>> class Test2(wx.Window):
...    pass
... 
>>> dir(Test1)[:10]
['AcceleratorTable', 'AcceptsFocus', 'AcceptsFocusFromKeyboard', 
 'AddChild', 'AddPendingEvent', 'AdjustForLayoutDirection', 
 'AssociateHandle', 'AutoLayout', 'BackgroundColour', 'BackgroundStyle']
>>> dir(Test2)[:10]
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', 
 '__getattribute__', '__hash__', '__init__', '__module__', '__new__']

In the old file that you posted, fakewidgets was imported before canvas.

If that doesn't work, place this code directly after import wx before any other imports:

import inspect

class DummyMeta(type):
    def __new__(meta, clsname, bases, clsdict):
        if clsname == 'Canvas':
            lineno = inspect.stack()[1][2]
            print "creating Canvas with mro: {0}".format(inspect.getmro(bases[0]))
            print "file:{0}:{1}".format(__file__, lineno)
        return super(DummyMeta, meta).__new__(meta, clsname, bases, clsdict)

class ScrolledWindowDummy(wx.Window):
    __metaclass__ = DummyMeta

wx.ScrolledWindow = ScrolledWindowDummy

This will show that a Canvas class is being created before mocking is in place and will give you the file and line number where this occurs. essentially, for MRO, you shouldn't see anything from wx. If I am wrong, then you wont see anything at all because you will have replaced the ScrolledWindowDummy with a class that doesn't have type DummyMeta before any class named 'Canvas' is created.

aaronasterling
thanks for the feedback, I'll investigate into it after work today. I was wondering if my package's imports were interfering somehow.
Steven Sproat
Yes, after importing the "fakewidgets" package inside my test package's __init__ seemed be breaking things. Removing the import from there and moving the import for fakewidgets in the test to be the final import seems to have fixed it for now. I'm suspecting there may be a bug in the Mock framework I'm using, doesn't seem to handle nested calls too well.
Steven Sproat
@Steven I think what you have is backwards. You want the fake widgets to be imported _before_ canvas so that canvas only accesses the mocks. If it made the bug go away though, it appears there might be a bug in the framework. Did you run that metaclass I gave you? That should get you to the bottom of it in about five minutes.
aaronasterling
+1  A: 

Make sure fakewidgets is the first to import the wx module, This means order of imports is important, e.g.

import fakewidgets
import wx

Alternatively, in stead of the dict.update trick, replace names explicitly, i.e.

import wx

wx.Window = Window
# for all other relevant widgets

Again, still make sure fakewidgets is the first to access the wx module.

Ivo van der Wijk