Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-128509: Use bytes singleton in marshal - #157398

Merged
vstinner merged 3 commits into
python:mainfrom
vstinner:marshal_bytes
Sep 13, 2026
Merged

gh-128509: Use bytes singleton in marshal#157398
vstinner merged 3 commits into
python:mainfrom
vstinner:marshal_bytes

Conversation

@vstinner

@vstinner vstinner commented Sep 13, 2026

Copy link
Copy Markdown
Member

Replace soft deprecated PyBytes_FromStringAndSize(NULL, n) with PyBytes_FromStringAndSize(str, n) so marshal can get bytes singleton.

Replace soft deprecated PyBytes_FromStringAndSize(NULL, n) with
PyBytes_FromStringAndSize(str, n) so marshal can get bytes singleton.
@vstinner

Copy link
Copy Markdown
Member Author

Create a script x.py with:

def func():
    constant_obj = b'x'
    runtime_obj = 'x'.encode()
    if constant_obj is not runtime_obj:
        print("marshal failed to get the singleton")
    else:
        print("all good")

func()

The script works as expected when run with ./python x.py since this command doesn't use cached PYC file, it always parse the script:

$ ./python x.py 
all good
$ ./python x.py 
all good

When using ./python -m x instead, the script fails if a cached PYC is loaded instead of parsing the .py source, so the second run:

$ ./python -m x  # no PYC cache
all good
$ ./python -m x  # use PYC cache (loaded by marshal)
marshal failed to get the singleton

Comment thread Lib/test/test_marshal.py Outdated

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but please test for different marshal versions.

@vstinner

Copy link
Copy Markdown
Member Author

Good idea! I add a loop on all marshal versions!

@vstinner
vstinner enabled auto-merge (squash) September 13, 2026 11:39
@vstinner
vstinner merged commit 658612a into python:main Sep 13, 2026
54 checks passed
@vstinner
vstinner deleted the marshal_bytes branch September 13, 2026 12:11
@vstinner

Copy link
Copy Markdown
Member Author

Merged. Thanks for the review @serhiy-storchaka, as usual!

@vstinner vstinner added needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Sep 13, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Sep 13, 2026

Copy link
Copy Markdown

GH-157432 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Sep 13, 2026
@bedevere-app

bedevere-app Bot commented Sep 13, 2026

Copy link
Copy Markdown

GH-157433 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 13, 2026
vstinner added a commit that referenced this pull request Sep 13, 2026
gh-128509: Use bytes singletons in marshal (GH-157398)

Replace soft deprecated PyBytes_FromStringAndSize(NULL, n) with
PyBytes_FromStringAndSize(str, n) so marshal can get bytes singleton.
(cherry picked from commit 658612a)

Co-authored-by: Victor Stinner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants