blob: 12e5156fbb7263c5cc3c94de21eb76dfd643360e [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
Serhiy Storchakaf24131f2015-04-16 11:19:43 +030046/*[clinic input]
47module _io
48class _io.FileIO "fileio *" "&PyFileIO_Type"
49[clinic start generated code]*/
50/*[clinic end generated code: output=da39a3ee5e6b4b0d input=1c77708b41fda70c]*/
51
52/*[python input]
53class io_ssize_t_converter(CConverter):
54 type = 'Py_ssize_t'
55 converter = '_PyIO_ConvertSsize_t'
56[python start generated code]*/
57/*[python end generated code: output=da39a3ee5e6b4b0d input=d0a811d3cbfd1b33]*/
58
Guido van Rossuma9e20242007-03-08 00:43:48 +000059typedef struct {
Antoine Pitrouae4b4722010-05-05 16:31:07 +000060 PyObject_HEAD
61 int fd;
Charles-François Natalidc3044c2012-01-09 22:40:02 +010062 unsigned int created : 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000063 unsigned int readable : 1;
64 unsigned int writable : 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +020065 unsigned int appending : 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000066 signed int seekable : 2; /* -1 means unknown */
67 unsigned int closefd : 1;
Antoine Pitrou796564c2013-07-30 19:59:21 +020068 char finalizing;
Antoine Pitroude687222014-06-29 20:07:28 -040069 unsigned int blksize;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000070 PyObject *weakreflist;
71 PyObject *dict;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000072} fileio;
Guido van Rossuma9e20242007-03-08 00:43:48 +000073
Collin Winteraf334382007-03-08 21:46:15 +000074PyTypeObject PyFileIO_Type;
75
Victor Stinnerd9d04192013-11-06 23:50:10 +010076_Py_IDENTIFIER(name);
77
Guido van Rossuma9e20242007-03-08 00:43:48 +000078#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
79
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000080int
81_PyFileIO_closed(PyObject *self)
82{
Antoine Pitrouae4b4722010-05-05 16:31:07 +000083 return ((fileio *)self)->fd < 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000084}
Antoine Pitrou08838b62009-01-21 00:55:13 +000085
Antoine Pitroue033e062010-10-29 10:38:18 +000086/* Because this can call arbitrary code, it shouldn't be called when
87 the refcount is 0 (that is, not directly from tp_dealloc unless
88 the refcount has been temporarily re-incremented). */
89static PyObject *
90fileio_dealloc_warn(fileio *self, PyObject *source)
91{
92 if (self->fd >= 0 && self->closefd) {
93 PyObject *exc, *val, *tb;
94 PyErr_Fetch(&exc, &val, &tb);
Victor Stinner914cde82016-03-19 01:03:51 +010095 if (PyErr_ResourceWarning(source, 1, "unclosed file %R", source)) {
Antoine Pitroue033e062010-10-29 10:38:18 +000096 /* Spurious errors can appear at shutdown */
97 if (PyErr_ExceptionMatches(PyExc_Warning))
98 PyErr_WriteUnraisable((PyObject *) self);
99 }
100 PyErr_Restore(exc, val, tb);
101 }
102 Py_RETURN_NONE;
103}
104
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000105static PyObject *
106portable_lseek(int fd, PyObject *posobj, int whence);
107
Antoine Pitroua28fcfd2009-03-13 23:42:55 +0000108static PyObject *portable_lseek(int fd, PyObject *posobj, int whence);
109
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +0000110/* Returns 0 on success, -1 with exception set on failure. */
Neal Norwitz88b44da2007-08-12 17:23:54 +0000111static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000112internal_close(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000113{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000114 int err = 0;
115 int save_errno = 0;
116 if (self->fd >= 0) {
117 int fd = self->fd;
118 self->fd = -1;
119 /* fd is accessible and someone else may have closed it */
120 if (_PyVerify_fd(fd)) {
121 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -0400122 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000123 err = close(fd);
124 if (err < 0)
125 save_errno = errno;
Steve Dower8fc89802015-04-12 00:26:27 -0400126 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000127 Py_END_ALLOW_THREADS
128 } else {
129 save_errno = errno;
130 err = -1;
131 }
132 }
133 if (err < 0) {
134 errno = save_errno;
135 PyErr_SetFromErrno(PyExc_IOError);
136 return -1;
137 }
138 return 0;
Neal Norwitz88b44da2007-08-12 17:23:54 +0000139}
140
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300141/*[clinic input]
142_io.FileIO.close
143
144Close the file.
145
146A closed file cannot be used for further I/O operations. close() may be
147called more than once without error.
148[clinic start generated code]*/
149
Neal Norwitz88b44da2007-08-12 17:23:54 +0000150static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300151_io_FileIO_close_impl(fileio *self)
152/*[clinic end generated code: output=7737a319ef3bad0b input=f35231760d54a522]*/
Neal Norwitz88b44da2007-08-12 17:23:54 +0000153{
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200154 PyObject *res;
155 PyObject *exc, *val, *tb;
156 int rc;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200157 _Py_IDENTIFIER(close);
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200158 res = _PyObject_CallMethodId((PyObject*)&PyRawIOBase_Type,
159 &PyId_close, "O", self);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000160 if (!self->closefd) {
161 self->fd = -1;
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200162 return res;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000163 }
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200164 if (res == NULL)
165 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200166 if (self->finalizing) {
Antoine Pitroue033e062010-10-29 10:38:18 +0000167 PyObject *r = fileio_dealloc_warn(self, (PyObject *) self);
168 if (r)
169 Py_DECREF(r);
170 else
171 PyErr_Clear();
172 }
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200173 rc = internal_close(self);
174 if (res == NULL)
175 _PyErr_ChainExceptions(exc, val, tb);
176 if (rc < 0)
177 Py_CLEAR(res);
178 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000179}
180
181static PyObject *
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000182fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000183{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000184 fileio *self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000185
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000186 assert(type != NULL && type->tp_alloc != NULL);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000187
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000188 self = (fileio *) type->tp_alloc(type, 0);
189 if (self != NULL) {
190 self->fd = -1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100191 self->created = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000192 self->readable = 0;
193 self->writable = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200194 self->appending = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000195 self->seekable = -1;
Antoine Pitroude687222014-06-29 20:07:28 -0400196 self->blksize = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000197 self->closefd = 1;
198 self->weakreflist = NULL;
199 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000200
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000201 return (PyObject *) self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000202}
203
Victor Stinnerdaf45552013-08-28 00:53:59 +0200204#ifdef O_CLOEXEC
205extern int _Py_open_cloexec_works;
206#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000207
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300208/*[clinic input]
209_io.FileIO.__init__
210 file as nameobj: object
211 mode: str = "r"
212 closefd: int(c_default="1") = True
213 opener: object = None
214
215Open a file.
216
217The mode can be 'r' (default), 'w', 'x' or 'a' for reading,
218writing, exclusive creation or appending. The file will be created if it
219doesn't exist when opened for writing or appending; it will be truncated
220when opened for writing. A FileExistsError will be raised if it already
221exists when opened for creating. Opening a file for creating implies
222writing so this mode behaves in a similar way to 'w'.Add a '+' to the mode
223to allow simultaneous reading and writing. A custom opener can be used by
224passing a callable as *opener*. The underlying file descriptor for the file
225object is then obtained by calling opener with (*name*, *flags*).
226*opener* must return an open file descriptor (passing os.open as *opener*
227results in functionality similar to passing None).
228[clinic start generated code]*/
229
Guido van Rossuma9e20242007-03-08 00:43:48 +0000230static int
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300231_io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
232 int closefd, PyObject *opener)
233/*[clinic end generated code: output=23413f68e6484bbd input=193164e293d6c097]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000234{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000235 const char *name = NULL;
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300236 PyObject *stringobj = NULL;
237 const char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000238#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000239 Py_UNICODE *widename = NULL;
Thomas Helleraf2be262007-07-12 11:03:13 +0000240#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000241 int ret = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200242 int rwa = 0, plus = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000243 int flags = 0;
244 int fd = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200245 int fd_is_own = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200246#ifdef O_CLOEXEC
247 int *atomic_flag_works = &_Py_open_cloexec_works;
248#elif !defined(MS_WINDOWS)
249 int *atomic_flag_works = NULL;
250#endif
Steve Dowerf2f373f2015-02-21 08:44:05 -0800251 struct _Py_stat_struct fdfstat;
Martin Panter0bb62b12015-12-06 03:15:05 +0000252 int fstat_result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000253 int async_err = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000254
Christian Heimes82adeff2015-04-16 17:21:54 +0200255 assert(PyFileIO_Check(self));
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000256 if (self->fd >= 0) {
Hynek Schlawack2cc71562012-05-25 10:05:53 +0200257 if (self->closefd) {
258 /* Have to close the existing file first. */
259 if (internal_close(self) < 0)
260 return -1;
261 }
262 else
263 self->fd = -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000264 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000265
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000266 if (PyFloat_Check(nameobj)) {
267 PyErr_SetString(PyExc_TypeError,
268 "integer argument expected, got float");
269 return -1;
270 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000271
Serhiy Storchaka78980432013-01-15 01:12:17 +0200272 fd = _PyLong_AsInt(nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000273 if (fd < 0) {
274 if (!PyErr_Occurred()) {
275 PyErr_SetString(PyExc_ValueError,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +0300276 "negative file descriptor");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000277 return -1;
278 }
279 PyErr_Clear();
280 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000281
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000282#ifdef MS_WINDOWS
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200283 if (PyUnicode_Check(nameobj)) {
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300284 Py_ssize_t length;
285 widename = PyUnicode_AsUnicodeAndSize(nameobj, &length);
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200286 if (widename == NULL)
287 return -1;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300288 if (wcslen(widename) != length) {
Serhiy Storchaka7e9d1d12015-04-20 10:12:28 +0300289 PyErr_SetString(PyExc_ValueError, "embedded null character");
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300290 return -1;
291 }
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200292 } else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000293#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000294 if (fd < 0)
295 {
Antoine Pitrou13348842012-01-29 18:36:34 +0100296 if (!PyUnicode_FSConverter(nameobj, &stringobj)) {
297 return -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000298 }
Antoine Pitrou13348842012-01-29 18:36:34 +0100299 name = PyBytes_AS_STRING(stringobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000300 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000301
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000302 s = mode;
303 while (*s) {
304 switch (*s++) {
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100305 case 'x':
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000306 if (rwa) {
307 bad_mode:
308 PyErr_SetString(PyExc_ValueError,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100309 "Must have exactly one of create/read/write/append "
Georg Brandl28928ae2010-10-21 13:45:52 +0000310 "mode and at most one plus");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000311 goto error;
312 }
313 rwa = 1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100314 self->created = 1;
315 self->writable = 1;
316 flags |= O_EXCL | O_CREAT;
317 break;
318 case 'r':
319 if (rwa)
320 goto bad_mode;
321 rwa = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000322 self->readable = 1;
323 break;
324 case 'w':
325 if (rwa)
326 goto bad_mode;
327 rwa = 1;
328 self->writable = 1;
329 flags |= O_CREAT | O_TRUNC;
330 break;
331 case 'a':
332 if (rwa)
333 goto bad_mode;
334 rwa = 1;
335 self->writable = 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200336 self->appending = 1;
337 flags |= O_APPEND | O_CREAT;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000338 break;
339 case 'b':
340 break;
341 case '+':
342 if (plus)
343 goto bad_mode;
344 self->readable = self->writable = 1;
345 plus = 1;
346 break;
347 default:
348 PyErr_Format(PyExc_ValueError,
349 "invalid mode: %.200s", mode);
350 goto error;
351 }
352 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000353
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000354 if (!rwa)
355 goto bad_mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000356
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000357 if (self->readable && self->writable)
358 flags |= O_RDWR;
359 else if (self->readable)
360 flags |= O_RDONLY;
361 else
362 flags |= O_WRONLY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000363
364#ifdef O_BINARY
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000365 flags |= O_BINARY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000366#endif
367
Victor Stinnerdaf45552013-08-28 00:53:59 +0200368#ifdef MS_WINDOWS
369 flags |= O_NOINHERIT;
370#elif defined(O_CLOEXEC)
371 flags |= O_CLOEXEC;
372#endif
Walter Dörwald0e411482007-06-06 16:55:38 +0000373
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000374 if (fd >= 0) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000375 self->fd = fd;
376 self->closefd = closefd;
377 }
378 else {
379 self->closefd = 1;
380 if (!closefd) {
381 PyErr_SetString(PyExc_ValueError,
382 "Cannot use closefd=False with file name");
383 goto error;
384 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000385
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000386 errno = 0;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200387 if (opener == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000388 do {
389 Py_BEGIN_ALLOW_THREADS
Thomas Helleraf2be262007-07-12 11:03:13 +0000390#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000391 if (widename != NULL)
392 self->fd = _wopen(widename, flags, 0666);
393 else
Thomas Helleraf2be262007-07-12 11:03:13 +0000394#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000395 self->fd = open(name, flags, 0666);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000396 Py_END_ALLOW_THREADS
397 } while (self->fd < 0 && errno == EINTR &&
398 !(async_err = PyErr_CheckSignals()));
Victor Stinner9672da72015-03-04 18:40:10 +0100399
400 if (async_err)
401 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200402 }
403 else {
404 PyObject *fdobj;
405
406#ifndef MS_WINDOWS
407 /* the opener may clear the atomic flag */
408 atomic_flag_works = NULL;
409#endif
410
411 fdobj = PyObject_CallFunction(opener, "Oi", nameobj, flags);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200412 if (fdobj == NULL)
413 goto error;
414 if (!PyLong_Check(fdobj)) {
415 Py_DECREF(fdobj);
416 PyErr_SetString(PyExc_TypeError,
417 "expected integer from opener");
418 goto error;
419 }
420
Serhiy Storchaka78980432013-01-15 01:12:17 +0200421 self->fd = _PyLong_AsInt(fdobj);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200422 Py_DECREF(fdobj);
Barry Warsaw480e2852016-06-08 17:47:26 -0400423 if (self->fd < 0) {
424 if (!PyErr_Occurred()) {
Barry Warsaw118598a2016-06-08 17:54:43 -0400425 /* The opener returned a negative but didn't set an
426 exception. See issue #27066 */
Barry Warsaw480e2852016-06-08 17:47:26 -0400427 PyErr_Format(PyExc_ValueError,
428 "opener returned %d", self->fd);
429 }
Ross Lagerwall59142db2011-10-31 20:34:46 +0200430 goto error;
431 }
432 }
433
Hynek Schlawack7f59fd72012-06-22 09:32:22 +0200434 fd_is_own = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000435 if (self->fd < 0) {
Victor Stinner9672da72015-03-04 18:40:10 +0100436 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000437 goto error;
438 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200439
440#ifndef MS_WINDOWS
441 if (_Py_set_inheritable(self->fd, 0, atomic_flag_works) < 0)
442 goto error;
443#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000444 }
Antoine Pitroude687222014-06-29 20:07:28 -0400445
446 self->blksize = DEFAULT_BUFFER_SIZE;
Martin Panter0bb62b12015-12-06 03:15:05 +0000447 Py_BEGIN_ALLOW_THREADS
448 fstat_result = _Py_fstat_noraise(self->fd, &fdfstat);
449 Py_END_ALLOW_THREADS
450 if (fstat_result < 0) {
Martin Panter49d3db92015-12-06 11:12:15 +0000451 /* Tolerate fstat() errors other than EBADF. See Issue #25717, where
452 an anonymous file on a Virtual Box shared folder filesystem would
453 raise ENOENT. */
Martin Panter0bb62b12015-12-06 03:15:05 +0000454#ifdef MS_WINDOWS
455 if (GetLastError() == ERROR_INVALID_HANDLE) {
456 PyErr_SetFromWindowsErr(0);
457#else
458 if (errno == EBADF) {
459 PyErr_SetFromErrno(PyExc_OSError);
460#endif
461 goto error;
462 }
Antoine Pitroude687222014-06-29 20:07:28 -0400463 }
Martin Panter0bb62b12015-12-06 03:15:05 +0000464 else {
465#if defined(S_ISDIR) && defined(EISDIR)
466 /* On Unix, open will succeed for directories.
467 In Python, there should be no file objects referring to
468 directories, so we need a check. */
469 if (S_ISDIR(fdfstat.st_mode)) {
470 errno = EISDIR;
471 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);
472 goto error;
473 }
Antoine Pitroude687222014-06-29 20:07:28 -0400474#endif /* defined(S_ISDIR) */
475#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin Panter0bb62b12015-12-06 03:15:05 +0000476 if (fdfstat.st_blksize > 1)
477 self->blksize = fdfstat.st_blksize;
Antoine Pitroude687222014-06-29 20:07:28 -0400478#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Martin Panter0bb62b12015-12-06 03:15:05 +0000479 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000480
Victor Stinner89e34362011-01-07 18:47:22 +0000481#if defined(MS_WINDOWS) || defined(__CYGWIN__)
482 /* don't translate newlines (\r\n <=> \n) */
483 _setmode(self->fd, O_BINARY);
484#endif
485
Victor Stinnerd9d04192013-11-06 23:50:10 +0100486 if (_PyObject_SetAttrId((PyObject *)self, &PyId_name, nameobj) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000487 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000488
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200489 if (self->appending) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000490 /* For consistent behaviour, we explicitly seek to the
491 end of file (otherwise, it might be done only on the
492 first write()). */
493 PyObject *pos = portable_lseek(self->fd, NULL, 2);
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200494 if (pos == NULL)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000495 goto error;
496 Py_DECREF(pos);
497 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000498
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000499 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000500
501 error:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000502 ret = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200503 if (!fd_is_own)
504 self->fd = -1;
Benjamin Petersonbbb04122010-10-30 23:16:28 +0000505 if (self->fd >= 0)
506 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000507
Guido van Rossuma9e20242007-03-08 00:43:48 +0000508 done:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000509 Py_CLEAR(stringobj);
510 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000511}
512
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000513static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000514fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000515{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000516 Py_VISIT(self->dict);
517 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000518}
519
520static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000521fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000522{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000523 Py_CLEAR(self->dict);
524 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000525}
526
Guido van Rossuma9e20242007-03-08 00:43:48 +0000527static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000528fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000529{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200530 self->finalizing = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000531 if (_PyIOBase_finalize((PyObject *) self) < 0)
532 return;
533 _PyObject_GC_UNTRACK(self);
534 if (self->weakreflist != NULL)
535 PyObject_ClearWeakRefs((PyObject *) self);
536 Py_CLEAR(self->dict);
537 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000538}
539
540static PyObject *
541err_closed(void)
542{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000543 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
544 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000545}
546
547static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200548err_mode(const char *action)
Guido van Rossum53807da2007-04-10 19:01:47 +0000549{
Antoine Pitrou712cb732013-12-21 15:51:54 +0100550 _PyIO_State *state = IO_STATE();
551 if (state != NULL)
552 PyErr_Format(state->unsupported_operation,
553 "File not open for %s", action);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000554 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000555}
556
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300557/*[clinic input]
558_io.FileIO.fileno
559
560Return the underlying file descriptor (an integer).
561[clinic start generated code]*/
562
Guido van Rossum53807da2007-04-10 19:01:47 +0000563static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300564_io_FileIO_fileno_impl(fileio *self)
565/*[clinic end generated code: output=a9626ce5398ece90 input=0b9b2de67335ada3]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000566{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000567 if (self->fd < 0)
568 return err_closed();
569 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000570}
571
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300572/*[clinic input]
573_io.FileIO.readable
574
575True if file was opened in a read mode.
576[clinic start generated code]*/
577
Guido van Rossuma9e20242007-03-08 00:43:48 +0000578static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300579_io_FileIO_readable_impl(fileio *self)
580/*[clinic end generated code: output=640744a6150fe9ba input=a3fdfed6eea721c5]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000581{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000582 if (self->fd < 0)
583 return err_closed();
584 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000585}
586
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300587/*[clinic input]
588_io.FileIO.writable
589
590True if file was opened in a write mode.
591[clinic start generated code]*/
592
Guido van Rossuma9e20242007-03-08 00:43:48 +0000593static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300594_io_FileIO_writable_impl(fileio *self)
595/*[clinic end generated code: output=96cefc5446e89977 input=c204a808ca2e1748]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000596{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000597 if (self->fd < 0)
598 return err_closed();
599 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000600}
601
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300602/*[clinic input]
603_io.FileIO.seekable
604
605True if file supports random-access.
606[clinic start generated code]*/
607
Guido van Rossuma9e20242007-03-08 00:43:48 +0000608static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300609_io_FileIO_seekable_impl(fileio *self)
610/*[clinic end generated code: output=47909ca0a42e9287 input=c8e5554d2fd63c7f]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000611{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000612 if (self->fd < 0)
613 return err_closed();
614 if (self->seekable < 0) {
615 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
616 if (pos == NULL) {
617 PyErr_Clear();
618 self->seekable = 0;
619 } else {
620 Py_DECREF(pos);
621 self->seekable = 1;
622 }
623 }
624 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000625}
626
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300627/*[clinic input]
628_io.FileIO.readinto
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700629 buffer: Py_buffer(accept={rwbuffer})
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300630 /
631
632Same as RawIOBase.readinto().
633[clinic start generated code]*/
634
Guido van Rossuma9e20242007-03-08 00:43:48 +0000635static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300636_io_FileIO_readinto_impl(fileio *self, Py_buffer *buffer)
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700637/*[clinic end generated code: output=b01a5a22c8415cb4 input=4721d7b68b154eaf]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000638{
Victor Stinner66aab0c2015-03-19 22:53:20 +0100639 Py_ssize_t n;
640 int err;
Guido van Rossum53807da2007-04-10 19:01:47 +0000641
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000642 if (self->fd < 0)
643 return err_closed();
644 if (!self->readable)
645 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000646
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300647 n = _Py_read(self->fd, buffer->buf, buffer->len);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100648 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100649 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100650
651 if (n == -1) {
652 if (err == EAGAIN) {
653 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000654 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100655 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000656 return NULL;
657 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000658
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000659 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000660}
661
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000662static size_t
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100663new_buffersize(fileio *self, size_t currentsize)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000664{
Antoine Pitroua3f44572012-04-17 13:50:58 +0200665 size_t addend;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100666
Nadeem Vawdad41a98b2011-10-13 13:34:16 +0200667 /* Expand the buffer by an amount proportional to the current size,
Antoine Pitroua3f44572012-04-17 13:50:58 +0200668 giving us amortized linear-time behavior. For bigger sizes, use a
669 less-than-double growth factor to avoid excessive allocation. */
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100670 assert(currentsize <= PY_SSIZE_T_MAX);
Antoine Pitroua3f44572012-04-17 13:50:58 +0200671 if (currentsize > 65536)
672 addend = currentsize >> 3;
673 else
674 addend = 256 + currentsize;
675 if (addend < SMALLCHUNK)
676 /* Avoid tiny read() calls. */
677 addend = SMALLCHUNK;
678 return addend + currentsize;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000679}
680
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300681/*[clinic input]
682_io.FileIO.readall
683
684Read all data from the file, returned as bytes.
685
686In non-blocking mode, returns as much as is immediately available,
687or None if no data is available. Return an empty bytes object at EOF.
688[clinic start generated code]*/
689
Guido van Rossum7165cb12007-07-10 06:54:34 +0000690static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300691_io_FileIO_readall_impl(fileio *self)
692/*[clinic end generated code: output=faa0292b213b4022 input=dbdc137f55602834]*/
Guido van Rossum7165cb12007-07-10 06:54:34 +0000693{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200694 struct _Py_stat_struct status;
Victor Stinnera2a64772011-10-11 22:45:02 +0200695 Py_off_t pos, end;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000696 PyObject *result;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100697 Py_ssize_t bytes_read = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100698 Py_ssize_t n;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100699 size_t bufsize;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000700
Victor Stinnerb79f28c2011-05-25 22:09:03 +0200701 if (self->fd < 0)
702 return err_closed();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000703 if (!_PyVerify_fd(self->fd))
704 return PyErr_SetFromErrno(PyExc_IOError);
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000705
Steve Dower8fc89802015-04-12 00:26:27 -0400706 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +0200707#ifdef MS_WINDOWS
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200708 pos = _lseeki64(self->fd, 0L, SEEK_CUR);
709#else
710 pos = lseek(self->fd, 0L, SEEK_CUR);
711#endif
Steve Dower8fc89802015-04-12 00:26:27 -0400712 _Py_END_SUPPRESS_IPH
713
Victor Stinnere134a7f2015-03-30 10:09:31 +0200714 if (_Py_fstat_noraise(self->fd, &status) == 0)
715 end = status.st_size;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200716 else
Victor Stinnera2a64772011-10-11 22:45:02 +0200717 end = (Py_off_t)-1;
Christian Heimesa872de52008-12-05 08:26:55 +0000718
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100719 if (end > 0 && end >= pos && pos >= 0 && end - pos < PY_SSIZE_T_MAX) {
720 /* This is probably a real file, so we try to allocate a
721 buffer one byte larger than the rest of the file. If the
722 calculation is right then we should get EOF without having
723 to enlarge the buffer. */
724 bufsize = (size_t)(end - pos + 1);
725 } else {
726 bufsize = SMALLCHUNK;
727 }
728
729 result = PyBytes_FromStringAndSize(NULL, bufsize);
730 if (result == NULL)
731 return NULL;
732
733 while (1) {
734 if (bytes_read >= (Py_ssize_t)bufsize) {
735 bufsize = new_buffersize(self, bytes_read);
736 if (bufsize > PY_SSIZE_T_MAX || bufsize <= 0) {
737 PyErr_SetString(PyExc_OverflowError,
738 "unbounded read returned more bytes "
Serhiy Storchakab817b772015-04-10 02:18:44 +0300739 "than a Python bytes object can hold");
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100740 Py_DECREF(result);
741 return NULL;
742 }
743
744 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
745 if (_PyBytes_Resize(&result, bufsize) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000746 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000747 }
748 }
Victor Stinner9672da72015-03-04 18:40:10 +0100749
Victor Stinner66aab0c2015-03-19 22:53:20 +0100750 n = _Py_read(self->fd,
751 PyBytes_AS_STRING(result) + bytes_read,
752 bufsize - bytes_read);
Victor Stinner9672da72015-03-04 18:40:10 +0100753
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000754 if (n == 0)
755 break;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100756 if (n == -1) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000757 if (errno == EAGAIN) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100758 PyErr_Clear();
Victor Stinnere10920f2014-07-02 22:59:31 +0200759 if (bytes_read > 0)
760 break;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000761 Py_DECREF(result);
762 Py_RETURN_NONE;
763 }
764 Py_DECREF(result);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000765 return NULL;
766 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100767 bytes_read += n;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200768 pos += n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000769 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000770
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100771 if (PyBytes_GET_SIZE(result) > bytes_read) {
772 if (_PyBytes_Resize(&result, bytes_read) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000773 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000774 }
775 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000776}
777
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300778/*[clinic input]
779_io.FileIO.read
780 size: io_ssize_t = -1
781 /
782
783Read at most size bytes, returned as bytes.
784
785Only makes one system call, so less data may be returned than requested.
786In non-blocking mode, returns None if no data is available.
787Return an empty bytes object at EOF.
788[clinic start generated code]*/
789
Guido van Rossuma9e20242007-03-08 00:43:48 +0000790static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300791_io_FileIO_read_impl(fileio *self, Py_ssize_t size)
792/*[clinic end generated code: output=42528d39dd0ca641 input=5c6caa5490c13a9b]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000793{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000794 char *ptr;
795 Py_ssize_t n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000796 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000797
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000798 if (self->fd < 0)
799 return err_closed();
800 if (!self->readable)
801 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000802
Victor Stinner66aab0c2015-03-19 22:53:20 +0100803 if (size < 0)
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300804 return _io_FileIO_readall_impl(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000805
Victor Stinner14b9b112013-06-25 00:37:25 +0200806#ifdef MS_WINDOWS
Victor Stinner66aab0c2015-03-19 22:53:20 +0100807 /* On Windows, the count parameter of read() is an int */
Victor Stinnerc655a722011-07-05 11:31:49 +0200808 if (size > INT_MAX)
809 size = INT_MAX;
810#endif
Victor Stinner66aab0c2015-03-19 22:53:20 +0100811
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000812 bytes = PyBytes_FromStringAndSize(NULL, size);
813 if (bytes == NULL)
814 return NULL;
815 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000816
Victor Stinner66aab0c2015-03-19 22:53:20 +0100817 n = _Py_read(self->fd, ptr, size);
818 if (n == -1) {
819 /* copy errno because Py_DECREF() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100820 int err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000821 Py_DECREF(bytes);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100822 if (err == EAGAIN) {
823 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000824 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100825 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000826 return NULL;
827 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000828
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000829 if (n != size) {
830 if (_PyBytes_Resize(&bytes, n) < 0) {
Victor Stinner85c761d2013-07-16 21:36:02 +0200831 Py_CLEAR(bytes);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000832 return NULL;
833 }
834 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000835
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000836 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000837}
838
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300839/*[clinic input]
840_io.FileIO.write
841 b: Py_buffer
842 /
843
Martin Panter6bb91f32016-05-28 00:41:57 +0000844Write buffer b to file, return number of bytes written.
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300845
846Only makes one system call, so not all of the data may be written.
847The number of bytes actually written is returned. In non-blocking mode,
848returns None if the write would block.
849[clinic start generated code]*/
850
Guido van Rossuma9e20242007-03-08 00:43:48 +0000851static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300852_io_FileIO_write_impl(fileio *self, Py_buffer *b)
Martin Panter6bb91f32016-05-28 00:41:57 +0000853/*[clinic end generated code: output=b4059db3d363a2f7 input=6e7908b36f0ce74f]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000854{
Victor Stinner66aab0c2015-03-19 22:53:20 +0100855 Py_ssize_t n;
856 int err;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000857
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000858 if (self->fd < 0)
859 return err_closed();
860 if (!self->writable)
861 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000862
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300863 n = _Py_write(self->fd, b->buf, b->len);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100864 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100865 err = errno;
Martin v. Löwis423be952008-08-13 15:53:07 +0000866
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000867 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100868 if (err == EAGAIN) {
869 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000870 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100871 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000872 return NULL;
873 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000874
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000875 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000876}
877
Guido van Rossum53807da2007-04-10 19:01:47 +0000878/* XXX Windows support below is likely incomplete */
879
Guido van Rossum53807da2007-04-10 19:01:47 +0000880/* Cribbed from posix_lseek() */
881static PyObject *
882portable_lseek(int fd, PyObject *posobj, int whence)
883{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000884 Py_off_t pos, res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000885
886#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000887 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
888 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000889#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000890 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000891#endif
892#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000893 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000894#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000895#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000896 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000897#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000898 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000899#endif /* SEEK_SET */
900
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000901 if (posobj == NULL)
902 pos = 0;
903 else {
904 if(PyFloat_Check(posobj)) {
905 PyErr_SetString(PyExc_TypeError, "an integer is required");
906 return NULL;
907 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000908#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000909 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000910#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000911 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000912#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000913 if (PyErr_Occurred())
914 return NULL;
915 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000916
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000917 if (_PyVerify_fd(fd)) {
918 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -0400919 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +0200920#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000921 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000922#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000923 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000924#endif
Steve Dower8fc89802015-04-12 00:26:27 -0400925 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000926 Py_END_ALLOW_THREADS
927 } else
928 res = -1;
929 if (res < 0)
930 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000931
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000932#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000933 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000934#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000935 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000936#endif
937}
938
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300939/*[clinic input]
940_io.FileIO.seek
941 pos: object
942 whence: int = 0
943 /
Guido van Rossuma9e20242007-03-08 00:43:48 +0000944
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300945Move to new file position and return the file position.
946
947Argument offset is a byte count. Optional argument whence defaults to
948SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values
949are SEEK_CUR or 1 (move relative to current position, positive or negative),
950and SEEK_END or 2 (move relative to end of file, usually negative, although
951many platforms allow seeking beyond the end of a file).
952
953Note that not all file objects are seekable.
954[clinic start generated code]*/
955
956static PyObject *
957_io_FileIO_seek_impl(fileio *self, PyObject *pos, int whence)
958/*[clinic end generated code: output=c976acdf054e6655 input=0439194b0774d454]*/
959{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000960 if (self->fd < 0)
961 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000962
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300963 return portable_lseek(self->fd, pos, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000964}
965
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300966/*[clinic input]
967_io.FileIO.tell
968
969Current file position.
970
971Can raise OSError for non seekable files.
972[clinic start generated code]*/
973
Guido van Rossuma9e20242007-03-08 00:43:48 +0000974static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300975_io_FileIO_tell_impl(fileio *self)
976/*[clinic end generated code: output=ffe2147058809d0b input=807e24ead4cec2f9]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000977{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000978 if (self->fd < 0)
979 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000980
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000981 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000982}
983
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000984#ifdef HAVE_FTRUNCATE
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300985/*[clinic input]
986_io.FileIO.truncate
987 size as posobj: object = NULL
988 /
989
990Truncate the file to at most size bytes and return the truncated size.
991
992Size defaults to the current file position, as returned by tell().
993The current file position is changed to the value of size.
994[clinic start generated code]*/
995
Guido van Rossuma9e20242007-03-08 00:43:48 +0000996static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300997_io_FileIO_truncate_impl(fileio *self, PyObject *posobj)
998/*[clinic end generated code: output=e49ca7a916c176fa input=9026af44686b7318]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000999{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001000 Py_off_t pos;
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001001 int ret;
1002 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001003
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001004 fd = self->fd;
1005 if (fd < 0)
1006 return err_closed();
1007 if (!self->writable)
1008 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001009
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001010 if (posobj == Py_None || posobj == NULL) {
1011 /* Get the current position. */
1012 posobj = portable_lseek(fd, NULL, 1);
1013 if (posobj == NULL)
1014 return NULL;
1015 }
1016 else {
1017 Py_INCREF(posobj);
1018 }
Guido van Rossum53807da2007-04-10 19:01:47 +00001019
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001020#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001021 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001022#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001023 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001024#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001025 if (PyErr_Occurred()){
1026 Py_DECREF(posobj);
1027 return NULL;
1028 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001029
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001030 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04001031 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001032 errno = 0;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07001033#ifdef MS_WINDOWS
1034 ret = _chsize_s(fd, pos);
1035#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001036 ret = ftruncate(fd, pos);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07001037#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04001038 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001039 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001040
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001041 if (ret != 0) {
1042 Py_DECREF(posobj);
1043 PyErr_SetFromErrno(PyExc_IOError);
1044 return NULL;
1045 }
Guido van Rossuma9e20242007-03-08 00:43:48 +00001046
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001047 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001048}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001049#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +00001050
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001051static const char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001052mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +00001053{
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001054 if (self->created) {
1055 if (self->readable)
1056 return "xb+";
1057 else
1058 return "xb";
1059 }
Antoine Pitroue93b63b2013-09-04 20:46:33 +02001060 if (self->appending) {
1061 if (self->readable)
1062 return "ab+";
1063 else
1064 return "ab";
1065 }
1066 else if (self->readable) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001067 if (self->writable)
1068 return "rb+";
1069 else
1070 return "rb";
1071 }
1072 else
1073 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +00001074}
Guido van Rossuma9e20242007-03-08 00:43:48 +00001075
1076static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001077fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001078{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001079 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001080
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001081 if (self->fd < 0)
1082 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +00001083
Martin v. Löwis767046a2011-10-14 15:35:36 +02001084 nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001085 if (nameobj == NULL) {
1086 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1087 PyErr_Clear();
1088 else
1089 return NULL;
Robert Collins933430a2014-10-18 13:32:43 +13001090 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +02001091 "<_io.FileIO fd=%d mode='%s' closefd=%s>",
1092 self->fd, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001093 }
1094 else {
Robert Collins933430a2014-10-18 13:32:43 +13001095 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +02001096 "<_io.FileIO name=%R mode='%s' closefd=%s>",
1097 nameobj, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001098 Py_DECREF(nameobj);
1099 }
1100 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001101}
1102
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001103/*[clinic input]
1104_io.FileIO.isatty
1105
1106True if the file is connected to a TTY device.
1107[clinic start generated code]*/
1108
Guido van Rossuma9e20242007-03-08 00:43:48 +00001109static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001110_io_FileIO_isatty_impl(fileio *self)
1111/*[clinic end generated code: output=932c39924e9a8070 input=cd94ca1f5e95e843]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +00001112{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001113 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +00001114
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001115 if (self->fd < 0)
1116 return err_closed();
1117 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001118 _Py_BEGIN_SUPPRESS_IPH
1119 if (_PyVerify_fd(self->fd))
1120 res = isatty(self->fd);
1121 else
1122 res = 0;
1123 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001124 Py_END_ALLOW_THREADS
1125 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001126}
1127
Antoine Pitrou243757e2010-11-05 21:15:39 +00001128static PyObject *
1129fileio_getstate(fileio *self)
1130{
1131 PyErr_Format(PyExc_TypeError,
1132 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
1133 return NULL;
1134}
1135
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001136#include "clinic/fileio.c.h"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001137
1138static PyMethodDef fileio_methods[] = {
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001139 _IO_FILEIO_READ_METHODDEF
1140 _IO_FILEIO_READALL_METHODDEF
1141 _IO_FILEIO_READINTO_METHODDEF
1142 _IO_FILEIO_WRITE_METHODDEF
1143 _IO_FILEIO_SEEK_METHODDEF
1144 _IO_FILEIO_TELL_METHODDEF
1145 _IO_FILEIO_TRUNCATE_METHODDEF
1146 _IO_FILEIO_CLOSE_METHODDEF
1147 _IO_FILEIO_SEEKABLE_METHODDEF
1148 _IO_FILEIO_READABLE_METHODDEF
1149 _IO_FILEIO_WRITABLE_METHODDEF
1150 _IO_FILEIO_FILENO_METHODDEF
1151 _IO_FILEIO_ISATTY_METHODDEF
Antoine Pitroue033e062010-10-29 10:38:18 +00001152 {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
Antoine Pitrou243757e2010-11-05 21:15:39 +00001153 {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001154 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001155};
1156
Guido van Rossum53807da2007-04-10 19:01:47 +00001157/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1158
Guido van Rossumb0428152007-04-08 17:44:42 +00001159static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001160get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001161{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001162 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001163}
1164
1165static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001166get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001167{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001168 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001169}
1170
1171static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001172get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001173{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001174 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001175}
1176
1177static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001178 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1179 {"closefd", (getter)get_closefd, NULL,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001180 "True if the file descriptor will be closed by close()."},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001181 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1182 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001183};
1184
Antoine Pitrou796564c2013-07-30 19:59:21 +02001185static PyMemberDef fileio_members[] = {
Antoine Pitroude687222014-06-29 20:07:28 -04001186 {"_blksize", T_UINT, offsetof(fileio, blksize), 0},
Antoine Pitrou796564c2013-07-30 19:59:21 +02001187 {"_finalizing", T_BOOL, offsetof(fileio, finalizing), 0},
1188 {NULL}
1189};
1190
Guido van Rossuma9e20242007-03-08 00:43:48 +00001191PyTypeObject PyFileIO_Type = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001192 PyVarObject_HEAD_INIT(NULL, 0)
1193 "_io.FileIO",
1194 sizeof(fileio),
1195 0,
1196 (destructor)fileio_dealloc, /* tp_dealloc */
1197 0, /* tp_print */
1198 0, /* tp_getattr */
1199 0, /* tp_setattr */
1200 0, /* tp_reserved */
1201 (reprfunc)fileio_repr, /* tp_repr */
1202 0, /* tp_as_number */
1203 0, /* tp_as_sequence */
1204 0, /* tp_as_mapping */
1205 0, /* tp_hash */
1206 0, /* tp_call */
1207 0, /* tp_str */
1208 PyObject_GenericGetAttr, /* tp_getattro */
1209 0, /* tp_setattro */
1210 0, /* tp_as_buffer */
1211 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +02001212 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001213 _io_FileIO___init____doc__, /* tp_doc */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001214 (traverseproc)fileio_traverse, /* tp_traverse */
1215 (inquiry)fileio_clear, /* tp_clear */
1216 0, /* tp_richcompare */
1217 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1218 0, /* tp_iter */
1219 0, /* tp_iternext */
1220 fileio_methods, /* tp_methods */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001221 fileio_members, /* tp_members */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001222 fileio_getsetlist, /* tp_getset */
1223 0, /* tp_base */
1224 0, /* tp_dict */
1225 0, /* tp_descr_get */
1226 0, /* tp_descr_set */
1227 offsetof(fileio, dict), /* tp_dictoffset */
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001228 _io_FileIO___init__, /* tp_init */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001229 PyType_GenericAlloc, /* tp_alloc */
1230 fileio_new, /* tp_new */
1231 PyObject_GC_Del, /* tp_free */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001232 0, /* tp_is_gc */
1233 0, /* tp_bases */
1234 0, /* tp_mro */
1235 0, /* tp_cache */
1236 0, /* tp_subclasses */
1237 0, /* tp_weaklist */
1238 0, /* tp_del */
1239 0, /* tp_version_tag */
1240 0, /* tp_finalize */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001241};