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.

classification
Title: Unexpected exception when calling function_proxy.__class__.__call__(function_proxy)
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.1, Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Crash with custom __getattribute__
View: 9756
Assigned To: Nosy List: DasIch, Trundle, vstinner
Priority: normal Keywords: patch

Created on 2011-01-17 02:10 by DasIch, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
check_is_func.diff Trundle, 2011-01-17 02:50 review
Messages (3)
msg126389 - (view) Author: Daniel Neuhäuser (DasIch) Date: 2011-01-17 02:10
Upon trying to create a proxy I stumbled upon this exception:

Traceback (most recent call last):
  File "foo.py", line 11, in <module>
    p.__class__.__call__(p)
SystemError: PyEval_EvalCodeEx: NULL globals

Investigating further led me to this code which reproduces the exception:

class Proxy(object):
    def __init__(self, proxied):
        self.proxied = proxied
    @property
    def __class__(self):
        return self.proxied.__class__
    def __call__(self):
        return self.proxied.__call__()

p = Proxy(lambda: 1)
p.__class__.__call__(p)

I understand that `p.__class__.__call__()` expects an argument of a different type however this is not obvious from the exception itself. PyPy on the other hand raises this exception:

Traceback (most recent call last):
  File "app_main.py", line 53, in run_toplevel
  File "foo.py", line 11, in <module>
    p.__class__.__call__(p)
TypeError: 'function' object expected, got 'Proxy' instead

Which explains the issue and is expected (at least by me) and apparently Jython raises an exception at least similar to PyPy's.
msg126391 - (view) Author: Andreas Stührk (Trundle) * Date: 2011-01-17 02:50
I think this is a duplicate of issue #9756: `methoddescr_call()` checks whether the given argument is acceptable as "self" argument and does so using `PyObject_IsInstance()`. As the class in the given code returns the type of the proxied object for the `__class__` attribute, that check will return true.

As a quick fix, the attached patch (against release27-maint branch) will raise a TypeError as expected by the OP, but the real issue is much broader.
msg134942 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-05-01 23:09
> I think this is a duplicate of issue #9756

Yes it is and the issue is now fixed. The example now fails with the correct exception:

Traceback (most recent call last):
  File " x.py", line 12, in <module>
    p.__class__.__call__(p)
TypeError: descriptor '__call__' requires a 'function' object but received a 'Proxy'
History
Date User Action Args
2022-04-11 14:57:11adminsetgithub: 55131
2011-05-01 23:09:24vstinnersetstatus: open -> closed

nosy: + vstinner
messages: + msg134942

superseder: Crash with custom __getattribute__
resolution: duplicate
2011-01-21 22:54:48terry.reedysetversions: - Python 2.6, Python 2.5
2011-01-17 02:50:11Trundlesetfiles: + check_is_func.diff

nosy: + Trundle
messages: + msg126391

keywords: + patch
2011-01-17 02:10:12DasIchcreate