René's URL Explorer Experiment


Title: `exec()`, `eval()` and `-c` keep a lone CR in string literals when the source is a `str` · Issue #9761 · RustPython/RustPython · GitHub

Open Graph Title: `exec()`, `eval()` and `-c` keep a lone CR in string literals when the source is a `str` · Issue #9761 · RustPython/RustPython

X Title: `exec()`, `eval()` and `-c` keep a lone CR in string literals when the source is a `str` · Issue #9761 · RustPython/RustPython

Description: Summary When source code is passed as a str to exec() or eval(), or with rustpython -c, a lone carriage return inside a string literal stays '\r'. CPython reads it as '\n', as it does for CRLF. So code saved with CR-only line endings get...

Open Graph Description: Summary When source code is passed as a str to exec() or eval(), or with rustpython -c, a lone carriage return inside a string literal stays '\r'. CPython reads it as '\n', as it does for CRLF. So ...

X Description: Summary When source code is passed as a str to exec() or eval(), or with rustpython -c, a lone carriage return inside a string literal stays '\r'. CPython reads it as '\n', as it do...

Opengraph URL: https://github.com/RustPython/RustPython/issues/9761

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`exec()`, `eval()` and `-c` keep a lone CR in string literals when the source is a `str`","articleBody":"## Summary\n\nWhen source code is passed as a `str` to `exec()` or `eval()`, or with `rustpython -c`, a lone carriage return inside a string literal stays `'\\r'`. CPython reads it as `'\\n'`, as it does for CRLF. So code saved with CR-only line endings gets different string values and docstrings in RustPython. Docstrings are also left uncleaned, because a docstring with only CR line breaks counts as one line. Within RustPython, the same text also gives different values depending on the call: `exec(text)` keeps the CR, but `exec(text.encode())` and `exec(compile(text, ...))` turn it into `'\\n'`.\n\nThe [language reference (\"Physical lines\")](https://docs.python.org/3/reference/lexical_analysis.html#physical-lines) says: \"Regardless of platform, each of these sequences is replaced by a single ASCII LF (linefeed) character. (This is done even inside string literals.)\"\n\n## Reproducer\n\n```python\n# source saved with old Mac (CR-only) line endings, e.g. read from a zip or with pkgutil.get_data()\ndata = b'\"\"\"Usage:\\r  tool run\"\"\"\\rBANNER = \"\"\"line one\\rline two\"\"\"\\r'\ntext = data.decode()\n\nns = {}; exec(data, ns)\nprint(\"exec(bytes)        \", ascii(ns[\"__doc__\"]), ascii(ns[\"BANNER\"]))\nns = {}; exec(compile(text, \"\u003cs>\", \"exec\"), ns)\nprint(\"exec(compile(str)) \", ascii(ns[\"__doc__\"]), ascii(ns[\"BANNER\"]))\nns = {}; exec(text, ns)\nprint(\"exec(str)          \", ascii(ns[\"__doc__\"]), ascii(ns[\"BANNER\"]))\nprint(\"eval(str)          \", ascii(eval('\"\"\"a\\rb\"\"\"')))\n```\n\n## Expected\n\nCPython 3.15.2 (3.13.18 prints the same; 3.12.13 differs only in not dedenting the docstring):\n\n```\nexec(bytes)         'Usage:\\ntool run' 'line one\\nline two'\nexec(compile(str))  'Usage:\\ntool run' 'line one\\nline two'\nexec(str)           'Usage:\\ntool run' 'line one\\nline two'\neval(str)           'a\\nb'\n```\n\n## Actual\n\nRustPython main (b66534d), same on ef023bc:\n\n```\nexec(bytes)         'Usage:\\ntool run' 'line one\\nline two'\nexec(compile(str))  'Usage:\\ntool run' 'line one\\nline two'\nexec(str)           'Usage:\\r  tool run' 'line one\\rline two'\neval(str)           'a\\rb'\n```\n\n`rustpython -c $'x = \"\"\"a\\rb\"\"\"\\nprint(ascii(x))'` prints `'a\\rb'` (CPython `'a\\nb'`).\n\nOn the `exec(str)` / `eval(str)` / `-c` paths, every literal kind keeps the CR: plain, raw (`r\"\"\"a\u003cCR>b\"\"\"`), bytes (`b\"\"\"...\"\"\"`, `rb\"\"\"...\"\"\"`), f-strings and t-strings (literal parts and a format spec), and a raw string with a backslash before the CR (`r\"a\\\u003cCR>b\"` gives `'a\\\\\\rb'`, CPython `'a\\\\\\nb'`). CRLF is handled correctly on these paths. Everything else is already correct: `exec`/`eval` of bytes, `compile()` of str or bytes, `ast.parse`, `codeop`, importing a CR-only module, running a CR-only script file, and stdin. Statements, comments and line numbers already treat a lone CR as a line break, for both str and bytes source.\n\n## Cause\n\n#9429 added newline normalization (`\\r\\n` and `\\r` to `\\n`) to the source decoder ([compile.rs#L464-L470](https://github.com/RustPython/RustPython/blob/b66534d448f8b94e7874d44ca607c42803c2399f/crates/vm/src/vm/compile.rs#L464-L470)). `compile()` always goes through that decoder, and so do `exec`/`eval` of bytes. Its description already notes that \"direct string paths that bypass the decoder are not changed\". `exec()` and `eval()` with a `str` pass the text straight to `run_code` ([builtins.rs#L640-L650](https://github.com/RustPython/RustPython/blob/b66534d448f8b94e7874d44ca607c42803c2399f/crates/vm/src/stdlib/builtins.rs#L640-L650), [#L591-L601](https://github.com/RustPython/RustPython/blob/b66534d448f8b94e7874d44ca607c42803c2399f/crates/vm/src/stdlib/builtins.rs#L591-L601), [#L701-L708](https://github.com/RustPython/RustPython/blob/b66534d448f8b94e7874d44ca607c42803c2399f/crates/vm/src/stdlib/builtins.rs#L701-L708)), and `-c` goes through `vm.run_string`. Both end in `compile_with_syntax_warning_handler`, which only replaces `\"\\r\\n\"` ([lib.rs#L2409-L2421](https://github.com/RustPython/RustPython/blob/b66534d448f8b94e7874d44ca607c42803c2399f/crates/compiler/src/lib.rs#L2409-L2421)). The parser copies literal text from the source as is ([string.rs#L499-L518](https://github.com/RustPython/ruff/blob/9290a8849542134d83f77e9360e7f0b93897fd93/crates/ruff_python_parser/src/string.rs#L499-L518) in the ruff fork; upstream ruff tracks the same parser behavior as astral-sh/ruff#16318). So any CR that reaches it unnormalized ends up in the value.\n\nI tried changing line 2416 to `source.replace(\"\\r\\n\", \"\\n\").replace('\\r', \"\\n\")` locally. With that change, all the cases above match CPython 3.15. Running the str paths through the same normalization as the decoder would work too.\n\nCPython's own tests do not catch this: `test_source_encoding`'s `test_crlf`/`test_crcrlf` cases go through bytes and script files, and `test_compile.test_other_newlines` uses `compile()`. All of these pass.\n\n\u003csub>Drafted with help from Claude Code; I reviewed and checked it before posting.\u003c/sub>\n","author":{"url":"https://github.com/taxor03","@type":"Person","name":"taxor03"},"datePublished":"2026-11-09T05:50:22Z","url":"https://github.com/RustPython/RustPython/issues/9761"}

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/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:cb7de5c6-b085-8d55-f9bb-b3ba6779d8fd
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idBDE4:A71F9:287209E:3331172:6B01D411
html-safe-noncee1796afed32c546ee53cc9624ff6999e1a04aa433ac30b898c994afea002e6f8
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCREU0OkE3MUY5OjI4NzIwOUU6MzMzMTE3Mjo2QjAxRDQxMSIsInZpc2l0b3JfaWQiOiI2MzA1NDM1MjY1NjQ1MjcwMDMzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac43325b06c0b1e0961d60633ec9111b169ade769a5a0888e5cbfb4b0b1f66242e
hovercard-subject-tagissue:6280745418
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/RustPython/RustPython/9761/issue_layout
twitter:imagehttps://opengraph.githubassets.com/0e844dce7172d392047a25cf0c5abb68878ca94c26882bd1abd2382eb593abfe/RustPython/RustPython/issues/9761
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/0e844dce7172d392047a25cf0c5abb68878ca94c26882bd1abd2382eb593abfe/RustPython/RustPython/issues/9761
og:image:altSummary When source code is passed as a str to exec() or eval(), or with rustpython -c, a lone carriage return inside a string literal stays '\r'. CPython reads it as '\n', as it does for CRLF. So ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernametaxor03
hostnamegithub.com
expected-hostnamegithub.com
Nonecc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c
turbo-cache-controlno-preview
go-importgithub.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git
octolytics-dimension-user_id39710557
octolytics-dimension-user_loginRustPython
octolytics-dimension-repository_id135201145
octolytics-dimension-repository_nwoRustPython/RustPython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id135201145
octolytics-dimension-repository_network_root_nwoRustPython/RustPython
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
release8c77c9e8439031ad54c75a250694842892f693e1
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/RustPython/RustPython/issues/9761#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FRustPython%2FRustPython%2Fissues%2F9761
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%2FRustPython%2FRustPython%2Fissues%2F9761
Sign uphttps://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=RustPython%2FRustPython
Reloadhttps://github.com/RustPython/RustPython/issues/9761
Reloadhttps://github.com/RustPython/RustPython/issues/9761
Reloadhttps://github.com/RustPython/RustPython/issues/9761
RustPythonhttps://github.com/RustPython
RustPythonhttps://github.com/RustPython/RustPython
Codehttps://github.com/RustPython/RustPython
Issues195 (195)https://github.com/RustPython/RustPython/issues
Pull requests132 (132)https://github.com/RustPython/RustPython/pulls
Discussionshttps://github.com/RustPython/RustPython/discussions
Actionshttps://github.com/RustPython/RustPython/actions
Projectshttps://github.com/RustPython/RustPython/projects
Wikihttps://github.com/RustPython/RustPython/wiki
Security and qualityhttps://github.com/RustPython/RustPython/security
Insightshttps://github.com/RustPython/RustPython/insights
https://github.com/RustPython/RustPython
New issuehttps://github.com/login?return_to=https://github.com/RustPython/RustPython/issues/9761
New issuehttps://github.com/login?return_to=https://github.com/RustPython/RustPython/issues/9761
exec(), eval() and -c keep a lone CR in string literals when the source is a strhttps://github.com/RustPython/RustPython/issues/9761#top
https://github.com/youknowone
https://github.com/copilot-swe-agent
https://github.com/taxor03
taxor03https://github.com/taxor03
on Nov 9, 2026https://github.com/RustPython/RustPython/issues/9761#issue-6280745418
language reference ("Physical lines")https://docs.python.org/3/reference/lexical_analysis.html#physical-lines
b66534dhttps://github.com/RustPython/RustPython/commit/b66534d448f8b94e7874d44ca607c42803c2399f
ef023bchttps://github.com/RustPython/RustPython/commit/ef023bcd619a74d5306b2042e2e135670332a43e
#9429https://github.com/RustPython/RustPython/pull/9429
compile.rs#L464-L470https://github.com/RustPython/RustPython/blob/b66534d448f8b94e7874d44ca607c42803c2399f/crates/vm/src/vm/compile.rs#L464-L470
builtins.rs#L640-L650https://github.com/RustPython/RustPython/blob/b66534d448f8b94e7874d44ca607c42803c2399f/crates/vm/src/stdlib/builtins.rs#L640-L650
#L591-L601https://github.com/RustPython/RustPython/blob/b66534d448f8b94e7874d44ca607c42803c2399f/crates/vm/src/stdlib/builtins.rs#L591-L601
#L701-L708https://github.com/RustPython/RustPython/blob/b66534d448f8b94e7874d44ca607c42803c2399f/crates/vm/src/stdlib/builtins.rs#L701-L708
lib.rs#L2409-L2421https://github.com/RustPython/RustPython/blob/b66534d448f8b94e7874d44ca607c42803c2399f/crates/compiler/src/lib.rs#L2409-L2421
string.rs#L499-L518https://github.com/RustPython/ruff/blob/9290a8849542134d83f77e9360e7f0b93897fd93/crates/ruff_python_parser/src/string.rs#L499-L518
astral-sh/ruff#16318https://github.com/astral-sh/ruff/issues/16318
youknowonehttps://github.com/youknowone
Copilothttps://github.com/apps/copilot-swe-agent
youknowonehttps://github.com/youknowone
on Nov 9, 2026https://github.com/RustPython/RustPython/issues/9761#event-35566242792
Copilothttps://github.com/apps/copilot-swe-agent
Normalize lone CR to LF in str source for `exec()`, `eval()` and `-c` #9762https://github.com/RustPython/RustPython/pull/9762
on Nov 9, 2026https://github.com/RustPython/RustPython/issues/9761#event-35566348805
Sign up for freehttps://github.com/signup?return_to=https://github.com/RustPython/RustPython/issues/9761
Sign in to commenthttps://github.com/login?return_to=https://github.com/RustPython/RustPython/issues/9761
Copilothttps://github.com/apps/copilot-swe-agent
youknowonehttps://github.com/youknowone
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.