[Python-Dev] doctest, exec and __module__

Fred Drake fdrake at acm.org
Wed Jun 25 21:37:28 CEST 2008


On Jun 25, 2008, at 2:45 PM, Martijn Faassen wrote:
> This places new key/value pairs into a dictionary, in this case  
> test.globs. Unfortunately when the execution results in a class  
> definition, it'll have its __module__ attribute set to  
> '__builtin__'. Try as I might, I couldn't convince exec to do it any  
> differently.

The zope.testing package has a way to work around this from setup/ 
teardown functions passed to the DocFileSuite (or whatever wrapper  
you're using).  See the module zope.testing.module for the functions.

Essentially, the __name__ needs to be set in test.globs; class  
construction uses the global value for __name__ to provide __module__;  
doctests normally don't have one, so it acquires the value from  
__builtin__.__name__ instead.  Which is, of course, horribly wrong.

The solution in zope.testing.module allows the class to be used with  
pickle within the test as well.


   -Fred

-- 
Fred Drake   <fdrake at acm.org>






More information about the Python-Dev mailing list