René's URL Explorer Experiment


Title: Optimiser breaks attribute lookup from metaclass property · Issue #94822 · python/cpython · GitHub

Open Graph Title: Optimiser breaks attribute lookup from metaclass property · Issue #94822 · python/cpython

X Title: Optimiser breaks attribute lookup from metaclass property · Issue #94822 · python/cpython

Description: Bug report This comes from a SymPy issue: sympy/sympy#23774 It looks like there is a nondeterministic error in the optimisation introduced in 96346cb from #27722 first included in CPython version 3.11.0a1. The optimisation speeds up call...

Open Graph Description: Bug report This comes from a SymPy issue: sympy/sympy#23774 It looks like there is a nondeterministic error in the optimisation introduced in 96346cb from #27722 first included in CPython version 3...

X Description: Bug report This comes from a SymPy issue: sympy/sympy#23774 It looks like there is a nondeterministic error in the optimisation introduced in 96346cb from #27722 first included in CPython version 3...

Opengraph URL: https://github.com/python/cpython/issues/94822

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Optimiser breaks attribute lookup from metaclass property","articleBody":"**Bug report**\r\n\r\nThis comes from a SymPy issue: https://github.com/sympy/sympy/issues/23774\r\n\r\nIt looks like there is a nondeterministic error in the optimisation introduced in 96346cb6d0593ef9ec122614347ccb053cd63433 from #27722 first included in CPython version 3.11.0a1. The optimisation speeds up calling methods but sometimes retrieves the wrong attribute from a class whose metaclass defines a property method.\r\n\r\nThe way that this arises is quite sensitive to small changes so I haven't been able to distil a standalone reproducer. I'll show how to reproduce this using SymPy below but first this is a simplified schematic of the situation:\r\n```python\r\nclass MetaA(type):\r\n    def __init__(cls, *args, **kws):\r\n        pass\r\n\r\nclass A(metaclass=MetaA):\r\n    def method(self, rule):\r\n        return 'A method'\r\n\r\nclass MetaB(MetaA):\r\n    @property\r\n    def method(self):\r\n        def method_inner(rule):\r\n            return 'MetaB function'\r\n        return method_inner\r\n\r\nclass B(A, metaclass=MetaB):\r\n    pass\r\n\r\nprint(B.method(1))   # MetaB function\r\nprint(B().method(1)) # A method\r\n```\r\nHere `B` is a subclass of `A` but an instance of `MetaB`. Both define `method` but the `MetaB` method should be used when accessed from the class `B` rather than an instance `B()`. The failure seen in SymPy is that sometimes `B.method(1)` will execute as `A.method(1)` which fails because of the missing `self` argument.\r\n\r\nThe following code reproduces the problem and fails something like 50% of the time with SymPy 1.10.1 and CPython 3.11.0a1-3.11.0b4:\r\n```python\r\nfrom sympy import *\r\n\r\nx, y = symbols('x, y')\r\nf = Function('f')\r\n\r\n# These two lines look irrelevant but are needed:\r\nexpr = sin(x*exp(y))\r\nDerivative(expr, y).subs(y, x).doit()\r\n\r\nexpr = Subs(Derivative(f(f(x)), x), f, cos)\r\n\r\n# This is where it blows up:\r\nexpr.doit()\r\n```\r\nThe failure is not deterministic:\r\n```console\r\n$ python bug.py\r\n$ python bug.py\r\n$ python bug.py\r\nTraceback (most recent call last):\r\n  File \"/home/oscar/current/sympy/sympy.git/bug.py\", line 13, in \u003cmodule\u003e\r\n    expr.doit()\r\n    ^^^^^^^^^^^\r\n  File \"/home/oscar/current/sympy/sympy.git/sympy/core/function.py\", line 2246, in doit\r\n    e = e.subs(vi, p[i])\r\n        ^^^^^^^^^^^^^^^^\r\n  File \"/home/oscar/current/sympy/sympy.git/sympy/core/basic.py\", line 997, in subs\r\n    rv = rv._subs(old, new, **kwargs)\r\n         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n  File \"/home/oscar/current/sympy/sympy.git/sympy/core/cache.py\", line 70, in wrapper\r\n    retval = cfunc(*args, **kwargs)\r\n             ^^^^^^^^^^^^^^^^^^^^^^\r\n  File \"/home/oscar/current/sympy/sympy.git/sympy/core/basic.py\", line 1109, in _subs\r\n    rv = self._eval_subs(old, new)\r\n         ^^^^^^^^^^^^^^^^^^^^^^^^^\r\n  File \"/home/oscar/current/sympy/sympy.git/sympy/core/function.py\", line 1737, in _eval_subs\r\n    nfree = new.xreplace(syms).free_symbols\r\n            ^^^^^^^^^^^^^^^^^^\r\nTypeError: Basic.xreplace() missing 1 required positional argument: 'rule'\r\n```\r\nThe failure can be reproduced deterministically by setting the hash seed:\r\n```console\r\n$ PYTHONHASHSEED=1 python bug.py\r\n...\r\nTypeError: Basic.xreplace() missing 1 required positional argument: 'rule'\r\n```\r\nIn the debugger the same code that already failed succeeds:\r\n```console\r\n$ PYTHONHASHSEED=1 python -m pdb bug.py \r\n\u003e /home/oscar/current/sympy/sympy.git/bug.py(1)\u003cmodule\u003e()\r\n-\u003e from sympy import *\r\n(Pdb) c\r\nTraceback (most recent call last):\r\n  File \"/media/oscar/EXT4_STUFF/src/cpython/Lib/pdb.py\", line 1768, in main\r\n    pdb._run(target)\r\n  File \"/media/oscar/EXT4_STUFF/src/cpython/Lib/pdb.py\", line 1646, in _run\r\n    self.run(target.code)\r\n  File \"/media/oscar/EXT4_STUFF/src/cpython/Lib/bdb.py\", line 597, in run\r\n    exec(cmd, globals, locals)\r\n  File \"\u003cstring\u003e\", line 1, in \u003cmodule\u003e\r\n  File \"/home/oscar/current/sympy/sympy.git/bug.py\", line 13, in \u003cmodule\u003e\r\n    expr.doit()\r\n  File \"/home/oscar/current/sympy/sympy.git/sympy/core/function.py\", line 2255, in doit\r\n    e = e.subs(vi, p[i])\r\n        ^^^^^^^^^^^^^^^^\r\n  File \"/home/oscar/current/sympy/sympy.git/sympy/core/basic.py\", line 993, in subs\r\n    rv = rv._subs(old, new, **kwargs)\r\n         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n  File \"/home/oscar/current/sympy/sympy.git/sympy/core/cache.py\", line 70, in wrapper\r\n    retval = cfunc(*args, **kwargs)\r\n             ^^^^^^^^^^^^^^^^^^^^^^\r\n  File \"/home/oscar/current/sympy/sympy.git/sympy/core/basic.py\", line 1105, in _subs\r\n    rv = self._eval_subs(old, new)\r\n         ^^^^^^^^^^^^^^^^^^^^^^^^^\r\n  File \"/home/oscar/current/sympy/sympy.git/sympy/core/function.py\", line 1746, in _eval_subs\r\n    nfree = new.xreplace(syms).free_symbols\r\n            ^^^^^^^^^^^^^^^^^^\r\nTypeError: Basic.xreplace() missing 1 required positional argument: 'rule'\r\nUncaught exception. Entering post mortem debugging\r\nRunning 'cont' or 'step' will restart the program\r\n\u003e /home/oscar/current/sympy/sympy.git/sympy/core/function.py(1746)_eval_subs()\r\n-\u003e nfree = new.xreplace(syms).free_symbols\r\n(Pdb) p new.xreplace\r\n\u003cfunction FunctionClass.xreplace.\u003clocals\u003e.\u003clambda\u003e at 0x7f0dd0f763e0\u003e\r\n(Pdb) p new.xreplace(syms)\r\ncos\r\n(Pdb) p new.xreplace(syms).free_symbols\r\nset()\r\n```\r\nThe actual arrangement of SymPy classes is something like this:\r\n```python\r\nclass ManagedProperties(type):\r\n    def __init__(cls, *args, **kws):\r\n        pass\r\n\r\nclass Basic(metaclass=ManagedProperties):\r\n    def xreplace(self, rule):\r\n        print('Basic')\r\n\r\nclass Expr(Basic):\r\n    pass\r\n\r\nclass FunctionClass(ManagedProperties):\r\n    @property\r\n    def xreplace(self):\r\n        return lambda rule: print('functionclass')\r\n\r\nclass Application(Basic, metaclass=FunctionClass):\r\n    pass\r\n\r\nclass Function(Application, Expr):\r\n    pass\r\n\r\nclass cos(Function):\r\n    pass\r\n\r\ncos.xreplace(1)\r\n```\r\n\r\n**Your environment**\r\n\r\n- CPython versions tested on: 3.11.0a1-3.11.0b4 (3.10.5 or lower does not have the bug)\r\n- Operating system and architecture: Ubuntu x86-64.","author":{"url":"https://github.com/oscarbenjamin","@type":"Person","name":"oscarbenjamin"},"datePublished":"2022-07-13T20:09:44.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":18},"url":"https://github.com/94822/cpython/issues/94822"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:078a3204-b69b-8267-df42-859aaab3e245
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idB574:3F1247:12DAAE4:19D07B1:6A53D204
html-safe-nonceb8fcfdec2a39d27ae9e9569060a021e141747dc1ef58c792b51120d43259fcf3
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNTc0OjNGMTI0NzoxMkRBQUU0OjE5RDA3QjE6NkE1M0QyMDQiLCJ2aXNpdG9yX2lkIjoiMjkzOTQzNDE1OTg0Njk2OTg2MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacecbeed25c0afacad98e1e286eafa247b098b52ad0559608e3535ad3268a023ee
hovercard-subject-tagissue:1303885045
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/94822/issue_layout
twitter:imagehttps://opengraph.githubassets.com/d52a9b555cf8bedaf89e2400a5daaea249c6a846146966f65217fb182408abbb/python/cpython/issues/94822
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/d52a9b555cf8bedaf89e2400a5daaea249c6a846146966f65217fb182408abbb/python/cpython/issues/94822
og:image:altBug report This comes from a SymPy issue: sympy/sympy#23774 It looks like there is a nondeterministic error in the optimisation introduced in 96346cb from #27722 first included in CPython version 3...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameoscarbenjamin
hostnamegithub.com
expected-hostnamegithub.com
Noneb9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release07a982c1d40157c619b364352b704c3ce66bb332
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/94822#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F94822
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F94822
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/94822
Reloadhttps://github.com/python/cpython/issues/94822
Reloadhttps://github.com/python/cpython/issues/94822
Please reload this pagehttps://github.com/python/cpython/issues/94822
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/94822
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 35k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 73.8k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.3k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality 0 https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Optimiser breaks attribute lookup from metaclass propertyhttps://github.com/python/cpython/issues/94822#top
https://github.com/brandtbucher
3.11only security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.11%22
3.12only security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.12%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/oscarbenjamin
oscarbenjaminhttps://github.com/oscarbenjamin
on Jul 13, 2022https://github.com/python/cpython/issues/94822#issue-1303885045
sympy/sympy#23774https://github.com/sympy/sympy/issues/23774
96346cbhttps://github.com/python/cpython/commit/96346cb6d0593ef9ec122614347ccb053cd63433
#27722https://github.com/python/cpython/pull/27722
brandtbucherhttps://github.com/brandtbucher
3.11only security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.11%22
3.12only security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.12%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
Release and Deferred blockers 🚫https://github.com/orgs/python/projects/2
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.