This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mtahmed
Recipients mtahmed
Date 2013-12-11.23:27:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1386804440.21.0.0153040236788.issue19956@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2013-12-11 23:27:20mtahmedsetrecipients: + mtahmed
2013-12-11 23:27:20mtahmedsetmessageid: <1386804440.21.0.0153040236788.issue19956@psf.upfronthosting.co.za>
2013-12-11 23:27:20mtahmedlinkissue19956 messages
2013-12-11 23:27:19mtahmedcreate