René's URL Explorer Experiment


Title: gh-159188: Do not immortalize constants in compile() of an AST by gpshead · Pull Request #159198 · python/cpython · GitHub

Open Graph Title: gh-159188: Do not immortalize constants in compile() of an AST by gpshead · Pull Request #159198 · python/cpython

X Title: gh-159188: Do not immortalize constants in compile() of an AST by gpshead · Pull Request #159198 · python/cpython

Description: On the free-threaded build, the marshal output for code compiled from an AST depended on which code objects had been created earlier in the process: SRC = "def f(a, b):\n return g(a=1, b=2)\n" # optionally first: compile(ast.parse("def other(a, b):\n return h(a=3, b=4)\n"), ...) d_src = marshal.dumps(compile(SRC, 'm.py', 'exec')) d_ast = marshal.dumps(compile(ast.parse(SRC), 'm.py', 'exec')) free-threaded main len(d_src) len(d_ast) nothing compiled before 230 230 an unrelated AST compiled before 230 237 The default build gives 230 in every case, and so does the free-threaded build with this PR. Cause and change The compiler makes a tuple of names (co_names, co_localsplusnames) the same object as an equal constant: keyword names, a from list, __static_attributes__. On the free-threaded build, intern_constants() replaces each constant by the equal immortal object of a per-interpreter table, if there is one, but does not replace the tuple of names. compile() of a source string turns that off with tstate->suppress_co_const_immortalization (gh-118527). The branch that handles AST objects returned before the flag was set. flowchart TB subgraph before["main, free-threaded"] direction TB b0["compiler: constant ('a', 'b')
is the same tuple as co_names"] b0 -- "compile(ast)" --> b2["immortalization
not suppressed"] b0 -- "compile(str)" --> b1["immortalization
suppressed"] b2 --> b4{"equal tuple already
in the immortal table?"} b4 -- yes --> b6["constant replaced, co_names not:
two tuples, marshal writes both"] b4 -- no --> b5["still one tuple"] b1 --> b3["still one tuple
marshal: object + reference"] end subgraph after["this PR"] direction TB a0["compiler: constant ('a', 'b')
is the same tuple as co_names"] a0 -- "compile(ast)" --> a1["immortalization
suppressed"] a0 -- "compile(str)" --> a1 a1 --> a3["still one tuple
marshal: object + reference"] end Loading The change is the same increment/decrement pair around _PyAST_Compile() in builtin_compile_impl() that already surrounds _Py_CompileString(). Effects Tools that compile an AST and write a .pyc (assertion rewriters, SourceLoader.source_to_code() given an AST) get reproducible output. Compiling the standard library from ASTs in one process, 179 of 2067 files gave different bytes depending on file order; now none do. Code from compile(ast) no longer has immortal constants, as is already the case for compile(str). It gets them when it is later loaded from a .pyc. Tools that compile many ASTs stop growing the immortal table: for 10000 distinct functions, +141286 memory blocks on main, +606 with this PR. The GIL enabled build is not affected. Not covered exec() of a string, Py_CompileString*(), PyRun_*() and marshal.loads() do not set the flag and keep the same dependence on history. None of them is on a path that writes .pyc files. Tests Every TestConstantsIdentity case (added by gh-159193) is now also compiled from an AST. test_constants_not_immortal (free-threaded only) checks that a tuple constant from compile() of a string or an AST is not immortal even after an equal one entered the table. Authored using Claude, reviewed by me. Issue: gh-159188

Open Graph Description: On the free-threaded build, the marshal output for code compiled from an AST depended on which code objects had been created earlier in the process: SRC = "def f(a, b):\n return g(a=1, b=2)...

X Description: On the free-threaded build, the marshal output for code compiled from an AST depended on which code objects had been created earlier in the process: SRC = "def f(a, b):\n return g(a=1, ...

Opengraph URL: https://github.com/python/cpython/pull/159198

X: @github

direct link

