blob: 59cd3f7ecf565cdbf030d61d6af1d7c4a35ea707 [file] [log] [blame]
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001/* Author: Daniel Stutzbach */
2
3#define PY_SSIZE_T_CLEAN
4#include "Python.h"
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8#include <stddef.h> /* For offsetof */
Antoine Pitrou19690592009-06-12 20:14:08 +00009#include "_iomodule.h"
Christian Heimes7f39c9f2008-01-25 12:18:43 +000010
11/*
12 * Known likely problems:
13 *
14 * - Files larger then 2**32-1
15 * - Files with unicode filenames
16 * - Passing numbers greater than 2**32-1 when an integer is expected
17 * - Making it work on Windows and other oddball platforms
18 *
19 * To Do:
20 *
21 * - autoconfify header file inclusion
22 */
23
24#ifdef MS_WINDOWS
25/* can simulate truncate with Win32 API functions; see file_truncate */
26#define HAVE_FTRUNCATE
27#define WIN32_LEAN_AND_MEAN
28#include <windows.h>
29#endif
30
Antoine Pitrou19690592009-06-12 20:14:08 +000031#if BUFSIZ < (8*1024)
32#define SMALLCHUNK (8*1024)
33#elif (BUFSIZ >= (2 << 25))
34#error "unreasonable BUFSIZ > 64MB defined"
35#else
36#define SMALLCHUNK BUFSIZ
37#endif
38
39#if SIZEOF_INT < 4
40#define BIGCHUNK (512 * 32)
41#else
42#define BIGCHUNK (512 * 1024)
43#endif
44
Christian Heimes7f39c9f2008-01-25 12:18:43 +000045typedef struct {
46 PyObject_HEAD
47 int fd;
Antoine Pitrou2a466582009-09-21 21:17:48 +000048 unsigned int readable : 1;
49 unsigned int writable : 1;
50 signed int seekable : 2; /* -1 means unknown */
51 unsigned int closefd : 1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +000052 PyObject *weakreflist;
Antoine Pitrou19690592009-06-12 20:14:08 +000053 PyObject *dict;
54} fileio;
Christian Heimes7f39c9f2008-01-25 12:18:43 +000055
56PyTypeObject PyFileIO_Type;
57
58#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
59
Antoine Pitrou19690592009-06-12 20:14:08 +000060int
61_PyFileIO_closed(PyObject *self)
62{
63 return ((fileio *)self)->fd < 0;
64}
65
Antoine Pitroue741cc62009-01-21 00:45:36 +000066static PyObject *
67portable_lseek(int fd, PyObject *posobj, int whence);
68
Antoine Pitrou19690592009-06-12 20:14:08 +000069static PyObject *portable_lseek(int fd, PyObject *posobj, int whence);
70
71/* Returns 0 on success, -1 with exception set on failure. */
Christian Heimes7f39c9f2008-01-25 12:18:43 +000072static int
Antoine Pitrou19690592009-06-12 20:14:08 +000073internal_close(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +000074{
Antoine Pitrou19690592009-06-12 20:14:08 +000075 int err = 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +000076 int save_errno = 0;
77 if (self->fd >= 0) {
78 int fd = self->fd;
79 self->fd = -1;
Antoine Pitrou19690592009-06-12 20:14:08 +000080 /* fd is accessible and someone else may have closed it */
81 if (_PyVerify_fd(fd)) {
82 Py_BEGIN_ALLOW_THREADS
83 err = close(fd);
84 if (err < 0)
85 save_errno = errno;
86 Py_END_ALLOW_THREADS
87 } else {
Christian Heimes7f39c9f2008-01-25 12:18:43 +000088 save_errno = errno;
Antoine Pitrou19690592009-06-12 20:14:08 +000089 err = -1;
90 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +000091 }
Antoine Pitrou19690592009-06-12 20:14:08 +000092 if (err < 0) {
93 errno = save_errno;
94 PyErr_SetFromErrno(PyExc_IOError);
95 return -1;
96 }
97 return 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +000098}
99
100static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000101fileio_close(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000102{
103 if (!self->closefd) {
Amaury Forgeot d'Arc32265652008-11-20 23:34:31 +0000104 self->fd = -1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000105 Py_RETURN_NONE;
106 }
107 errno = internal_close(self);
Antoine Pitrou19690592009-06-12 20:14:08 +0000108 if (errno < 0)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000109 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000110
Antoine Pitrou19690592009-06-12 20:14:08 +0000111 return PyObject_CallMethod((PyObject*)&PyRawIOBase_Type,
112 "close", "O", self);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000113}
114
115static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000116fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000117{
Antoine Pitrou19690592009-06-12 20:14:08 +0000118 fileio *self;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000119
120 assert(type != NULL && type->tp_alloc != NULL);
121
Antoine Pitrou19690592009-06-12 20:14:08 +0000122 self = (fileio *) type->tp_alloc(type, 0);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000123 if (self != NULL) {
124 self->fd = -1;
Christian Heimesab5f8792008-10-30 21:26:15 +0000125 self->readable = 0;
126 self->writable = 0;
127 self->seekable = -1;
128 self->closefd = 1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000129 self->weakreflist = NULL;
130 }
131
132 return (PyObject *) self;
133}
134
135/* On Unix, open will succeed for directories.
136 In Python, there should be no file objects referring to
137 directories, so we need a check. */
138
139static int
Antoine Pitrou19690592009-06-12 20:14:08 +0000140dircheck(fileio* self, const char *name)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000141{
142#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
143 struct stat buf;
144 if (self->fd < 0)
145 return 0;
146 if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000147 char *msg = strerror(EISDIR);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000148 PyObject *exc;
Antoine Pitrou19690592009-06-12 20:14:08 +0000149 if (internal_close(self))
150 return -1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000151
Benjamin Peterson7af65562008-12-29 17:56:58 +0000152 exc = PyObject_CallFunction(PyExc_IOError, "(iss)",
153 EISDIR, msg, name);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000154 PyErr_SetObject(PyExc_IOError, exc);
155 Py_XDECREF(exc);
156 return -1;
157 }
158#endif
159 return 0;
160}
161
Benjamin Peterson5848d1f2009-01-19 00:08:08 +0000162static int
163check_fd(int fd)
164{
165#if defined(HAVE_FSTAT)
166 struct stat buf;
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000167 if (!_PyVerify_fd(fd) || (fstat(fd, &buf) < 0 && errno == EBADF)) {
Benjamin Peterson5848d1f2009-01-19 00:08:08 +0000168 PyObject *exc;
169 char *msg = strerror(EBADF);
170 exc = PyObject_CallFunction(PyExc_OSError, "(is)",
171 EBADF, msg);
172 PyErr_SetObject(PyExc_OSError, exc);
173 Py_XDECREF(exc);
174 return -1;
175 }
176#endif
177 return 0;
178}
179
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000180
181static int
182fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
183{
Antoine Pitrou19690592009-06-12 20:14:08 +0000184 fileio *self = (fileio *) oself;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000185 static char *kwlist[] = {"file", "mode", "closefd", NULL};
Antoine Pitrou19690592009-06-12 20:14:08 +0000186 const char *name = NULL;
187 PyObject *nameobj, *stringobj = NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000188 char *mode = "r";
189 char *s;
190#ifdef MS_WINDOWS
191 Py_UNICODE *widename = NULL;
192#endif
193 int ret = 0;
194 int rwa = 0, plus = 0, append = 0;
195 int flags = 0;
196 int fd = -1;
197 int closefd = 1;
198
199 assert(PyFileIO_Check(oself));
200 if (self->fd >= 0) {
201 /* Have to close the existing file first. */
202 if (internal_close(self) < 0)
203 return -1;
204 }
205
Antoine Pitrou19690592009-06-12 20:14:08 +0000206 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:fileio",
207 kwlist, &nameobj, &mode, &closefd))
208 return -1;
209
210 if (PyFloat_Check(nameobj)) {
211 PyErr_SetString(PyExc_TypeError,
212 "integer argument expected, got float");
213 return -1;
214 }
215
216 fd = PyLong_AsLong(nameobj);
217 if (fd < 0) {
218 if (!PyErr_Occurred()) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000219 PyErr_SetString(PyExc_ValueError,
220 "Negative filedescriptor");
221 return -1;
222 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000223 PyErr_Clear();
Antoine Pitrou19690592009-06-12 20:14:08 +0000224 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000225
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000226#ifdef MS_WINDOWS
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +0000227 if (PyUnicode_Check(nameobj))
228 widename = PyUnicode_AS_UNICODE(nameobj);
Antoine Pitrou19690592009-06-12 20:14:08 +0000229 if (widename == NULL)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000230#endif
Antoine Pitrou19690592009-06-12 20:14:08 +0000231 if (fd < 0)
232 {
233 if (PyBytes_Check(nameobj) || PyByteArray_Check(nameobj)) {
234 Py_ssize_t namelen;
235 if (PyObject_AsCharBuffer(nameobj, &name, &namelen) < 0)
236 return -1;
237 }
238 else {
239 PyObject *u = PyUnicode_FromObject(nameobj);
240
241 if (u == NULL)
242 return -1;
243
244 stringobj = PyUnicode_AsEncodedString(
245 u, Py_FileSystemDefaultEncoding, "surrogateescape");
246 Py_DECREF(u);
247 if (stringobj == NULL)
248 return -1;
249 if (!PyBytes_Check(stringobj)) {
250 PyErr_SetString(PyExc_TypeError,
251 "encoder failed to return bytes");
252 goto error;
253 }
254 name = PyBytes_AS_STRING(stringobj);
255 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000256 }
257
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000258 s = mode;
259 while (*s) {
260 switch (*s++) {
261 case 'r':
262 if (rwa) {
263 bad_mode:
264 PyErr_SetString(PyExc_ValueError,
265 "Must have exactly one of read/write/append mode");
266 goto error;
267 }
268 rwa = 1;
269 self->readable = 1;
270 break;
271 case 'w':
272 if (rwa)
273 goto bad_mode;
274 rwa = 1;
275 self->writable = 1;
276 flags |= O_CREAT | O_TRUNC;
277 break;
278 case 'a':
279 if (rwa)
280 goto bad_mode;
281 rwa = 1;
282 self->writable = 1;
283 flags |= O_CREAT;
284 append = 1;
285 break;
Benjamin Petersonbfc51562008-11-22 01:59:15 +0000286 case 'b':
287 break;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000288 case '+':
289 if (plus)
290 goto bad_mode;
291 self->readable = self->writable = 1;
292 plus = 1;
293 break;
294 default:
295 PyErr_Format(PyExc_ValueError,
296 "invalid mode: %.200s", mode);
297 goto error;
298 }
299 }
300
301 if (!rwa)
302 goto bad_mode;
303
304 if (self->readable && self->writable)
305 flags |= O_RDWR;
306 else if (self->readable)
307 flags |= O_RDONLY;
308 else
309 flags |= O_WRONLY;
310
311#ifdef O_BINARY
312 flags |= O_BINARY;
313#endif
314
315#ifdef O_APPEND
316 if (append)
317 flags |= O_APPEND;
318#endif
319
320 if (fd >= 0) {
Antoine Pitrou19690592009-06-12 20:14:08 +0000321 if (check_fd(fd))
322 goto error;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000323 self->fd = fd;
324 self->closefd = closefd;
325 }
326 else {
327 self->closefd = 1;
328 if (!closefd) {
329 PyErr_SetString(PyExc_ValueError,
Amaury Forgeot d'Arc9f616f42008-10-29 23:15:57 +0000330 "Cannot use closefd=False with file name");
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000331 goto error;
332 }
333
334 Py_BEGIN_ALLOW_THREADS
335 errno = 0;
336#ifdef MS_WINDOWS
337 if (widename != NULL)
338 self->fd = _wopen(widename, flags, 0666);
339 else
340#endif
341 self->fd = open(name, flags, 0666);
342 Py_END_ALLOW_THREADS
Benjamin Petersonf22c26e2008-09-01 14:13:43 +0000343 if (self->fd < 0) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000344#ifdef MS_WINDOWS
Hirokazu Yamamoto99a1b202009-01-01 15:45:39 +0000345 if (widename != NULL)
346 PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename);
347 else
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000348#endif
Hirokazu Yamamoto99a1b202009-01-01 15:45:39 +0000349 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000350 goto error;
351 }
Benjamin Peterson7af65562008-12-29 17:56:58 +0000352 if(dircheck(self, name) < 0)
Benjamin Petersonf22c26e2008-09-01 14:13:43 +0000353 goto error;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000354 }
355
Antoine Pitrou19690592009-06-12 20:14:08 +0000356 if (PyObject_SetAttrString((PyObject *)self, "name", nameobj) < 0)
357 goto error;
358
Antoine Pitroue741cc62009-01-21 00:45:36 +0000359 if (append) {
360 /* For consistent behaviour, we explicitly seek to the
361 end of file (otherwise, it might be done only on the
362 first write()). */
363 PyObject *pos = portable_lseek(self->fd, NULL, 2);
364 if (pos == NULL)
365 goto error;
366 Py_DECREF(pos);
367 }
368
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000369 goto done;
370
371 error:
372 ret = -1;
373
374 done:
Antoine Pitrou19690592009-06-12 20:14:08 +0000375 Py_CLEAR(stringobj);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000376 return ret;
377}
378
Antoine Pitrou19690592009-06-12 20:14:08 +0000379static int
380fileio_traverse(fileio *self, visitproc visit, void *arg)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000381{
Antoine Pitrou19690592009-06-12 20:14:08 +0000382 Py_VISIT(self->dict);
383 return 0;
384}
385
386static int
387fileio_clear(fileio *self)
388{
389 Py_CLEAR(self->dict);
390 return 0;
391}
392
393static void
394fileio_dealloc(fileio *self)
395{
396 if (_PyIOBase_finalize((PyObject *) self) < 0)
397 return;
398 _PyObject_GC_UNTRACK(self);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000399 if (self->weakreflist != NULL)
400 PyObject_ClearWeakRefs((PyObject *) self);
Antoine Pitrou19690592009-06-12 20:14:08 +0000401 Py_CLEAR(self->dict);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000402 Py_TYPE(self)->tp_free((PyObject *)self);
403}
404
405static PyObject *
406err_closed(void)
407{
408 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
409 return NULL;
410}
411
412static PyObject *
413err_mode(char *action)
414{
415 PyErr_Format(PyExc_ValueError, "File not open for %s", action);
416 return NULL;
417}
418
419static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000420fileio_fileno(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000421{
422 if (self->fd < 0)
423 return err_closed();
424 return PyInt_FromLong((long) self->fd);
425}
426
427static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000428fileio_readable(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000429{
430 if (self->fd < 0)
431 return err_closed();
432 return PyBool_FromLong((long) self->readable);
433}
434
435static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000436fileio_writable(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000437{
438 if (self->fd < 0)
439 return err_closed();
440 return PyBool_FromLong((long) self->writable);
441}
442
443static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000444fileio_seekable(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000445{
446 if (self->fd < 0)
447 return err_closed();
448 if (self->seekable < 0) {
Antoine Pitrou19690592009-06-12 20:14:08 +0000449 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
450 if (pos == NULL) {
451 PyErr_Clear();
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000452 self->seekable = 0;
Antoine Pitrou19690592009-06-12 20:14:08 +0000453 } else {
454 Py_DECREF(pos);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000455 self->seekable = 1;
Antoine Pitrou19690592009-06-12 20:14:08 +0000456 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000457 }
458 return PyBool_FromLong((long) self->seekable);
459}
460
461static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000462fileio_readinto(fileio *self, PyObject *args)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000463{
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000464 Py_buffer pbuf;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000465 Py_ssize_t n;
466
467 if (self->fd < 0)
468 return err_closed();
469 if (!self->readable)
470 return err_mode("reading");
471
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000472 if (!PyArg_ParseTuple(args, "w*", &pbuf))
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000473 return NULL;
474
Antoine Pitrou19690592009-06-12 20:14:08 +0000475 if (_PyVerify_fd(self->fd)) {
476 Py_BEGIN_ALLOW_THREADS
477 errno = 0;
478 n = read(self->fd, pbuf.buf, pbuf.len);
479 Py_END_ALLOW_THREADS
480 } else
481 n = -1;
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000482 PyBuffer_Release(&pbuf);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000483 if (n < 0) {
484 if (errno == EAGAIN)
485 Py_RETURN_NONE;
486 PyErr_SetFromErrno(PyExc_IOError);
487 return NULL;
488 }
489
490 return PyLong_FromSsize_t(n);
491}
492
Antoine Pitrou19690592009-06-12 20:14:08 +0000493static size_t
494new_buffersize(fileio *self, size_t currentsize)
495{
496#ifdef HAVE_FSTAT
497 off_t pos, end;
498 struct stat st;
499 if (fstat(self->fd, &st) == 0) {
500 end = st.st_size;
501 pos = lseek(self->fd, 0L, SEEK_CUR);
502 /* Files claiming a size smaller than SMALLCHUNK may
503 actually be streaming pseudo-files. In this case, we
504 apply the more aggressive algorithm below.
505 */
506 if (end >= SMALLCHUNK && end >= pos && pos >= 0) {
507 /* Add 1 so if the file were to grow we'd notice. */
508 return currentsize + end - pos + 1;
509 }
510 }
511#endif
512 if (currentsize > SMALLCHUNK) {
513 /* Keep doubling until we reach BIGCHUNK;
514 then keep adding BIGCHUNK. */
515 if (currentsize <= BIGCHUNK)
516 return currentsize + currentsize;
517 else
518 return currentsize + BIGCHUNK;
519 }
520 return currentsize + SMALLCHUNK;
521}
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000522
523static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000524fileio_readall(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000525{
526 PyObject *result;
527 Py_ssize_t total = 0;
528 int n;
529
Antoine Pitrou19690592009-06-12 20:14:08 +0000530 if (!_PyVerify_fd(self->fd))
531 return PyErr_SetFromErrno(PyExc_IOError);
532
533 result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000534 if (result == NULL)
535 return NULL;
536
537 while (1) {
Antoine Pitrou19690592009-06-12 20:14:08 +0000538 size_t newsize = new_buffersize(self, total);
539 if (newsize > PY_SSIZE_T_MAX || newsize <= 0) {
540 PyErr_SetString(PyExc_OverflowError,
541 "unbounded read returned more bytes "
542 "than a Python string can hold ");
543 Py_DECREF(result);
544 return NULL;
545 }
546
547 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)newsize) {
548 if (_PyBytes_Resize(&result, newsize) < 0) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000549 if (total == 0) {
550 Py_DECREF(result);
551 return NULL;
552 }
553 PyErr_Clear();
554 break;
555 }
556 }
557 Py_BEGIN_ALLOW_THREADS
558 errno = 0;
559 n = read(self->fd,
Antoine Pitrou19690592009-06-12 20:14:08 +0000560 PyBytes_AS_STRING(result) + total,
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000561 newsize - total);
562 Py_END_ALLOW_THREADS
563 if (n == 0)
564 break;
565 if (n < 0) {
566 if (total > 0)
567 break;
568 if (errno == EAGAIN) {
569 Py_DECREF(result);
570 Py_RETURN_NONE;
571 }
572 Py_DECREF(result);
573 PyErr_SetFromErrno(PyExc_IOError);
574 return NULL;
575 }
576 total += n;
577 }
578
Antoine Pitrou19690592009-06-12 20:14:08 +0000579 if (PyBytes_GET_SIZE(result) > total) {
580 if (_PyBytes_Resize(&result, total) < 0) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000581 /* This should never happen, but just in case */
582 Py_DECREF(result);
583 return NULL;
584 }
585 }
586 return result;
587}
588
589static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000590fileio_read(fileio *self, PyObject *args)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000591{
592 char *ptr;
593 Py_ssize_t n;
594 Py_ssize_t size = -1;
595 PyObject *bytes;
596
597 if (self->fd < 0)
598 return err_closed();
599 if (!self->readable)
600 return err_mode("reading");
601
Benjamin Petersonddd392c2009-12-13 19:19:07 +0000602 if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size))
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000603 return NULL;
604
605 if (size < 0) {
606 return fileio_readall(self);
607 }
608
Antoine Pitrou19690592009-06-12 20:14:08 +0000609 bytes = PyBytes_FromStringAndSize(NULL, size);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000610 if (bytes == NULL)
611 return NULL;
Antoine Pitrou19690592009-06-12 20:14:08 +0000612 ptr = PyBytes_AS_STRING(bytes);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000613
Antoine Pitrou19690592009-06-12 20:14:08 +0000614 if (_PyVerify_fd(self->fd)) {
615 Py_BEGIN_ALLOW_THREADS
616 errno = 0;
617 n = read(self->fd, ptr, size);
618 Py_END_ALLOW_THREADS
619 } else
620 n = -1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000621
622 if (n < 0) {
Antoine Pitrou19690592009-06-12 20:14:08 +0000623 Py_DECREF(bytes);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000624 if (errno == EAGAIN)
625 Py_RETURN_NONE;
626 PyErr_SetFromErrno(PyExc_IOError);
627 return NULL;
628 }
629
630 if (n != size) {
Antoine Pitrou19690592009-06-12 20:14:08 +0000631 if (_PyBytes_Resize(&bytes, n) < 0) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000632 Py_DECREF(bytes);
633 return NULL;
634 }
635 }
636
637 return (PyObject *) bytes;
638}
639
640static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000641fileio_write(fileio *self, PyObject *args)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000642{
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000643 Py_buffer pbuf;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000644 Py_ssize_t n;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000645
646 if (self->fd < 0)
647 return err_closed();
648 if (!self->writable)
649 return err_mode("writing");
650
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000651 if (!PyArg_ParseTuple(args, "s*", &pbuf))
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000652 return NULL;
653
Antoine Pitrou19690592009-06-12 20:14:08 +0000654 if (_PyVerify_fd(self->fd)) {
655 Py_BEGIN_ALLOW_THREADS
656 errno = 0;
657 n = write(self->fd, pbuf.buf, pbuf.len);
658 Py_END_ALLOW_THREADS
659 } else
660 n = -1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000661
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000662 PyBuffer_Release(&pbuf);
663
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000664 if (n < 0) {
665 if (errno == EAGAIN)
666 Py_RETURN_NONE;
667 PyErr_SetFromErrno(PyExc_IOError);
668 return NULL;
669 }
670
671 return PyLong_FromSsize_t(n);
672}
673
674/* XXX Windows support below is likely incomplete */
675
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000676/* Cribbed from posix_lseek() */
677static PyObject *
678portable_lseek(int fd, PyObject *posobj, int whence)
679{
680 Py_off_t pos, res;
681
682#ifdef SEEK_SET
683 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
684 switch (whence) {
685#if SEEK_SET != 0
686 case 0: whence = SEEK_SET; break;
687#endif
688#if SEEK_CUR != 1
689 case 1: whence = SEEK_CUR; break;
690#endif
Benjamin Peterson8024cec2009-01-20 14:31:08 +0000691#if SEEK_END != 2
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000692 case 2: whence = SEEK_END; break;
693#endif
694 }
695#endif /* SEEK_SET */
696
697 if (posobj == NULL)
698 pos = 0;
699 else {
700 if(PyFloat_Check(posobj)) {
701 PyErr_SetString(PyExc_TypeError, "an integer is required");
702 return NULL;
703 }
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000704#if defined(HAVE_LARGEFILE_SUPPORT)
705 pos = PyLong_AsLongLong(posobj);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000706#else
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000707 pos = PyLong_AsLong(posobj);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000708#endif
709 if (PyErr_Occurred())
710 return NULL;
711 }
712
Antoine Pitrou19690592009-06-12 20:14:08 +0000713 if (_PyVerify_fd(fd)) {
714 Py_BEGIN_ALLOW_THREADS
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000715#if defined(MS_WIN64) || defined(MS_WINDOWS)
Antoine Pitrou19690592009-06-12 20:14:08 +0000716 res = _lseeki64(fd, pos, whence);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000717#else
Antoine Pitrou19690592009-06-12 20:14:08 +0000718 res = lseek(fd, pos, whence);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000719#endif
Antoine Pitrou19690592009-06-12 20:14:08 +0000720 Py_END_ALLOW_THREADS
721 } else
722 res = -1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000723 if (res < 0)
724 return PyErr_SetFromErrno(PyExc_IOError);
725
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000726#if defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000727 return PyLong_FromLongLong(res);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000728#else
729 return PyLong_FromLong(res);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000730#endif
731}
732
733static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000734fileio_seek(fileio *self, PyObject *args)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000735{
736 PyObject *posobj;
737 int whence = 0;
738
739 if (self->fd < 0)
740 return err_closed();
741
742 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
743 return NULL;
744
745 return portable_lseek(self->fd, posobj, whence);
746}
747
748static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000749fileio_tell(fileio *self, PyObject *args)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000750{
751 if (self->fd < 0)
752 return err_closed();
753
754 return portable_lseek(self->fd, NULL, 1);
755}
756
757#ifdef HAVE_FTRUNCATE
758static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000759fileio_truncate(fileio *self, PyObject *args)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000760{
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000761 PyObject *posobj = NULL; /* the new size wanted by the user */
762#ifndef MS_WINDOWS
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000763 Py_off_t pos;
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000764#endif
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000765 int ret;
766 int fd;
767
768 fd = self->fd;
769 if (fd < 0)
770 return err_closed();
771 if (!self->writable)
772 return err_mode("writing");
773
774 if (!PyArg_ParseTuple(args, "|O", &posobj))
775 return NULL;
776
777 if (posobj == Py_None || posobj == NULL) {
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000778 /* Get the current position. */
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000779 posobj = portable_lseek(fd, NULL, 1);
780 if (posobj == NULL)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000781 return NULL;
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000782 }
783 else {
784 Py_INCREF(posobj);
785 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000786
787#ifdef MS_WINDOWS
788 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
789 so don't even try using it. */
790 {
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000791 PyObject *oldposobj, *tempposobj;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000792 HANDLE hFile;
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000793
794 /* we save the file pointer position */
795 oldposobj = portable_lseek(fd, NULL, 1);
796 if (oldposobj == NULL) {
797 Py_DECREF(posobj);
798 return NULL;
799 }
800
801 /* we then move to the truncation position */
802 tempposobj = portable_lseek(fd, posobj, 0);
803 if (tempposobj == NULL) {
804 Py_DECREF(oldposobj);
805 Py_DECREF(posobj);
806 return NULL;
807 }
808 Py_DECREF(tempposobj);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000809
810 /* Truncate. Note that this may grow the file! */
811 Py_BEGIN_ALLOW_THREADS
812 errno = 0;
813 hFile = (HANDLE)_get_osfhandle(fd);
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000814 ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000815 if (ret == 0) {
816 ret = SetEndOfFile(hFile) == 0;
817 if (ret)
818 errno = EACCES;
819 }
820 Py_END_ALLOW_THREADS
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000821
822 /* we restore the file pointer position in any case */
823 tempposobj = portable_lseek(fd, oldposobj, 0);
824 Py_DECREF(oldposobj);
825 if (tempposobj == NULL) {
826 Py_DECREF(posobj);
827 return NULL;
828 }
829 Py_DECREF(tempposobj);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000830 }
831#else
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000832
833#if defined(HAVE_LARGEFILE_SUPPORT)
834 pos = PyLong_AsLongLong(posobj);
835#else
836 pos = PyLong_AsLong(posobj);
837#endif
838 if (PyErr_Occurred()){
839 Py_DECREF(posobj);
840 return NULL;
841 }
842
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000843 Py_BEGIN_ALLOW_THREADS
844 errno = 0;
845 ret = ftruncate(fd, pos);
846 Py_END_ALLOW_THREADS
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000847
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000848#endif /* !MS_WINDOWS */
849
850 if (ret != 0) {
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000851 Py_DECREF(posobj);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000852 PyErr_SetFromErrno(PyExc_IOError);
853 return NULL;
854 }
855
856 return posobj;
857}
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000858#endif /* HAVE_FTRUNCATE */
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000859
860static char *
Antoine Pitrou19690592009-06-12 20:14:08 +0000861mode_string(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000862{
863 if (self->readable) {
864 if (self->writable)
Benjamin Petersonbfc51562008-11-22 01:59:15 +0000865 return "rb+";
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000866 else
Benjamin Petersonbfc51562008-11-22 01:59:15 +0000867 return "rb";
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000868 }
869 else
Benjamin Petersonbfc51562008-11-22 01:59:15 +0000870 return "wb";
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000871}
872
873static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000874fileio_repr(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000875{
Antoine Pitrou19690592009-06-12 20:14:08 +0000876 PyObject *nameobj, *res;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000877
Antoine Pitrou19690592009-06-12 20:14:08 +0000878 if (self->fd < 0)
879 return PyString_FromFormat("<_io.FileIO [closed]>");
880
881 nameobj = PyObject_GetAttrString((PyObject *) self, "name");
882 if (nameobj == NULL) {
883 if (PyErr_ExceptionMatches(PyExc_AttributeError))
884 PyErr_Clear();
885 else
886 return NULL;
887 res = PyString_FromFormat("<_io.FileIO fd=%d mode='%s'>",
888 self->fd, mode_string(self));
889 }
890 else {
891 PyObject *repr = PyObject_Repr(nameobj);
892 Py_DECREF(nameobj);
893 if (repr == NULL)
894 return NULL;
895 res = PyString_FromFormat("<_io.FileIO name=%s mode='%s'>",
896 PyString_AS_STRING(repr),
897 mode_string(self));
898 Py_DECREF(repr);
899 }
900 return res;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000901}
902
903static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000904fileio_isatty(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000905{
906 long res;
907
908 if (self->fd < 0)
909 return err_closed();
910 Py_BEGIN_ALLOW_THREADS
911 res = isatty(self->fd);
912 Py_END_ALLOW_THREADS
913 return PyBool_FromLong(res);
914}
915
916
917PyDoc_STRVAR(fileio_doc,
918"file(name: str[, mode: str]) -> file IO object\n"
919"\n"
920"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
921"writing or appending. The file will be created if it doesn't exist\n"
922"when opened for writing or appending; it will be truncated when\n"
923"opened for writing. Add a '+' to the mode to allow simultaneous\n"
924"reading and writing.");
925
926PyDoc_STRVAR(read_doc,
927"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
928"\n"
929"Only makes one system call, so less data may be returned than requested\n"
930"In non-blocking mode, returns None if no data is available.\n"
931"On end-of-file, returns ''.");
932
933PyDoc_STRVAR(readall_doc,
934"readall() -> bytes. read all data from the file, returned as bytes.\n"
935"\n"
936"In non-blocking mode, returns as much as is immediately available,\n"
937"or None if no data is available. On end-of-file, returns ''.");
938
939PyDoc_STRVAR(write_doc,
940"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
941"\n"
942"Only makes one system call, so not all of the data may be written.\n"
943"The number of bytes actually written is returned.");
944
945PyDoc_STRVAR(fileno_doc,
946"fileno() -> int. \"file descriptor\".\n"
947"\n"
948"This is needed for lower-level file interfaces, such the fcntl module.");
949
950PyDoc_STRVAR(seek_doc,
951"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
952"\n"
953"Argument offset is a byte count. Optional argument whence defaults to\n"
954"0 (offset from start of file, offset should be >= 0); other values are 1\n"
955"(move relative to current position, positive or negative), and 2 (move\n"
956"relative to end of file, usually negative, although many platforms allow\n"
957"seeking beyond the end of a file)."
958"\n"
959"Note that not all file objects are seekable.");
960
961#ifdef HAVE_FTRUNCATE
962PyDoc_STRVAR(truncate_doc,
963"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
964"\n"
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000965"Size defaults to the current file position, as returned by tell()."
966"The current file position is changed to the value of size.");
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000967#endif
968
969PyDoc_STRVAR(tell_doc,
970"tell() -> int. Current file position");
971
972PyDoc_STRVAR(readinto_doc,
Antoine Pitrou19690592009-06-12 20:14:08 +0000973"readinto() -> Same as RawIOBase.readinto().");
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000974
975PyDoc_STRVAR(close_doc,
976"close() -> None. Close the file.\n"
977"\n"
978"A closed file cannot be used for further I/O operations. close() may be\n"
979"called more than once without error. Changes the fileno to -1.");
980
981PyDoc_STRVAR(isatty_doc,
982"isatty() -> bool. True if the file is connected to a tty device.");
983
984PyDoc_STRVAR(seekable_doc,
985"seekable() -> bool. True if file supports random-access.");
986
987PyDoc_STRVAR(readable_doc,
988"readable() -> bool. True if file was opened in a read mode.");
989
990PyDoc_STRVAR(writable_doc,
991"writable() -> bool. True if file was opened in a write mode.");
992
993static PyMethodDef fileio_methods[] = {
994 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
995 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
996 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
997 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
998 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
999 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
1000#ifdef HAVE_FTRUNCATE
1001 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
1002#endif
1003 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
1004 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
1005 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
1006 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
1007 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
1008 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
1009 {NULL, NULL} /* sentinel */
1010};
1011
1012/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1013
1014static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +00001015get_closed(fileio *self, void *closure)
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001016{
1017 return PyBool_FromLong((long)(self->fd < 0));
1018}
1019
1020static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +00001021get_closefd(fileio *self, void *closure)
Amaury Forgeot d'Arc32265652008-11-20 23:34:31 +00001022{
1023 return PyBool_FromLong((long)(self->closefd));
1024}
1025
1026static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +00001027get_mode(fileio *self, void *closure)
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001028{
Antoine Pitrou19690592009-06-12 20:14:08 +00001029 return PyUnicode_FromString(mode_string(self));
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001030}
1031
1032static PyGetSetDef fileio_getsetlist[] = {
1033 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Amaury Forgeot d'Arc32265652008-11-20 23:34:31 +00001034 {"closefd", (getter)get_closefd, NULL,
1035 "True if the file descriptor will be closed"},
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001036 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
Antoine Pitrou19690592009-06-12 20:14:08 +00001037 {NULL},
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001038};
1039
1040PyTypeObject PyFileIO_Type = {
Hirokazu Yamamoto09979a12008-09-23 16:11:09 +00001041 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrou19690592009-06-12 20:14:08 +00001042 "_io.FileIO",
1043 sizeof(fileio),
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001044 0,
1045 (destructor)fileio_dealloc, /* tp_dealloc */
1046 0, /* tp_print */
1047 0, /* tp_getattr */
1048 0, /* tp_setattr */
Antoine Pitrou19690592009-06-12 20:14:08 +00001049 0, /* tp_reserved */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001050 (reprfunc)fileio_repr, /* tp_repr */
1051 0, /* tp_as_number */
1052 0, /* tp_as_sequence */
1053 0, /* tp_as_mapping */
1054 0, /* tp_hash */
1055 0, /* tp_call */
1056 0, /* tp_str */
1057 PyObject_GenericGetAttr, /* tp_getattro */
1058 0, /* tp_setattro */
1059 0, /* tp_as_buffer */
Antoine Pitrou19690592009-06-12 20:14:08 +00001060 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
1061 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001062 fileio_doc, /* tp_doc */
Antoine Pitrou19690592009-06-12 20:14:08 +00001063 (traverseproc)fileio_traverse, /* tp_traverse */
1064 (inquiry)fileio_clear, /* tp_clear */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001065 0, /* tp_richcompare */
Antoine Pitrou19690592009-06-12 20:14:08 +00001066 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001067 0, /* tp_iter */
1068 0, /* tp_iternext */
1069 fileio_methods, /* tp_methods */
1070 0, /* tp_members */
1071 fileio_getsetlist, /* tp_getset */
1072 0, /* tp_base */
1073 0, /* tp_dict */
1074 0, /* tp_descr_get */
1075 0, /* tp_descr_set */
Antoine Pitrou19690592009-06-12 20:14:08 +00001076 offsetof(fileio, dict), /* tp_dictoffset */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001077 fileio_init, /* tp_init */
1078 PyType_GenericAlloc, /* tp_alloc */
1079 fileio_new, /* tp_new */
Antoine Pitrou19690592009-06-12 20:14:08 +00001080 PyObject_GC_Del, /* tp_free */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001081};