Skip to content

Commit 2bd9e79

Browse files
authored
gh-151814: Add test for TextIOWrapper.write unbounded memory growth from repeated writes (GH-152244)
Use tracemalloc to measure memory usage.
1 parent f9f33a6 commit 2bd9e79

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

‎Lib/test/test_io/test_textio.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sys
77
import threading
88
import time
9+
import tracemalloc
910
import unittest
1011
import warnings
1112
import weakref
@@ -1580,6 +1581,21 @@ def write(self, data):
15801581
t.write("x"*chunk_size)
15811582
self.assertEqual([b"abcdef", b"ghi", b"x"*chunk_size], buf._write_stack)
15821583

1584+
def test_write_empty_no_memory_growth(self):
1585+
# gh-151814: Writing empty string should have stable memory usage.
1586+
t = self.TextIOWrapper(self.BytesIO())
1587+
nwrites = 200_000
1588+
tracemalloc.start()
1589+
try:
1590+
for _ in range(nwrites):
1591+
t.write("")
1592+
_, peak = tracemalloc.get_traced_memory()
1593+
finally:
1594+
tracemalloc.stop()
1595+
1596+
# No longer leaks several bytes per write.
1597+
self.assertLess(peak, nwrites)
1598+
15831599
def test_issue119506(self):
15841600
chunk_size = 8192
15851601

0 commit comments

Comments
 (0)