NSObject, doctest

Erik van Blokland <[email protected]> Sat, 7 Dec 2013 21:33:33 +0100
Newsgroups gmane.comp.python.pyobjc.devel
Message-ID <[email protected]>
Hey,

I have a small problem writing doctests for a module with a NSObject subclass. 
Here's a small test:

from AppKit import *
"""
An odd interaction between doctest and an NSObject subclass.
"""
class MyClass(NSObject):
   def __new__(cls, a, b):
       self = cls.alloc().init()
       self.a = a
       self.b = b
       return self

   def doSomething(self):
       return a+b

def _test():
   def testOne():
       """
       Test one
>>> m = MyClass(1,1)
>>> m.doSomething()
       2
       """
   import doctest
   doctest.testmod()

_test()

Here's the result, using Python 2.7.2 (default, Oct 11 2012, 20:14:37) and objc 2.3.2a0.

> <class 'NSAppleEventManagerSuspensionID'>
> Traceback (most recent call last):
>  File "/Users/erik/Desktop/docTestTest.py", line 53, in <module>
>    _test()
>  File "/Users/erik/Desktop/docTestTest.py", line 51, in _test
>    doctest.testmod()
>  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py", line 1850, in testmod
>    for test in finder.find(m, name, globs=globs, extraglobs=extraglobs):
>  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py", line 865, in find
>    self._find(tests, obj, name, module, source_lines, globs, {})
>  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py", line 917, in _find
>    self._from_module(module, val)):
>  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py", line 880, in _from_module
>    elif inspect.getmodule(object) is not None:
>  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 486, in getmodule
>    file = getabsfile(object, _filename)
>  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 469, in getabsfile
>    _filename = getsourcefile(object) or getfile(object)
>  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 447, in getsourcefile
>    filename = getfile(object)
>  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 408, in getfile
>    object = sys.modules.get(object.__module__)
> AttributeError: __module__

Is there something in the nature of pyobjc objects that makes them confuse doctest?
Thanks!
Erik
------------------------------------------------------------------------------
Sponsored by Intel(R) XDK 
Develop, test and display web and hybrid apps with a single code base.
Download it for free now!
http://pubads.g.doubleclick.net/gampad/clk?id=111408631&iu=/4140/ostg.clktrk