Skip to content

All exceptions are swallowed when looking up __set_name__ on an object in a class namespace during construction of that class #112453

Closed as not planned
@AlexWaygood

Description

@AlexWaygood

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 of Foo:

>>> class Vanilla:
...     def __set_name__(self, owner, name):
...         print('set_name called')
...
>>> class Foo:
...     attr = Vanilla()
...
set_name called

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:

>>> class Meta(type):
...     def __getattribute__(self, attr):
...         if attr == "__set_name__":
...             raise SystemExit('NO')
...         return object.__getattribute__(self, attr)
...
>>> class Problematic(metaclass=Meta): pass
...
>>> try:
...     Problematic.__set_name__
... except SystemExit:
...     print('exception was raised')
...
exception was raised
>>> class Bar:
...     attr = Problematic()
...
>>>

__set_name__, like all dunders, is looked up on the Problematic class object itself, rather than the instance of Problematic found in the class dictionary of Foo, so SystemExit should be raised during the creation of the Bar class when the interpreter tries to determine whether or not the Problematic class has a __set_name__ attribute. Instead, however, the SystemExit is silently swallowed.

I think the issue lies in this section of code here:

cpython/Objects/typeobject.c

Lines 9989 to 9992 in d44ee42

if (set_name == NULL) {
if (PyErr_Occurred()) {
goto error;
}

I'm interested in working on this.

CPython versions tested on:

3.8, CPython main branch

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions