Message205942
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. |
|
Date |
User |
Action |
Args |
2013-12-11 23:27:20 | mtahmed | set | recipients:
+ mtahmed |
2013-12-11 23:27:20 | mtahmed | set | messageid: <1386804440.21.0.0153040236788.issue19956@psf.upfronthosting.co.za> |
2013-12-11 23:27:20 | mtahmed | link | issue19956 messages |
2013-12-11 23:27:19 | mtahmed | create | |
|