truncate() returns the new size and position.
write() returns the number of bytes/characters written/buffered.
FileIO.close() calls self.flush().
Implement readinto() for buffered readers.
Tests th check all these.
Test proper behavior of __enter__/__exit__.
diff --git a/Modules/_fileio.c b/Modules/_fileio.c
index 985fa81..97b2199 100644
--- a/Modules/_fileio.c
+++ b/Modules/_fileio.c
@@ -530,6 +530,9 @@
if (!PyArg_ParseTuple(args, "|O", &posobj))
return NULL;
+ if (posobj == Py_None)
+ posobj = NULL;
+
if (posobj == NULL)
whence = 1;
else
@@ -545,19 +548,22 @@
pos = PyLong_Check(posobj) ?
PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
#endif
- Py_DECREF(posobj);
- if (PyErr_Occurred())
+ if (PyErr_Occurred()) {
+ Py_DECREF(posobj);
return NULL;
+ }
Py_BEGIN_ALLOW_THREADS
errno = 0;
pos = ftruncate(fd, pos);
Py_END_ALLOW_THREADS
- if (errno < 0)
+ if (pos < 0) {
+ Py_DECREF(posobj);
PyErr_SetFromErrno(PyExc_IOError);
+ }
- Py_RETURN_NONE;
+ return posobj;
}
static char *