[issue1108] Problem with doctest and decorated functions

2007-09-05 Thread Daniel Larsson

New submission from Daniel Larsson:

Seems like doctest won't recognize functions inside the module under
test are actually in that module, if the function is decorated by a
decorator that wraps the function in an externally defined function,
such as in this silly example:

# decorator.py
import functools

def simplelog(f):
@functools.wraps(f)
def new_f(*args, **kwds):
print "Wrapper calling func"
return f(*args, **kwds)
return new_f

# test.py
from decorator import simplelog

@simplelog
def test():
"""
This test should fail, since the decorator prints output.
Seems I don't get called though
>>> test()
'works!'
"""
return "works!"

if __name__ == '__main__':
import doctest
doctest.testmod()

--

The problem lies in DocTestFinder._from_module, which checks if the
function's func_globals attribute is the same as the module's __dict__
attribute.

I'd propose to do the __module__/inspect.getmodule() checks (aren't they
 both checking the same thing btw?) before the inspect.isfunction check.

--
components: Library (Lib)
messages: 55660
nosy: danilo
severity: normal
status: open
title: Problem with doctest and decorated functions
type: behavior
versions: Python 2.5

__
Tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue1108>
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1108] Problem with doctest and decorated functions

2007-09-06 Thread Daniel Larsson

Daniel Larsson added the comment:

Here's a patch that alters the order of checks in DocTestFinder._from_module

__
Tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue1108>
__--- doctest.py.orig	2007-09-05 17:14:55.0 +0200
+++ doctest.py	2007-09-05 17:10:23.0 +0200
@@ -835,12 +835,12 @@
 """
 if module is None:
 return True
+elif inspect.getmodule(object) is not None:
+return module is inspect.getmodule(object)
 elif inspect.isfunction(object):
 return module.__dict__ is object.func_globals
 elif inspect.isclass(object):
 return module.__name__ == object.__module__
-elif inspect.getmodule(object) is not None:
-return module is inspect.getmodule(object)
 elif hasattr(object, '__module__'):
 return module.__name__ == object.__module__
 elif isinstance(object, property):
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com