Skip to content

Commit 06a1c3f

Browse files
authored
gh-120754: Update estimated_size in C truncate (#121357)
Sometimes a large file is truncated (test_largefile). While estimated_size is used as a estimate (the read will stil get the number of bytes in the file), that it is much larger than the actual size of data can result in a significant over allocation and sometimes lead to a MemoryError / running out of memory. This brings the C implementation to match the Python _pyio implementation.
1 parent 19d1e43 commit 06a1c3f

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

Modules/_io/fileio.c

+6
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,12 @@ _io_FileIO_truncate_impl(fileio *self, PyTypeObject *cls, PyObject *posobj)
10941094
return NULL;
10951095
}
10961096

1097+
/* Sometimes a large file is truncated. While estimated_size is used as a
1098+
estimate, that it is much larger than the actual size can result in a
1099+
significant over allocation and sometimes a MemoryError / running out of
1100+
memory. */
1101+
self->estimated_size = pos;
1102+
10971103
return posobj;
10981104
}
10991105
#endif /* HAVE_FTRUNCATE */

0 commit comments

Comments
 (0)