Domain: github.com

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:b697b982-beba-7887-1e9a-e405009b3b90
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-id9594:38E318:3664BA2:44D45B0:6B021BD8
html-safe-noncea4565b680871117434f98c474d8904f627c412bb897ad79624f081a40f42894c
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NTk0OjM4RTMxODozNjY0QkEyOjQ0RDQ1QjA6NkIwMjFCRDgiLCJ2aXNpdG9yX2lkIjoiNzI0MDA4NDYyODU3NjczNjg4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac11237962089c5dc4cb7f84aff7c8b75e040a9e5791ecd8e131e3305fbd20b235
hovercard-subject-tagpull_request:4822332486
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/python/cpython/pull/159198/files
twitter:imagehttps://avatars.githubusercontent.com/u/68491?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/68491?s=400&v=4
og:image:altOn the free-threaded build, the marshal output for code compiled from an AST depended on which code objects had been created earlier in the process: SRC = "def f(a, b):\n return g(a=1, b=2)...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
diff-viewunified
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 full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/pull/159198/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F159198%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
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
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://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
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F159198%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/pull/159198/files
Reloadhttps://github.com/python/cpython/pull/159198/files
Reloadhttps://github.com/python/cpython/pull/159198/files
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
gpsheadhttps://github.com/gpshead
python:mainhttps://github.com/python/cpython/tree/main
gpshead:ft-const-identityhttps://github.com/gpshead/cpython/tree/ft-const-identity
Conversation 2 https://github.com/python/cpython/pull/159198
Commits 1 https://github.com/python/cpython/pull/159198/commits
Checks 59 https://github.com/python/cpython/pull/159198/checks
Files changed https://github.com/python/cpython/pull/159198/files
Please reload this pagehttps://github.com/python/cpython/pull/159198/files
gh-159188: Do not immortalize constants in compile() of an AST https://github.com/python/cpython/pull/159198/files#top
Show all changes 1 commit https://github.com/python/cpython/pull/159198/files
120941d gh-159188: Do not immortalize constants in compile() of an AST gpshead Oct 11, 2026 https://github.com/python/cpython/pull/159198/commits/120941de6ff75101bb54e10810338d8f4243474c
Clear filters https://github.com/python/cpython/pull/159198/files
Please reload this pagehttps://github.com/python/cpython/pull/159198/files
Please reload this pagehttps://github.com/python/cpython/pull/159198/files
test_compile.py https://github.com/python/cpython/pull/159198/files#diff-c69ceae0611c40012b4691e3536bf17efacdb5b51b8e155eec9a939108e42e43
2026-10-11-06-36-25.gh-issue-159188.Fq7wLm.rst https://github.com/python/cpython/pull/159198/files#diff-79df7914b17e7ae01479b3b273338b8891af8effbe7bea02805bdccc17eae2a4
bltinmodule.c https://github.com/python/cpython/pull/159198/files#diff-e4fd8b8ee6a147f86c0719ff122aca6dfca36edbd4812c87892698b3b72e40a1
Lib/test/test_compile.pyhttps://github.com/python/cpython/pull/159198/files#diff-c69ceae0611c40012b4691e3536bf17efacdb5b51b8e155eec9a939108e42e43
View file https://github.com/gpshead/cpython/blob/120941de6ff75101bb54e10810338d8f4243474c/Lib/test/test_compile.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/159198/{{ revealButtonHref }}
https://github.com/python/cpython/pull/159198/files#diff-c69ceae0611c40012b4691e3536bf17efacdb5b51b8e155eec9a939108e42e43
https://github.com/python/cpython/pull/159198/files#diff-c69ceae0611c40012b4691e3536bf17efacdb5b51b8e155eec9a939108e42e43
https://github.com/python/cpython/pull/159198/files#diff-c69ceae0611c40012b4691e3536bf17efacdb5b51b8e155eec9a939108e42e43
https://github.com/python/cpython/pull/159198/files#diff-c69ceae0611c40012b4691e3536bf17efacdb5b51b8e155eec9a939108e42e43
https://github.com/python/cpython/pull/159198/files#diff-c69ceae0611c40012b4691e3536bf17efacdb5b51b8e155eec9a939108e42e43
https://github.com/python/cpython/pull/159198/files#diff-c69ceae0611c40012b4691e3536bf17efacdb5b51b8e155eec9a939108e42e43
https://github.com/python/cpython/pull/159198/files#diff-c69ceae0611c40012b4691e3536bf17efacdb5b51b8e155eec9a939108e42e43
https://github.com/python/cpython/pull/159198/files#diff-c69ceae0611c40012b4691e3536bf17efacdb5b51b8e155eec9a939108e42e43
https://github.com/python/cpython/pull/159198/files#diff-c69ceae0611c40012b4691e3536bf17efacdb5b51b8e155eec9a939108e42e43
https://github.com/python/cpython/pull/159198/files#diff-c69ceae0611c40012b4691e3536bf17efacdb5b51b8e155eec9a939108e42e43
Misc/NEWS.d/next/Core_and_Builtins/2026-10-11-06-36-25.gh-issue-159188.Fq7wLm.rsthttps://github.com/python/cpython/pull/159198/files#diff-79df7914b17e7ae01479b3b273338b8891af8effbe7bea02805bdccc17eae2a4
View file https://github.com/gpshead/cpython/blob/120941de6ff75101bb54e10810338d8f4243474c/Misc/NEWS.d/next/Core_and_Builtins/2026-10-11-06-36-25.gh-issue-159188.Fq7wLm.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/159198/{{ revealButtonHref }}
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L243
Python/bltinmodule.chttps://github.com/python/cpython/pull/159198/files#diff-e4fd8b8ee6a147f86c0719ff122aca6dfca36edbd4812c87892698b3b72e40a1
View file https://github.com/gpshead/cpython/blob/120941de6ff75101bb54e10810338d8f4243474c/Python/bltinmodule.c
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/159198/{{ revealButtonHref }}
https://github.com/python/cpython/pull/159198/files#diff-e4fd8b8ee6a147f86c0719ff122aca6dfca36edbd4812c87892698b3b72e40a1
https://github.com/python/cpython/pull/159198/files#diff-e4fd8b8ee6a147f86c0719ff122aca6dfca36edbd4812c87892698b3b72e40a1
https://github.com/python/cpython/pull/159198/files#diff-e4fd8b8ee6a147f86c0719ff122aca6dfca36edbd4812c87892698b3b72e40a1
Please reload this pagehttps://github.com/python/cpython/pull/159198/files
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.