Skip to content

Commit 69a524e

Browse files
committed
gh-151814: Add test for TextIOWrapper.write unbounded memory growth from repeated writes
Use tracemalloc to measure memory usage.
1 parent 55bc312 commit 69a524e

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
@@ -1522,6 +1523,21 @@ def write(self, data):
15221523
t.write("x"*chunk_size)
15231524
self.assertEqual([b"abcdef", b"ghi", b"x"*chunk_size], buf._write_stack)
15241525

1526+
def test_write_empty_no_memory_growth(self):
1527+
# gh-151814: Writing empty string should have stable memory usage.
1528+
t = self.TextIOWrapper(self.BytesIO())
1529+
nwrites = 200_000
1530+
tracemalloc.start()
1531+
try:
1532+
for _ in range(nwrites):
1533+
t.write("")
1534+
_, peak = tracemalloc.get_traced_memory()
1535+
finally:
1536+
tracemalloc.stop()
1537+
1538+
# No longer leaks several bytes per write.
1539+
self.assertLess(peak, nwrites)
1540+
15251541
def test_issue119506(self):
15261542
chunk_size = 8192
15271543

0 commit comments

Comments
 (0)