You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
inspect.signature doesn't always return a signature #66000
Now, according to the documentation, inspect.signature should return a signature or fail, but in this case it returns the actual object, which is misleading at least. This happens because _Method implements a proxy __getattr__, any accessed attribute becoming again a _Method. At the same time, inspect.signature happily uses
try:
obj.__signature__
except AttributeError:
...
to obtain the signature, if present.
The attached patch checks if __signature__ is an actual Signature object. I searched, but couldn't find any, if __signature__ can be anything, so I hope that this approach works.
This behaviour is indeed a bug. However, I think that the solution you propose is wrong.
If we ignore invalid contents of __signature__ we are masking a bug or incorrect behaviour. In this case, you should have checked the requested attribute name in '__getattr__', and return something other than _Method, if it is a '__signature__'.
Please find attached a patch, that checks if __signature__ is an instance of Signature class, and raises a TypeError if it isn't.
New changeset cc0f5d6ccb70 by Yury Selivanov in branch '3.4':
inspect: Validate that __signature__ is None or an instance of Signature. http://hg.python.org/cpython/rev/cc0f5d6ccb70
New changeset fa5b985f0920 by Yury Selivanov in branch 'default':
inspect: Validate that __signature__ is None or an instance of Signature. http://hg.python.org/cpython/rev/fa5b985f0920
Activity
PCManticore commentedon Jun 18, 2014
Hello. I noticed the following behaviour while working with xmlrpc proxy methods.
Now, according to the documentation, inspect.signature should return a signature or fail, but in this case it returns the actual object, which is misleading at least. This happens because _Method implements a proxy __getattr__, any accessed attribute becoming again a _Method. At the same time, inspect.signature happily uses
try:
obj.__signature__
except AttributeError:
...
to obtain the signature, if present.
The attached patch checks if __signature__ is an actual Signature object. I searched, but couldn't find any, if __signature__ can be anything, so I hope that this approach works.
1st1 commentedon Jun 20, 2014
This behaviour is indeed a bug. However, I think that the solution you propose is wrong.
If we ignore invalid contents of __signature__ we are masking a bug or incorrect behaviour. In this case, you should have checked the requested attribute name in '__getattr__', and return something other than _Method, if it is a '__signature__'.
Please find attached a patch, that checks if __signature__ is an instance of Signature class, and raises a TypeError if it isn't.
PCManticore commentedon Jun 20, 2014
Your patch looks good to me.
python-dev commentedon Jun 23, 2014
New changeset cc0f5d6ccb70 by Yury Selivanov in branch '3.4':
inspect: Validate that __signature__ is None or an instance of Signature.
http://hg.python.org/cpython/rev/cc0f5d6ccb70
New changeset fa5b985f0920 by Yury Selivanov in branch 'default':
inspect: Validate that __signature__ is None or an instance of Signature.
http://hg.python.org/cpython/rev/fa5b985f0920
1st1 commentedon Jun 23, 2014
Fixed in 3.4 and 3.5.
Thanks for the bug report!