New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
All exceptions are swallowed when looking up __set_name__
on an object in a class namespace during construction of that class
#112453
Comments
Or is the issue with the way Python looks up special names in general? It seems like there may be lots of other possible examples of this bug: >>> class Meta(type):
... def __getattribute__(self, attr):
... if attr in {"__complex__", "__enter__", "__iter__"}:
... raise SystemExit()
... return object.__getattribute__(self, attr)
...
>>> class Foo(metaclass=Meta): pass
...
>>> with Foo(): pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
with Foo(): pass
TypeError: 'Foo' object does not support the context manager protocol
>>> complex(Foo())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
complex(Foo())
TypeError: complex() first argument must be a string or a number, not 'Foo'
>>> iter(Foo())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
iter(Foo())
TypeError: 'Foo' object is not iterable
>>> Foo.__iter__ # raises SystemExit... @serhiy-storchaka, thoughts? |
In fact, it seems like this behaviour is documented in the data model: https://docs.python.org/3/reference/datamodel.html#special-method-lookup
So perhaps this is not a bug at all, @serhiy-storchaka? |
If it simply bypasses the But there is an old bug in Lines 4776 to 4783 in d44ee42
There should be an existing old issue for this. I once tried to solve it (or maybe similar issue in other place of this file), but I was not able to prove that the solution was correct, so it was not applied. If errors are ignored, it is because of technical limitations and the design flaws. It should not be reproduced in Python. |
It looks like that. |
Bug report
Bug description:
During construction of a class
Foo
, all objects in the class dictionary are checked to see if they have a__set_name__
attribute. If an object in the dictionary does have a__set_name__
attribute, it is called as part of the construction ofFoo
:But what if an object in a class namespace is an instance of class that has a metaclass that raises a non-
AttributeError
exception if__set_name__
is looked up on the class object? In this case, the exception is silently ignored when__set_name__
is looked up:__set_name__
, like all dunders, is looked up on theProblematic
class object itself, rather than the instance ofProblematic
found in the class dictionary ofFoo
, soSystemExit
should be raised during the creation of theBar
class when the interpreter tries to determine whether or not theProblematic
class has a__set_name__
attribute. Instead, however, theSystemExit
is silently swallowed.I think the issue lies in this section of code here:
cpython/Objects/typeobject.c
Lines 9989 to 9992 in d44ee42
I'm interested in working on this.
CPython versions tested on:
3.8, CPython main branch
Operating systems tested on:
Windows
The text was updated successfully, but these errors were encountered: