Skip to content
6 changes: 3 additions & 3 deletions Lib/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,9 +849,9 @@ def write(self, s):

def writelines(self, iterable):
file = self._file
rv = file.writelines(iterable)
self._check(file)
return rv
for line in iterable:
file.write(line)
self._check(file)

def detach(self):
return self._file.detach()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The ``writelines()`` method in ``SpooledTemporaryFile`` previously only checked whether to roll over to a disk-backed file after the entire iterable was written. This led to high memory usage when handling large iterators, as the in-memory buffer grew without limit. The method was modified to write each line individually and check the buffer size after each write. If the maximum size is exceeded, the file rolls over to disk immediately, preventing excessive memory consumption.
Loading