aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Modules/_io/stringio.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_io/stringio.c')
-rw-r--r--Modules/_io/stringio.c24
1 files changed, 17 insertions, 7 deletions
diff --git a/Modules/_io/stringio.c b/Modules/_io/stringio.c
index 9d1bfa3ea05..56913fafefb 100644
--- a/Modules/_io/stringio.c
+++ b/Modules/_io/stringio.c
@@ -444,7 +444,7 @@ stringio_iternext(PyObject *op)
/*[clinic input]
@critical_section
_io.StringIO.truncate
- pos as size: Py_ssize_t(accept={int, NoneType}, c_default="((stringio *)self)->pos") = None
+ pos: object = None
/
Truncate size to pos.
@@ -455,16 +455,26 @@ Returns the new absolute position.
[clinic start generated code]*/
static PyObject *
-_io_StringIO_truncate_impl(stringio *self, Py_ssize_t size)
-/*[clinic end generated code: output=eb3aef8e06701365 input=fa8a6c98bb2ba780]*/
+_io_StringIO_truncate_impl(stringio *self, PyObject *pos)
+/*[clinic end generated code: output=c76c43b5ecfaf4e2 input=d59fd2ee49757ae6]*/
{
CHECK_INITIALIZED(self);
CHECK_CLOSED(self);
- if (size < 0) {
- PyErr_Format(PyExc_ValueError,
- "Negative size value %zd", size);
- return NULL;
+ Py_ssize_t size;
+ if (pos == Py_None) {
+ size = self->pos;
+ }
+ else {
+ size = PyLong_AsLong(pos);
+ if (size == -1 && PyErr_Occurred()) {
+ return NULL;
+ }
+ if (size < 0) {
+ PyErr_Format(PyExc_ValueError,
+ "negative pos value %zd", size);
+ return NULL;
+ }
}
if (size < self->string_size) {