blob: f3ce776993c03cc74cb85e7853f6d295890ad277 [file] [log] [blame]
Guido van Rossuma9e20242007-03-08 00:43:48 +00001/* Author: Daniel Stutzbach */
2
3#define PY_SSIZE_T_CLEAN
4#include "Python.h"
Antoine Pitroue033e062010-10-29 10:38:18 +00005#include "structmember.h"
Benjamin Peterson2614cda2010-03-21 22:36:19 +00006#ifdef HAVE_SYS_TYPES_H
Guido van Rossuma9e20242007-03-08 00:43:48 +00007#include <sys/types.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +00008#endif
9#ifdef HAVE_SYS_STAT_H
Guido van Rossuma9e20242007-03-08 00:43:48 +000010#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000011#endif
12#ifdef HAVE_FCNTL_H
Guido van Rossuma9e20242007-03-08 00:43:48 +000013#include <fcntl.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000014#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +000015#include <stddef.h> /* For offsetof */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000016#include "_iomodule.h"
Guido van Rossuma9e20242007-03-08 00:43:48 +000017
18/*
19 * Known likely problems:
20 *
21 * - Files larger then 2**32-1
22 * - Files with unicode filenames
23 * - Passing numbers greater than 2**32-1 when an integer is expected
24 * - Making it work on Windows and other oddball platforms
25 *
26 * To Do:
27 *
28 * - autoconfify header file inclusion
Guido van Rossuma9e20242007-03-08 00:43:48 +000029 */
30
31#ifdef MS_WINDOWS
32/* can simulate truncate with Win32 API functions; see file_truncate */
Thomas Hellerfdeee3a2007-07-12 11:21:36 +000033#define HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +000034#define WIN32_LEAN_AND_MEAN
35#include <windows.h>
36#endif
37
Christian Heimesa872de52008-12-05 08:26:55 +000038#if BUFSIZ < (8*1024)
39#define SMALLCHUNK (8*1024)
40#elif (BUFSIZ >= (2 << 25))
41#error "unreasonable BUFSIZ > 64MB defined"
42#else
43#define SMALLCHUNK BUFSIZ
44#endif
45
Guido van Rossuma9e20242007-03-08 00:43:48 +000046typedef struct {
Antoine Pitrouae4b4722010-05-05 16:31:07 +000047 PyObject_HEAD
48 int fd;
Charles-François Natalidc3044c2012-01-09 22:40:02 +010049 unsigned int created : 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000050 unsigned int readable : 1;
51 unsigned int writable : 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +020052 unsigned int appending : 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000053 signed int seekable : 2; /* -1 means unknown */
54 unsigned int closefd : 1;
Antoine Pitroue033e062010-10-29 10:38:18 +000055 unsigned int deallocating: 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000056 PyObject *weakreflist;
57 PyObject *dict;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000058} fileio;
Guido van Rossuma9e20242007-03-08 00:43:48 +000059
Collin Winteraf334382007-03-08 21:46:15 +000060PyTypeObject PyFileIO_Type;
61
Guido van Rossuma9e20242007-03-08 00:43:48 +000062#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
63
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000064int
65_PyFileIO_closed(PyObject *self)
66{
Antoine Pitrouae4b4722010-05-05 16:31:07 +000067 return ((fileio *)self)->fd < 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000068}
Antoine Pitrou08838b62009-01-21 00:55:13 +000069
Antoine Pitroue033e062010-10-29 10:38:18 +000070/* Because this can call arbitrary code, it shouldn't be called when
71 the refcount is 0 (that is, not directly from tp_dealloc unless
72 the refcount has been temporarily re-incremented). */
73static PyObject *
74fileio_dealloc_warn(fileio *self, PyObject *source)
75{
76 if (self->fd >= 0 && self->closefd) {
77 PyObject *exc, *val, *tb;
78 PyErr_Fetch(&exc, &val, &tb);
79 if (PyErr_WarnFormat(PyExc_ResourceWarning, 1,
80 "unclosed file %R", source)) {
81 /* Spurious errors can appear at shutdown */
82 if (PyErr_ExceptionMatches(PyExc_Warning))
83 PyErr_WriteUnraisable((PyObject *) self);
84 }
85 PyErr_Restore(exc, val, tb);
86 }
87 Py_RETURN_NONE;
88}
89
Antoine Pitrou7fb111b2009-03-04 11:14:01 +000090static PyObject *
91portable_lseek(int fd, PyObject *posobj, int whence);
92
Antoine Pitroua28fcfd2009-03-13 23:42:55 +000093static PyObject *portable_lseek(int fd, PyObject *posobj, int whence);
94
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000095/* Returns 0 on success, -1 with exception set on failure. */
Neal Norwitz88b44da2007-08-12 17:23:54 +000096static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000097internal_close(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +000098{
Antoine Pitrouae4b4722010-05-05 16:31:07 +000099 int err = 0;
100 int save_errno = 0;
101 if (self->fd >= 0) {
102 int fd = self->fd;
103 self->fd = -1;
104 /* fd is accessible and someone else may have closed it */
105 if (_PyVerify_fd(fd)) {
106 Py_BEGIN_ALLOW_THREADS
107 err = close(fd);
108 if (err < 0)
109 save_errno = errno;
110 Py_END_ALLOW_THREADS
111 } else {
112 save_errno = errno;
113 err = -1;
114 }
115 }
116 if (err < 0) {
117 errno = save_errno;
118 PyErr_SetFromErrno(PyExc_IOError);
119 return -1;
120 }
121 return 0;
Neal Norwitz88b44da2007-08-12 17:23:54 +0000122}
123
124static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000125fileio_close(fileio *self)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000126{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200127 _Py_IDENTIFIER(close);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000128 if (!self->closefd) {
129 self->fd = -1;
130 Py_RETURN_NONE;
131 }
Antoine Pitroue033e062010-10-29 10:38:18 +0000132 if (self->deallocating) {
133 PyObject *r = fileio_dealloc_warn(self, (PyObject *) self);
134 if (r)
135 Py_DECREF(r);
136 else
137 PyErr_Clear();
138 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000139 errno = internal_close(self);
140 if (errno < 0)
141 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000142
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200143 return _PyObject_CallMethodId((PyObject*)&PyRawIOBase_Type,
144 &PyId_close, "O", self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000145}
146
147static PyObject *
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000148fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000149{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000150 fileio *self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000151
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000152 assert(type != NULL && type->tp_alloc != NULL);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000153
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000154 self = (fileio *) type->tp_alloc(type, 0);
155 if (self != NULL) {
156 self->fd = -1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100157 self->created = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000158 self->readable = 0;
159 self->writable = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200160 self->appending = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000161 self->seekable = -1;
162 self->closefd = 1;
163 self->weakreflist = NULL;
164 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000165
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000166 return (PyObject *) self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000167}
168
169/* On Unix, open will succeed for directories.
170 In Python, there should be no file objects referring to
171 directories, so we need a check. */
172
173static int
Antoine Pitrou9235b252012-07-06 18:48:24 +0200174dircheck(fileio* self, PyObject *nameobj)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000175{
Christian Heimes91e8b812013-06-23 23:51:44 +0200176#if defined(HAVE_FSTAT) && defined(S_ISDIR) && defined(EISDIR)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000177 struct stat buf;
178 if (self->fd < 0)
179 return 0;
180 if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
Antoine Pitrou9235b252012-07-06 18:48:24 +0200181 errno = EISDIR;
182 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000183 return -1;
184 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000185#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000186 return 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000187}
188
Benjamin Peterson806d4022009-01-19 15:11:51 +0000189static int
190check_fd(int fd)
191{
192#if defined(HAVE_FSTAT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000193 struct stat buf;
194 if (!_PyVerify_fd(fd) || (fstat(fd, &buf) < 0 && errno == EBADF)) {
195 PyObject *exc;
196 char *msg = strerror(EBADF);
197 exc = PyObject_CallFunction(PyExc_OSError, "(is)",
198 EBADF, msg);
199 PyErr_SetObject(PyExc_OSError, exc);
200 Py_XDECREF(exc);
201 return -1;
202 }
Benjamin Peterson806d4022009-01-19 15:11:51 +0000203#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000204 return 0;
Benjamin Peterson806d4022009-01-19 15:11:51 +0000205}
206
Guido van Rossuma9e20242007-03-08 00:43:48 +0000207
208static int
209fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
210{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000211 fileio *self = (fileio *) oself;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200212 static char *kwlist[] = {"file", "mode", "closefd", "opener", NULL};
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000213 const char *name = NULL;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200214 PyObject *nameobj, *stringobj = NULL, *opener = Py_None;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000215 char *mode = "r";
216 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000217#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000218 Py_UNICODE *widename = NULL;
Thomas Helleraf2be262007-07-12 11:03:13 +0000219#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000220 int ret = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200221 int rwa = 0, plus = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000222 int flags = 0;
223 int fd = -1;
224 int closefd = 1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200225 int fd_is_own = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000226
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000227 assert(PyFileIO_Check(oself));
228 if (self->fd >= 0) {
Hynek Schlawack2cc71562012-05-25 10:05:53 +0200229 if (self->closefd) {
230 /* Have to close the existing file first. */
231 if (internal_close(self) < 0)
232 return -1;
233 }
234 else
235 self->fd = -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000236 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000237
Ross Lagerwall59142db2011-10-31 20:34:46 +0200238 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|siO:fileio",
239 kwlist, &nameobj, &mode, &closefd,
240 &opener))
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000241 return -1;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000242
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000243 if (PyFloat_Check(nameobj)) {
244 PyErr_SetString(PyExc_TypeError,
245 "integer argument expected, got float");
246 return -1;
247 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000248
Serhiy Storchaka441d30f2013-01-19 12:26:26 +0200249 fd = _PyLong_AsInt(nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000250 if (fd < 0) {
251 if (!PyErr_Occurred()) {
252 PyErr_SetString(PyExc_ValueError,
253 "Negative filedescriptor");
254 return -1;
255 }
256 PyErr_Clear();
257 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000258
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000259#ifdef MS_WINDOWS
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200260 if (PyUnicode_Check(nameobj)) {
Antoine Pitrou13348842012-01-29 18:36:34 +0100261 int rv = _PyUnicode_HasNULChars(nameobj);
262 if (rv) {
263 if (rv != -1)
264 PyErr_SetString(PyExc_TypeError, "embedded NUL character");
265 return -1;
266 }
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200267 widename = PyUnicode_AsUnicode(nameobj);
268 if (widename == NULL)
269 return -1;
270 } else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000271#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000272 if (fd < 0)
273 {
Antoine Pitrou13348842012-01-29 18:36:34 +0100274 if (!PyUnicode_FSConverter(nameobj, &stringobj)) {
275 return -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000276 }
Antoine Pitrou13348842012-01-29 18:36:34 +0100277 name = PyBytes_AS_STRING(stringobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000278 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000279
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000280 s = mode;
281 while (*s) {
282 switch (*s++) {
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100283 case 'x':
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000284 if (rwa) {
285 bad_mode:
286 PyErr_SetString(PyExc_ValueError,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100287 "Must have exactly one of create/read/write/append "
Georg Brandl28928ae2010-10-21 13:45:52 +0000288 "mode and at most one plus");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000289 goto error;
290 }
291 rwa = 1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100292 self->created = 1;
293 self->writable = 1;
294 flags |= O_EXCL | O_CREAT;
295 break;
296 case 'r':
297 if (rwa)
298 goto bad_mode;
299 rwa = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000300 self->readable = 1;
301 break;
302 case 'w':
303 if (rwa)
304 goto bad_mode;
305 rwa = 1;
306 self->writable = 1;
307 flags |= O_CREAT | O_TRUNC;
308 break;
309 case 'a':
310 if (rwa)
311 goto bad_mode;
312 rwa = 1;
313 self->writable = 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200314 self->appending = 1;
315 flags |= O_APPEND | O_CREAT;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000316 break;
317 case 'b':
318 break;
319 case '+':
320 if (plus)
321 goto bad_mode;
322 self->readable = self->writable = 1;
323 plus = 1;
324 break;
325 default:
326 PyErr_Format(PyExc_ValueError,
327 "invalid mode: %.200s", mode);
328 goto error;
329 }
330 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000331
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000332 if (!rwa)
333 goto bad_mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000334
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000335 if (self->readable && self->writable)
336 flags |= O_RDWR;
337 else if (self->readable)
338 flags |= O_RDONLY;
339 else
340 flags |= O_WRONLY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000341
342#ifdef O_BINARY
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000343 flags |= O_BINARY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000344#endif
345
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000346 if (fd >= 0) {
347 if (check_fd(fd))
348 goto error;
349 self->fd = fd;
350 self->closefd = closefd;
351 }
352 else {
353 self->closefd = 1;
354 if (!closefd) {
355 PyErr_SetString(PyExc_ValueError,
356 "Cannot use closefd=False with file name");
357 goto error;
358 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000359
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000360 errno = 0;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200361 if (opener == Py_None) {
362 Py_BEGIN_ALLOW_THREADS
Thomas Helleraf2be262007-07-12 11:03:13 +0000363#ifdef MS_WINDOWS
Ross Lagerwall59142db2011-10-31 20:34:46 +0200364 if (widename != NULL)
365 self->fd = _wopen(widename, flags, 0666);
366 else
Thomas Helleraf2be262007-07-12 11:03:13 +0000367#endif
Ross Lagerwall59142db2011-10-31 20:34:46 +0200368 self->fd = open(name, flags, 0666);
369 Py_END_ALLOW_THREADS
370 } else {
371 PyObject *fdobj = PyObject_CallFunction(
372 opener, "Oi", nameobj, flags);
373 if (fdobj == NULL)
374 goto error;
375 if (!PyLong_Check(fdobj)) {
376 Py_DECREF(fdobj);
377 PyErr_SetString(PyExc_TypeError,
378 "expected integer from opener");
379 goto error;
380 }
381
Serhiy Storchaka9101e232013-01-19 12:41:45 +0200382 self->fd = _PyLong_AsInt(fdobj);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200383 Py_DECREF(fdobj);
384 if (self->fd == -1) {
385 goto error;
386 }
387 }
388
Hynek Schlawack7f59fd72012-06-22 09:32:22 +0200389 fd_is_own = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000390 if (self->fd < 0) {
Christian Heimes0b489542007-10-31 19:20:48 +0000391#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000392 if (widename != NULL)
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200393 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000394 else
Christian Heimes0b489542007-10-31 19:20:48 +0000395#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000396 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
397 goto error;
398 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000399 }
Antoine Pitrou9235b252012-07-06 18:48:24 +0200400 if (dircheck(self, nameobj) < 0)
401 goto error;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000402
Victor Stinner89e34362011-01-07 18:47:22 +0000403#if defined(MS_WINDOWS) || defined(__CYGWIN__)
404 /* don't translate newlines (\r\n <=> \n) */
405 _setmode(self->fd, O_BINARY);
406#endif
407
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000408 if (PyObject_SetAttrString((PyObject *)self, "name", nameobj) < 0)
409 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000410
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200411 if (self->appending) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000412 /* For consistent behaviour, we explicitly seek to the
413 end of file (otherwise, it might be done only on the
414 first write()). */
415 PyObject *pos = portable_lseek(self->fd, NULL, 2);
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200416 if (pos == NULL)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000417 goto error;
418 Py_DECREF(pos);
419 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000420
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000421 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000422
423 error:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000424 ret = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200425 if (!fd_is_own)
426 self->fd = -1;
Benjamin Petersonbbb04122010-10-30 23:16:28 +0000427 if (self->fd >= 0)
428 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000429
Guido van Rossuma9e20242007-03-08 00:43:48 +0000430 done:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000431 Py_CLEAR(stringobj);
432 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000433}
434
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000435static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000436fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000437{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000438 Py_VISIT(self->dict);
439 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000440}
441
442static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000443fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000444{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000445 Py_CLEAR(self->dict);
446 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000447}
448
Guido van Rossuma9e20242007-03-08 00:43:48 +0000449static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000450fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000451{
Antoine Pitroue033e062010-10-29 10:38:18 +0000452 self->deallocating = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000453 if (_PyIOBase_finalize((PyObject *) self) < 0)
454 return;
455 _PyObject_GC_UNTRACK(self);
456 if (self->weakreflist != NULL)
457 PyObject_ClearWeakRefs((PyObject *) self);
458 Py_CLEAR(self->dict);
459 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000460}
461
462static PyObject *
463err_closed(void)
464{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000465 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
466 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000467}
468
469static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000470err_mode(char *action)
471{
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000472 PyErr_Format(IO_STATE->unsupported_operation,
473 "File not open for %s", action);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000474 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000475}
476
477static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000478fileio_fileno(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000479{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000480 if (self->fd < 0)
481 return err_closed();
482 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000483}
484
485static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000486fileio_readable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000487{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000488 if (self->fd < 0)
489 return err_closed();
490 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000491}
492
493static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000494fileio_writable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000495{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000496 if (self->fd < 0)
497 return err_closed();
498 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000499}
500
501static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000502fileio_seekable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000503{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000504 if (self->fd < 0)
505 return err_closed();
506 if (self->seekable < 0) {
507 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
508 if (pos == NULL) {
509 PyErr_Clear();
510 self->seekable = 0;
511 } else {
512 Py_DECREF(pos);
513 self->seekable = 1;
514 }
515 }
516 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000517}
518
519static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000520fileio_readinto(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000521{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000522 Py_buffer pbuf;
Victor Stinnere6edec22011-01-04 00:29:35 +0000523 Py_ssize_t n, len;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100524 int err;
Guido van Rossum53807da2007-04-10 19:01:47 +0000525
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000526 if (self->fd < 0)
527 return err_closed();
528 if (!self->readable)
529 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000530
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000531 if (!PyArg_ParseTuple(args, "w*", &pbuf))
532 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000533
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000534 if (_PyVerify_fd(self->fd)) {
Victor Stinnere6edec22011-01-04 00:29:35 +0000535 len = pbuf.len;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000536 Py_BEGIN_ALLOW_THREADS
537 errno = 0;
Victor Stinnere6edec22011-01-04 00:29:35 +0000538#if defined(MS_WIN64) || defined(MS_WINDOWS)
539 if (len > INT_MAX)
540 len = INT_MAX;
541 n = read(self->fd, pbuf.buf, (int)len);
542#else
Victor Stinner72344792011-01-11 00:04:12 +0000543 n = read(self->fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +0000544#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000545 Py_END_ALLOW_THREADS
546 } else
547 n = -1;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100548 err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000549 PyBuffer_Release(&pbuf);
550 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100551 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000552 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100553 errno = err;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000554 PyErr_SetFromErrno(PyExc_IOError);
555 return NULL;
556 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000557
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000558 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000559}
560
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000561static size_t
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200562new_buffersize(fileio *self, size_t currentsize
563#ifdef HAVE_FSTAT
Victor Stinnera2a64772011-10-11 22:45:02 +0200564 , Py_off_t pos, Py_off_t end
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200565#endif
566 )
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000567{
Antoine Pitroua3f44572012-04-17 13:50:58 +0200568 size_t addend;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000569#ifdef HAVE_FSTAT
Victor Stinnera2a64772011-10-11 22:45:02 +0200570 if (end != (Py_off_t)-1) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000571 /* Files claiming a size smaller than SMALLCHUNK may
572 actually be streaming pseudo-files. In this case, we
573 apply the more aggressive algorithm below.
574 */
575 if (end >= SMALLCHUNK && end >= pos && pos >= 0) {
576 /* Add 1 so if the file were to grow we'd notice. */
Victor Stinnerc5af7732011-10-11 23:00:31 +0200577 Py_off_t bufsize = currentsize + end - pos + 1;
578 if (bufsize < PY_SSIZE_T_MAX)
579 return (size_t)bufsize;
580 else
581 return PY_SSIZE_T_MAX;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000582 }
583 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000584#endif
Nadeem Vawdad41a98b2011-10-13 13:34:16 +0200585 /* Expand the buffer by an amount proportional to the current size,
Antoine Pitroua3f44572012-04-17 13:50:58 +0200586 giving us amortized linear-time behavior. For bigger sizes, use a
587 less-than-double growth factor to avoid excessive allocation. */
588 if (currentsize > 65536)
589 addend = currentsize >> 3;
590 else
591 addend = 256 + currentsize;
592 if (addend < SMALLCHUNK)
593 /* Avoid tiny read() calls. */
594 addend = SMALLCHUNK;
595 return addend + currentsize;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000596}
597
Guido van Rossum7165cb12007-07-10 06:54:34 +0000598static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000599fileio_readall(fileio *self)
Guido van Rossum7165cb12007-07-10 06:54:34 +0000600{
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200601#ifdef HAVE_FSTAT
602 struct stat st;
Victor Stinnera2a64772011-10-11 22:45:02 +0200603 Py_off_t pos, end;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200604#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000605 PyObject *result;
606 Py_ssize_t total = 0;
Victor Stinnerc44057d2013-01-03 03:33:21 +0100607 Py_ssize_t n;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200608 size_t newsize;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000609
Victor Stinnerb79f28c2011-05-25 22:09:03 +0200610 if (self->fd < 0)
611 return err_closed();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000612 if (!_PyVerify_fd(self->fd))
613 return PyErr_SetFromErrno(PyExc_IOError);
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000614
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000615 result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK);
616 if (result == NULL)
617 return NULL;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000618
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200619#ifdef HAVE_FSTAT
620#if defined(MS_WIN64) || defined(MS_WINDOWS)
621 pos = _lseeki64(self->fd, 0L, SEEK_CUR);
622#else
623 pos = lseek(self->fd, 0L, SEEK_CUR);
624#endif
625 if (fstat(self->fd, &st) == 0)
626 end = st.st_size;
627 else
Victor Stinnera2a64772011-10-11 22:45:02 +0200628 end = (Py_off_t)-1;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200629#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000630 while (1) {
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200631#ifdef HAVE_FSTAT
632 newsize = new_buffersize(self, total, pos, end);
633#else
634 newsize = new_buffersize(self, total);
635#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000636 if (newsize > PY_SSIZE_T_MAX || newsize <= 0) {
637 PyErr_SetString(PyExc_OverflowError,
638 "unbounded read returned more bytes "
639 "than a Python string can hold ");
640 Py_DECREF(result);
641 return NULL;
642 }
Christian Heimesa872de52008-12-05 08:26:55 +0000643
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000644 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)newsize) {
645 if (_PyBytes_Resize(&result, newsize) < 0) {
646 if (total == 0) {
647 Py_DECREF(result);
648 return NULL;
649 }
650 PyErr_Clear();
651 break;
652 }
653 }
654 Py_BEGIN_ALLOW_THREADS
655 errno = 0;
Victor Stinnerc44057d2013-01-03 03:33:21 +0100656 n = newsize - total;
657#if defined(MS_WIN64) || defined(MS_WINDOWS)
658 if (n > INT_MAX)
659 n = INT_MAX;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000660 n = read(self->fd,
661 PyBytes_AS_STRING(result) + total,
Victor Stinnerc44057d2013-01-03 03:33:21 +0100662 (int)n);
663#else
664 n = read(self->fd,
665 PyBytes_AS_STRING(result) + total,
666 n);
667#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000668 Py_END_ALLOW_THREADS
669 if (n == 0)
670 break;
671 if (n < 0) {
Gregory P. Smith51359922012-06-23 23:55:39 -0700672 if (errno == EINTR) {
673 if (PyErr_CheckSignals()) {
674 Py_DECREF(result);
675 return NULL;
676 }
677 continue;
678 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000679 if (total > 0)
680 break;
681 if (errno == EAGAIN) {
682 Py_DECREF(result);
683 Py_RETURN_NONE;
684 }
685 Py_DECREF(result);
686 PyErr_SetFromErrno(PyExc_IOError);
687 return NULL;
688 }
689 total += n;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200690#ifdef HAVE_FSTAT
691 pos += n;
692#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000693 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000694
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000695 if (PyBytes_GET_SIZE(result) > total) {
696 if (_PyBytes_Resize(&result, total) < 0) {
697 /* This should never happen, but just in case */
698 Py_DECREF(result);
699 return NULL;
700 }
701 }
702 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000703}
704
Guido van Rossuma9e20242007-03-08 00:43:48 +0000705static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000706fileio_read(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000707{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000708 char *ptr;
709 Py_ssize_t n;
710 Py_ssize_t size = -1;
711 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000712
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000713 if (self->fd < 0)
714 return err_closed();
715 if (!self->readable)
716 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000717
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000718 if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size))
719 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000720
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000721 if (size < 0) {
722 return fileio_readall(self);
723 }
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000724
Victor Stinnerc655a722011-07-05 11:31:49 +0200725#if defined(MS_WIN64) || defined(MS_WINDOWS)
726 if (size > INT_MAX)
727 size = INT_MAX;
728#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000729 bytes = PyBytes_FromStringAndSize(NULL, size);
730 if (bytes == NULL)
731 return NULL;
732 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000733
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000734 if (_PyVerify_fd(self->fd)) {
735 Py_BEGIN_ALLOW_THREADS
736 errno = 0;
Victor Stinnerc655a722011-07-05 11:31:49 +0200737#if defined(MS_WIN64) || defined(MS_WINDOWS)
738 n = read(self->fd, ptr, (int)size);
739#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000740 n = read(self->fd, ptr, size);
Victor Stinnerc655a722011-07-05 11:31:49 +0200741#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000742 Py_END_ALLOW_THREADS
743 } else
744 n = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000745
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000746 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100747 int err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000748 Py_DECREF(bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100749 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000750 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100751 errno = err;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000752 PyErr_SetFromErrno(PyExc_IOError);
753 return NULL;
754 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000755
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000756 if (n != size) {
757 if (_PyBytes_Resize(&bytes, n) < 0) {
758 Py_DECREF(bytes);
759 return NULL;
760 }
761 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000762
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000763 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000764}
765
766static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000767fileio_write(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000768{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000769 Py_buffer pbuf;
Victor Stinnere6edec22011-01-04 00:29:35 +0000770 Py_ssize_t n, len;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100771 int err;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000772
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000773 if (self->fd < 0)
774 return err_closed();
775 if (!self->writable)
776 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000777
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000778 if (!PyArg_ParseTuple(args, "y*", &pbuf))
779 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000780
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000781 if (_PyVerify_fd(self->fd)) {
782 Py_BEGIN_ALLOW_THREADS
783 errno = 0;
Victor Stinnere6edec22011-01-04 00:29:35 +0000784 len = pbuf.len;
785#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinnere0daff12011-03-20 23:36:35 +0100786 if (len > 32767 && isatty(self->fd)) {
787 /* Issue #11395: the Windows console returns an error (12: not
788 enough space error) on writing into stdout if stdout mode is
789 binary and the length is greater than 66,000 bytes (or less,
790 depending on heap usage). */
791 len = 32767;
792 }
793 else if (len > INT_MAX)
Victor Stinnere6edec22011-01-04 00:29:35 +0000794 len = INT_MAX;
795 n = write(self->fd, pbuf.buf, (int)len);
796#else
Victor Stinner72344792011-01-11 00:04:12 +0000797 n = write(self->fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +0000798#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000799 Py_END_ALLOW_THREADS
800 } else
801 n = -1;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100802 err = errno;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000803
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000804 PyBuffer_Release(&pbuf);
Martin v. Löwis423be952008-08-13 15:53:07 +0000805
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000806 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100807 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000808 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100809 errno = err;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000810 PyErr_SetFromErrno(PyExc_IOError);
811 return NULL;
812 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000813
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000814 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000815}
816
Guido van Rossum53807da2007-04-10 19:01:47 +0000817/* XXX Windows support below is likely incomplete */
818
Guido van Rossum53807da2007-04-10 19:01:47 +0000819/* Cribbed from posix_lseek() */
820static PyObject *
821portable_lseek(int fd, PyObject *posobj, int whence)
822{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000823 Py_off_t pos, res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000824
825#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000826 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
827 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000828#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000829 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000830#endif
831#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000832 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000833#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000834#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000835 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000836#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000837 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000838#endif /* SEEK_SET */
839
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000840 if (posobj == NULL)
841 pos = 0;
842 else {
843 if(PyFloat_Check(posobj)) {
844 PyErr_SetString(PyExc_TypeError, "an integer is required");
845 return NULL;
846 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000847#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000848 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000849#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000850 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000851#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000852 if (PyErr_Occurred())
853 return NULL;
854 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000855
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000856 if (_PyVerify_fd(fd)) {
857 Py_BEGIN_ALLOW_THREADS
Guido van Rossum53807da2007-04-10 19:01:47 +0000858#if defined(MS_WIN64) || defined(MS_WINDOWS)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000859 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000860#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000861 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000862#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000863 Py_END_ALLOW_THREADS
864 } else
865 res = -1;
866 if (res < 0)
867 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000868
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000869#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000870 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000871#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000872 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000873#endif
874}
875
Guido van Rossuma9e20242007-03-08 00:43:48 +0000876static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000877fileio_seek(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000878{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000879 PyObject *posobj;
880 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000881
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000882 if (self->fd < 0)
883 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000884
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000885 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
886 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000887
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000888 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000889}
890
891static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000892fileio_tell(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000893{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000894 if (self->fd < 0)
895 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000896
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000897 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000898}
899
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000900#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000901static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000902fileio_truncate(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000903{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000904 PyObject *posobj = NULL; /* the new size wanted by the user */
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000905#ifndef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000906 Py_off_t pos;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000907#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000908 int ret;
909 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000910
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000911 fd = self->fd;
912 if (fd < 0)
913 return err_closed();
914 if (!self->writable)
915 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000916
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000917 if (!PyArg_ParseTuple(args, "|O", &posobj))
918 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000919
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000920 if (posobj == Py_None || posobj == NULL) {
921 /* Get the current position. */
922 posobj = portable_lseek(fd, NULL, 1);
923 if (posobj == NULL)
924 return NULL;
925 }
926 else {
927 Py_INCREF(posobj);
928 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000929
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000930#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000931 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
932 so don't even try using it. */
933 {
934 PyObject *oldposobj, *tempposobj;
935 HANDLE hFile;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000936
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000937 /* we save the file pointer position */
938 oldposobj = portable_lseek(fd, NULL, 1);
939 if (oldposobj == NULL) {
940 Py_DECREF(posobj);
941 return NULL;
942 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000943
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000944 /* we then move to the truncation position */
945 tempposobj = portable_lseek(fd, posobj, 0);
946 if (tempposobj == NULL) {
947 Py_DECREF(oldposobj);
948 Py_DECREF(posobj);
949 return NULL;
950 }
951 Py_DECREF(tempposobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000952
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000953 /* Truncate. Note that this may grow the file! */
954 Py_BEGIN_ALLOW_THREADS
955 errno = 0;
956 hFile = (HANDLE)_get_osfhandle(fd);
957 ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
958 if (ret == 0) {
959 ret = SetEndOfFile(hFile) == 0;
960 if (ret)
961 errno = EACCES;
962 }
963 Py_END_ALLOW_THREADS
964
965 /* we restore the file pointer position in any case */
966 tempposobj = portable_lseek(fd, oldposobj, 0);
967 Py_DECREF(oldposobj);
968 if (tempposobj == NULL) {
969 Py_DECREF(posobj);
970 return NULL;
971 }
972 Py_DECREF(tempposobj);
973 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000974#else
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000975
976#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000977 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000978#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000979 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000980#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000981 if (PyErr_Occurred()){
982 Py_DECREF(posobj);
983 return NULL;
984 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000985
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000986 Py_BEGIN_ALLOW_THREADS
987 errno = 0;
988 ret = ftruncate(fd, pos);
989 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000990
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000991#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000992
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000993 if (ret != 0) {
994 Py_DECREF(posobj);
995 PyErr_SetFromErrno(PyExc_IOError);
996 return NULL;
997 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000998
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000999 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001000}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001001#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +00001002
1003static char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001004mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +00001005{
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001006 if (self->created) {
1007 if (self->readable)
1008 return "xb+";
1009 else
1010 return "xb";
1011 }
Antoine Pitroue93b63b2013-09-04 20:46:33 +02001012 if (self->appending) {
1013 if (self->readable)
1014 return "ab+";
1015 else
1016 return "ab";
1017 }
1018 else if (self->readable) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001019 if (self->writable)
1020 return "rb+";
1021 else
1022 return "rb";
1023 }
1024 else
1025 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +00001026}
Guido van Rossuma9e20242007-03-08 00:43:48 +00001027
1028static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001029fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001030{
Martin v. Löwis767046a2011-10-14 15:35:36 +02001031 _Py_IDENTIFIER(name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001032 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001033
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001034 if (self->fd < 0)
1035 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +00001036
Martin v. Löwis767046a2011-10-14 15:35:36 +02001037 nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001038 if (nameobj == NULL) {
1039 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1040 PyErr_Clear();
1041 else
1042 return NULL;
1043 res = PyUnicode_FromFormat("<_io.FileIO fd=%d mode='%s'>",
1044 self->fd, mode_string(self));
1045 }
1046 else {
1047 res = PyUnicode_FromFormat("<_io.FileIO name=%R mode='%s'>",
1048 nameobj, mode_string(self));
1049 Py_DECREF(nameobj);
1050 }
1051 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001052}
1053
1054static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001055fileio_isatty(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001056{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001057 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +00001058
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001059 if (self->fd < 0)
1060 return err_closed();
1061 Py_BEGIN_ALLOW_THREADS
1062 res = isatty(self->fd);
1063 Py_END_ALLOW_THREADS
1064 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001065}
1066
Antoine Pitrou243757e2010-11-05 21:15:39 +00001067static PyObject *
1068fileio_getstate(fileio *self)
1069{
1070 PyErr_Format(PyExc_TypeError,
1071 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
1072 return NULL;
1073}
1074
Guido van Rossuma9e20242007-03-08 00:43:48 +00001075
1076PyDoc_STRVAR(fileio_doc,
Ross Lagerwall59142db2011-10-31 20:34:46 +02001077"file(name: str[, mode: str][, opener: None]) -> file IO object\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001078"\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001079"Open a file. The mode can be 'r', 'w', 'x' or 'a' for reading (default),\n"
Charles-François Natalid612de12012-01-14 11:51:00 +01001080"writing, exclusive creation or appending. The file will be created if it\n"
1081"doesn't exist when opened for writing or appending; it will be truncated\n"
1082"when opened for writing. A `FileExistsError` will be raised if it already\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001083"exists when opened for creating. Opening a file for creating implies\n"
1084"writing so this mode behaves in a similar way to 'w'.Add a '+' to the mode\n"
1085"to allow simultaneous reading and writing. A custom opener can be used by\n"
1086"passing a callable as *opener*. The underlying file descriptor for the file\n"
Ross Lagerwall59142db2011-10-31 20:34:46 +02001087"object is then obtained by calling opener with (*name*, *flags*).\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001088"*opener* must return an open file descriptor (passing os.open as *opener*\n"
1089"results in functionality similar to passing None).");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001090
1091PyDoc_STRVAR(read_doc,
1092"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
1093"\n"
1094"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +00001095"In non-blocking mode, returns None if no data is available.\n"
1096"On end-of-file, returns ''.");
1097
1098PyDoc_STRVAR(readall_doc,
1099"readall() -> bytes. read all data from the file, returned as bytes.\n"
1100"\n"
1101"In non-blocking mode, returns as much as is immediately available,\n"
1102"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001103
1104PyDoc_STRVAR(write_doc,
1105"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
1106"\n"
1107"Only makes one system call, so not all of the data may be written.\n"
1108"The number of bytes actually written is returned.");
1109
1110PyDoc_STRVAR(fileno_doc,
1111"fileno() -> int. \"file descriptor\".\n"
1112"\n"
1113"This is needed for lower-level file interfaces, such the fcntl module.");
1114
1115PyDoc_STRVAR(seek_doc,
1116"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
1117"\n"
1118"Argument offset is a byte count. Optional argument whence defaults to\n"
1119"0 (offset from start of file, offset should be >= 0); other values are 1\n"
1120"(move relative to current position, positive or negative), and 2 (move\n"
1121"relative to end of file, usually negative, although many platforms allow\n"
1122"seeking beyond the end of a file)."
1123"\n"
1124"Note that not all file objects are seekable.");
1125
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001126#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +00001127PyDoc_STRVAR(truncate_doc,
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001128"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001129"\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +00001130"Size defaults to the current file position, as returned by tell()."
1131"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001132#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +00001133
1134PyDoc_STRVAR(tell_doc,
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001135"tell() -> int. Current file position");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001136
1137PyDoc_STRVAR(readinto_doc,
Benjamin Peterson9a8082f2009-03-05 00:55:56 +00001138"readinto() -> Same as RawIOBase.readinto().");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001139
1140PyDoc_STRVAR(close_doc,
1141"close() -> None. Close the file.\n"
1142"\n"
1143"A closed file cannot be used for further I/O operations. close() may be\n"
1144"called more than once without error. Changes the fileno to -1.");
1145
1146PyDoc_STRVAR(isatty_doc,
1147"isatty() -> bool. True if the file is connected to a tty device.");
1148
Guido van Rossuma9e20242007-03-08 00:43:48 +00001149PyDoc_STRVAR(seekable_doc,
1150"seekable() -> bool. True if file supports random-access.");
1151
1152PyDoc_STRVAR(readable_doc,
1153"readable() -> bool. True if file was opened in a read mode.");
1154
1155PyDoc_STRVAR(writable_doc,
1156"writable() -> bool. True if file was opened in a write mode.");
1157
1158static PyMethodDef fileio_methods[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001159 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
1160 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
1161 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
1162 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
1163 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
1164 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001165#ifdef HAVE_FTRUNCATE
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001166 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001167#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001168 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
1169 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
1170 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
1171 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
1172 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
1173 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Antoine Pitroue033e062010-10-29 10:38:18 +00001174 {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
Antoine Pitrou243757e2010-11-05 21:15:39 +00001175 {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001176 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001177};
1178
Guido van Rossum53807da2007-04-10 19:01:47 +00001179/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1180
Guido van Rossumb0428152007-04-08 17:44:42 +00001181static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001182get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001183{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001184 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001185}
1186
1187static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001188get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001189{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001190 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001191}
1192
1193static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001194get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001195{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001196 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001197}
1198
1199static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001200 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1201 {"closefd", (getter)get_closefd, NULL,
1202 "True if the file descriptor will be closed"},
1203 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1204 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001205};
1206
Guido van Rossuma9e20242007-03-08 00:43:48 +00001207PyTypeObject PyFileIO_Type = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001208 PyVarObject_HEAD_INIT(NULL, 0)
1209 "_io.FileIO",
1210 sizeof(fileio),
1211 0,
1212 (destructor)fileio_dealloc, /* tp_dealloc */
1213 0, /* tp_print */
1214 0, /* tp_getattr */
1215 0, /* tp_setattr */
1216 0, /* tp_reserved */
1217 (reprfunc)fileio_repr, /* tp_repr */
1218 0, /* tp_as_number */
1219 0, /* tp_as_sequence */
1220 0, /* tp_as_mapping */
1221 0, /* tp_hash */
1222 0, /* tp_call */
1223 0, /* tp_str */
1224 PyObject_GenericGetAttr, /* tp_getattro */
1225 0, /* tp_setattro */
1226 0, /* tp_as_buffer */
1227 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
1228 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1229 fileio_doc, /* tp_doc */
1230 (traverseproc)fileio_traverse, /* tp_traverse */
1231 (inquiry)fileio_clear, /* tp_clear */
1232 0, /* tp_richcompare */
1233 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1234 0, /* tp_iter */
1235 0, /* tp_iternext */
1236 fileio_methods, /* tp_methods */
1237 0, /* tp_members */
1238 fileio_getsetlist, /* tp_getset */
1239 0, /* tp_base */
1240 0, /* tp_dict */
1241 0, /* tp_descr_get */
1242 0, /* tp_descr_set */
1243 offsetof(fileio, dict), /* tp_dictoffset */
1244 fileio_init, /* tp_init */
1245 PyType_GenericAlloc, /* tp_alloc */
1246 fileio_new, /* tp_new */
1247 PyObject_GC_Del, /* tp_free */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001248};