]> granicus.if.org Git - python/commitdiff
Make cStringIO.truncate raise IOError for negative
authorMartin v. Löwis <martin@v.loewis.de>
Sun, 19 Nov 2006 10:41:41 +0000 (10:41 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Sun, 19 Nov 2006 10:41:41 +0000 (10:41 +0000)
arguments (even for -1). Fixes the last bit of
#1359365.

Lib/test/test_StringIO.py
Misc/NEWS
Modules/cStringIO.c

index aa36b098443c336dd2b7e777ff82f81b4fd4e088..9f79b02e16c8812f0780d8e43e52d03b218b2d1c 100644 (file)
@@ -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')
 
index 6fd73f6426583bf363dd6770b65d8fffc42f3d15..9b2aebae3b0f167fe13cf17651b1e27baf88150b 100644 (file)
--- 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
index 100891ba4ad321a93f91868a0b4d257a89526404..3f762b09fb02692a388bdd805e3e99fc32370c34 100644 (file)
@@ -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;