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

2020-05-17 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

Here is minimal reproducing code.

import types
import inspect

class A:
def say(self): print("A.say")
a = A()

class B: pass
b = B()
b.say = types.MethodType(a.say, b)

Let us examine MethodType first.  Calling 'b.say()' asks the previously 
neglected question: Is b.say a proper, sane callable?  I claim not. With 
3.9.0a6, the call fails with "TypeError: say() takes 1 positional argument but 
2 were given".  b.say() calls a.say(b), which calls A.say(a, b).  If A.say took 
another parameter, such as 'name', 'b.say()' might work, but only by accident.

Here is another buggy use of MethodType, 
b.A = types.MethodType(A, b)
b.A()
# TypeError: A() takes no arguments
Again, given 'def __init__(self, something)', b.A() might work, but only by 
accident.

types.MethodType is an example of "[This module] defines names for some object 
types that are used by the standard Python interpreter, but not exposed as 
builtins like int or str are."  The names are mainly intended to be used for 
isinstance checks and rarely, if at all, for object creation.

The MethodType entry lack a signature and only says "The type of methods of 
user-defined class instances."  Its docstring, "method(function, 
instance)\n\nCreate a bound instance method object." does have a signature.  
However, 'function' must be a function compatible with being passed 'instance' 
as the first argument.  This is false for both A and a.say; both result in 
buggy 'callables'.

MethodType checks that its first argument, assigned to the .__func__ attribute, 
is callable (has a '.__call__' attribute) but apparently does not check further.

As for getsource.  For b.A and b.say, it raises "TypeError: module, class, 
method, function, traceback, frame, or code object was expected, got {type}", 
where type is 'type' and 'method' respectively.  For both, the message is 
slightly confusing in that the function got something on the list (type=class). 
 For both, getsource could be patched to work with the buggy inputs.  I the 
latter is a bad idea.  Built-in functions usually fail with buggy inputs.  We 
should either improve the error message for methods or just close this.

--
nosy: +serhiy.storchaka

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2020-05-11 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

I am working on an explanation of why I paused the PR.

--
versions: +Python 3.9 -Python 3.5

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2020-05-10 Thread Furkan Onder


Furkan Onder  added the comment:

PR has been sent.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2020-05-10 Thread Furkan Onder


Change by Furkan Onder :


--
nosy: +furkanonder
nosy_count: 6.0 -> 7.0
pull_requests: +19335
stage: needs patch -> patch review
pull_request: https://github.com/python/cpython/pull/20025

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2017-06-18 Thread Terry J. Reedy

Terry J. Reedy added the comment:

Any review of the revised patch?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2015-04-14 Thread Usman Ehtesham Gul

Usman Ehtesham Gul added the comment:

Made changes based on David Murray's review comments including adding unit test 
on getfile.

--
Added file: http://bugs.python.org/file39022/issue_19956_1.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue19956
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2015-04-13 Thread R. David Murray

R. David Murray added the comment:

Thanks for the patch.  I've made some review comments on the tests.

--
nosy: +r.david.murray

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue19956
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2015-04-13 Thread Usman Ehtesham Gul

Usman Ehtesham Gul added the comment:

After discussing with Eric Snow, this case scenario is an edge case. The 
assumption in the inspect module is that __func__ for a MethodType object is a 
function. The MethodType should be used for functions and not methods.

Patch attached for this.

This needs to be documented in the docs (separate ticket will be opened for 
that).

--
keywords: +patch
nosy: +eric.snow, ueg1990
Added file: http://bugs.python.org/file38928/issue_19956.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue19956
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2013-12-13 Thread Terry J. Reedy

Terry J. Reedy added the comment:

I do not think you have exactly identified a bug, certainly not one we would 
fix in an existing release. The behavior in more of an unintended consequence 
of separate decisions resulting from an unanticipated usage. I am not sure 
what, if anything, should be done. Changing the exception message could still 
be done in 3.4, but not making getsource recursive for bound methods.

--
nosy: +terry.reedy
stage:  - needs patch
type: behavior - enhancement
versions: +Python 3.5 -Python 2.6, Python 2.7, Python 3.1, Python 3.2, Python 
3.3

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue19956
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2013-12-12 Thread Vajrasky Kok

Vajrasky Kok added the comment:

FYI, if you change:

setattr(b, 'say', types.MethodType(f.say, b))

to:

setattr(b, 'say', types.MethodType(Foo.say, b))

it will print the source correctly.

--
nosy: +vajrasky

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue19956
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2013-12-12 Thread Muhammad Tauqir Ahmad

Muhammad Tauqir Ahmad added the comment:

Yes I understand your change and other possible changes will fix the 
reproducer. I am already using a different workaround in my code.

The issue is about `inspect.getsource()` having incorrect behavior (or at least 
inaccurate error message) and MethodType able to be constructed from another 
method leading to strange undocumented behavior (as far as I can tell).

I do not know what the correct resolution is to this issue which is why I 
posted this here so someone can suggest/approve a resolution and I can submit a 
patch once something is decided.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue19956
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2013-12-11 Thread Muhammad Tauqir Ahmad

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 rep...@bugs.python.org
http://bugs.python.org/issue19956
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com