[issue23217] help() function incorrectly captures comment preceding a nested function

Gwenlliana report at bugs.python.org
Sun Jan 18 21:22:15 CET 2015


Gwenlliana added the comment:

The capture actually worked correctly. It seems to be caused by the artifacts introduced by the evaluation function decorators.

Decorator lists for functions are compiled to a series of high-order function applications to the original function, followed by an assignment which stores the resulting function object to the name of the original function. A decompilation of the sample code might be something like this:

  3           0 LOAD_CONST               0 (<code object outer at ...>)
              3 MAKE_FUNCTION            0
              6 STORE_NAME               0 (outer)

 10           9 LOAD_NAME                0 (outer)
             12 LOAD_CONST               1 (<code object f ...>)
             15 MAKE_FUNCTION            0
             18 CALL_FUNCTION            1
             21 STORE_NAME               1 (f)
             24 LOAD_CONST               2 (None)
             27 RETURN_VALUE        

It works in the same way with `f = outer(lambda: None)`. Thus evaluating `f.__name__` or `f.func_name` would actually gives the __name__ property of that inner function object, thus `help(f)` actually queries the help information of function inner.

Similarly, lots of properties of the original function object f would be shadowed by the decoration process, including the __doc__ property. You'll find documenting f takes no effect on the value of `f.__doc__`:

    @outer
    def f():
    """ The docstring of function f """
        return

>>> help(f)
Help on function inner in module tmp:

inner()
    #comment

----------
nosy: +Gwenlliana

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23217>
_______________________________________


More information about the Python-bugs-list mailing list