Skip to content

gh-161934: Make print() use a single write() call - #161936

Open
methane wants to merge 8 commits into
python:mainfrom
methane:codex/textio-print2
Open

methane wants to merge 8 commits into
python:mainfrom
methane:codex/textio-print2

Conversation

@methane

@methane methane commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

@read-the-docs-community

read-the-docs-community Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #36012934 | 📁 Comparing 2434c39 against main (d8d560b)

  🔍 Preview build  

17 files changed · + 1 added · ± 16 modified

+ Added

± Modified

@methane
methane force-pushed the codex/textio-print2 branch from fd7f7ec to d5def80 Compare November 14, 2026 05:48

@cmaloney cmaloney left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking forward to microbenchmark performance numbers, two high level comments:

  1. This pretty comprehensively rewrites write in TextIOWrapper while adding the new write_list. That is intricate and I think should be a PR of its own for reviewability. There's a lot of buffering edgecases and bugfix comments in rewritten code.

  2. If someone prints 40 million ints comma separated or 16 x 1GB strings this is going to spend a long time building up the list while not printing (and if it OOMs likely never printing). I think there needs to be a pending_bytes_count > chunk_size just like TextIOWrapper's buffering had. Happy for that to be a modern constant default of multi-MB though. Care about the edge cases (ex. printing a list of 16 x 1GB strings) having reasonable behavior.

    With unintended recursion while developing things tend to run into long accidental strings and often I print() debug to figure out "what is happening".

@methane

methane commented Nov 14, 2026

Copy link
Copy Markdown
Member Author

Looking forward to microbenchmark performance numbers,

https://gist.github.com/methane/fb1d38b82ae1934eb19e10dcfef72a08

  1. This pretty comprehensively rewrites write in TextIOWrapper while adding the new write_list. That is intricate and I think should be a PR of its own for reviewability. There's a lot of buffering edgecases and bugfix comments in rewritten code.

Most of the changes to textio.c are due to splitting up write(). I've created a separate PR for that part first.

#161991

@methane
methane force-pushed the codex/textio-print2 branch from e3f5eaa to 939ee2d Compare November 19, 2026 12:46
Use print's existing join and per-part fallback for TextIOWrapper slow paths. Route custom buffers through write() to avoid repeating their closed callback.
@methane
methane force-pushed the codex/textio-print2 branch from 939ee2d to 2434c39 Compare November 19, 2026 12:48
Comment thread Lib/test/test_print.py
return self.x


class RecordingFile:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think there's too much new test added here. RecordingFile and RecordingBytesIO are already models that exist in other I/O tests, want to reuse existing infrastructure and not repeat already exercised cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review topic-IO type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants