doctest and decorators

Daniel Larsson daniel.j.larsson at gmail.com
Wed Sep 5 06:20:44 EDT 2007


The __module__ attribute is set, but the problem is the test in
doctest.py(DocTestFinder._from_module)

        ...
        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)

On 9/5/07, Michele Simionato <michele.simionato at gmail.com> wrote:
>
> > En Tue, 04 Sep 2007 19:29:11 -0300, Daniel Larsson
> > <daniel.j.lars... at gmail.com> escribi?:
> >
> >
> >
> > > On 9/5/07, Ferenczi Viktor <pyt... at cx.hu> wrote:
> >
> > >> > > @functools.wraps(f)
> > >> > > Pass the function to be wrapped by the decorator to the wraps
> > >> function.
> > >> > Ooops, right. That doesn't change the fact that decorated functions
> > >> get
> > >> > hidden from doctest though.
> >
> > > I have no issue when the decorator is defined in the same module as
> the
> > > decorated function, my problem is running doctests on functions using
> an
> > > imported decorator. Having to implement the decorator in every source
> > > module
> > > isn't very practical.
>
> I cannot reproduce your problem. Using functools.wraps
> the __module__ attribute is set correctly and everything
> works, even for decorators defined in separated modules.
> Care to post a complete example of what you are doing?


inspect.isfunction(object) returns true, but the function's func_globals
isn't the same as the module's __dict__, since the function is actually my
decorator wrapper function.

Here's my two files again:

# 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()



         Michele Simionato
>
> P.S. for some reason your messages are not appearing on
> Google groups, I see only the replies.
>
>
Weird... afraid I have no clue why not :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070905/7fb3c8cc/attachment.html>


More information about the Python-list mailing list