Title: 3.12.0b1 includes backwards incompatible change to operation of `super()` · Issue #105035 · python/cpython · GitHub
Open Graph Title: 3.12.0b1 includes backwards incompatible change to operation of `super()` · Issue #105035 · python/cpython
X Title: 3.12.0b1 includes backwards incompatible change to operation of `super()` · Issue #105035 · python/cpython
Description: Bug report The optimisation to LOAD_SUPER_ATTR introduced by #104270 appears to have introduced a backwards incompatibility. The following code will reproduce the problem: class MyInstance: def __new__(cls, ptr, name, bases, attrs): self...
Open Graph Description: Bug report The optimisation to LOAD_SUPER_ATTR introduced by #104270 appears to have introduced a backwards incompatibility. The following code will reproduce the problem: class MyInstance: def __n...
X Description: Bug report The optimisation to LOAD_SUPER_ATTR introduced by #104270 appears to have introduced a backwards incompatibility. The following code will reproduce the problem: class MyInstance: def __n...
Opengraph URL: https://github.com/python/cpython/issues/105035
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"3.12.0b1 includes backwards incompatible change to operation of `super()`","articleBody":"# Bug report\r\n\r\nThe optimisation to LOAD_SUPER_ATTR introduced by #104270 appears to have introduced a backwards incompatibility.\r\n\r\nThe following code will reproduce the problem:\r\n```\r\nclass MyInstance:\r\n def __new__(cls, ptr, name, bases, attrs):\r\n self = super().__new__(cls, name, bases, attrs)\r\n print(f\"{MyInstance=} {self=} {type(self)=}, {super(MyInstance, type(self))=}\")\r\n super(MyInstance, type(self)).__setattr__(self, \"ptr\", ptr)\r\n return self\r\n\r\n def __setattr__(self, name, value):\r\n raise Exception()\r\n\r\n\r\nclass MyClass(MyInstance, type):\r\n def __new__(cls, name):\r\n self = super().__new__(cls, id(name), name, (MyInstance,), {})\r\n\r\n return self\r\n\r\n\r\nclass1 = MyClass(\"Class1\")\r\nprint(f\"{class1.ptr=}\")\r\n\r\nclass2 = MyClass(\"Class2\")\r\nprint(f\"{class2.ptr=}\")\r\n```\r\nUnder 3.12.0a7 (and previous stable Python versions going back to at least 3.7), this will succeed, outputting:\r\n```\r\nMyInstance=\u003cclass '__main__.MyInstance'\u003e self=\u003cclass '__main__.Class1'\u003e type(self)=\u003cclass '__main__.MyClass'\u003e, super()=\u003csuper: \u003cclass 'MyInstance'\u003e, \u003cMyClass object\u003e\u003e\r\nclass1.ptr=4364457392\r\nMyInstance=\u003cclass '__main__.MyInstance'\u003e self=\u003cclass '__main__.Class2'\u003e type(self)=\u003cclass '__main__.MyClass'\u003e, super()=\u003csuper: \u003cclass 'MyInstance'\u003e, \u003cMyClass object\u003e\u003e\r\nclass2.ptr=4365761904\r\n```\r\n\r\nUnder 3.12.0b1, it raises an error:\r\n```\r\nMyInstance=\u003cclass '__main__.MyInstance'\u003e self=\u003cclass '__main__.Class1'\u003e type(self)=\u003cclass '__main__.MyClass'\u003e, super()=\u003csuper: \u003cclass 'MyInstance'\u003e, \u003cMyClass object\u003e\u003e\r\nclass1.ptr=4370144336\r\nMyInstance=\u003cclass '__main__.MyInstance'\u003e self=\u003cclass '__main__.Class2'\u003e type(self)=\u003cclass '__main__.MyClass'\u003e, super()=\u003csuper: \u003cclass 'MyInstance'\u003e, \u003cMyClass object\u003e\u003e\r\nTraceback (most recent call last):\r\n File \"/Users/rkm/beeware/rubicon/objc/repro.py\", line 24, in \u003cmodule\u003e\r\n class2 = MyClass(\"Class2\")\r\n ^^^^^^^^^^^^^^^^^\r\n File \"/Users/rkm/beeware/rubicon/objc/repro.py\", line 16, in __new__\r\n self = super().__new__(cls, id(name), name, (MyInstance,), {})\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"/Users/rkm/beeware/rubicon/objc/repro.py\", line 7, in __new__\r\n super().__setattr__(self, \"ptr\", ptr)\r\nTypeError: expected 2 arguments, got 3\r\n```\r\n\r\nThat is - the first `MyClass` instance can be instantiated; however, the second instance fails in mid-construction when invoking `__setattr__`. The state of the objects prior to the `__setattr__` invocation appear to be identical, but the `__setattr__` method behaves differently on the second invocation.\r\n\r\nGit bisect has narrowed the cause down to #104270 (CC @carljm, @markshannon).\r\n\r\nThe reproduction case also fails if the call to `super`:\r\n```\r\n super(MyInstance, type(self)).__setattr__(self, \"ptr\", ptr)\r\n```\r\nis replaced with the simpler:\r\n```\r\n super().__setattr__(self, \"ptr\", ptr)\r\n```\r\n## Background\r\n\r\nThis test case has been extracted from [rubicon-objc](https://github.com/beeware/rubicon-objc), causing beeware/rubicon-objc#313. Rubicon is a wrapper around the Objective C runtime used by macOS and iOS; `MyInstance` is an analog of `ObjCInstance`, and `MyClass` is a an analog of `ObjCClass`. `ObjCInstance` has an implementation of `__setattr__` to redirect attribute access to the underlying ObjC calls. However, during construction, `ObjCInstance` needs to store a `ptr` of the underlying ObjC instance. This isn't a valid ObjC attribute, so `super()` is used to access the underlying `__setattr__` implementation to set the attribute.\r\n\r\n# Your environment\r\n\r\n- CPython versions tested on: 3.7.9, 3.10.11, 3.12.0a7, 3.12.0b1\r\n- Operating system and architecture: macOS ARM64\r\n\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-105094\n* gh-105117\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/freakboy3742","@type":"Person","name":"freakboy3742"},"datePublished":"2023-05-28T04:34:48.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":7},"url":"https://github.com/105035/cpython/issues/105035"}
| 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:26507364-13ad-cc06-c2fc-b1d21580c793 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 973C:217FCE:F6F29E:154C17D:6969F260 |
| html-safe-nonce | bf7b5cc36894fffe7be4315b1a09b9cede9f79ec6a7d9e198a79bf24e19840fa |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NzNDOjIxN0ZDRTpGNkYyOUU6MTU0QzE3RDo2OTY5RjI2MCIsInZpc2l0b3JfaWQiOiI1NzU1MzA4ODA3Njc0ODg2MDgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 95f4b50ca342c230b0bd9e2e3d6c0a5984489d5e9ec59ed7e893009f72ac032f |
| hovercard-subject-tag | issue:1729164357 |
| 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/105035/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e99715abd089bd0a5eee7330ece058f0c5100751cf3713f9e12ad7ee32bde600/python/cpython/issues/105035 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e99715abd089bd0a5eee7330ece058f0c5100751cf3713f9e12ad7ee32bde600/python/cpython/issues/105035 |
| og:image:alt | Bug report The optimisation to LOAD_SUPER_ATTR introduced by #104270 appears to have introduced a backwards incompatibility. The following code will reproduce the problem: class MyInstance: def __n... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | freakboy3742 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7b32f1c7c4549428ee399213e8345494fc55b5637195d3fc5f493657579235e8 |
| 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 | bdde15ad1b403e23b08bbd89b53fbe6bdf688cad |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width