[New-bugs-announce] [issue19956] inspect.getsource(obj.foo) fails when foo is an injected method constructed from another method

Muhammad Tauqir Ahmad report at bugs.python.org
Thu Dec 12 00:27:20 CET 2013


New submission from Muhammad Tauqir Ahmad:

If a method `foo` of object instance `obj` is injected into it using a method from a different object instance, `inspect.getsource(obj.foo)` fails with the error message:

TypeError: <bound method Foo.say of <__main__.Foo object at 0x7fd348662cd0>> is not a module, class, method, function, traceback, frame, or code object

in inspect.py:getfile()

What basically is happening is that if you have `obj1`, `obj2` and `obj2` has method `foo`, setting `obj1.foo = types.MethodType(obj2.foo, obj1)` succeeds but is "double-bound-method" if that's a term. So during `getsource()`, it fails because `obj1.foo.__func__` is a method not a function as is expected.

Possible solutions:
1. Error message should be more clear if this is the intended behavior - right now it claims it's not a method,function etc. when it is indeed a method.
2. MethodType() should fail if the first argument is not a function.
3. inspect.getsource() should recursively keep "unpacking" till it finds an object that it can get the source for. 

Reproducer attached.

----------
components: Library (Lib)
files: reproducer.py
messages: 205942
nosy: mtahmed
priority: normal
severity: normal
status: open
title: inspect.getsource(obj.foo) fails when foo is an injected method constructed from another method
type: behavior
versions: Python 2.6, Python 2.7, Python 3.1, Python 3.2, Python 3.3
Added file: http://bugs.python.org/file33098/reproducer.py

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


More information about the New-bugs-announce mailing list