doctest and decorators

Ferenczi Viktor python at cx.hu
Wed Sep 5 00:09:46 CEST 2007


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

Run my test script (one file) with the -v (verbose) option. Without the -v 
option it does not show output. This fact is documented in the Python manual 
at the doctest module.

--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- 
import functools

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

@simplelog
def test():
    """
    >>> test()
    Wrapper calling func
    'works!'
    """
    return 'works!'

def fn():
    """
    >>> fn()
    'ok'
    """
    return 'ok'

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

Regard, Viktor



More information about the Python-list mailing list