diff --git a/Lib/_pyio.py b/Lib/_pyio.py index c91a647a2f67b0d..350d70061d4624e 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -342,10 +342,12 @@ def tell(self): return self.seek(0, 1) def truncate(self, pos=None): - """Truncate file to size bytes. + """Resize stream to at most 'pos' bytes. - Size defaults to the current IO position as reported by tell(). Return - the new size. + The position in the stream is left unchanged. The size + defaults to the current IO position as reported by tell(). If + the stream's size is increased, the contents of the new file + area are undetermined. Returns the new size. """ self._unsupported("truncate") @@ -1670,10 +1672,12 @@ def tell(self): return os.lseek(self._fd, 0, SEEK_CUR) def truncate(self, size=None): - """Truncate the file to at most size bytes. + """Resize stream to at most 'size' bytes. - Size defaults to the current file position, as returned by tell(). - The current file position is changed to the value of size. + The position in the stream is left unchanged. The size + defaults to the current IO position as reported by tell(). If + the stream's size is increased, the contents of the new file + area are undetermined. Returns the new size. """ self._checkClosed() self._checkWritable() @@ -1778,7 +1782,13 @@ def write(self, s): self._unsupported("write") def truncate(self, pos=None): - """Truncate size to pos, where pos is an int.""" + """Resize stream to at most 'pos' characters. + + The position in the stream is left unchanged. The size + defaults to the current IO position as reported by tell(). If + the stream's size is increased, the contents of the new file + area are undetermined. Returns the new size. + """ self._unsupported("truncate") def readline(self): diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c index ba33f8c50d6aade..a52979357d2530b 100644 --- a/Modules/_io/bytesio.c +++ b/Modules/_io/bytesio.c @@ -552,15 +552,17 @@ _io.BytesIO.truncate size: Py_ssize_t(accept={int, NoneType}, c_default="self->pos") = None / -Truncate the file to at most size bytes. +Resize stream to at most 'size' bytes. -Size defaults to the current file position, as returned by tell(). -The current file position is unchanged. Returns the new size. +Position in the stream is left unchanged. Size defaults to +the current IO position as reported by tell(). If the stream's +size is increased, the contents of the new file area are +undetermined. Returns the new size. [clinic start generated code]*/ static PyObject * _io_BytesIO_truncate_impl(bytesio *self, Py_ssize_t size) -/*[clinic end generated code: output=9ad17650c15fa09b input=423759dd42d2f7c1]*/ +/*[clinic end generated code: output=9ad17650c15fa09b input=0f69785dcc22737e]*/ { CHECK_CLOSED(self); CHECK_EXPORTS(self); diff --git a/Modules/_io/clinic/bytesio.c.h b/Modules/_io/clinic/bytesio.c.h index 5c155cb3066e3b5..f31e34551b63e47 100644 --- a/Modules/_io/clinic/bytesio.c.h +++ b/Modules/_io/clinic/bytesio.c.h @@ -314,10 +314,12 @@ PyDoc_STRVAR(_io_BytesIO_truncate__doc__, "truncate($self, size=None, /)\n" "--\n" "\n" -"Truncate the file to at most size bytes.\n" +"Resize stream to at most \'size\' bytes.\n" "\n" -"Size defaults to the current file position, as returned by tell().\n" -"The current file position is unchanged. Returns the new size."); +"Position in the stream is left unchanged. Size defaults to\n" +"the current IO position as reported by tell(). If the stream\'s\n" +"size is increased, the contents of the new file area are\n" +"undetermined. Returns the new size."); #define _IO_BYTESIO_TRUNCATE_METHODDEF \ {"truncate", (PyCFunction)_io_BytesIO_truncate, METH_FASTCALL, _io_BytesIO_truncate__doc__}, @@ -444,4 +446,4 @@ _io_BytesIO___init__(PyObject *self, PyObject *args, PyObject *kwargs) exit: return return_value; } -/*[clinic end generated code: output=9ba9a68c8c5669e7 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=934f685bde756d6d input=a9049054013a1b77]*/ diff --git a/Modules/_io/clinic/fileio.c.h b/Modules/_io/clinic/fileio.c.h index 8a9958aaecf0de9..048fa8716d5c198 100644 --- a/Modules/_io/clinic/fileio.c.h +++ b/Modules/_io/clinic/fileio.c.h @@ -322,10 +322,12 @@ PyDoc_STRVAR(_io_FileIO_truncate__doc__, "truncate($self, size=None, /)\n" "--\n" "\n" -"Truncate the file to at most size bytes and return the truncated size.\n" +"Resize stream to at most \'size\' bytes.\n" "\n" -"Size defaults to the current file position, as returned by tell().\n" -"The current file position is changed to the value of size."); +"Position in the stream is left unchanged. Size defaults to\n" +"the current IO position as reported by tell(). If the stream\'s\n" +"size is increased, the contents of the new file area are\n" +"undetermined. Returns the new size."); #define _IO_FILEIO_TRUNCATE_METHODDEF \ {"truncate", (PyCFunction)_io_FileIO_truncate, METH_FASTCALL, _io_FileIO_truncate__doc__}, @@ -373,4 +375,4 @@ _io_FileIO_isatty(fileio *self, PyObject *Py_UNUSED(ignored)) #ifndef _IO_FILEIO_TRUNCATE_METHODDEF #define _IO_FILEIO_TRUNCATE_METHODDEF #endif /* !defined(_IO_FILEIO_TRUNCATE_METHODDEF) */ -/*[clinic end generated code: output=a8796438c8b7c49a input=a9049054013a1b77]*/ +/*[clinic end generated code: output=b0ef426afce9a6c3 input=a9049054013a1b77]*/ diff --git a/Modules/_io/clinic/stringio.c.h b/Modules/_io/clinic/stringio.c.h index 72b2540d47aab98..1bbf30ce7e0b55d 100644 --- a/Modules/_io/clinic/stringio.c.h +++ b/Modules/_io/clinic/stringio.c.h @@ -103,11 +103,12 @@ PyDoc_STRVAR(_io_StringIO_truncate__doc__, "truncate($self, pos=None, /)\n" "--\n" "\n" -"Truncate size to pos.\n" +"Resize stream to at most \'pos\' characters.\n" "\n" -"The pos argument defaults to the current file position, as\n" -"returned by tell(). The current file position is unchanged.\n" -"Returns the new absolute position."); +"The position in the stream is left unchanged. The size defaults to\n" +"the current IO position as reported by tell(). If the stream\'s size\n" +"is increased, the contents of the new file area are undetermined.\n" +"Returns the new size."); #define _IO_STRINGIO_TRUNCATE_METHODDEF \ {"truncate", (PyCFunction)_io_StringIO_truncate, METH_FASTCALL, _io_StringIO_truncate__doc__}, @@ -286,4 +287,4 @@ _io_StringIO_seekable(stringio *self, PyObject *Py_UNUSED(ignored)) { return _io_StringIO_seekable_impl(self); } -/*[clinic end generated code: output=73c4d6e5cc3b1a58 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=64a903b7c3c45564 input=a9049054013a1b77]*/ diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index 0d5bf3b22114e5e..427c1b59bb64385 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -979,15 +979,17 @@ _io.FileIO.truncate size as posobj: object = NULL / -Truncate the file to at most size bytes and return the truncated size. +Resize stream to at most 'size' bytes. -Size defaults to the current file position, as returned by tell(). -The current file position is changed to the value of size. +Position in the stream is left unchanged. Size defaults to +the current IO position as reported by tell(). If the stream's +size is increased, the contents of the new file area are +undetermined. Returns the new size. [clinic start generated code]*/ static PyObject * _io_FileIO_truncate_impl(fileio *self, PyObject *posobj) -/*[clinic end generated code: output=e49ca7a916c176fa input=9026af44686b7318]*/ +/*[clinic end generated code: output=e49ca7a916c176fa input=b60ed73a8ce11d96]*/ { Py_off_t pos; int ret; diff --git a/Modules/_io/iobase.c b/Modules/_io/iobase.c index 0c329bff840bd3c..b696290444ca05d 100644 --- a/Modules/_io/iobase.c +++ b/Modules/_io/iobase.c @@ -118,10 +118,12 @@ _io__IOBase_tell_impl(PyObject *self) } PyDoc_STRVAR(iobase_truncate_doc, - "Truncate file to size bytes.\n" + "Resize stream to at most 'size' bytes.\n" "\n" - "File pointer is left unchanged. Size defaults to the current IO\n" - "position as reported by tell(). Returns the new size."); + "Position in the stream is left unchanged. Size defaults to\n" + "the current IO position as reported by tell(). If the stream's\n" + "size is increased, the contents of the new file area are undetermined.\n" + "Returns the new size."); static PyObject * iobase_truncate(PyObject *self, PyObject *args) diff --git a/Modules/_io/stringio.c b/Modules/_io/stringio.c index 718b1ac1d82db2b..9a7483e744a8d0c 100644 --- a/Modules/_io/stringio.c +++ b/Modules/_io/stringio.c @@ -435,16 +435,17 @@ _io.StringIO.truncate pos as size: Py_ssize_t(accept={int, NoneType}, c_default="self->pos") = None / -Truncate size to pos. +Resize stream to at most 'pos' characters. -The pos argument defaults to the current file position, as -returned by tell(). The current file position is unchanged. -Returns the new absolute position. +The position in the stream is left unchanged. The size defaults to +the current IO position as reported by tell(). If the stream's size +is increased, the contents of the new file area are undetermined. +Returns the new size. [clinic start generated code]*/ static PyObject * _io_StringIO_truncate_impl(stringio *self, Py_ssize_t size) -/*[clinic end generated code: output=eb3aef8e06701365 input=5505cff90ca48b96]*/ +/*[clinic end generated code: output=eb3aef8e06701365 input=f2b6a674a34b14a5]*/ { CHECK_INITIALIZED(self); CHECK_CLOSED(self);