Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-138775: fix handlepython -m base64 stdin correct with EOF signal#138776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
gpshead merged 6 commits intopython:mainfromyihong0618:hy/handle_base64_sysin
Nov 11, 2025

Conversation

@yihong0618
Copy link
Contributor

@yihong0618yihong0618 commentedSep 11, 2025
edited by bedevere-appbot
Loading

This makepython -m base64 the same behavior asast orjson

Signed-off-by: yihong0618 <zouzou0208@gmail.com>
@yihong0618yihong0618 changed the titlegh-138775: fix handlepython -m base64 stdin correct with EOF single.gh-138775: fix handlepython -m base64 stdin correct with EOF signal.Sep 11, 2025
@picnixzpicnixz changed the titlegh-138775: fix handlepython -m base64 stdin correct with EOF signal.gh-138775: fix handlepython -m base64 stdin correct with EOF signalSep 11, 2025
@aisk
Copy link
Contributor

aisk commentedSep 11, 2025
edited
Loading

I’m concerned that the current implementation reads the input in chunks, this allows the function to process the input as a stream via a pipe and avoids using too much memory with large inputs:

cpython/Lib/base64.py

Lines 531 to 537 in4978bfc

defencode(input,output):
"""Encode a file; input and output are binary files."""
whiles:=input.read(MAXBINSIZE):
whilelen(s)<MAXBINSIZEand (ns:=input.read(MAXBINSIZE-len(s))):
s+=ns
line=binascii.b2a_base64(s)
output.write(line)

With this change, however, all input is read into memory, which breaks the chunked behavior.

@yihong0618
Copy link
ContributorAuthor

I’m concerned that the current implementation reads the input in chunks, this allows the function to process the input as a stream via a pipe and avoids using too much memory with large inputs. :

cpython/Lib/base64.py

Lines 531 to 537 in4978bfc

defencode(input,output):
"""Encode a file; input and output are binary files."""
whiles:=input.read(MAXBINSIZE):
whilelen(s)<MAXBINSIZEand (ns:=input.read(MAXBINSIZE-len(s))):
s+=ns
line=binascii.b2a_base64(s)
output.write(line)

With this change, however, all input is read into memory, which breaks the chunked behavior.

Thanks for the comments

I will checked ifisatty to check
if
echo 1234 | ./python.exe -m base64
or
./python.exe -m base64
and do not break the old method

and in stdin old behavior is wrong, so I think is fine.
will check isatty if can check or not.

Signed-off-by: yihong0618 <zouzou0208@gmail.com>
@yihong0618
Copy link
ContributorAuthor

keep the old behavior and useisatty to check
as dochttps://docs.python.org/3/library/sys.html#sys.stdin

@yihong0618
Copy link
ContributorAuthor

Thank you very much for the review again, I will try to learn how to express the change more clearly.
and do better next time, thanks again

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@picnixz
Copy link
Member

I will try to learn how to express the change more clearly.
and do better next time, thanks again

Don't worry. Considering English isn't your first language, you can just leave formulation matters to others.

yihong0618 reacted with heart emoji

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
@yihong0618

This comment was marked as resolved.

@ZeroIntensity

This comment was marked as resolved.

@yihong0618

This comment was marked as resolved.

Signed-off-by: yihong0618 <zouzou0208@gmail.com>
@gpsheadgpshead added needs backport to 3.13bugs and security fixes needs backport to 3.14bugs and security fixes labelsNov 11, 2025
@gpsheadgpsheadenabled auto-merge (squash)November 11, 2025 23:22
@gpshead
Copy link
Member

I went with this PR after playing around with the BufferedReader connected to a terminal - it really is difficult to tell when you have an EOF in that scenario vs what thebase64.encode() code wants to do when reading fixed sized blocks at a time in order to produce the fixed sized output lines. subsequent .read() calls after a short read or even after a 0 length read (the normal EOF indicator) still block and could later return more data (ugh, terminals!). So doing the isatty check does seem most practical here given that theio stack.

yihong0618 reacted with heart emoji

@gpsheadgpshead merged commitf5c2a41 intopython:mainNov 11, 2025
54 checks passed
@miss-islington-app
Copy link

Thanks@yihong0618 for the PR, and@gpshead for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestNov 11, 2025
… signal (pythonGH-138776)* fix: handle  stdin correct with EOF single.* fix: flollow the comments when pipe stdin use buffer* Apply suggestions from code review* fix: apply review comments in Lib/base64.py* fix: address comments* Reword comment and NEWS entry.---------(cherry picked from commit f5c2a41f9a6b3be95c5be9dbae0a4a3342d356dc)Co-authored-by: yihong <zouzou0208@gmail.com>Signed-off-by: yihong0618 <zouzou0208@gmail.com>Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>Co-authored-by: Peter Bierma <zintensitydev@gmail.com>Co-authored-by: Gregory P. Smith <greg@krypto.org>
@bedevere-app
Copy link

GH-141432 is a backport of this pull request to the3.14 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.14bugs and security fixes labelNov 11, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestNov 11, 2025
… signal (pythonGH-138776)* fix: handle  stdin correct with EOF single.* fix: flollow the comments when pipe stdin use buffer* Apply suggestions from code review* fix: apply review comments in Lib/base64.py* fix: address comments* Reword comment and NEWS entry.---------(cherry picked from commit f5c2a41f9a6b3be95c5be9dbae0a4a3342d356dc)Co-authored-by: yihong <zouzou0208@gmail.com>Signed-off-by: yihong0618 <zouzou0208@gmail.com>Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>Co-authored-by: Peter Bierma <zintensitydev@gmail.com>Co-authored-by: Gregory P. Smith <greg@krypto.org>
@bedevere-app
Copy link

GH-141433 is a backport of this pull request to the3.13 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.13bugs and security fixes labelNov 11, 2025
gpshead added a commit that referenced this pull requestNov 12, 2025
…F signal (GH-138776) (#141433)gh-138775: fix handle `python -m base64` stdin correct with EOF signal (GH-138776)* fix: handle  stdin correct with EOF single.* fix: flollow the comments when pipe stdin use buffer* Apply suggestions from code review* fix: apply review comments in Lib/base64.py* fix: address comments* Reword comment and NEWS entry.---------(cherry picked from commitf5c2a41)Signed-off-by: yihong0618 <zouzou0208@gmail.com>Co-authored-by: yihong <zouzou0208@gmail.com>Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>Co-authored-by: Peter Bierma <zintensitydev@gmail.com>Co-authored-by: Gregory P. Smith <greg@krypto.org>
gpshead added a commit that referenced this pull requestNov 12, 2025
…F signal (GH-138776) (#141432)gh-138775: fix handle `python -m base64` stdin correct with EOF signal (GH-138776)* fix: handle  stdin correct with EOF single.* fix: flollow the comments when pipe stdin use buffer* Apply suggestions from code review* fix: apply review comments in Lib/base64.py* fix: address comments* Reword comment and NEWS entry.---------(cherry picked from commitf5c2a41)Signed-off-by: yihong0618 <zouzou0208@gmail.com>Co-authored-by: yihong <zouzou0208@gmail.com>Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>Co-authored-by: Peter Bierma <zintensitydev@gmail.com>Co-authored-by: Gregory P. Smith <greg@krypto.org>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@picnixzpicnixzpicnixz left review comments

@ZeroIntensityZeroIntensityZeroIntensity left review comments

@gpsheadgpsheadgpshead approved these changes

@aiskaiskaisk approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

5 participants

@yihong0618@aisk@picnixz@ZeroIntensity@gpshead

[8]ページ先頭

©2009-2025 Movatter.jp