blob: e757c8263bdf00134c5df052ca23b8cd85d96f37 [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;
52 signed int seekable : 2; /* -1 means unknown */
53 unsigned int closefd : 1;
Antoine Pitrou796564c2013-07-30 19:59:21 +020054 char finalizing;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000055 PyObject *weakreflist;
56 PyObject *dict;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000057} fileio;
Guido van Rossuma9e20242007-03-08 00:43:48 +000058
Collin Winteraf334382007-03-08 21:46:15 +000059PyTypeObject PyFileIO_Type;
60
Guido van Rossuma9e20242007-03-08 00:43:48 +000061#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
62
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000063int
64_PyFileIO_closed(PyObject *self)
65{
Antoine Pitrouae4b4722010-05-05 16:31:07 +000066 return ((fileio *)self)->fd < 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000067}
Antoine Pitrou08838b62009-01-21 00:55:13 +000068
Antoine Pitroue033e062010-10-29 10:38:18 +000069/* Because this can call arbitrary code, it shouldn't be called when
70 the refcount is 0 (that is, not directly from tp_dealloc unless
71 the refcount has been temporarily re-incremented). */
72static PyObject *
73fileio_dealloc_warn(fileio *self, PyObject *source)
74{
75 if (self->fd >= 0 && self->closefd) {
76 PyObject *exc, *val, *tb;
77 PyErr_Fetch(&exc, &val, &tb);
78 if (PyErr_WarnFormat(PyExc_ResourceWarning, 1,
79 "unclosed file %R", source)) {
80 /* Spurious errors can appear at shutdown */
81 if (PyErr_ExceptionMatches(PyExc_Warning))
82 PyErr_WriteUnraisable((PyObject *) self);
83 }
84 PyErr_Restore(exc, val, tb);
85 }
86 Py_RETURN_NONE;
87}
88
Antoine Pitrou7fb111b2009-03-04 11:14:01 +000089static PyObject *
90portable_lseek(int fd, PyObject *posobj, int whence);
91
Antoine Pitroua28fcfd2009-03-13 23:42:55 +000092static PyObject *portable_lseek(int fd, PyObject *posobj, int whence);
93
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000094/* Returns 0 on success, -1 with exception set on failure. */
Neal Norwitz88b44da2007-08-12 17:23:54 +000095static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000096internal_close(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +000097{
Antoine Pitrouae4b4722010-05-05 16:31:07 +000098 int err = 0;
99 int save_errno = 0;
100 if (self->fd >= 0) {
101 int fd = self->fd;
102 self->fd = -1;
103 /* fd is accessible and someone else may have closed it */
104 if (_PyVerify_fd(fd)) {
105 Py_BEGIN_ALLOW_THREADS
106 err = close(fd);
107 if (err < 0)
108 save_errno = errno;
109 Py_END_ALLOW_THREADS
110 } else {
111 save_errno = errno;
112 err = -1;
113 }
114 }
115 if (err < 0) {
116 errno = save_errno;
117 PyErr_SetFromErrno(PyExc_IOError);
118 return -1;
119 }
120 return 0;
Neal Norwitz88b44da2007-08-12 17:23:54 +0000121}
122
123static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000124fileio_close(fileio *self)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000125{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200126 _Py_IDENTIFIER(close);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000127 if (!self->closefd) {
128 self->fd = -1;
129 Py_RETURN_NONE;
130 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200131 if (self->finalizing) {
Antoine Pitroue033e062010-10-29 10:38:18 +0000132 PyObject *r = fileio_dealloc_warn(self, (PyObject *) self);
133 if (r)
134 Py_DECREF(r);
135 else
136 PyErr_Clear();
137 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000138 errno = internal_close(self);
139 if (errno < 0)
140 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000141
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200142 return _PyObject_CallMethodId((PyObject*)&PyRawIOBase_Type,
143 &PyId_close, "O", self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000144}
145
146static PyObject *
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000147fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000148{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000149 fileio *self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000150
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000151 assert(type != NULL && type->tp_alloc != NULL);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000152
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000153 self = (fileio *) type->tp_alloc(type, 0);
154 if (self != NULL) {
155 self->fd = -1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100156 self->created = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000157 self->readable = 0;
158 self->writable = 0;
159 self->seekable = -1;
160 self->closefd = 1;
161 self->weakreflist = NULL;
162 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000163
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000164 return (PyObject *) self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000165}
166
167/* On Unix, open will succeed for directories.
168 In Python, there should be no file objects referring to
169 directories, so we need a check. */
170
171static int
Antoine Pitrou9235b252012-07-06 18:48:24 +0200172dircheck(fileio* self, PyObject *nameobj)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000173{
Christian Heimes91e8b812013-06-23 23:51:44 +0200174#if defined(HAVE_FSTAT) && defined(S_ISDIR) && defined(EISDIR)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000175 struct stat buf;
176 if (self->fd < 0)
177 return 0;
178 if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
Antoine Pitrou9235b252012-07-06 18:48:24 +0200179 errno = EISDIR;
180 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000181 return -1;
182 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000183#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000184 return 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000185}
186
Benjamin Peterson806d4022009-01-19 15:11:51 +0000187static int
188check_fd(int fd)
189{
190#if defined(HAVE_FSTAT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000191 struct stat buf;
192 if (!_PyVerify_fd(fd) || (fstat(fd, &buf) < 0 && errno == EBADF)) {
193 PyObject *exc;
194 char *msg = strerror(EBADF);
195 exc = PyObject_CallFunction(PyExc_OSError, "(is)",
196 EBADF, msg);
197 PyErr_SetObject(PyExc_OSError, exc);
198 Py_XDECREF(exc);
199 return -1;
200 }
Benjamin Peterson806d4022009-01-19 15:11:51 +0000201#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000202 return 0;
Benjamin Peterson806d4022009-01-19 15:11:51 +0000203}
204
Victor Stinnerdaf45552013-08-28 00:53:59 +0200205#ifdef O_CLOEXEC
206extern int _Py_open_cloexec_works;
207#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000208
209static int
210fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
211{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000212 fileio *self = (fileio *) oself;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200213 static char *kwlist[] = {"file", "mode", "closefd", "opener", NULL};
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000214 const char *name = NULL;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200215 PyObject *nameobj, *stringobj = NULL, *opener = Py_None;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000216 char *mode = "r";
217 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000218#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000219 Py_UNICODE *widename = NULL;
Thomas Helleraf2be262007-07-12 11:03:13 +0000220#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000221 int ret = 0;
222 int rwa = 0, plus = 0, append = 0;
223 int flags = 0;
224 int fd = -1;
225 int closefd = 1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200226 int fd_is_own = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200227#ifdef O_CLOEXEC
228 int *atomic_flag_works = &_Py_open_cloexec_works;
229#elif !defined(MS_WINDOWS)
230 int *atomic_flag_works = NULL;
231#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000232
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000233 assert(PyFileIO_Check(oself));
234 if (self->fd >= 0) {
Hynek Schlawack2cc71562012-05-25 10:05:53 +0200235 if (self->closefd) {
236 /* Have to close the existing file first. */
237 if (internal_close(self) < 0)
238 return -1;
239 }
240 else
241 self->fd = -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000242 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000243
Ross Lagerwall59142db2011-10-31 20:34:46 +0200244 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|siO:fileio",
245 kwlist, &nameobj, &mode, &closefd,
246 &opener))
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000247 return -1;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000248
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000249 if (PyFloat_Check(nameobj)) {
250 PyErr_SetString(PyExc_TypeError,
251 "integer argument expected, got float");
252 return -1;
253 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000254
Serhiy Storchaka78980432013-01-15 01:12:17 +0200255 fd = _PyLong_AsInt(nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000256 if (fd < 0) {
257 if (!PyErr_Occurred()) {
258 PyErr_SetString(PyExc_ValueError,
259 "Negative filedescriptor");
260 return -1;
261 }
262 PyErr_Clear();
263 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000264
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000265#ifdef MS_WINDOWS
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200266 if (PyUnicode_Check(nameobj)) {
Antoine Pitrou13348842012-01-29 18:36:34 +0100267 int rv = _PyUnicode_HasNULChars(nameobj);
268 if (rv) {
269 if (rv != -1)
270 PyErr_SetString(PyExc_TypeError, "embedded NUL character");
271 return -1;
272 }
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200273 widename = PyUnicode_AsUnicode(nameobj);
274 if (widename == NULL)
275 return -1;
276 } else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000277#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000278 if (fd < 0)
279 {
Antoine Pitrou13348842012-01-29 18:36:34 +0100280 if (!PyUnicode_FSConverter(nameobj, &stringobj)) {
281 return -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000282 }
Antoine Pitrou13348842012-01-29 18:36:34 +0100283 name = PyBytes_AS_STRING(stringobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000284 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000285
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000286 s = mode;
287 while (*s) {
288 switch (*s++) {
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100289 case 'x':
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000290 if (rwa) {
291 bad_mode:
292 PyErr_SetString(PyExc_ValueError,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100293 "Must have exactly one of create/read/write/append "
Georg Brandl28928ae2010-10-21 13:45:52 +0000294 "mode and at most one plus");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000295 goto error;
296 }
297 rwa = 1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100298 self->created = 1;
299 self->writable = 1;
300 flags |= O_EXCL | O_CREAT;
301 break;
302 case 'r':
303 if (rwa)
304 goto bad_mode;
305 rwa = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000306 self->readable = 1;
307 break;
308 case 'w':
309 if (rwa)
310 goto bad_mode;
311 rwa = 1;
312 self->writable = 1;
313 flags |= O_CREAT | O_TRUNC;
314 break;
315 case 'a':
316 if (rwa)
317 goto bad_mode;
318 rwa = 1;
319 self->writable = 1;
320 flags |= O_CREAT;
321 append = 1;
322 break;
323 case 'b':
324 break;
325 case '+':
326 if (plus)
327 goto bad_mode;
328 self->readable = self->writable = 1;
329 plus = 1;
330 break;
331 default:
332 PyErr_Format(PyExc_ValueError,
333 "invalid mode: %.200s", mode);
334 goto error;
335 }
336 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000337
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000338 if (!rwa)
339 goto bad_mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000340
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000341 if (self->readable && self->writable)
342 flags |= O_RDWR;
343 else if (self->readable)
344 flags |= O_RDONLY;
345 else
346 flags |= O_WRONLY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000347
348#ifdef O_BINARY
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000349 flags |= O_BINARY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000350#endif
351
Walter Dörwald0e411482007-06-06 16:55:38 +0000352#ifdef O_APPEND
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000353 if (append)
354 flags |= O_APPEND;
Walter Dörwald0e411482007-06-06 16:55:38 +0000355#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +0200356#ifdef MS_WINDOWS
357 flags |= O_NOINHERIT;
358#elif defined(O_CLOEXEC)
359 flags |= O_CLOEXEC;
360#endif
Walter Dörwald0e411482007-06-06 16:55:38 +0000361
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000362 if (fd >= 0) {
363 if (check_fd(fd))
364 goto error;
365 self->fd = fd;
366 self->closefd = closefd;
367 }
368 else {
369 self->closefd = 1;
370 if (!closefd) {
371 PyErr_SetString(PyExc_ValueError,
372 "Cannot use closefd=False with file name");
373 goto error;
374 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000375
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000376 errno = 0;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200377 if (opener == Py_None) {
378 Py_BEGIN_ALLOW_THREADS
Thomas Helleraf2be262007-07-12 11:03:13 +0000379#ifdef MS_WINDOWS
Ross Lagerwall59142db2011-10-31 20:34:46 +0200380 if (widename != NULL)
381 self->fd = _wopen(widename, flags, 0666);
382 else
Thomas Helleraf2be262007-07-12 11:03:13 +0000383#endif
Ross Lagerwall59142db2011-10-31 20:34:46 +0200384 self->fd = open(name, flags, 0666);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200385
Ross Lagerwall59142db2011-10-31 20:34:46 +0200386 Py_END_ALLOW_THREADS
Victor Stinnerdaf45552013-08-28 00:53:59 +0200387 }
388 else {
389 PyObject *fdobj;
390
391#ifndef MS_WINDOWS
392 /* the opener may clear the atomic flag */
393 atomic_flag_works = NULL;
394#endif
395
396 fdobj = PyObject_CallFunction(opener, "Oi", nameobj, flags);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200397 if (fdobj == NULL)
398 goto error;
399 if (!PyLong_Check(fdobj)) {
400 Py_DECREF(fdobj);
401 PyErr_SetString(PyExc_TypeError,
402 "expected integer from opener");
403 goto error;
404 }
405
Serhiy Storchaka78980432013-01-15 01:12:17 +0200406 self->fd = _PyLong_AsInt(fdobj);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200407 Py_DECREF(fdobj);
408 if (self->fd == -1) {
409 goto error;
410 }
411 }
412
Hynek Schlawack7f59fd72012-06-22 09:32:22 +0200413 fd_is_own = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000414 if (self->fd < 0) {
Victor Stinner292c8352012-10-30 02:17:38 +0100415 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000416 goto error;
417 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200418
419#ifndef MS_WINDOWS
420 if (_Py_set_inheritable(self->fd, 0, atomic_flag_works) < 0)
421 goto error;
422#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000423 }
Antoine Pitrou9235b252012-07-06 18:48:24 +0200424 if (dircheck(self, nameobj) < 0)
425 goto error;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000426
Victor Stinner89e34362011-01-07 18:47:22 +0000427#if defined(MS_WINDOWS) || defined(__CYGWIN__)
428 /* don't translate newlines (\r\n <=> \n) */
429 _setmode(self->fd, O_BINARY);
430#endif
431
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000432 if (PyObject_SetAttrString((PyObject *)self, "name", nameobj) < 0)
433 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000434
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000435 if (append) {
436 /* For consistent behaviour, we explicitly seek to the
437 end of file (otherwise, it might be done only on the
438 first write()). */
439 PyObject *pos = portable_lseek(self->fd, NULL, 2);
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200440 if (pos == NULL)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000441 goto error;
442 Py_DECREF(pos);
443 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000444
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000445 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000446
447 error:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000448 ret = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200449 if (!fd_is_own)
450 self->fd = -1;
Benjamin Petersonbbb04122010-10-30 23:16:28 +0000451 if (self->fd >= 0)
452 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000453
Guido van Rossuma9e20242007-03-08 00:43:48 +0000454 done:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000455 Py_CLEAR(stringobj);
456 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000457}
458
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000459static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000460fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000461{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000462 Py_VISIT(self->dict);
463 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000464}
465
466static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000467fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000468{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000469 Py_CLEAR(self->dict);
470 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000471}
472
Guido van Rossuma9e20242007-03-08 00:43:48 +0000473static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000474fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000475{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200476 self->finalizing = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000477 if (_PyIOBase_finalize((PyObject *) self) < 0)
478 return;
479 _PyObject_GC_UNTRACK(self);
480 if (self->weakreflist != NULL)
481 PyObject_ClearWeakRefs((PyObject *) self);
482 Py_CLEAR(self->dict);
483 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000484}
485
486static PyObject *
487err_closed(void)
488{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000489 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
490 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000491}
492
493static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000494err_mode(char *action)
495{
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000496 PyErr_Format(IO_STATE->unsupported_operation,
497 "File not open for %s", action);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000498 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000499}
500
501static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000502fileio_fileno(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 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000507}
508
509static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000510fileio_readable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000511{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000512 if (self->fd < 0)
513 return err_closed();
514 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000515}
516
517static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000518fileio_writable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000519{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000520 if (self->fd < 0)
521 return err_closed();
522 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000523}
524
525static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000526fileio_seekable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000527{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000528 if (self->fd < 0)
529 return err_closed();
530 if (self->seekable < 0) {
531 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
532 if (pos == NULL) {
533 PyErr_Clear();
534 self->seekable = 0;
535 } else {
536 Py_DECREF(pos);
537 self->seekable = 1;
538 }
539 }
540 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000541}
542
543static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000544fileio_readinto(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000545{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000546 Py_buffer pbuf;
Victor Stinnere6edec22011-01-04 00:29:35 +0000547 Py_ssize_t n, len;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100548 int err;
Guido van Rossum53807da2007-04-10 19:01:47 +0000549
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000550 if (self->fd < 0)
551 return err_closed();
552 if (!self->readable)
553 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000554
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000555 if (!PyArg_ParseTuple(args, "w*", &pbuf))
556 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000557
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000558 if (_PyVerify_fd(self->fd)) {
Victor Stinnere6edec22011-01-04 00:29:35 +0000559 len = pbuf.len;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000560 Py_BEGIN_ALLOW_THREADS
561 errno = 0;
Victor Stinner14b9b112013-06-25 00:37:25 +0200562#ifdef MS_WINDOWS
Victor Stinnere6edec22011-01-04 00:29:35 +0000563 if (len > INT_MAX)
564 len = INT_MAX;
565 n = read(self->fd, pbuf.buf, (int)len);
566#else
Victor Stinner72344792011-01-11 00:04:12 +0000567 n = read(self->fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +0000568#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000569 Py_END_ALLOW_THREADS
570 } else
571 n = -1;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100572 err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000573 PyBuffer_Release(&pbuf);
574 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100575 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000576 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100577 errno = err;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000578 PyErr_SetFromErrno(PyExc_IOError);
579 return NULL;
580 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000581
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000582 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000583}
584
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100585#ifndef HAVE_FSTAT
586
587static PyObject *
588fileio_readall(fileio *self)
589{
590 _Py_IDENTIFIER(readall);
591 return _PyObject_CallMethodId((PyObject*)&PyRawIOBase_Type,
592 &PyId_readall, "O", self);
593}
594
595#else
596
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000597static size_t
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100598new_buffersize(fileio *self, size_t currentsize)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000599{
Antoine Pitroua3f44572012-04-17 13:50:58 +0200600 size_t addend;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100601
Nadeem Vawdad41a98b2011-10-13 13:34:16 +0200602 /* Expand the buffer by an amount proportional to the current size,
Antoine Pitroua3f44572012-04-17 13:50:58 +0200603 giving us amortized linear-time behavior. For bigger sizes, use a
604 less-than-double growth factor to avoid excessive allocation. */
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100605 assert(currentsize <= PY_SSIZE_T_MAX);
Antoine Pitroua3f44572012-04-17 13:50:58 +0200606 if (currentsize > 65536)
607 addend = currentsize >> 3;
608 else
609 addend = 256 + currentsize;
610 if (addend < SMALLCHUNK)
611 /* Avoid tiny read() calls. */
612 addend = SMALLCHUNK;
613 return addend + currentsize;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000614}
615
Guido van Rossum7165cb12007-07-10 06:54:34 +0000616static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000617fileio_readall(fileio *self)
Guido van Rossum7165cb12007-07-10 06:54:34 +0000618{
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200619 struct stat st;
Victor Stinnera2a64772011-10-11 22:45:02 +0200620 Py_off_t pos, end;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000621 PyObject *result;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100622 Py_ssize_t bytes_read = 0;
Victor Stinnerc44057d2013-01-03 03:33:21 +0100623 Py_ssize_t n;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100624 size_t bufsize;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000625
Victor Stinnerb79f28c2011-05-25 22:09:03 +0200626 if (self->fd < 0)
627 return err_closed();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000628 if (!_PyVerify_fd(self->fd))
629 return PyErr_SetFromErrno(PyExc_IOError);
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000630
Victor Stinner14b9b112013-06-25 00:37:25 +0200631#ifdef MS_WINDOWS
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200632 pos = _lseeki64(self->fd, 0L, SEEK_CUR);
633#else
634 pos = lseek(self->fd, 0L, SEEK_CUR);
635#endif
636 if (fstat(self->fd, &st) == 0)
637 end = st.st_size;
638 else
Victor Stinnera2a64772011-10-11 22:45:02 +0200639 end = (Py_off_t)-1;
Christian Heimesa872de52008-12-05 08:26:55 +0000640
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100641 if (end > 0 && end >= pos && pos >= 0 && end - pos < PY_SSIZE_T_MAX) {
642 /* This is probably a real file, so we try to allocate a
643 buffer one byte larger than the rest of the file. If the
644 calculation is right then we should get EOF without having
645 to enlarge the buffer. */
646 bufsize = (size_t)(end - pos + 1);
647 } else {
648 bufsize = SMALLCHUNK;
649 }
650
651 result = PyBytes_FromStringAndSize(NULL, bufsize);
652 if (result == NULL)
653 return NULL;
654
655 while (1) {
656 if (bytes_read >= (Py_ssize_t)bufsize) {
657 bufsize = new_buffersize(self, bytes_read);
658 if (bufsize > PY_SSIZE_T_MAX || bufsize <= 0) {
659 PyErr_SetString(PyExc_OverflowError,
660 "unbounded read returned more bytes "
Victor Stinner3e269392013-05-18 00:38:43 +0200661 "than a Python string can hold");
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100662 Py_DECREF(result);
663 return NULL;
664 }
665
666 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
667 if (_PyBytes_Resize(&result, bufsize) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000668 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000669 }
670 }
671 Py_BEGIN_ALLOW_THREADS
672 errno = 0;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100673 n = bufsize - bytes_read;
Victor Stinner14b9b112013-06-25 00:37:25 +0200674#ifdef MS_WINDOWS
Victor Stinnerc44057d2013-01-03 03:33:21 +0100675 if (n > INT_MAX)
676 n = INT_MAX;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100677 n = read(self->fd, PyBytes_AS_STRING(result) + bytes_read, (int)n);
Victor Stinnerc44057d2013-01-03 03:33:21 +0100678#else
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100679 n = read(self->fd, PyBytes_AS_STRING(result) + bytes_read, n);
Victor Stinnerc44057d2013-01-03 03:33:21 +0100680#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000681 Py_END_ALLOW_THREADS
682 if (n == 0)
683 break;
684 if (n < 0) {
Gregory P. Smith51359922012-06-23 23:55:39 -0700685 if (errno == EINTR) {
686 if (PyErr_CheckSignals()) {
687 Py_DECREF(result);
688 return NULL;
689 }
690 continue;
691 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100692 if (bytes_read > 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000693 break;
694 if (errno == EAGAIN) {
695 Py_DECREF(result);
696 Py_RETURN_NONE;
697 }
698 Py_DECREF(result);
699 PyErr_SetFromErrno(PyExc_IOError);
700 return NULL;
701 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100702 bytes_read += n;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200703 pos += n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000704 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000705
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100706 if (PyBytes_GET_SIZE(result) > bytes_read) {
707 if (_PyBytes_Resize(&result, bytes_read) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000708 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000709 }
710 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000711}
712
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100713#endif /* HAVE_FSTAT */
714
Guido van Rossuma9e20242007-03-08 00:43:48 +0000715static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000716fileio_read(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000717{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000718 char *ptr;
719 Py_ssize_t n;
720 Py_ssize_t size = -1;
721 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000722
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000723 if (self->fd < 0)
724 return err_closed();
725 if (!self->readable)
726 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000727
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000728 if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size))
729 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000730
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000731 if (size < 0) {
732 return fileio_readall(self);
733 }
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000734
Victor Stinner14b9b112013-06-25 00:37:25 +0200735#ifdef MS_WINDOWS
Victor Stinnerc655a722011-07-05 11:31:49 +0200736 if (size > INT_MAX)
737 size = INT_MAX;
738#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000739 bytes = PyBytes_FromStringAndSize(NULL, size);
740 if (bytes == NULL)
741 return NULL;
742 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000743
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000744 if (_PyVerify_fd(self->fd)) {
745 Py_BEGIN_ALLOW_THREADS
746 errno = 0;
Victor Stinner14b9b112013-06-25 00:37:25 +0200747#ifdef MS_WINDOWS
Victor Stinnerc655a722011-07-05 11:31:49 +0200748 n = read(self->fd, ptr, (int)size);
749#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000750 n = read(self->fd, ptr, size);
Victor Stinnerc655a722011-07-05 11:31:49 +0200751#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000752 Py_END_ALLOW_THREADS
753 } else
754 n = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000755
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000756 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100757 int err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000758 Py_DECREF(bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100759 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000760 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100761 errno = err;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000762 PyErr_SetFromErrno(PyExc_IOError);
763 return NULL;
764 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000765
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000766 if (n != size) {
767 if (_PyBytes_Resize(&bytes, n) < 0) {
Victor Stinner85c761d2013-07-16 21:36:02 +0200768 Py_CLEAR(bytes);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000769 return NULL;
770 }
771 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000772
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000773 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000774}
775
776static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000777fileio_write(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000778{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000779 Py_buffer pbuf;
Victor Stinnere6edec22011-01-04 00:29:35 +0000780 Py_ssize_t n, len;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100781 int err;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000782
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000783 if (self->fd < 0)
784 return err_closed();
785 if (!self->writable)
786 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000787
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000788 if (!PyArg_ParseTuple(args, "y*", &pbuf))
789 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000790
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000791 if (_PyVerify_fd(self->fd)) {
792 Py_BEGIN_ALLOW_THREADS
793 errno = 0;
Victor Stinnere6edec22011-01-04 00:29:35 +0000794 len = pbuf.len;
Victor Stinner14b9b112013-06-25 00:37:25 +0200795#ifdef MS_WINDOWS
Victor Stinnere0daff12011-03-20 23:36:35 +0100796 if (len > 32767 && isatty(self->fd)) {
797 /* Issue #11395: the Windows console returns an error (12: not
798 enough space error) on writing into stdout if stdout mode is
799 binary and the length is greater than 66,000 bytes (or less,
800 depending on heap usage). */
801 len = 32767;
802 }
803 else if (len > INT_MAX)
Victor Stinnere6edec22011-01-04 00:29:35 +0000804 len = INT_MAX;
805 n = write(self->fd, pbuf.buf, (int)len);
806#else
Victor Stinner72344792011-01-11 00:04:12 +0000807 n = write(self->fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +0000808#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000809 Py_END_ALLOW_THREADS
810 } else
811 n = -1;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100812 err = errno;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000813
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000814 PyBuffer_Release(&pbuf);
Martin v. Löwis423be952008-08-13 15:53:07 +0000815
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000816 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100817 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000818 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100819 errno = err;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000820 PyErr_SetFromErrno(PyExc_IOError);
821 return NULL;
822 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000823
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000824 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000825}
826
Guido van Rossum53807da2007-04-10 19:01:47 +0000827/* XXX Windows support below is likely incomplete */
828
Guido van Rossum53807da2007-04-10 19:01:47 +0000829/* Cribbed from posix_lseek() */
830static PyObject *
831portable_lseek(int fd, PyObject *posobj, int whence)
832{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000833 Py_off_t pos, res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000834
835#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000836 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
837 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000838#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000839 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000840#endif
841#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000842 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000843#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000844#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000845 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000846#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000847 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000848#endif /* SEEK_SET */
849
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000850 if (posobj == NULL)
851 pos = 0;
852 else {
853 if(PyFloat_Check(posobj)) {
854 PyErr_SetString(PyExc_TypeError, "an integer is required");
855 return NULL;
856 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000857#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000858 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000859#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000860 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000861#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000862 if (PyErr_Occurred())
863 return NULL;
864 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000865
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000866 if (_PyVerify_fd(fd)) {
867 Py_BEGIN_ALLOW_THREADS
Victor Stinner14b9b112013-06-25 00:37:25 +0200868#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000869 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000870#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000871 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000872#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000873 Py_END_ALLOW_THREADS
874 } else
875 res = -1;
876 if (res < 0)
877 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000878
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000879#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000880 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000881#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000882 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000883#endif
884}
885
Guido van Rossuma9e20242007-03-08 00:43:48 +0000886static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000887fileio_seek(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000888{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000889 PyObject *posobj;
890 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000891
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000892 if (self->fd < 0)
893 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000894
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000895 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
896 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000897
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000898 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000899}
900
901static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000902fileio_tell(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000903{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000904 if (self->fd < 0)
905 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000906
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000907 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000908}
909
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000910#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000911static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000912fileio_truncate(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000913{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000914 PyObject *posobj = NULL; /* the new size wanted by the user */
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000915#ifndef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000916 Py_off_t pos;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000917#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000918 int ret;
919 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000920
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000921 fd = self->fd;
922 if (fd < 0)
923 return err_closed();
924 if (!self->writable)
925 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000926
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000927 if (!PyArg_ParseTuple(args, "|O", &posobj))
928 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000929
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000930 if (posobj == Py_None || posobj == NULL) {
931 /* Get the current position. */
932 posobj = portable_lseek(fd, NULL, 1);
933 if (posobj == NULL)
934 return NULL;
935 }
936 else {
937 Py_INCREF(posobj);
938 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000939
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000940#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000941 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
942 so don't even try using it. */
943 {
944 PyObject *oldposobj, *tempposobj;
945 HANDLE hFile;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000946
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000947 /* we save the file pointer position */
948 oldposobj = portable_lseek(fd, NULL, 1);
949 if (oldposobj == NULL) {
950 Py_DECREF(posobj);
951 return NULL;
952 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000953
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000954 /* we then move to the truncation position */
955 tempposobj = portable_lseek(fd, posobj, 0);
956 if (tempposobj == NULL) {
957 Py_DECREF(oldposobj);
958 Py_DECREF(posobj);
959 return NULL;
960 }
961 Py_DECREF(tempposobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000962
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000963 /* Truncate. Note that this may grow the file! */
964 Py_BEGIN_ALLOW_THREADS
965 errno = 0;
966 hFile = (HANDLE)_get_osfhandle(fd);
967 ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
968 if (ret == 0) {
969 ret = SetEndOfFile(hFile) == 0;
970 if (ret)
971 errno = EACCES;
972 }
973 Py_END_ALLOW_THREADS
974
975 /* we restore the file pointer position in any case */
976 tempposobj = portable_lseek(fd, oldposobj, 0);
977 Py_DECREF(oldposobj);
978 if (tempposobj == NULL) {
979 Py_DECREF(posobj);
980 return NULL;
981 }
982 Py_DECREF(tempposobj);
983 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000984#else
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000985
986#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000987 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000988#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000989 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000990#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000991 if (PyErr_Occurred()){
992 Py_DECREF(posobj);
993 return NULL;
994 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000995
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000996 Py_BEGIN_ALLOW_THREADS
997 errno = 0;
998 ret = ftruncate(fd, pos);
999 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001000
Thomas Hellerfdeee3a2007-07-12 11:21:36 +00001001#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001002
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001003 if (ret != 0) {
1004 Py_DECREF(posobj);
1005 PyErr_SetFromErrno(PyExc_IOError);
1006 return NULL;
1007 }
Guido van Rossuma9e20242007-03-08 00:43:48 +00001008
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001009 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001010}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001011#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +00001012
1013static char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001014mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +00001015{
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001016 if (self->created) {
1017 if (self->readable)
1018 return "xb+";
1019 else
1020 return "xb";
1021 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001022 if (self->readable) {
1023 if (self->writable)
1024 return "rb+";
1025 else
1026 return "rb";
1027 }
1028 else
1029 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +00001030}
Guido van Rossuma9e20242007-03-08 00:43:48 +00001031
1032static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001033fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001034{
Martin v. Löwis767046a2011-10-14 15:35:36 +02001035 _Py_IDENTIFIER(name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001036 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001037
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001038 if (self->fd < 0)
1039 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +00001040
Martin v. Löwis767046a2011-10-14 15:35:36 +02001041 nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001042 if (nameobj == NULL) {
1043 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1044 PyErr_Clear();
1045 else
1046 return NULL;
1047 res = PyUnicode_FromFormat("<_io.FileIO fd=%d mode='%s'>",
1048 self->fd, mode_string(self));
1049 }
1050 else {
1051 res = PyUnicode_FromFormat("<_io.FileIO name=%R mode='%s'>",
1052 nameobj, mode_string(self));
1053 Py_DECREF(nameobj);
1054 }
1055 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001056}
1057
1058static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001059fileio_isatty(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001060{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001061 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +00001062
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001063 if (self->fd < 0)
1064 return err_closed();
1065 Py_BEGIN_ALLOW_THREADS
1066 res = isatty(self->fd);
1067 Py_END_ALLOW_THREADS
1068 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001069}
1070
Antoine Pitrou243757e2010-11-05 21:15:39 +00001071static PyObject *
1072fileio_getstate(fileio *self)
1073{
1074 PyErr_Format(PyExc_TypeError,
1075 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
1076 return NULL;
1077}
1078
Guido van Rossuma9e20242007-03-08 00:43:48 +00001079
1080PyDoc_STRVAR(fileio_doc,
Ross Lagerwall59142db2011-10-31 20:34:46 +02001081"file(name: str[, mode: str][, opener: None]) -> file IO object\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001082"\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001083"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 +01001084"writing, exclusive creation or appending. The file will be created if it\n"
1085"doesn't exist when opened for writing or appending; it will be truncated\n"
1086"when opened for writing. A `FileExistsError` will be raised if it already\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001087"exists when opened for creating. Opening a file for creating implies\n"
1088"writing so this mode behaves in a similar way to 'w'.Add a '+' to the mode\n"
1089"to allow simultaneous reading and writing. A custom opener can be used by\n"
1090"passing a callable as *opener*. The underlying file descriptor for the file\n"
Ross Lagerwall59142db2011-10-31 20:34:46 +02001091"object is then obtained by calling opener with (*name*, *flags*).\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001092"*opener* must return an open file descriptor (passing os.open as *opener*\n"
1093"results in functionality similar to passing None).");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001094
1095PyDoc_STRVAR(read_doc,
1096"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
1097"\n"
1098"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +00001099"In non-blocking mode, returns None if no data is available.\n"
1100"On end-of-file, returns ''.");
1101
1102PyDoc_STRVAR(readall_doc,
1103"readall() -> bytes. read all data from the file, returned as bytes.\n"
1104"\n"
1105"In non-blocking mode, returns as much as is immediately available,\n"
1106"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001107
1108PyDoc_STRVAR(write_doc,
1109"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
1110"\n"
1111"Only makes one system call, so not all of the data may be written.\n"
1112"The number of bytes actually written is returned.");
1113
1114PyDoc_STRVAR(fileno_doc,
1115"fileno() -> int. \"file descriptor\".\n"
1116"\n"
1117"This is needed for lower-level file interfaces, such the fcntl module.");
1118
1119PyDoc_STRVAR(seek_doc,
1120"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
1121"\n"
1122"Argument offset is a byte count. Optional argument whence defaults to\n"
1123"0 (offset from start of file, offset should be >= 0); other values are 1\n"
1124"(move relative to current position, positive or negative), and 2 (move\n"
1125"relative to end of file, usually negative, although many platforms allow\n"
1126"seeking beyond the end of a file)."
1127"\n"
1128"Note that not all file objects are seekable.");
1129
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001130#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +00001131PyDoc_STRVAR(truncate_doc,
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001132"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001133"\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +00001134"Size defaults to the current file position, as returned by tell()."
1135"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001136#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +00001137
1138PyDoc_STRVAR(tell_doc,
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001139"tell() -> int. Current file position");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001140
1141PyDoc_STRVAR(readinto_doc,
Benjamin Peterson9a8082f2009-03-05 00:55:56 +00001142"readinto() -> Same as RawIOBase.readinto().");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001143
1144PyDoc_STRVAR(close_doc,
1145"close() -> None. Close the file.\n"
1146"\n"
1147"A closed file cannot be used for further I/O operations. close() may be\n"
1148"called more than once without error. Changes the fileno to -1.");
1149
1150PyDoc_STRVAR(isatty_doc,
1151"isatty() -> bool. True if the file is connected to a tty device.");
1152
Guido van Rossuma9e20242007-03-08 00:43:48 +00001153PyDoc_STRVAR(seekable_doc,
1154"seekable() -> bool. True if file supports random-access.");
1155
1156PyDoc_STRVAR(readable_doc,
1157"readable() -> bool. True if file was opened in a read mode.");
1158
1159PyDoc_STRVAR(writable_doc,
1160"writable() -> bool. True if file was opened in a write mode.");
1161
1162static PyMethodDef fileio_methods[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001163 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
1164 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
1165 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
1166 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
1167 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
1168 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001169#ifdef HAVE_FTRUNCATE
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001170 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001171#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001172 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
1173 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
1174 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
1175 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
1176 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
1177 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Antoine Pitroue033e062010-10-29 10:38:18 +00001178 {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
Antoine Pitrou243757e2010-11-05 21:15:39 +00001179 {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001180 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001181};
1182
Guido van Rossum53807da2007-04-10 19:01:47 +00001183/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1184
Guido van Rossumb0428152007-04-08 17:44:42 +00001185static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001186get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001187{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001188 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001189}
1190
1191static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001192get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001193{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001194 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001195}
1196
1197static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001198get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001199{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001200 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001201}
1202
1203static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001204 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1205 {"closefd", (getter)get_closefd, NULL,
1206 "True if the file descriptor will be closed"},
1207 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1208 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001209};
1210
Antoine Pitrou796564c2013-07-30 19:59:21 +02001211static PyMemberDef fileio_members[] = {
1212 {"_finalizing", T_BOOL, offsetof(fileio, finalizing), 0},
1213 {NULL}
1214};
1215
Guido van Rossuma9e20242007-03-08 00:43:48 +00001216PyTypeObject PyFileIO_Type = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001217 PyVarObject_HEAD_INIT(NULL, 0)
1218 "_io.FileIO",
1219 sizeof(fileio),
1220 0,
1221 (destructor)fileio_dealloc, /* tp_dealloc */
1222 0, /* tp_print */
1223 0, /* tp_getattr */
1224 0, /* tp_setattr */
1225 0, /* tp_reserved */
1226 (reprfunc)fileio_repr, /* tp_repr */
1227 0, /* tp_as_number */
1228 0, /* tp_as_sequence */
1229 0, /* tp_as_mapping */
1230 0, /* tp_hash */
1231 0, /* tp_call */
1232 0, /* tp_str */
1233 PyObject_GenericGetAttr, /* tp_getattro */
1234 0, /* tp_setattro */
1235 0, /* tp_as_buffer */
1236 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +02001237 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001238 fileio_doc, /* tp_doc */
1239 (traverseproc)fileio_traverse, /* tp_traverse */
1240 (inquiry)fileio_clear, /* tp_clear */
1241 0, /* tp_richcompare */
1242 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1243 0, /* tp_iter */
1244 0, /* tp_iternext */
1245 fileio_methods, /* tp_methods */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001246 fileio_members, /* tp_members */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001247 fileio_getsetlist, /* tp_getset */
1248 0, /* tp_base */
1249 0, /* tp_dict */
1250 0, /* tp_descr_get */
1251 0, /* tp_descr_set */
1252 offsetof(fileio, dict), /* tp_dictoffset */
1253 fileio_init, /* tp_init */
1254 PyType_GenericAlloc, /* tp_alloc */
1255 fileio_new, /* tp_new */
1256 PyObject_GC_Del, /* tp_free */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001257 0, /* tp_is_gc */
1258 0, /* tp_bases */
1259 0, /* tp_mro */
1260 0, /* tp_cache */
1261 0, /* tp_subclasses */
1262 0, /* tp_weaklist */
1263 0, /* tp_del */
1264 0, /* tp_version_tag */
1265 0, /* tp_finalize */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001266};