Skip to content

Conversation

@yihong0618
Copy link
Contributor

@yihong0618 yihong0618 commented Sep 11, 2025

This make python -m base64 the same behavior as ast or json

@yihong0618 yihong0618 changed the title gh-138775: fix handle python -m base64 stdin correct with EOF single. gh-138775: fix handle python -m base64 stdin correct with EOF signal. Sep 11, 2025
@picnixz picnixz changed the title gh-138775: fix handle python -m base64 stdin correct with EOF signal. gh-138775: fix handle python -m base64 stdin correct with EOF signal Sep 11, 2025
@aisk
Copy link
Contributor

aisk commented Sep 11, 2025

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 in 4978bfc

def encode(input, output):
"""Encode a file; input and output are binary files."""
while s := input.read(MAXBINSIZE):
while len(s) < MAXBINSIZE and (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
Contributor Author

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 in 4978bfc

def encode(input, output):
"""Encode a file; input and output are binary files."""
while s := input.read(MAXBINSIZE):
while len(s) < MAXBINSIZE and (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 if isatty 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.

@yihong0618
Copy link
Contributor Author

keep the old behavior and use isatty to check
as doc https://docs.python.org/3/library/sys.html#sys.stdin

@yihong0618
Copy link
Contributor Author

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

@picnixz
Copy link
Member

picnixz commented Sep 11, 2025

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

This comment was marked as resolved.

@ZeroIntensity

This comment was marked as resolved.

@yihong0618

This comment was marked as resolved.

Signed-off-by: yihong0618 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants