Title: All exceptions are swallowed when looking up `__set_name__` on an object in a class namespace during construction of that class · Issue #112453 · python/cpython · GitHub
Open Graph Title: All exceptions are swallowed when looking up `__set_name__` on an object in a class namespace during construction of that class · Issue #112453 · python/cpython
X Title: All exceptions are swallowed when looking up `__set_name__` on an object in a class namespace during construction of that class · Issue #112453 · python/cpython
Description: 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...
Open Graph Description: 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 hav...
X Description: 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 hav...
Opengraph URL: https://github.com/python/cpython/issues/112453
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"All exceptions are swallowed when looking up `__set_name__` on an object in a class namespace during construction of that class","articleBody":"# Bug report\n\n### Bug description:\n\nDuring 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`:\n\n```pycon\n\u003e\u003e\u003e class Vanilla:\n... def __set_name__(self, owner, name):\n... print('set_name called')\n...\n\u003e\u003e\u003e class Foo:\n... attr = Vanilla()\n...\nset_name called\n```\n\nBut 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:\n\n```pycon\n\u003e\u003e\u003e class Meta(type):\n... def __getattribute__(self, attr):\n... if attr == \"__set_name__\":\n... raise SystemExit('NO')\n... return object.__getattribute__(self, attr)\n...\n\u003e\u003e\u003e class Problematic(metaclass=Meta): pass\n...\n\u003e\u003e\u003e try:\n... Problematic.__set_name__\n... except SystemExit:\n... print('exception was raised')\n...\nexception was raised\n\u003e\u003e\u003e class Bar:\n... attr = Problematic()\n...\n\u003e\u003e\u003e\n```\n\n`__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.\n\nI think the issue lies in this section of code here:\n\nhttps://github.com/python/cpython/blob/d44ee42cd7c9a40e1d7096b95476fe47156f571f/Objects/typeobject.c#L9989-L9992\n\nI'm interested in working on this.\n\n### CPython versions tested on:\n\n3.8, CPython main branch\n\n### Operating systems tested on:\n\nWindows","author":{"url":"https://github.com/AlexWaygood","@type":"Person","name":"AlexWaygood"},"datePublished":"2023-11-27T13:09:42.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/112453/cpython/issues/112453"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:288ce31b-5ab2-df21-b76a-422445898cec |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AF9E:2965D1:369795:4B20F5:6A59219A |
| html-safe-nonce | 2eaa59eadaeaae777015bb7aa9b8fe872650088e9c5b44ab9aa3c536f8093c6f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRjlFOjI5NjVEMTozNjk3OTU6NEIyMEY1OjZBNTkyMTlBIiwidmlzaXRvcl9pZCI6IjM4MzQ5MzMwNjI5NDY4NTczNzAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d86e2013078dc8772f81cb9a115a977eabfc5a94392858c094e1d0082ddc8630 |
| hovercard-subject-tag | issue:2012240099 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/112453/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c88cdb11e6a02515cd59164e8d79c45eabe05db03eaf92f8b4ecccec1d0ae5a3/python/cpython/issues/112453 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c88cdb11e6a02515cd59164e8d79c45eabe05db03eaf92f8b4ecccec1d0ae5a3/python/cpython/issues/112453 |
| og:image:alt | 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 hav... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | AlexWaygood |
| hostname | github.com |
| expected-hostname | github.com |
| None | 30abb2b48d3b1b476bbaf01f49ead8ef91062bfd762d3fa28a956f4c99653343 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f2e5b4fd472ea10ac216e358bbd0821a90dae8bb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width