From cffcc8b195d0086d6089ac4563ac17b4c95471e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Sun, 19 Nov 2006 10:41:41 +0000 Subject: Make cStringIO.truncate raise IOError for negative arguments (even for -1). Fixes the last bit of #1359365. --- Lib/test/test_StringIO.py | 1 + Misc/NEWS | 3 +++ Modules/cStringIO.c | 12 +++++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_StringIO.py b/Lib/test/test_StringIO.py index aa36b09..9f79b02 100644 --- a/Lib/test/test_StringIO.py +++ b/Lib/test/test_StringIO.py @@ -62,6 +62,7 @@ class TestGenericStringIO(unittest.TestCase): eq(f.getvalue(), 'abcde') f.write('xyz') eq(f.getvalue(), 'abcdexyz') + self.assertRaises(IOError, f.truncate, -1) f.close() self.assertRaises(ValueError, f.write, 'frobnitz') diff --git a/Misc/NEWS b/Misc/NEWS index 6fd73f6..9b2aeba 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -101,6 +101,9 @@ Core and builtins Library ------- +- cStringIO.truncate(-1) now raises an IOError, like StringIO and + regular files. + - Patch #1472877: Fix Tix subwidget name resolution. - Patch #1594554: Always close a tkSimpleDialog on ok(), even diff --git a/Modules/cStringIO.c b/Modules/cStringIO.c index 100891b..3f762b0 100644 --- a/Modules/cStringIO.c +++ b/Modules/cStringIO.c @@ -289,7 +289,17 @@ IO_truncate(IOobject *self, PyObject *args) { if (!IO__opencheck(self)) return NULL; if (!PyArg_ParseTuple(args, "|n:truncate", &pos)) return NULL; - if (pos < 0) pos = self->pos; + + if (PyTuple_Size(args) == 0) { + /* No argument passed, truncate to current position */ + pos = self->pos; + } + + if (pos < 0) { + errno = EINVAL; + PyErr_SetFromErrno(PyExc_IOError); + return NULL; + } if (self->string_size > pos) self->string_size = pos; self->pos = self->string_size; -- cgit v0.12