blob: b35a51b4e1afece34f1f93df85c788281bc4308e [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 Pitroude687222014-06-29 20:07:28 -040056 unsigned int blksize;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000057 PyObject *weakreflist;
58 PyObject *dict;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000059} fileio;
Guido van Rossuma9e20242007-03-08 00:43:48 +000060
Collin Winteraf334382007-03-08 21:46:15 +000061PyTypeObject PyFileIO_Type;
62
Victor Stinnerd9d04192013-11-06 23:50:10 +010063_Py_IDENTIFIER(name);
64
Guido van Rossuma9e20242007-03-08 00:43:48 +000065#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
66
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000067int
68_PyFileIO_closed(PyObject *self)
69{
Antoine Pitrouae4b4722010-05-05 16:31:07 +000070 return ((fileio *)self)->fd < 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000071}
Antoine Pitrou08838b62009-01-21 00:55:13 +000072
Antoine Pitroue033e062010-10-29 10:38:18 +000073/* Because this can call arbitrary code, it shouldn't be called when
74 the refcount is 0 (that is, not directly from tp_dealloc unless
75 the refcount has been temporarily re-incremented). */
76static PyObject *
77fileio_dealloc_warn(fileio *self, PyObject *source)
78{
79 if (self->fd >= 0 && self->closefd) {
80 PyObject *exc, *val, *tb;
81 PyErr_Fetch(&exc, &val, &tb);
82 if (PyErr_WarnFormat(PyExc_ResourceWarning, 1,
83 "unclosed file %R", source)) {
84 /* Spurious errors can appear at shutdown */
85 if (PyErr_ExceptionMatches(PyExc_Warning))
86 PyErr_WriteUnraisable((PyObject *) self);
87 }
88 PyErr_Restore(exc, val, tb);
89 }
90 Py_RETURN_NONE;
91}
92
Antoine Pitrou7fb111b2009-03-04 11:14:01 +000093static PyObject *
94portable_lseek(int fd, PyObject *posobj, int whence);
95
Antoine Pitroua28fcfd2009-03-13 23:42:55 +000096static PyObject *portable_lseek(int fd, PyObject *posobj, int whence);
97
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000098/* Returns 0 on success, -1 with exception set on failure. */
Neal Norwitz88b44da2007-08-12 17:23:54 +000099static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000100internal_close(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000101{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000102 int err = 0;
103 int save_errno = 0;
104 if (self->fd >= 0) {
105 int fd = self->fd;
106 self->fd = -1;
107 /* fd is accessible and someone else may have closed it */
108 if (_PyVerify_fd(fd)) {
109 Py_BEGIN_ALLOW_THREADS
110 err = close(fd);
111 if (err < 0)
112 save_errno = errno;
113 Py_END_ALLOW_THREADS
114 } else {
115 save_errno = errno;
116 err = -1;
117 }
118 }
119 if (err < 0) {
120 errno = save_errno;
121 PyErr_SetFromErrno(PyExc_IOError);
122 return -1;
123 }
124 return 0;
Neal Norwitz88b44da2007-08-12 17:23:54 +0000125}
126
127static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000128fileio_close(fileio *self)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000129{
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200130 PyObject *res;
131 PyObject *exc, *val, *tb;
132 int rc;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200133 _Py_IDENTIFIER(close);
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200134 res = _PyObject_CallMethodId((PyObject*)&PyRawIOBase_Type,
135 &PyId_close, "O", self);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000136 if (!self->closefd) {
137 self->fd = -1;
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200138 return res;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000139 }
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200140 if (res == NULL)
141 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200142 if (self->finalizing) {
Antoine Pitroue033e062010-10-29 10:38:18 +0000143 PyObject *r = fileio_dealloc_warn(self, (PyObject *) self);
144 if (r)
145 Py_DECREF(r);
146 else
147 PyErr_Clear();
148 }
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200149 rc = internal_close(self);
150 if (res == NULL)
151 _PyErr_ChainExceptions(exc, val, tb);
152 if (rc < 0)
153 Py_CLEAR(res);
154 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000155}
156
157static PyObject *
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000158fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000159{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000160 fileio *self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000161
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000162 assert(type != NULL && type->tp_alloc != NULL);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000163
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000164 self = (fileio *) type->tp_alloc(type, 0);
165 if (self != NULL) {
166 self->fd = -1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100167 self->created = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000168 self->readable = 0;
169 self->writable = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200170 self->appending = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000171 self->seekable = -1;
Antoine Pitroude687222014-06-29 20:07:28 -0400172 self->blksize = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000173 self->closefd = 1;
174 self->weakreflist = NULL;
175 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000176
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000177 return (PyObject *) self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000178}
179
Benjamin Peterson806d4022009-01-19 15:11:51 +0000180static int
181check_fd(int fd)
182{
Steve Dowerf2f373f2015-02-21 08:44:05 -0800183 struct _Py_stat_struct buf;
Steve Dower8acde7d2015-03-07 18:14:07 -0800184 if (_Py_fstat(fd, &buf) < 0 &&
185#ifdef MS_WINDOWS
186 GetLastError() == ERROR_INVALID_HANDLE
187#else
188 errno == EBADF
189#endif
190 ) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000191 PyObject *exc;
192 char *msg = strerror(EBADF);
193 exc = PyObject_CallFunction(PyExc_OSError, "(is)",
194 EBADF, msg);
195 PyErr_SetObject(PyExc_OSError, exc);
196 Py_XDECREF(exc);
197 return -1;
198 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000199 return 0;
Benjamin Peterson806d4022009-01-19 15:11:51 +0000200}
201
Victor Stinnerdaf45552013-08-28 00:53:59 +0200202#ifdef O_CLOEXEC
203extern int _Py_open_cloexec_works;
204#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000205
206static int
207fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
208{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000209 fileio *self = (fileio *) oself;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200210 static char *kwlist[] = {"file", "mode", "closefd", "opener", NULL};
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000211 const char *name = NULL;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200212 PyObject *nameobj, *stringobj = NULL, *opener = Py_None;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000213 char *mode = "r";
214 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000215#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000216 Py_UNICODE *widename = NULL;
Thomas Helleraf2be262007-07-12 11:03:13 +0000217#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000218 int ret = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200219 int rwa = 0, plus = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000220 int flags = 0;
221 int fd = -1;
222 int closefd = 1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200223 int fd_is_own = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200224#ifdef O_CLOEXEC
225 int *atomic_flag_works = &_Py_open_cloexec_works;
226#elif !defined(MS_WINDOWS)
227 int *atomic_flag_works = NULL;
228#endif
Steve Dowerf2f373f2015-02-21 08:44:05 -0800229 struct _Py_stat_struct fdfstat;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000230 int async_err = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000231
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000232 assert(PyFileIO_Check(oself));
233 if (self->fd >= 0) {
Hynek Schlawack2cc71562012-05-25 10:05:53 +0200234 if (self->closefd) {
235 /* Have to close the existing file first. */
236 if (internal_close(self) < 0)
237 return -1;
238 }
239 else
240 self->fd = -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000241 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000242
Ross Lagerwall59142db2011-10-31 20:34:46 +0200243 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|siO:fileio",
244 kwlist, &nameobj, &mode, &closefd,
245 &opener))
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000246 return -1;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000247
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000248 if (PyFloat_Check(nameobj)) {
249 PyErr_SetString(PyExc_TypeError,
250 "integer argument expected, got float");
251 return -1;
252 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000253
Serhiy Storchaka78980432013-01-15 01:12:17 +0200254 fd = _PyLong_AsInt(nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000255 if (fd < 0) {
256 if (!PyErr_Occurred()) {
257 PyErr_SetString(PyExc_ValueError,
258 "Negative filedescriptor");
259 return -1;
260 }
261 PyErr_Clear();
262 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000263
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000264#ifdef MS_WINDOWS
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200265 if (PyUnicode_Check(nameobj)) {
Antoine Pitrou13348842012-01-29 18:36:34 +0100266 int rv = _PyUnicode_HasNULChars(nameobj);
267 if (rv) {
268 if (rv != -1)
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300269 PyErr_SetString(PyExc_ValueError, "embedded null character");
Antoine Pitrou13348842012-01-29 18:36:34 +0100270 return -1;
271 }
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200272 widename = PyUnicode_AsUnicode(nameobj);
273 if (widename == NULL)
274 return -1;
275 } else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000276#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000277 if (fd < 0)
278 {
Antoine Pitrou13348842012-01-29 18:36:34 +0100279 if (!PyUnicode_FSConverter(nameobj, &stringobj)) {
280 return -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000281 }
Antoine Pitrou13348842012-01-29 18:36:34 +0100282 name = PyBytes_AS_STRING(stringobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000283 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000284
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000285 s = mode;
286 while (*s) {
287 switch (*s++) {
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100288 case 'x':
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000289 if (rwa) {
290 bad_mode:
291 PyErr_SetString(PyExc_ValueError,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100292 "Must have exactly one of create/read/write/append "
Georg Brandl28928ae2010-10-21 13:45:52 +0000293 "mode and at most one plus");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000294 goto error;
295 }
296 rwa = 1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100297 self->created = 1;
298 self->writable = 1;
299 flags |= O_EXCL | O_CREAT;
300 break;
301 case 'r':
302 if (rwa)
303 goto bad_mode;
304 rwa = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000305 self->readable = 1;
306 break;
307 case 'w':
308 if (rwa)
309 goto bad_mode;
310 rwa = 1;
311 self->writable = 1;
312 flags |= O_CREAT | O_TRUNC;
313 break;
314 case 'a':
315 if (rwa)
316 goto bad_mode;
317 rwa = 1;
318 self->writable = 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200319 self->appending = 1;
320 flags |= O_APPEND | O_CREAT;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000321 break;
322 case 'b':
323 break;
324 case '+':
325 if (plus)
326 goto bad_mode;
327 self->readable = self->writable = 1;
328 plus = 1;
329 break;
330 default:
331 PyErr_Format(PyExc_ValueError,
332 "invalid mode: %.200s", mode);
333 goto error;
334 }
335 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000336
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000337 if (!rwa)
338 goto bad_mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000339
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000340 if (self->readable && self->writable)
341 flags |= O_RDWR;
342 else if (self->readable)
343 flags |= O_RDONLY;
344 else
345 flags |= O_WRONLY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000346
347#ifdef O_BINARY
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000348 flags |= O_BINARY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000349#endif
350
Victor Stinnerdaf45552013-08-28 00:53:59 +0200351#ifdef MS_WINDOWS
352 flags |= O_NOINHERIT;
353#elif defined(O_CLOEXEC)
354 flags |= O_CLOEXEC;
355#endif
Walter Dörwald0e411482007-06-06 16:55:38 +0000356
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000357 if (fd >= 0) {
358 if (check_fd(fd))
359 goto error;
360 self->fd = fd;
361 self->closefd = closefd;
362 }
363 else {
364 self->closefd = 1;
365 if (!closefd) {
366 PyErr_SetString(PyExc_ValueError,
367 "Cannot use closefd=False with file name");
368 goto error;
369 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000370
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000371 errno = 0;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200372 if (opener == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000373 do {
374 Py_BEGIN_ALLOW_THREADS
Thomas Helleraf2be262007-07-12 11:03:13 +0000375#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000376 if (widename != NULL)
377 self->fd = _wopen(widename, flags, 0666);
378 else
Thomas Helleraf2be262007-07-12 11:03:13 +0000379#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000380 self->fd = open(name, flags, 0666);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000381 Py_END_ALLOW_THREADS
382 } while (self->fd < 0 && errno == EINTR &&
383 !(async_err = PyErr_CheckSignals()));
Victor Stinner9672da72015-03-04 18:40:10 +0100384
385 if (async_err)
386 goto error;
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 Stinner9672da72015-03-04 18:40:10 +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 Pitroude687222014-06-29 20:07:28 -0400424
425 self->blksize = DEFAULT_BUFFER_SIZE;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800426 if (_Py_fstat(self->fd, &fdfstat) < 0) {
427 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrou9235b252012-07-06 18:48:24 +0200428 goto error;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800429 }
Antoine Pitroude687222014-06-29 20:07:28 -0400430#if defined(S_ISDIR) && defined(EISDIR)
431 /* On Unix, open will succeed for directories.
432 In Python, there should be no file objects referring to
433 directories, so we need a check. */
434 if (S_ISDIR(fdfstat.st_mode)) {
435 errno = EISDIR;
436 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);
437 goto error;
438 }
439#endif /* defined(S_ISDIR) */
440#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
441 if (fdfstat.st_blksize > 1)
442 self->blksize = fdfstat.st_blksize;
443#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000444
Victor Stinner89e34362011-01-07 18:47:22 +0000445#if defined(MS_WINDOWS) || defined(__CYGWIN__)
446 /* don't translate newlines (\r\n <=> \n) */
447 _setmode(self->fd, O_BINARY);
448#endif
449
Victor Stinnerd9d04192013-11-06 23:50:10 +0100450 if (_PyObject_SetAttrId((PyObject *)self, &PyId_name, nameobj) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000451 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000452
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200453 if (self->appending) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000454 /* For consistent behaviour, we explicitly seek to the
455 end of file (otherwise, it might be done only on the
456 first write()). */
457 PyObject *pos = portable_lseek(self->fd, NULL, 2);
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200458 if (pos == NULL)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000459 goto error;
460 Py_DECREF(pos);
461 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000462
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000463 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000464
465 error:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000466 ret = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200467 if (!fd_is_own)
468 self->fd = -1;
Benjamin Petersonbbb04122010-10-30 23:16:28 +0000469 if (self->fd >= 0)
470 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000471
Guido van Rossuma9e20242007-03-08 00:43:48 +0000472 done:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000473 Py_CLEAR(stringobj);
474 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000475}
476
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000477static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000478fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000479{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000480 Py_VISIT(self->dict);
481 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000482}
483
484static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000485fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000486{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000487 Py_CLEAR(self->dict);
488 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000489}
490
Guido van Rossuma9e20242007-03-08 00:43:48 +0000491static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000492fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000493{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200494 self->finalizing = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000495 if (_PyIOBase_finalize((PyObject *) self) < 0)
496 return;
497 _PyObject_GC_UNTRACK(self);
498 if (self->weakreflist != NULL)
499 PyObject_ClearWeakRefs((PyObject *) self);
500 Py_CLEAR(self->dict);
501 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000502}
503
504static PyObject *
505err_closed(void)
506{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000507 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
508 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000509}
510
511static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000512err_mode(char *action)
513{
Antoine Pitrou712cb732013-12-21 15:51:54 +0100514 _PyIO_State *state = IO_STATE();
515 if (state != NULL)
516 PyErr_Format(state->unsupported_operation,
517 "File not open for %s", action);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000518 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000519}
520
521static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000522fileio_fileno(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000523{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000524 if (self->fd < 0)
525 return err_closed();
526 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000527}
528
529static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000530fileio_readable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000531{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000532 if (self->fd < 0)
533 return err_closed();
534 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000535}
536
537static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000538fileio_writable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000539{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000540 if (self->fd < 0)
541 return err_closed();
542 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000543}
544
545static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000546fileio_seekable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000547{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000548 if (self->fd < 0)
549 return err_closed();
550 if (self->seekable < 0) {
551 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
552 if (pos == NULL) {
553 PyErr_Clear();
554 self->seekable = 0;
555 } else {
556 Py_DECREF(pos);
557 self->seekable = 1;
558 }
559 }
560 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000561}
562
563static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000564fileio_readinto(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000565{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000566 Py_buffer pbuf;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100567 Py_ssize_t n;
568 int err;
Guido van Rossum53807da2007-04-10 19:01:47 +0000569
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000570 if (self->fd < 0)
571 return err_closed();
572 if (!self->readable)
573 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000574
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000575 if (!PyArg_ParseTuple(args, "w*", &pbuf))
576 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000577
Victor Stinner66aab0c2015-03-19 22:53:20 +0100578 n = _Py_read(self->fd, pbuf.buf, pbuf.len);
579 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100580 err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000581 PyBuffer_Release(&pbuf);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100582
583 if (n == -1) {
584 if (err == EAGAIN) {
585 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000586 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100587 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000588 return NULL;
589 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000590
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000591 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000592}
593
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000594static size_t
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100595new_buffersize(fileio *self, size_t currentsize)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000596{
Antoine Pitroua3f44572012-04-17 13:50:58 +0200597 size_t addend;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100598
Nadeem Vawdad41a98b2011-10-13 13:34:16 +0200599 /* Expand the buffer by an amount proportional to the current size,
Antoine Pitroua3f44572012-04-17 13:50:58 +0200600 giving us amortized linear-time behavior. For bigger sizes, use a
601 less-than-double growth factor to avoid excessive allocation. */
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100602 assert(currentsize <= PY_SSIZE_T_MAX);
Antoine Pitroua3f44572012-04-17 13:50:58 +0200603 if (currentsize > 65536)
604 addend = currentsize >> 3;
605 else
606 addend = 256 + currentsize;
607 if (addend < SMALLCHUNK)
608 /* Avoid tiny read() calls. */
609 addend = SMALLCHUNK;
610 return addend + currentsize;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000611}
612
Guido van Rossum7165cb12007-07-10 06:54:34 +0000613static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000614fileio_readall(fileio *self)
Guido van Rossum7165cb12007-07-10 06:54:34 +0000615{
Steve Dowerf2f373f2015-02-21 08:44:05 -0800616 struct _Py_stat_struct st;
Victor Stinnera2a64772011-10-11 22:45:02 +0200617 Py_off_t pos, end;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000618 PyObject *result;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100619 Py_ssize_t bytes_read = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100620 Py_ssize_t n;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100621 size_t bufsize;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000622
Victor Stinnerb79f28c2011-05-25 22:09:03 +0200623 if (self->fd < 0)
624 return err_closed();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000625 if (!_PyVerify_fd(self->fd))
626 return PyErr_SetFromErrno(PyExc_IOError);
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000627
Victor Stinner14b9b112013-06-25 00:37:25 +0200628#ifdef MS_WINDOWS
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200629 pos = _lseeki64(self->fd, 0L, SEEK_CUR);
630#else
631 pos = lseek(self->fd, 0L, SEEK_CUR);
632#endif
Steve Dowerf2f373f2015-02-21 08:44:05 -0800633 if (_Py_fstat(self->fd, &st) == 0)
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200634 end = st.st_size;
635 else
Victor Stinnera2a64772011-10-11 22:45:02 +0200636 end = (Py_off_t)-1;
Christian Heimesa872de52008-12-05 08:26:55 +0000637
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100638 if (end > 0 && end >= pos && pos >= 0 && end - pos < PY_SSIZE_T_MAX) {
639 /* This is probably a real file, so we try to allocate a
640 buffer one byte larger than the rest of the file. If the
641 calculation is right then we should get EOF without having
642 to enlarge the buffer. */
643 bufsize = (size_t)(end - pos + 1);
644 } else {
645 bufsize = SMALLCHUNK;
646 }
647
648 result = PyBytes_FromStringAndSize(NULL, bufsize);
649 if (result == NULL)
650 return NULL;
651
652 while (1) {
653 if (bytes_read >= (Py_ssize_t)bufsize) {
654 bufsize = new_buffersize(self, bytes_read);
655 if (bufsize > PY_SSIZE_T_MAX || bufsize <= 0) {
656 PyErr_SetString(PyExc_OverflowError,
657 "unbounded read returned more bytes "
Victor Stinner3e269392013-05-18 00:38:43 +0200658 "than a Python string can hold");
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100659 Py_DECREF(result);
660 return NULL;
661 }
662
663 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
664 if (_PyBytes_Resize(&result, bufsize) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000665 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000666 }
667 }
Victor Stinner9672da72015-03-04 18:40:10 +0100668
Victor Stinner66aab0c2015-03-19 22:53:20 +0100669 n = _Py_read(self->fd,
670 PyBytes_AS_STRING(result) + bytes_read,
671 bufsize - bytes_read);
Victor Stinner9672da72015-03-04 18:40:10 +0100672
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000673 if (n == 0)
674 break;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100675 if (n == -1) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000676 if (errno == EAGAIN) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100677 PyErr_Clear();
Victor Stinnere10920f2014-07-02 22:59:31 +0200678 if (bytes_read > 0)
679 break;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000680 Py_DECREF(result);
681 Py_RETURN_NONE;
682 }
683 Py_DECREF(result);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000684 return NULL;
685 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100686 bytes_read += n;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200687 pos += n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000688 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000689
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100690 if (PyBytes_GET_SIZE(result) > bytes_read) {
691 if (_PyBytes_Resize(&result, bytes_read) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000692 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000693 }
694 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000695}
696
Guido van Rossuma9e20242007-03-08 00:43:48 +0000697static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000698fileio_read(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000699{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000700 char *ptr;
701 Py_ssize_t n;
702 Py_ssize_t size = -1;
703 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000704
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000705 if (self->fd < 0)
706 return err_closed();
707 if (!self->readable)
708 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000709
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000710 if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size))
711 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000712
Victor Stinner66aab0c2015-03-19 22:53:20 +0100713 if (size < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000714 return fileio_readall(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000715
Victor Stinner14b9b112013-06-25 00:37:25 +0200716#ifdef MS_WINDOWS
Victor Stinner66aab0c2015-03-19 22:53:20 +0100717 /* On Windows, the count parameter of read() is an int */
Victor Stinnerc655a722011-07-05 11:31:49 +0200718 if (size > INT_MAX)
719 size = INT_MAX;
720#endif
Victor Stinner66aab0c2015-03-19 22:53:20 +0100721
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000722 bytes = PyBytes_FromStringAndSize(NULL, size);
723 if (bytes == NULL)
724 return NULL;
725 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000726
Victor Stinner66aab0c2015-03-19 22:53:20 +0100727 n = _Py_read(self->fd, ptr, size);
728 if (n == -1) {
729 /* copy errno because Py_DECREF() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100730 int err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000731 Py_DECREF(bytes);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100732 if (err == EAGAIN) {
733 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000734 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100735 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000736 return NULL;
737 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000738
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000739 if (n != size) {
740 if (_PyBytes_Resize(&bytes, n) < 0) {
Victor Stinner85c761d2013-07-16 21:36:02 +0200741 Py_CLEAR(bytes);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000742 return NULL;
743 }
744 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000745
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000746 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000747}
748
749static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000750fileio_write(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000751{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000752 Py_buffer pbuf;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100753 Py_ssize_t n;
754 int err;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000755
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000756 if (self->fd < 0)
757 return err_closed();
758 if (!self->writable)
759 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000760
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000761 if (!PyArg_ParseTuple(args, "y*", &pbuf))
762 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000763
Victor Stinner66aab0c2015-03-19 22:53:20 +0100764 n = _Py_write(self->fd, pbuf.buf, pbuf.len);
765 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100766 err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000767 PyBuffer_Release(&pbuf);
Martin v. Löwis423be952008-08-13 15:53:07 +0000768
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000769 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100770 if (err == EAGAIN) {
771 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000772 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100773 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000774 return NULL;
775 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000776
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000777 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000778}
779
Guido van Rossum53807da2007-04-10 19:01:47 +0000780/* XXX Windows support below is likely incomplete */
781
Guido van Rossum53807da2007-04-10 19:01:47 +0000782/* Cribbed from posix_lseek() */
783static PyObject *
784portable_lseek(int fd, PyObject *posobj, int whence)
785{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000786 Py_off_t pos, res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000787
788#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000789 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
790 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000791#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000792 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000793#endif
794#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000795 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000796#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000797#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000798 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000799#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000800 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000801#endif /* SEEK_SET */
802
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000803 if (posobj == NULL)
804 pos = 0;
805 else {
806 if(PyFloat_Check(posobj)) {
807 PyErr_SetString(PyExc_TypeError, "an integer is required");
808 return NULL;
809 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000810#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000811 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000812#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000813 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000814#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000815 if (PyErr_Occurred())
816 return NULL;
817 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000818
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000819 if (_PyVerify_fd(fd)) {
820 Py_BEGIN_ALLOW_THREADS
Victor Stinner14b9b112013-06-25 00:37:25 +0200821#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000822 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000823#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000824 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000825#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000826 Py_END_ALLOW_THREADS
827 } else
828 res = -1;
829 if (res < 0)
830 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000831
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000832#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000833 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000834#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000835 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000836#endif
837}
838
Guido van Rossuma9e20242007-03-08 00:43:48 +0000839static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000840fileio_seek(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000841{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000842 PyObject *posobj;
843 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000844
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000845 if (self->fd < 0)
846 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000847
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000848 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
849 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000850
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000851 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000852}
853
854static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000855fileio_tell(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000856{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000857 if (self->fd < 0)
858 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000859
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000860 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000861}
862
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000863#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000864static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000865fileio_truncate(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000866{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000867 PyObject *posobj = NULL; /* the new size wanted by the user */
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000868#ifndef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000869 Py_off_t pos;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000870#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000871 int ret;
872 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000873
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000874 fd = self->fd;
875 if (fd < 0)
876 return err_closed();
877 if (!self->writable)
878 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000879
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000880 if (!PyArg_ParseTuple(args, "|O", &posobj))
881 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000882
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000883 if (posobj == Py_None || posobj == NULL) {
884 /* Get the current position. */
885 posobj = portable_lseek(fd, NULL, 1);
886 if (posobj == NULL)
887 return NULL;
888 }
889 else {
890 Py_INCREF(posobj);
891 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000892
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000893#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000894 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
895 so don't even try using it. */
896 {
897 PyObject *oldposobj, *tempposobj;
898 HANDLE hFile;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000899
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000900 /* we save the file pointer position */
901 oldposobj = portable_lseek(fd, NULL, 1);
902 if (oldposobj == NULL) {
903 Py_DECREF(posobj);
904 return NULL;
905 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000906
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000907 /* we then move to the truncation position */
908 tempposobj = portable_lseek(fd, posobj, 0);
909 if (tempposobj == NULL) {
910 Py_DECREF(oldposobj);
911 Py_DECREF(posobj);
912 return NULL;
913 }
914 Py_DECREF(tempposobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000915
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000916 /* Truncate. Note that this may grow the file! */
917 Py_BEGIN_ALLOW_THREADS
918 errno = 0;
919 hFile = (HANDLE)_get_osfhandle(fd);
920 ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
921 if (ret == 0) {
922 ret = SetEndOfFile(hFile) == 0;
923 if (ret)
924 errno = EACCES;
925 }
926 Py_END_ALLOW_THREADS
927
928 /* we restore the file pointer position in any case */
929 tempposobj = portable_lseek(fd, oldposobj, 0);
930 Py_DECREF(oldposobj);
931 if (tempposobj == NULL) {
932 Py_DECREF(posobj);
933 return NULL;
934 }
935 Py_DECREF(tempposobj);
936 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000937#else
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000938
939#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000940 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000941#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000942 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000943#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000944 if (PyErr_Occurred()){
945 Py_DECREF(posobj);
946 return NULL;
947 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000948
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000949 Py_BEGIN_ALLOW_THREADS
950 errno = 0;
951 ret = ftruncate(fd, pos);
952 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000953
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000954#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000955
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000956 if (ret != 0) {
957 Py_DECREF(posobj);
958 PyErr_SetFromErrno(PyExc_IOError);
959 return NULL;
960 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000961
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000962 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000963}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000964#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +0000965
966static char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000967mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +0000968{
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100969 if (self->created) {
970 if (self->readable)
971 return "xb+";
972 else
973 return "xb";
974 }
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200975 if (self->appending) {
976 if (self->readable)
977 return "ab+";
978 else
979 return "ab";
980 }
981 else if (self->readable) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000982 if (self->writable)
983 return "rb+";
984 else
985 return "rb";
986 }
987 else
988 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000989}
Guido van Rossuma9e20242007-03-08 00:43:48 +0000990
991static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000992fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000993{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000994 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000995
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000996 if (self->fd < 0)
997 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +0000998
Martin v. Löwis767046a2011-10-14 15:35:36 +0200999 nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001000 if (nameobj == NULL) {
1001 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1002 PyErr_Clear();
1003 else
1004 return NULL;
Robert Collins933430a2014-10-18 13:32:43 +13001005 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +02001006 "<_io.FileIO fd=%d mode='%s' closefd=%s>",
1007 self->fd, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001008 }
1009 else {
Robert Collins933430a2014-10-18 13:32:43 +13001010 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +02001011 "<_io.FileIO name=%R mode='%s' closefd=%s>",
1012 nameobj, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001013 Py_DECREF(nameobj);
1014 }
1015 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001016}
1017
1018static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001019fileio_isatty(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001020{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001021 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +00001022
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001023 if (self->fd < 0)
1024 return err_closed();
1025 Py_BEGIN_ALLOW_THREADS
1026 res = isatty(self->fd);
1027 Py_END_ALLOW_THREADS
1028 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001029}
1030
Antoine Pitrou243757e2010-11-05 21:15:39 +00001031static PyObject *
1032fileio_getstate(fileio *self)
1033{
1034 PyErr_Format(PyExc_TypeError,
1035 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
1036 return NULL;
1037}
1038
Guido van Rossuma9e20242007-03-08 00:43:48 +00001039
1040PyDoc_STRVAR(fileio_doc,
Ross Lagerwall59142db2011-10-31 20:34:46 +02001041"file(name: str[, mode: str][, opener: None]) -> file IO object\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001042"\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001043"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 +01001044"writing, exclusive creation or appending. The file will be created if it\n"
1045"doesn't exist when opened for writing or appending; it will be truncated\n"
1046"when opened for writing. A `FileExistsError` will be raised if it already\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001047"exists when opened for creating. Opening a file for creating implies\n"
1048"writing so this mode behaves in a similar way to 'w'.Add a '+' to the mode\n"
1049"to allow simultaneous reading and writing. A custom opener can be used by\n"
1050"passing a callable as *opener*. The underlying file descriptor for the file\n"
Ross Lagerwall59142db2011-10-31 20:34:46 +02001051"object is then obtained by calling opener with (*name*, *flags*).\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001052"*opener* must return an open file descriptor (passing os.open as *opener*\n"
1053"results in functionality similar to passing None).");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001054
1055PyDoc_STRVAR(read_doc,
1056"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
1057"\n"
1058"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +00001059"In non-blocking mode, returns None if no data is available.\n"
1060"On end-of-file, returns ''.");
1061
1062PyDoc_STRVAR(readall_doc,
1063"readall() -> bytes. read all data from the file, returned as bytes.\n"
1064"\n"
1065"In non-blocking mode, returns as much as is immediately available,\n"
1066"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001067
1068PyDoc_STRVAR(write_doc,
1069"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
1070"\n"
1071"Only makes one system call, so not all of the data may be written.\n"
1072"The number of bytes actually written is returned.");
1073
1074PyDoc_STRVAR(fileno_doc,
1075"fileno() -> int. \"file descriptor\".\n"
1076"\n"
1077"This is needed for lower-level file interfaces, such the fcntl module.");
1078
1079PyDoc_STRVAR(seek_doc,
Berker Peksagb87630c2014-09-24 12:43:29 +03001080"seek(offset: int[, whence: int]) -> int. Move to new file position and\n"
1081"return the file position.\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001082"\n"
1083"Argument offset is a byte count. Optional argument whence defaults to\n"
1084"0 (offset from start of file, offset should be >= 0); other values are 1\n"
1085"(move relative to current position, positive or negative), and 2 (move\n"
1086"relative to end of file, usually negative, although many platforms allow\n"
1087"seeking beyond the end of a file)."
1088"\n"
1089"Note that not all file objects are seekable.");
1090
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001091#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +00001092PyDoc_STRVAR(truncate_doc,
Berker Peksagb87630c2014-09-24 12:43:29 +03001093"truncate([size: int]) -> int. Truncate the file to at most size bytes\n"
1094"and return the truncated size.\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001095"\n"
Berker Peksagb87630c2014-09-24 12:43:29 +03001096"Size defaults to the current file position, as returned by tell().\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +00001097"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001098#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +00001099
1100PyDoc_STRVAR(tell_doc,
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001101"tell() -> int. Current file position");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001102
1103PyDoc_STRVAR(readinto_doc,
Benjamin Peterson9a8082f2009-03-05 00:55:56 +00001104"readinto() -> Same as RawIOBase.readinto().");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001105
1106PyDoc_STRVAR(close_doc,
1107"close() -> None. Close the file.\n"
1108"\n"
1109"A closed file cannot be used for further I/O operations. close() may be\n"
1110"called more than once without error. Changes the fileno to -1.");
1111
1112PyDoc_STRVAR(isatty_doc,
1113"isatty() -> bool. True if the file is connected to a tty device.");
1114
Guido van Rossuma9e20242007-03-08 00:43:48 +00001115PyDoc_STRVAR(seekable_doc,
1116"seekable() -> bool. True if file supports random-access.");
1117
1118PyDoc_STRVAR(readable_doc,
1119"readable() -> bool. True if file was opened in a read mode.");
1120
1121PyDoc_STRVAR(writable_doc,
1122"writable() -> bool. True if file was opened in a write mode.");
1123
1124static PyMethodDef fileio_methods[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001125 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
1126 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
1127 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
1128 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
1129 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
1130 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001131#ifdef HAVE_FTRUNCATE
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001132 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001133#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001134 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
1135 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
1136 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
1137 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
1138 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
1139 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Antoine Pitroue033e062010-10-29 10:38:18 +00001140 {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
Antoine Pitrou243757e2010-11-05 21:15:39 +00001141 {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001142 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001143};
1144
Guido van Rossum53807da2007-04-10 19:01:47 +00001145/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1146
Guido van Rossumb0428152007-04-08 17:44:42 +00001147static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001148get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001149{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001150 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001151}
1152
1153static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001154get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001155{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001156 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001157}
1158
1159static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001160get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001161{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001162 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001163}
1164
1165static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001166 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1167 {"closefd", (getter)get_closefd, NULL,
1168 "True if the file descriptor will be closed"},
1169 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1170 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001171};
1172
Antoine Pitrou796564c2013-07-30 19:59:21 +02001173static PyMemberDef fileio_members[] = {
Antoine Pitroude687222014-06-29 20:07:28 -04001174 {"_blksize", T_UINT, offsetof(fileio, blksize), 0},
Antoine Pitrou796564c2013-07-30 19:59:21 +02001175 {"_finalizing", T_BOOL, offsetof(fileio, finalizing), 0},
1176 {NULL}
1177};
1178
Guido van Rossuma9e20242007-03-08 00:43:48 +00001179PyTypeObject PyFileIO_Type = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001180 PyVarObject_HEAD_INIT(NULL, 0)
1181 "_io.FileIO",
1182 sizeof(fileio),
1183 0,
1184 (destructor)fileio_dealloc, /* tp_dealloc */
1185 0, /* tp_print */
1186 0, /* tp_getattr */
1187 0, /* tp_setattr */
1188 0, /* tp_reserved */
1189 (reprfunc)fileio_repr, /* tp_repr */
1190 0, /* tp_as_number */
1191 0, /* tp_as_sequence */
1192 0, /* tp_as_mapping */
1193 0, /* tp_hash */
1194 0, /* tp_call */
1195 0, /* tp_str */
1196 PyObject_GenericGetAttr, /* tp_getattro */
1197 0, /* tp_setattro */
1198 0, /* tp_as_buffer */
1199 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +02001200 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001201 fileio_doc, /* tp_doc */
1202 (traverseproc)fileio_traverse, /* tp_traverse */
1203 (inquiry)fileio_clear, /* tp_clear */
1204 0, /* tp_richcompare */
1205 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1206 0, /* tp_iter */
1207 0, /* tp_iternext */
1208 fileio_methods, /* tp_methods */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001209 fileio_members, /* tp_members */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001210 fileio_getsetlist, /* tp_getset */
1211 0, /* tp_base */
1212 0, /* tp_dict */
1213 0, /* tp_descr_get */
1214 0, /* tp_descr_set */
1215 offsetof(fileio, dict), /* tp_dictoffset */
1216 fileio_init, /* tp_init */
1217 PyType_GenericAlloc, /* tp_alloc */
1218 fileio_new, /* tp_new */
1219 PyObject_GC_Del, /* tp_free */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001220 0, /* tp_is_gc */
1221 0, /* tp_bases */
1222 0, /* tp_mro */
1223 0, /* tp_cache */
1224 0, /* tp_subclasses */
1225 0, /* tp_weaklist */
1226 0, /* tp_del */
1227 0, /* tp_version_tag */
1228 0, /* tp_finalize */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001229};