blob: 27995e543b03002861c421e0e465032734504b9a [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 Pitrou796564c2013-07-30 19:59:21 +020055 char finalizing;
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 Pitrou796564c2013-07-30 19:59:21 +0200132 if (self->finalizing) {
Antoine Pitroue033e062010-10-29 10:38:18 +0000133 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
Victor Stinnerdaf45552013-08-28 00:53:59 +0200207#ifdef O_CLOEXEC
208extern int _Py_open_cloexec_works;
209#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000210
211static int
212fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
213{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000214 fileio *self = (fileio *) oself;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200215 static char *kwlist[] = {"file", "mode", "closefd", "opener", NULL};
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000216 const char *name = NULL;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200217 PyObject *nameobj, *stringobj = NULL, *opener = Py_None;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000218 char *mode = "r";
219 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000220#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000221 Py_UNICODE *widename = NULL;
Thomas Helleraf2be262007-07-12 11:03:13 +0000222#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000223 int ret = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200224 int rwa = 0, plus = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000225 int flags = 0;
226 int fd = -1;
227 int closefd = 1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200228 int fd_is_own = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200229#ifdef O_CLOEXEC
230 int *atomic_flag_works = &_Py_open_cloexec_works;
231#elif !defined(MS_WINDOWS)
232 int *atomic_flag_works = NULL;
233#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000234
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000235 assert(PyFileIO_Check(oself));
236 if (self->fd >= 0) {
Hynek Schlawack2cc71562012-05-25 10:05:53 +0200237 if (self->closefd) {
238 /* Have to close the existing file first. */
239 if (internal_close(self) < 0)
240 return -1;
241 }
242 else
243 self->fd = -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000244 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000245
Ross Lagerwall59142db2011-10-31 20:34:46 +0200246 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|siO:fileio",
247 kwlist, &nameobj, &mode, &closefd,
248 &opener))
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000249 return -1;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000250
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000251 if (PyFloat_Check(nameobj)) {
252 PyErr_SetString(PyExc_TypeError,
253 "integer argument expected, got float");
254 return -1;
255 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000256
Serhiy Storchaka78980432013-01-15 01:12:17 +0200257 fd = _PyLong_AsInt(nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000258 if (fd < 0) {
259 if (!PyErr_Occurred()) {
260 PyErr_SetString(PyExc_ValueError,
261 "Negative filedescriptor");
262 return -1;
263 }
264 PyErr_Clear();
265 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000266
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000267#ifdef MS_WINDOWS
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200268 if (PyUnicode_Check(nameobj)) {
Antoine Pitrou13348842012-01-29 18:36:34 +0100269 int rv = _PyUnicode_HasNULChars(nameobj);
270 if (rv) {
271 if (rv != -1)
272 PyErr_SetString(PyExc_TypeError, "embedded NUL character");
273 return -1;
274 }
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200275 widename = PyUnicode_AsUnicode(nameobj);
276 if (widename == NULL)
277 return -1;
278 } else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000279#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000280 if (fd < 0)
281 {
Antoine Pitrou13348842012-01-29 18:36:34 +0100282 if (!PyUnicode_FSConverter(nameobj, &stringobj)) {
283 return -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000284 }
Antoine Pitrou13348842012-01-29 18:36:34 +0100285 name = PyBytes_AS_STRING(stringobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000286 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000287
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000288 s = mode;
289 while (*s) {
290 switch (*s++) {
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100291 case 'x':
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000292 if (rwa) {
293 bad_mode:
294 PyErr_SetString(PyExc_ValueError,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100295 "Must have exactly one of create/read/write/append "
Georg Brandl28928ae2010-10-21 13:45:52 +0000296 "mode and at most one plus");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000297 goto error;
298 }
299 rwa = 1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100300 self->created = 1;
301 self->writable = 1;
302 flags |= O_EXCL | O_CREAT;
303 break;
304 case 'r':
305 if (rwa)
306 goto bad_mode;
307 rwa = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000308 self->readable = 1;
309 break;
310 case 'w':
311 if (rwa)
312 goto bad_mode;
313 rwa = 1;
314 self->writable = 1;
315 flags |= O_CREAT | O_TRUNC;
316 break;
317 case 'a':
318 if (rwa)
319 goto bad_mode;
320 rwa = 1;
321 self->writable = 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200322 self->appending = 1;
323 flags |= O_APPEND | O_CREAT;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000324 break;
325 case 'b':
326 break;
327 case '+':
328 if (plus)
329 goto bad_mode;
330 self->readable = self->writable = 1;
331 plus = 1;
332 break;
333 default:
334 PyErr_Format(PyExc_ValueError,
335 "invalid mode: %.200s", mode);
336 goto error;
337 }
338 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000339
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000340 if (!rwa)
341 goto bad_mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000342
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000343 if (self->readable && self->writable)
344 flags |= O_RDWR;
345 else if (self->readable)
346 flags |= O_RDONLY;
347 else
348 flags |= O_WRONLY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000349
350#ifdef O_BINARY
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000351 flags |= O_BINARY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000352#endif
353
Victor Stinnerdaf45552013-08-28 00:53:59 +0200354#ifdef MS_WINDOWS
355 flags |= O_NOINHERIT;
356#elif defined(O_CLOEXEC)
357 flags |= O_CLOEXEC;
358#endif
Walter Dörwald0e411482007-06-06 16:55:38 +0000359
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000360 if (fd >= 0) {
361 if (check_fd(fd))
362 goto error;
363 self->fd = fd;
364 self->closefd = closefd;
365 }
366 else {
367 self->closefd = 1;
368 if (!closefd) {
369 PyErr_SetString(PyExc_ValueError,
370 "Cannot use closefd=False with file name");
371 goto error;
372 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000373
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000374 errno = 0;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200375 if (opener == Py_None) {
376 Py_BEGIN_ALLOW_THREADS
Thomas Helleraf2be262007-07-12 11:03:13 +0000377#ifdef MS_WINDOWS
Ross Lagerwall59142db2011-10-31 20:34:46 +0200378 if (widename != NULL)
379 self->fd = _wopen(widename, flags, 0666);
380 else
Thomas Helleraf2be262007-07-12 11:03:13 +0000381#endif
Ross Lagerwall59142db2011-10-31 20:34:46 +0200382 self->fd = open(name, flags, 0666);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200383
Ross Lagerwall59142db2011-10-31 20:34:46 +0200384 Py_END_ALLOW_THREADS
Victor Stinnerdaf45552013-08-28 00:53:59 +0200385 }
386 else {
387 PyObject *fdobj;
388
389#ifndef MS_WINDOWS
390 /* the opener may clear the atomic flag */
391 atomic_flag_works = NULL;
392#endif
393
394 fdobj = PyObject_CallFunction(opener, "Oi", nameobj, flags);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200395 if (fdobj == NULL)
396 goto error;
397 if (!PyLong_Check(fdobj)) {
398 Py_DECREF(fdobj);
399 PyErr_SetString(PyExc_TypeError,
400 "expected integer from opener");
401 goto error;
402 }
403
Serhiy Storchaka78980432013-01-15 01:12:17 +0200404 self->fd = _PyLong_AsInt(fdobj);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200405 Py_DECREF(fdobj);
406 if (self->fd == -1) {
407 goto error;
408 }
409 }
410
Hynek Schlawack7f59fd72012-06-22 09:32:22 +0200411 fd_is_own = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000412 if (self->fd < 0) {
Victor Stinner292c8352012-10-30 02:17:38 +0100413 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000414 goto error;
415 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200416
417#ifndef MS_WINDOWS
418 if (_Py_set_inheritable(self->fd, 0, atomic_flag_works) < 0)
419 goto error;
420#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000421 }
Antoine Pitrou9235b252012-07-06 18:48:24 +0200422 if (dircheck(self, nameobj) < 0)
423 goto error;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000424
Victor Stinner89e34362011-01-07 18:47:22 +0000425#if defined(MS_WINDOWS) || defined(__CYGWIN__)
426 /* don't translate newlines (\r\n <=> \n) */
427 _setmode(self->fd, O_BINARY);
428#endif
429
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000430 if (PyObject_SetAttrString((PyObject *)self, "name", nameobj) < 0)
431 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000432
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200433 if (self->appending) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000434 /* For consistent behaviour, we explicitly seek to the
435 end of file (otherwise, it might be done only on the
436 first write()). */
437 PyObject *pos = portable_lseek(self->fd, NULL, 2);
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200438 if (pos == NULL)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000439 goto error;
440 Py_DECREF(pos);
441 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000442
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000443 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000444
445 error:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000446 ret = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200447 if (!fd_is_own)
448 self->fd = -1;
Benjamin Petersonbbb04122010-10-30 23:16:28 +0000449 if (self->fd >= 0)
450 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000451
Guido van Rossuma9e20242007-03-08 00:43:48 +0000452 done:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000453 Py_CLEAR(stringobj);
454 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000455}
456
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000457static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000458fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000459{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000460 Py_VISIT(self->dict);
461 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000462}
463
464static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000465fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000466{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000467 Py_CLEAR(self->dict);
468 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000469}
470
Guido van Rossuma9e20242007-03-08 00:43:48 +0000471static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000472fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000473{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200474 self->finalizing = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000475 if (_PyIOBase_finalize((PyObject *) self) < 0)
476 return;
477 _PyObject_GC_UNTRACK(self);
478 if (self->weakreflist != NULL)
479 PyObject_ClearWeakRefs((PyObject *) self);
480 Py_CLEAR(self->dict);
481 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000482}
483
484static PyObject *
485err_closed(void)
486{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000487 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
488 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000489}
490
491static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000492err_mode(char *action)
493{
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000494 PyErr_Format(IO_STATE->unsupported_operation,
495 "File not open for %s", action);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000496 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000497}
498
499static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000500fileio_fileno(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000501{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000502 if (self->fd < 0)
503 return err_closed();
504 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000505}
506
507static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000508fileio_readable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000509{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000510 if (self->fd < 0)
511 return err_closed();
512 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000513}
514
515static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000516fileio_writable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000517{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000518 if (self->fd < 0)
519 return err_closed();
520 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000521}
522
523static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000524fileio_seekable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000525{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000526 if (self->fd < 0)
527 return err_closed();
528 if (self->seekable < 0) {
529 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
530 if (pos == NULL) {
531 PyErr_Clear();
532 self->seekable = 0;
533 } else {
534 Py_DECREF(pos);
535 self->seekable = 1;
536 }
537 }
538 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000539}
540
541static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000542fileio_readinto(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000543{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000544 Py_buffer pbuf;
Victor Stinnere6edec22011-01-04 00:29:35 +0000545 Py_ssize_t n, len;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100546 int err;
Guido van Rossum53807da2007-04-10 19:01:47 +0000547
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000548 if (self->fd < 0)
549 return err_closed();
550 if (!self->readable)
551 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000552
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000553 if (!PyArg_ParseTuple(args, "w*", &pbuf))
554 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000555
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000556 if (_PyVerify_fd(self->fd)) {
Victor Stinnere6edec22011-01-04 00:29:35 +0000557 len = pbuf.len;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000558 Py_BEGIN_ALLOW_THREADS
559 errno = 0;
Victor Stinner14b9b112013-06-25 00:37:25 +0200560#ifdef MS_WINDOWS
Victor Stinnere6edec22011-01-04 00:29:35 +0000561 if (len > INT_MAX)
562 len = INT_MAX;
563 n = read(self->fd, pbuf.buf, (int)len);
564#else
Victor Stinner72344792011-01-11 00:04:12 +0000565 n = read(self->fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +0000566#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000567 Py_END_ALLOW_THREADS
568 } else
569 n = -1;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100570 err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000571 PyBuffer_Release(&pbuf);
572 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100573 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000574 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100575 errno = err;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000576 PyErr_SetFromErrno(PyExc_IOError);
577 return NULL;
578 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000579
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000580 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000581}
582
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100583#ifndef HAVE_FSTAT
584
585static PyObject *
586fileio_readall(fileio *self)
587{
588 _Py_IDENTIFIER(readall);
589 return _PyObject_CallMethodId((PyObject*)&PyRawIOBase_Type,
590 &PyId_readall, "O", self);
591}
592
593#else
594
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000595static size_t
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100596new_buffersize(fileio *self, size_t currentsize)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000597{
Antoine Pitroua3f44572012-04-17 13:50:58 +0200598 size_t addend;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100599
Nadeem Vawdad41a98b2011-10-13 13:34:16 +0200600 /* Expand the buffer by an amount proportional to the current size,
Antoine Pitroua3f44572012-04-17 13:50:58 +0200601 giving us amortized linear-time behavior. For bigger sizes, use a
602 less-than-double growth factor to avoid excessive allocation. */
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100603 assert(currentsize <= PY_SSIZE_T_MAX);
Antoine Pitroua3f44572012-04-17 13:50:58 +0200604 if (currentsize > 65536)
605 addend = currentsize >> 3;
606 else
607 addend = 256 + currentsize;
608 if (addend < SMALLCHUNK)
609 /* Avoid tiny read() calls. */
610 addend = SMALLCHUNK;
611 return addend + currentsize;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000612}
613
Guido van Rossum7165cb12007-07-10 06:54:34 +0000614static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000615fileio_readall(fileio *self)
Guido van Rossum7165cb12007-07-10 06:54:34 +0000616{
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200617 struct stat st;
Victor Stinnera2a64772011-10-11 22:45:02 +0200618 Py_off_t pos, end;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000619 PyObject *result;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100620 Py_ssize_t bytes_read = 0;
Victor Stinnerc44057d2013-01-03 03:33:21 +0100621 Py_ssize_t n;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100622 size_t bufsize;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000623
Victor Stinnerb79f28c2011-05-25 22:09:03 +0200624 if (self->fd < 0)
625 return err_closed();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000626 if (!_PyVerify_fd(self->fd))
627 return PyErr_SetFromErrno(PyExc_IOError);
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000628
Victor Stinner14b9b112013-06-25 00:37:25 +0200629#ifdef MS_WINDOWS
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200630 pos = _lseeki64(self->fd, 0L, SEEK_CUR);
631#else
632 pos = lseek(self->fd, 0L, SEEK_CUR);
633#endif
634 if (fstat(self->fd, &st) == 0)
635 end = st.st_size;
636 else
Victor Stinnera2a64772011-10-11 22:45:02 +0200637 end = (Py_off_t)-1;
Christian Heimesa872de52008-12-05 08:26:55 +0000638
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100639 if (end > 0 && end >= pos && pos >= 0 && end - pos < PY_SSIZE_T_MAX) {
640 /* This is probably a real file, so we try to allocate a
641 buffer one byte larger than the rest of the file. If the
642 calculation is right then we should get EOF without having
643 to enlarge the buffer. */
644 bufsize = (size_t)(end - pos + 1);
645 } else {
646 bufsize = SMALLCHUNK;
647 }
648
649 result = PyBytes_FromStringAndSize(NULL, bufsize);
650 if (result == NULL)
651 return NULL;
652
653 while (1) {
654 if (bytes_read >= (Py_ssize_t)bufsize) {
655 bufsize = new_buffersize(self, bytes_read);
656 if (bufsize > PY_SSIZE_T_MAX || bufsize <= 0) {
657 PyErr_SetString(PyExc_OverflowError,
658 "unbounded read returned more bytes "
Victor Stinner3e269392013-05-18 00:38:43 +0200659 "than a Python string can hold");
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100660 Py_DECREF(result);
661 return NULL;
662 }
663
664 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
665 if (_PyBytes_Resize(&result, bufsize) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000666 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000667 }
668 }
669 Py_BEGIN_ALLOW_THREADS
670 errno = 0;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100671 n = bufsize - bytes_read;
Victor Stinner14b9b112013-06-25 00:37:25 +0200672#ifdef MS_WINDOWS
Victor Stinnerc44057d2013-01-03 03:33:21 +0100673 if (n > INT_MAX)
674 n = INT_MAX;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100675 n = read(self->fd, PyBytes_AS_STRING(result) + bytes_read, (int)n);
Victor Stinnerc44057d2013-01-03 03:33:21 +0100676#else
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100677 n = read(self->fd, PyBytes_AS_STRING(result) + bytes_read, n);
Victor Stinnerc44057d2013-01-03 03:33:21 +0100678#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000679 Py_END_ALLOW_THREADS
680 if (n == 0)
681 break;
682 if (n < 0) {
Gregory P. Smith51359922012-06-23 23:55:39 -0700683 if (errno == EINTR) {
684 if (PyErr_CheckSignals()) {
685 Py_DECREF(result);
686 return NULL;
687 }
688 continue;
689 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100690 if (bytes_read > 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000691 break;
692 if (errno == EAGAIN) {
693 Py_DECREF(result);
694 Py_RETURN_NONE;
695 }
696 Py_DECREF(result);
697 PyErr_SetFromErrno(PyExc_IOError);
698 return NULL;
699 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100700 bytes_read += n;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200701 pos += n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000702 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000703
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100704 if (PyBytes_GET_SIZE(result) > bytes_read) {
705 if (_PyBytes_Resize(&result, bytes_read) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000706 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000707 }
708 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000709}
710
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100711#endif /* HAVE_FSTAT */
712
Guido van Rossuma9e20242007-03-08 00:43:48 +0000713static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000714fileio_read(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000715{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000716 char *ptr;
717 Py_ssize_t n;
718 Py_ssize_t size = -1;
719 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000720
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000721 if (self->fd < 0)
722 return err_closed();
723 if (!self->readable)
724 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000725
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000726 if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size))
727 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000728
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000729 if (size < 0) {
730 return fileio_readall(self);
731 }
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000732
Victor Stinner14b9b112013-06-25 00:37:25 +0200733#ifdef MS_WINDOWS
Victor Stinnerc655a722011-07-05 11:31:49 +0200734 if (size > INT_MAX)
735 size = INT_MAX;
736#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000737 bytes = PyBytes_FromStringAndSize(NULL, size);
738 if (bytes == NULL)
739 return NULL;
740 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000741
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000742 if (_PyVerify_fd(self->fd)) {
743 Py_BEGIN_ALLOW_THREADS
744 errno = 0;
Victor Stinner14b9b112013-06-25 00:37:25 +0200745#ifdef MS_WINDOWS
Victor Stinnerc655a722011-07-05 11:31:49 +0200746 n = read(self->fd, ptr, (int)size);
747#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000748 n = read(self->fd, ptr, size);
Victor Stinnerc655a722011-07-05 11:31:49 +0200749#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000750 Py_END_ALLOW_THREADS
751 } else
752 n = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000753
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000754 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100755 int err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000756 Py_DECREF(bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100757 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000758 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100759 errno = err;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000760 PyErr_SetFromErrno(PyExc_IOError);
761 return NULL;
762 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000763
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000764 if (n != size) {
765 if (_PyBytes_Resize(&bytes, n) < 0) {
Victor Stinner85c761d2013-07-16 21:36:02 +0200766 Py_CLEAR(bytes);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000767 return NULL;
768 }
769 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000770
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000771 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000772}
773
774static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000775fileio_write(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000776{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000777 Py_buffer pbuf;
Victor Stinnere6edec22011-01-04 00:29:35 +0000778 Py_ssize_t n, len;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100779 int err;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000780
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000781 if (self->fd < 0)
782 return err_closed();
783 if (!self->writable)
784 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000785
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000786 if (!PyArg_ParseTuple(args, "y*", &pbuf))
787 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000788
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000789 if (_PyVerify_fd(self->fd)) {
790 Py_BEGIN_ALLOW_THREADS
791 errno = 0;
Victor Stinnere6edec22011-01-04 00:29:35 +0000792 len = pbuf.len;
Victor Stinner14b9b112013-06-25 00:37:25 +0200793#ifdef MS_WINDOWS
Victor Stinnere0daff12011-03-20 23:36:35 +0100794 if (len > 32767 && isatty(self->fd)) {
795 /* Issue #11395: the Windows console returns an error (12: not
796 enough space error) on writing into stdout if stdout mode is
797 binary and the length is greater than 66,000 bytes (or less,
798 depending on heap usage). */
799 len = 32767;
800 }
801 else if (len > INT_MAX)
Victor Stinnere6edec22011-01-04 00:29:35 +0000802 len = INT_MAX;
803 n = write(self->fd, pbuf.buf, (int)len);
804#else
Victor Stinner72344792011-01-11 00:04:12 +0000805 n = write(self->fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +0000806#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000807 Py_END_ALLOW_THREADS
808 } else
809 n = -1;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100810 err = errno;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000811
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000812 PyBuffer_Release(&pbuf);
Martin v. Löwis423be952008-08-13 15:53:07 +0000813
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000814 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100815 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000816 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100817 errno = err;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000818 PyErr_SetFromErrno(PyExc_IOError);
819 return NULL;
820 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000821
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000822 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000823}
824
Guido van Rossum53807da2007-04-10 19:01:47 +0000825/* XXX Windows support below is likely incomplete */
826
Guido van Rossum53807da2007-04-10 19:01:47 +0000827/* Cribbed from posix_lseek() */
828static PyObject *
829portable_lseek(int fd, PyObject *posobj, int whence)
830{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000831 Py_off_t pos, res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000832
833#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000834 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
835 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000836#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000837 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000838#endif
839#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000840 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000841#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000842#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000843 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000844#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000845 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000846#endif /* SEEK_SET */
847
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000848 if (posobj == NULL)
849 pos = 0;
850 else {
851 if(PyFloat_Check(posobj)) {
852 PyErr_SetString(PyExc_TypeError, "an integer is required");
853 return NULL;
854 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000855#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000856 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000857#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000858 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000859#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000860 if (PyErr_Occurred())
861 return NULL;
862 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000863
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000864 if (_PyVerify_fd(fd)) {
865 Py_BEGIN_ALLOW_THREADS
Victor Stinner14b9b112013-06-25 00:37:25 +0200866#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000867 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000868#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000869 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000870#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000871 Py_END_ALLOW_THREADS
872 } else
873 res = -1;
874 if (res < 0)
875 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000876
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000877#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000878 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000879#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000880 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000881#endif
882}
883
Guido van Rossuma9e20242007-03-08 00:43:48 +0000884static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000885fileio_seek(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000886{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000887 PyObject *posobj;
888 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000889
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000890 if (self->fd < 0)
891 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000892
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000893 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
894 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000895
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000896 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000897}
898
899static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000900fileio_tell(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000901{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000902 if (self->fd < 0)
903 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000904
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000905 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000906}
907
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000908#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000909static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000910fileio_truncate(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000911{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000912 PyObject *posobj = NULL; /* the new size wanted by the user */
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000913#ifndef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000914 Py_off_t pos;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000915#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000916 int ret;
917 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000918
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000919 fd = self->fd;
920 if (fd < 0)
921 return err_closed();
922 if (!self->writable)
923 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000924
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000925 if (!PyArg_ParseTuple(args, "|O", &posobj))
926 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000927
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000928 if (posobj == Py_None || posobj == NULL) {
929 /* Get the current position. */
930 posobj = portable_lseek(fd, NULL, 1);
931 if (posobj == NULL)
932 return NULL;
933 }
934 else {
935 Py_INCREF(posobj);
936 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000937
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000938#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000939 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
940 so don't even try using it. */
941 {
942 PyObject *oldposobj, *tempposobj;
943 HANDLE hFile;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000944
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000945 /* we save the file pointer position */
946 oldposobj = portable_lseek(fd, NULL, 1);
947 if (oldposobj == NULL) {
948 Py_DECREF(posobj);
949 return NULL;
950 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000951
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000952 /* we then move to the truncation position */
953 tempposobj = portable_lseek(fd, posobj, 0);
954 if (tempposobj == NULL) {
955 Py_DECREF(oldposobj);
956 Py_DECREF(posobj);
957 return NULL;
958 }
959 Py_DECREF(tempposobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000960
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000961 /* Truncate. Note that this may grow the file! */
962 Py_BEGIN_ALLOW_THREADS
963 errno = 0;
964 hFile = (HANDLE)_get_osfhandle(fd);
965 ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
966 if (ret == 0) {
967 ret = SetEndOfFile(hFile) == 0;
968 if (ret)
969 errno = EACCES;
970 }
971 Py_END_ALLOW_THREADS
972
973 /* we restore the file pointer position in any case */
974 tempposobj = portable_lseek(fd, oldposobj, 0);
975 Py_DECREF(oldposobj);
976 if (tempposobj == NULL) {
977 Py_DECREF(posobj);
978 return NULL;
979 }
980 Py_DECREF(tempposobj);
981 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000982#else
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000983
984#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000985 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000986#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000987 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000988#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000989 if (PyErr_Occurred()){
990 Py_DECREF(posobj);
991 return NULL;
992 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000993
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000994 Py_BEGIN_ALLOW_THREADS
995 errno = 0;
996 ret = ftruncate(fd, pos);
997 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000998
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000999#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001000
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001001 if (ret != 0) {
1002 Py_DECREF(posobj);
1003 PyErr_SetFromErrno(PyExc_IOError);
1004 return NULL;
1005 }
Guido van Rossuma9e20242007-03-08 00:43:48 +00001006
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001007 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001008}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001009#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +00001010
1011static char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001012mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +00001013{
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001014 if (self->created) {
1015 if (self->readable)
1016 return "xb+";
1017 else
1018 return "xb";
1019 }
Antoine Pitroue93b63b2013-09-04 20:46:33 +02001020 if (self->appending) {
1021 if (self->readable)
1022 return "ab+";
1023 else
1024 return "ab";
1025 }
1026 else if (self->readable) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001027 if (self->writable)
1028 return "rb+";
1029 else
1030 return "rb";
1031 }
1032 else
1033 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +00001034}
Guido van Rossuma9e20242007-03-08 00:43:48 +00001035
1036static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001037fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001038{
Martin v. Löwis767046a2011-10-14 15:35:36 +02001039 _Py_IDENTIFIER(name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001040 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001041
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001042 if (self->fd < 0)
1043 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +00001044
Martin v. Löwis767046a2011-10-14 15:35:36 +02001045 nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001046 if (nameobj == NULL) {
1047 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1048 PyErr_Clear();
1049 else
1050 return NULL;
1051 res = PyUnicode_FromFormat("<_io.FileIO fd=%d mode='%s'>",
1052 self->fd, mode_string(self));
1053 }
1054 else {
1055 res = PyUnicode_FromFormat("<_io.FileIO name=%R mode='%s'>",
1056 nameobj, mode_string(self));
1057 Py_DECREF(nameobj);
1058 }
1059 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001060}
1061
1062static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001063fileio_isatty(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001064{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001065 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +00001066
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001067 if (self->fd < 0)
1068 return err_closed();
1069 Py_BEGIN_ALLOW_THREADS
1070 res = isatty(self->fd);
1071 Py_END_ALLOW_THREADS
1072 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001073}
1074
Antoine Pitrou243757e2010-11-05 21:15:39 +00001075static PyObject *
1076fileio_getstate(fileio *self)
1077{
1078 PyErr_Format(PyExc_TypeError,
1079 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
1080 return NULL;
1081}
1082
Guido van Rossuma9e20242007-03-08 00:43:48 +00001083
1084PyDoc_STRVAR(fileio_doc,
Ross Lagerwall59142db2011-10-31 20:34:46 +02001085"file(name: str[, mode: str][, opener: None]) -> file IO object\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001086"\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001087"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 +01001088"writing, exclusive creation or appending. The file will be created if it\n"
1089"doesn't exist when opened for writing or appending; it will be truncated\n"
1090"when opened for writing. A `FileExistsError` will be raised if it already\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001091"exists when opened for creating. Opening a file for creating implies\n"
1092"writing so this mode behaves in a similar way to 'w'.Add a '+' to the mode\n"
1093"to allow simultaneous reading and writing. A custom opener can be used by\n"
1094"passing a callable as *opener*. The underlying file descriptor for the file\n"
Ross Lagerwall59142db2011-10-31 20:34:46 +02001095"object is then obtained by calling opener with (*name*, *flags*).\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001096"*opener* must return an open file descriptor (passing os.open as *opener*\n"
1097"results in functionality similar to passing None).");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001098
1099PyDoc_STRVAR(read_doc,
1100"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
1101"\n"
1102"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +00001103"In non-blocking mode, returns None if no data is available.\n"
1104"On end-of-file, returns ''.");
1105
1106PyDoc_STRVAR(readall_doc,
1107"readall() -> bytes. read all data from the file, returned as bytes.\n"
1108"\n"
1109"In non-blocking mode, returns as much as is immediately available,\n"
1110"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001111
1112PyDoc_STRVAR(write_doc,
1113"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
1114"\n"
1115"Only makes one system call, so not all of the data may be written.\n"
1116"The number of bytes actually written is returned.");
1117
1118PyDoc_STRVAR(fileno_doc,
1119"fileno() -> int. \"file descriptor\".\n"
1120"\n"
1121"This is needed for lower-level file interfaces, such the fcntl module.");
1122
1123PyDoc_STRVAR(seek_doc,
1124"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
1125"\n"
1126"Argument offset is a byte count. Optional argument whence defaults to\n"
1127"0 (offset from start of file, offset should be >= 0); other values are 1\n"
1128"(move relative to current position, positive or negative), and 2 (move\n"
1129"relative to end of file, usually negative, although many platforms allow\n"
1130"seeking beyond the end of a file)."
1131"\n"
1132"Note that not all file objects are seekable.");
1133
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001134#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +00001135PyDoc_STRVAR(truncate_doc,
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001136"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001137"\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +00001138"Size defaults to the current file position, as returned by tell()."
1139"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001140#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +00001141
1142PyDoc_STRVAR(tell_doc,
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001143"tell() -> int. Current file position");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001144
1145PyDoc_STRVAR(readinto_doc,
Benjamin Peterson9a8082f2009-03-05 00:55:56 +00001146"readinto() -> Same as RawIOBase.readinto().");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001147
1148PyDoc_STRVAR(close_doc,
1149"close() -> None. Close the file.\n"
1150"\n"
1151"A closed file cannot be used for further I/O operations. close() may be\n"
1152"called more than once without error. Changes the fileno to -1.");
1153
1154PyDoc_STRVAR(isatty_doc,
1155"isatty() -> bool. True if the file is connected to a tty device.");
1156
Guido van Rossuma9e20242007-03-08 00:43:48 +00001157PyDoc_STRVAR(seekable_doc,
1158"seekable() -> bool. True if file supports random-access.");
1159
1160PyDoc_STRVAR(readable_doc,
1161"readable() -> bool. True if file was opened in a read mode.");
1162
1163PyDoc_STRVAR(writable_doc,
1164"writable() -> bool. True if file was opened in a write mode.");
1165
1166static PyMethodDef fileio_methods[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001167 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
1168 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
1169 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
1170 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
1171 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
1172 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001173#ifdef HAVE_FTRUNCATE
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001174 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001175#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001176 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
1177 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
1178 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
1179 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
1180 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
1181 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Antoine Pitroue033e062010-10-29 10:38:18 +00001182 {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
Antoine Pitrou243757e2010-11-05 21:15:39 +00001183 {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001184 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001185};
1186
Guido van Rossum53807da2007-04-10 19:01:47 +00001187/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1188
Guido van Rossumb0428152007-04-08 17:44:42 +00001189static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001190get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001191{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001192 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001193}
1194
1195static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001196get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001197{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001198 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001199}
1200
1201static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001202get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001203{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001204 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001205}
1206
1207static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001208 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1209 {"closefd", (getter)get_closefd, NULL,
1210 "True if the file descriptor will be closed"},
1211 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1212 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001213};
1214
Antoine Pitrou796564c2013-07-30 19:59:21 +02001215static PyMemberDef fileio_members[] = {
1216 {"_finalizing", T_BOOL, offsetof(fileio, finalizing), 0},
1217 {NULL}
1218};
1219
Guido van Rossuma9e20242007-03-08 00:43:48 +00001220PyTypeObject PyFileIO_Type = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001221 PyVarObject_HEAD_INIT(NULL, 0)
1222 "_io.FileIO",
1223 sizeof(fileio),
1224 0,
1225 (destructor)fileio_dealloc, /* tp_dealloc */
1226 0, /* tp_print */
1227 0, /* tp_getattr */
1228 0, /* tp_setattr */
1229 0, /* tp_reserved */
1230 (reprfunc)fileio_repr, /* tp_repr */
1231 0, /* tp_as_number */
1232 0, /* tp_as_sequence */
1233 0, /* tp_as_mapping */
1234 0, /* tp_hash */
1235 0, /* tp_call */
1236 0, /* tp_str */
1237 PyObject_GenericGetAttr, /* tp_getattro */
1238 0, /* tp_setattro */
1239 0, /* tp_as_buffer */
1240 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +02001241 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001242 fileio_doc, /* tp_doc */
1243 (traverseproc)fileio_traverse, /* tp_traverse */
1244 (inquiry)fileio_clear, /* tp_clear */
1245 0, /* tp_richcompare */
1246 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1247 0, /* tp_iter */
1248 0, /* tp_iternext */
1249 fileio_methods, /* tp_methods */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001250 fileio_members, /* tp_members */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001251 fileio_getsetlist, /* tp_getset */
1252 0, /* tp_base */
1253 0, /* tp_dict */
1254 0, /* tp_descr_get */
1255 0, /* tp_descr_set */
1256 offsetof(fileio, dict), /* tp_dictoffset */
1257 fileio_init, /* tp_init */
1258 PyType_GenericAlloc, /* tp_alloc */
1259 fileio_new, /* tp_new */
1260 PyObject_GC_Del, /* tp_free */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001261 0, /* tp_is_gc */
1262 0, /* tp_bases */
1263 0, /* tp_mro */
1264 0, /* tp_cache */
1265 0, /* tp_subclasses */
1266 0, /* tp_weaklist */
1267 0, /* tp_del */
1268 0, /* tp_version_tag */
1269 0, /* tp_finalize */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001270};