开发者

TypeError: unbound method __init__() .... during unit tests after re-packaging

开发者 https://www.devze.com 2023-01-10 06:15 出处:网络
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.

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, DIA开发者_如何学CGONAL, 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())


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.


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.


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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消