https://github.com/python/cpython/commit/2bd9e79a3f449c0b45bfc182b0c22747a3322398
commit: 2bd9e79a3f449c0b45bfc182b0c22747a3322398
branch: main
author: Cody Maloney <[email protected]>
committer: ambv <[email protected]>
date: 2026-10-23T11:45:02-07:00
summary:
gh-151814: Add test for TextIOWrapper.write unbounded memory growth from
repeated writes (GH-152244)
Use tracemalloc to measure memory usage.
files:
M Lib/test/test_io/test_textio.py
diff --git a/Lib/test/test_io/test_textio.py b/Lib/test/test_io/test_textio.py
index 04867f113b05a9..cd3abd49ba8dde 100644
--- a/Lib/test/test_io/test_textio.py
+++ b/Lib/test/test_io/test_textio.py
@@ -6,6 +6,7 @@
import sys
import threading
import time
+import tracemalloc
import unittest
import warnings
import weakref
@@ -1580,6 +1581,21 @@ def write(self, data):
t.write("x"*chunk_size)
self.assertEqual([b"abcdef", b"ghi", b"x"*chunk_size],
buf._write_stack)
+ def test_write_empty_no_memory_growth(self):
+ # gh-151814: Writing empty string should have stable memory usage.
+ t = self.TextIOWrapper(self.BytesIO())
+ nwrites = 200_000
+ tracemalloc.start()
+ try:
+ for _ in range(nwrites):
+ t.write("")
+ _, peak = tracemalloc.get_traced_memory()
+ finally:
+ tracemalloc.stop()
+
+ # No longer leaks several bytes per write.
+ self.assertLess(peak, nwrites)
+
def test_issue119506(self):
chunk_size = 8192
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]