blob: a09c39f76ad3a2bcaa40a0544185745cf1cffe53 [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
Steve Dowerbfce0f92016-12-28 15:41:09 -080012#ifdef HAVE_IO_H
13#include <io.h>
14#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000015#ifdef HAVE_FCNTL_H
Guido van Rossuma9e20242007-03-08 00:43:48 +000016#include <fcntl.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000017#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +000018#include <stddef.h> /* For offsetof */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000019#include "_iomodule.h"
Guido van Rossuma9e20242007-03-08 00:43:48 +000020
21/*
22 * Known likely problems:
23 *
24 * - Files larger then 2**32-1
25 * - Files with unicode filenames
26 * - Passing numbers greater than 2**32-1 when an integer is expected
27 * - Making it work on Windows and other oddball platforms
28 *
29 * To Do:
30 *
31 * - autoconfify header file inclusion
Guido van Rossuma9e20242007-03-08 00:43:48 +000032 */
33
34#ifdef MS_WINDOWS
35/* can simulate truncate with Win32 API functions; see file_truncate */
Thomas Hellerfdeee3a2007-07-12 11:21:36 +000036#define HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +000037#define WIN32_LEAN_AND_MEAN
38#include <windows.h>
39#endif
40
Christian Heimesa872de52008-12-05 08:26:55 +000041#if BUFSIZ < (8*1024)
42#define SMALLCHUNK (8*1024)
43#elif (BUFSIZ >= (2 << 25))
44#error "unreasonable BUFSIZ > 64MB defined"
45#else
46#define SMALLCHUNK BUFSIZ
47#endif
48
Serhiy Storchakaf24131f2015-04-16 11:19:43 +030049/*[clinic input]
50module _io
51class _io.FileIO "fileio *" "&PyFileIO_Type"
52[clinic start generated code]*/
53/*[clinic end generated code: output=da39a3ee5e6b4b0d input=1c77708b41fda70c]*/
54
Guido van Rossuma9e20242007-03-08 00:43:48 +000055typedef struct {
Antoine Pitrouae4b4722010-05-05 16:31:07 +000056 PyObject_HEAD
57 int fd;
Charles-François Natalidc3044c2012-01-09 22:40:02 +010058 unsigned int created : 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000059 unsigned int readable : 1;
60 unsigned int writable : 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +020061 unsigned int appending : 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000062 signed int seekable : 2; /* -1 means unknown */
63 unsigned int closefd : 1;
Antoine Pitrou796564c2013-07-30 19:59:21 +020064 char finalizing;
Antoine Pitroude687222014-06-29 20:07:28 -040065 unsigned int blksize;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000066 PyObject *weakreflist;
67 PyObject *dict;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000068} fileio;
Guido van Rossuma9e20242007-03-08 00:43:48 +000069
Collin Winteraf334382007-03-08 21:46:15 +000070PyTypeObject PyFileIO_Type;
71
Victor Stinnerd9d04192013-11-06 23:50:10 +010072_Py_IDENTIFIER(name);
73
Guido van Rossuma9e20242007-03-08 00:43:48 +000074#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
75
Victor Stinner99970732017-05-02 15:10:39 +020076/* Forward declarations */
77static PyObject* portable_lseek(fileio *self, PyObject *posobj, int whence);
78
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000079int
80_PyFileIO_closed(PyObject *self)
81{
Antoine Pitrouae4b4722010-05-05 16:31:07 +000082 return ((fileio *)self)->fd < 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000083}
Antoine Pitrou08838b62009-01-21 00:55:13 +000084
Antoine Pitroue033e062010-10-29 10:38:18 +000085/* Because this can call arbitrary code, it shouldn't be called when
86 the refcount is 0 (that is, not directly from tp_dealloc unless
87 the refcount has been temporarily re-incremented). */
88static PyObject *
89fileio_dealloc_warn(fileio *self, PyObject *source)
90{
91 if (self->fd >= 0 && self->closefd) {
92 PyObject *exc, *val, *tb;
93 PyErr_Fetch(&exc, &val, &tb);
Victor Stinner914cde82016-03-19 01:03:51 +010094 if (PyErr_ResourceWarning(source, 1, "unclosed file %R", source)) {
Antoine Pitroue033e062010-10-29 10:38:18 +000095 /* Spurious errors can appear at shutdown */
96 if (PyErr_ExceptionMatches(PyExc_Warning))
97 PyErr_WriteUnraisable((PyObject *) self);
98 }
99 PyErr_Restore(exc, val, tb);
100 }
101 Py_RETURN_NONE;
102}
103
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +0000104/* Returns 0 on success, -1 with exception set on failure. */
Neal Norwitz88b44da2007-08-12 17:23:54 +0000105static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000106internal_close(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000107{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000108 int err = 0;
109 int save_errno = 0;
110 if (self->fd >= 0) {
111 int fd = self->fd;
112 self->fd = -1;
113 /* fd is accessible and someone else may have closed it */
Steve Dower940f33a2016-09-08 11:21:54 -0700114 Py_BEGIN_ALLOW_THREADS
115 _Py_BEGIN_SUPPRESS_IPH
116 err = close(fd);
117 if (err < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000118 save_errno = errno;
Steve Dower940f33a2016-09-08 11:21:54 -0700119 _Py_END_SUPPRESS_IPH
120 Py_END_ALLOW_THREADS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000121 }
122 if (err < 0) {
123 errno = save_errno;
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300124 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000125 return -1;
126 }
127 return 0;
Neal Norwitz88b44da2007-08-12 17:23:54 +0000128}
129
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300130/*[clinic input]
131_io.FileIO.close
132
133Close the file.
134
135A closed file cannot be used for further I/O operations. close() may be
136called more than once without error.
137[clinic start generated code]*/
138
Neal Norwitz88b44da2007-08-12 17:23:54 +0000139static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300140_io_FileIO_close_impl(fileio *self)
141/*[clinic end generated code: output=7737a319ef3bad0b input=f35231760d54a522]*/
Neal Norwitz88b44da2007-08-12 17:23:54 +0000142{
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200143 PyObject *res;
144 PyObject *exc, *val, *tb;
145 int rc;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200146 _Py_IDENTIFIER(close);
Victor Stinner61bdb0d2016-12-09 15:39:28 +0100147 res = _PyObject_CallMethodIdObjArgs((PyObject*)&PyRawIOBase_Type,
148 &PyId_close, self, NULL);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000149 if (!self->closefd) {
150 self->fd = -1;
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200151 return res;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000152 }
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200153 if (res == NULL)
154 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200155 if (self->finalizing) {
Antoine Pitroue033e062010-10-29 10:38:18 +0000156 PyObject *r = fileio_dealloc_warn(self, (PyObject *) self);
157 if (r)
158 Py_DECREF(r);
159 else
160 PyErr_Clear();
161 }
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200162 rc = internal_close(self);
163 if (res == NULL)
164 _PyErr_ChainExceptions(exc, val, tb);
165 if (rc < 0)
166 Py_CLEAR(res);
167 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000168}
169
170static PyObject *
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000171fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000172{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000173 fileio *self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000174
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000175 assert(type != NULL && type->tp_alloc != NULL);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000176
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000177 self = (fileio *) type->tp_alloc(type, 0);
178 if (self != NULL) {
179 self->fd = -1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100180 self->created = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000181 self->readable = 0;
182 self->writable = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200183 self->appending = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000184 self->seekable = -1;
Antoine Pitroude687222014-06-29 20:07:28 -0400185 self->blksize = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000186 self->closefd = 1;
187 self->weakreflist = NULL;
188 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000189
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000190 return (PyObject *) self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000191}
192
Victor Stinnerdaf45552013-08-28 00:53:59 +0200193#ifdef O_CLOEXEC
194extern int _Py_open_cloexec_works;
195#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000196
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300197/*[clinic input]
198_io.FileIO.__init__
199 file as nameobj: object
200 mode: str = "r"
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200201 closefd: bool(accept={int}) = True
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300202 opener: object = None
203
204Open a file.
205
206The mode can be 'r' (default), 'w', 'x' or 'a' for reading,
207writing, exclusive creation or appending. The file will be created if it
208doesn't exist when opened for writing or appending; it will be truncated
209when opened for writing. A FileExistsError will be raised if it already
210exists when opened for creating. Opening a file for creating implies
211writing so this mode behaves in a similar way to 'w'.Add a '+' to the mode
212to allow simultaneous reading and writing. A custom opener can be used by
213passing a callable as *opener*. The underlying file descriptor for the file
214object is then obtained by calling opener with (*name*, *flags*).
215*opener* must return an open file descriptor (passing os.open as *opener*
216results in functionality similar to passing None).
217[clinic start generated code]*/
218
Guido van Rossuma9e20242007-03-08 00:43:48 +0000219static int
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300220_io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
221 int closefd, PyObject *opener)
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200222/*[clinic end generated code: output=23413f68e6484bbd input=1596c9157a042a39]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000223{
Thomas Helleraf2be262007-07-12 11:03:13 +0000224#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000225 Py_UNICODE *widename = NULL;
Steve Dowereacee982017-02-04 14:38:11 -0800226#else
227 const char *name = NULL;
Thomas Helleraf2be262007-07-12 11:03:13 +0000228#endif
Steve Dowereacee982017-02-04 14:38:11 -0800229 PyObject *stringobj = NULL;
230 const char *s;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000231 int ret = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200232 int rwa = 0, plus = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000233 int flags = 0;
234 int fd = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200235 int fd_is_own = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200236#ifdef O_CLOEXEC
237 int *atomic_flag_works = &_Py_open_cloexec_works;
238#elif !defined(MS_WINDOWS)
239 int *atomic_flag_works = NULL;
240#endif
Steve Dowerf2f373f2015-02-21 08:44:05 -0800241 struct _Py_stat_struct fdfstat;
Martin Panter0bb62b12015-12-06 03:15:05 +0000242 int fstat_result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000243 int async_err = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000244
Christian Heimes82adeff2015-04-16 17:21:54 +0200245 assert(PyFileIO_Check(self));
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000246 if (self->fd >= 0) {
Hynek Schlawack2cc71562012-05-25 10:05:53 +0200247 if (self->closefd) {
248 /* Have to close the existing file first. */
249 if (internal_close(self) < 0)
250 return -1;
251 }
252 else
253 self->fd = -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000254 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000255
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000256 if (PyFloat_Check(nameobj)) {
257 PyErr_SetString(PyExc_TypeError,
258 "integer argument expected, got float");
259 return -1;
260 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000261
Serhiy Storchaka78980432013-01-15 01:12:17 +0200262 fd = _PyLong_AsInt(nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000263 if (fd < 0) {
264 if (!PyErr_Occurred()) {
265 PyErr_SetString(PyExc_ValueError,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +0300266 "negative file descriptor");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000267 return -1;
268 }
269 PyErr_Clear();
270 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000271
Steve Dowereacee982017-02-04 14:38:11 -0800272 if (fd < 0) {
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000273#ifdef MS_WINDOWS
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300274 Py_ssize_t length;
Steve Dowereacee982017-02-04 14:38:11 -0800275 if (!PyUnicode_FSDecoder(nameobj, &stringobj)) {
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300276 return -1;
277 }
Steve Dowereacee982017-02-04 14:38:11 -0800278 widename = PyUnicode_AsUnicodeAndSize(stringobj, &length);
279 if (widename == NULL)
280 return -1;
281#else
Antoine Pitrou13348842012-01-29 18:36:34 +0100282 if (!PyUnicode_FSConverter(nameobj, &stringobj)) {
283 return -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000284 }
Antoine Pitrou13348842012-01-29 18:36:34 +0100285 name = PyBytes_AS_STRING(stringobj);
Steve Dowereacee982017-02-04 14:38:11 -0800286#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000287 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000288
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000289 s = mode;
290 while (*s) {
291 switch (*s++) {
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100292 case 'x':
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000293 if (rwa) {
294 bad_mode:
295 PyErr_SetString(PyExc_ValueError,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100296 "Must have exactly one of create/read/write/append "
Georg Brandl28928ae2010-10-21 13:45:52 +0000297 "mode and at most one plus");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000298 goto error;
299 }
300 rwa = 1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100301 self->created = 1;
302 self->writable = 1;
303 flags |= O_EXCL | O_CREAT;
304 break;
305 case 'r':
306 if (rwa)
307 goto bad_mode;
308 rwa = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000309 self->readable = 1;
310 break;
311 case 'w':
312 if (rwa)
313 goto bad_mode;
314 rwa = 1;
315 self->writable = 1;
316 flags |= O_CREAT | O_TRUNC;
317 break;
318 case 'a':
319 if (rwa)
320 goto bad_mode;
321 rwa = 1;
322 self->writable = 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200323 self->appending = 1;
324 flags |= O_APPEND | O_CREAT;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000325 break;
326 case 'b':
327 break;
328 case '+':
329 if (plus)
330 goto bad_mode;
331 self->readable = self->writable = 1;
332 plus = 1;
333 break;
334 default:
335 PyErr_Format(PyExc_ValueError,
336 "invalid mode: %.200s", mode);
337 goto error;
338 }
339 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000340
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000341 if (!rwa)
342 goto bad_mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000343
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000344 if (self->readable && self->writable)
345 flags |= O_RDWR;
346 else if (self->readable)
347 flags |= O_RDONLY;
348 else
349 flags |= O_WRONLY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000350
351#ifdef O_BINARY
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000352 flags |= O_BINARY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000353#endif
354
Victor Stinnerdaf45552013-08-28 00:53:59 +0200355#ifdef MS_WINDOWS
356 flags |= O_NOINHERIT;
357#elif defined(O_CLOEXEC)
358 flags |= O_CLOEXEC;
359#endif
Walter Dörwald0e411482007-06-06 16:55:38 +0000360
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000361 if (fd >= 0) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000362 self->fd = fd;
363 self->closefd = closefd;
364 }
365 else {
366 self->closefd = 1;
367 if (!closefd) {
368 PyErr_SetString(PyExc_ValueError,
369 "Cannot use closefd=False with file name");
370 goto error;
371 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000372
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000373 errno = 0;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200374 if (opener == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000375 do {
376 Py_BEGIN_ALLOW_THREADS
Thomas Helleraf2be262007-07-12 11:03:13 +0000377#ifdef MS_WINDOWS
Steve Dowereacee982017-02-04 14:38:11 -0800378 self->fd = _wopen(widename, flags, 0666);
379#else
380 self->fd = open(name, flags, 0666);
Thomas Helleraf2be262007-07-12 11:03:13 +0000381#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000382 Py_END_ALLOW_THREADS
383 } while (self->fd < 0 && errno == EINTR &&
384 !(async_err = PyErr_CheckSignals()));
Victor Stinner9672da72015-03-04 18:40:10 +0100385
386 if (async_err)
387 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200388 }
389 else {
390 PyObject *fdobj;
391
392#ifndef MS_WINDOWS
393 /* the opener may clear the atomic flag */
394 atomic_flag_works = NULL;
395#endif
396
397 fdobj = PyObject_CallFunction(opener, "Oi", nameobj, flags);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200398 if (fdobj == NULL)
399 goto error;
400 if (!PyLong_Check(fdobj)) {
401 Py_DECREF(fdobj);
402 PyErr_SetString(PyExc_TypeError,
403 "expected integer from opener");
404 goto error;
405 }
406
Serhiy Storchaka78980432013-01-15 01:12:17 +0200407 self->fd = _PyLong_AsInt(fdobj);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200408 Py_DECREF(fdobj);
Barry Warsaw480e2852016-06-08 17:47:26 -0400409 if (self->fd < 0) {
410 if (!PyErr_Occurred()) {
Barry Warsaw118598a2016-06-08 17:54:43 -0400411 /* The opener returned a negative but didn't set an
412 exception. See issue #27066 */
Barry Warsaw480e2852016-06-08 17:47:26 -0400413 PyErr_Format(PyExc_ValueError,
414 "opener returned %d", self->fd);
415 }
Ross Lagerwall59142db2011-10-31 20:34:46 +0200416 goto error;
417 }
418 }
419
Hynek Schlawack7f59fd72012-06-22 09:32:22 +0200420 fd_is_own = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000421 if (self->fd < 0) {
Victor Stinner9672da72015-03-04 18:40:10 +0100422 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000423 goto error;
424 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200425
426#ifndef MS_WINDOWS
427 if (_Py_set_inheritable(self->fd, 0, atomic_flag_works) < 0)
428 goto error;
429#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000430 }
Antoine Pitroude687222014-06-29 20:07:28 -0400431
432 self->blksize = DEFAULT_BUFFER_SIZE;
Martin Panter0bb62b12015-12-06 03:15:05 +0000433 Py_BEGIN_ALLOW_THREADS
434 fstat_result = _Py_fstat_noraise(self->fd, &fdfstat);
435 Py_END_ALLOW_THREADS
436 if (fstat_result < 0) {
Martin Panter49d3db92015-12-06 11:12:15 +0000437 /* Tolerate fstat() errors other than EBADF. See Issue #25717, where
438 an anonymous file on a Virtual Box shared folder filesystem would
439 raise ENOENT. */
Martin Panter0bb62b12015-12-06 03:15:05 +0000440#ifdef MS_WINDOWS
441 if (GetLastError() == ERROR_INVALID_HANDLE) {
442 PyErr_SetFromWindowsErr(0);
443#else
444 if (errno == EBADF) {
445 PyErr_SetFromErrno(PyExc_OSError);
446#endif
447 goto error;
448 }
Antoine Pitroude687222014-06-29 20:07:28 -0400449 }
Martin Panter0bb62b12015-12-06 03:15:05 +0000450 else {
451#if defined(S_ISDIR) && defined(EISDIR)
452 /* On Unix, open will succeed for directories.
453 In Python, there should be no file objects referring to
454 directories, so we need a check. */
455 if (S_ISDIR(fdfstat.st_mode)) {
456 errno = EISDIR;
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300457 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
Martin Panter0bb62b12015-12-06 03:15:05 +0000458 goto error;
459 }
Antoine Pitroude687222014-06-29 20:07:28 -0400460#endif /* defined(S_ISDIR) */
461#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin Panter0bb62b12015-12-06 03:15:05 +0000462 if (fdfstat.st_blksize > 1)
463 self->blksize = fdfstat.st_blksize;
Antoine Pitroude687222014-06-29 20:07:28 -0400464#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Martin Panter0bb62b12015-12-06 03:15:05 +0000465 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000466
Victor Stinner89e34362011-01-07 18:47:22 +0000467#if defined(MS_WINDOWS) || defined(__CYGWIN__)
468 /* don't translate newlines (\r\n <=> \n) */
469 _setmode(self->fd, O_BINARY);
470#endif
471
Victor Stinnerd9d04192013-11-06 23:50:10 +0100472 if (_PyObject_SetAttrId((PyObject *)self, &PyId_name, nameobj) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000473 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000474
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200475 if (self->appending) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000476 /* For consistent behaviour, we explicitly seek to the
477 end of file (otherwise, it might be done only on the
478 first write()). */
Victor Stinner99970732017-05-02 15:10:39 +0200479 PyObject *pos = portable_lseek(self, NULL, 2);
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200480 if (pos == NULL)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000481 goto error;
482 Py_DECREF(pos);
483 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000484
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000485 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000486
487 error:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000488 ret = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200489 if (!fd_is_own)
490 self->fd = -1;
Benjamin Petersonbbb04122010-10-30 23:16:28 +0000491 if (self->fd >= 0)
492 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000493
Guido van Rossuma9e20242007-03-08 00:43:48 +0000494 done:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000495 Py_CLEAR(stringobj);
496 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000497}
498
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000499static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000500fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000501{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000502 Py_VISIT(self->dict);
503 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000504}
505
506static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000507fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000508{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000509 Py_CLEAR(self->dict);
510 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000511}
512
Guido van Rossuma9e20242007-03-08 00:43:48 +0000513static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000514fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000515{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200516 self->finalizing = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000517 if (_PyIOBase_finalize((PyObject *) self) < 0)
518 return;
519 _PyObject_GC_UNTRACK(self);
520 if (self->weakreflist != NULL)
521 PyObject_ClearWeakRefs((PyObject *) self);
522 Py_CLEAR(self->dict);
523 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000524}
525
526static PyObject *
527err_closed(void)
528{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000529 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
530 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000531}
532
533static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200534err_mode(const char *action)
Guido van Rossum53807da2007-04-10 19:01:47 +0000535{
Antoine Pitrou712cb732013-12-21 15:51:54 +0100536 _PyIO_State *state = IO_STATE();
537 if (state != NULL)
538 PyErr_Format(state->unsupported_operation,
539 "File not open for %s", action);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000540 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000541}
542
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300543/*[clinic input]
544_io.FileIO.fileno
545
546Return the underlying file descriptor (an integer).
547[clinic start generated code]*/
548
Guido van Rossum53807da2007-04-10 19:01:47 +0000549static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300550_io_FileIO_fileno_impl(fileio *self)
551/*[clinic end generated code: output=a9626ce5398ece90 input=0b9b2de67335ada3]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000552{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000553 if (self->fd < 0)
554 return err_closed();
555 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000556}
557
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300558/*[clinic input]
559_io.FileIO.readable
560
561True if file was opened in a read mode.
562[clinic start generated code]*/
563
Guido van Rossuma9e20242007-03-08 00:43:48 +0000564static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300565_io_FileIO_readable_impl(fileio *self)
566/*[clinic end generated code: output=640744a6150fe9ba input=a3fdfed6eea721c5]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000567{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000568 if (self->fd < 0)
569 return err_closed();
570 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000571}
572
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300573/*[clinic input]
574_io.FileIO.writable
575
576True if file was opened in a write mode.
577[clinic start generated code]*/
578
Guido van Rossuma9e20242007-03-08 00:43:48 +0000579static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300580_io_FileIO_writable_impl(fileio *self)
581/*[clinic end generated code: output=96cefc5446e89977 input=c204a808ca2e1748]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000582{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000583 if (self->fd < 0)
584 return err_closed();
585 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000586}
587
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300588/*[clinic input]
589_io.FileIO.seekable
590
591True if file supports random-access.
592[clinic start generated code]*/
593
Guido van Rossuma9e20242007-03-08 00:43:48 +0000594static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300595_io_FileIO_seekable_impl(fileio *self)
596/*[clinic end generated code: output=47909ca0a42e9287 input=c8e5554d2fd63c7f]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000597{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000598 if (self->fd < 0)
599 return err_closed();
600 if (self->seekable < 0) {
Victor Stinner99970732017-05-02 15:10:39 +0200601 /* portable_lseek() sets the seekable attribute */
602 PyObject *pos = portable_lseek(self, NULL, SEEK_CUR);
603 assert(self->seekable >= 0);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000604 if (pos == NULL) {
605 PyErr_Clear();
Victor Stinner99970732017-05-02 15:10:39 +0200606 }
607 else {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000608 Py_DECREF(pos);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000609 }
610 }
611 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000612}
613
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300614/*[clinic input]
615_io.FileIO.readinto
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700616 buffer: Py_buffer(accept={rwbuffer})
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300617 /
618
619Same as RawIOBase.readinto().
620[clinic start generated code]*/
621
Guido van Rossuma9e20242007-03-08 00:43:48 +0000622static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300623_io_FileIO_readinto_impl(fileio *self, Py_buffer *buffer)
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700624/*[clinic end generated code: output=b01a5a22c8415cb4 input=4721d7b68b154eaf]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000625{
Victor Stinner66aab0c2015-03-19 22:53:20 +0100626 Py_ssize_t n;
627 int err;
Guido van Rossum53807da2007-04-10 19:01:47 +0000628
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000629 if (self->fd < 0)
630 return err_closed();
631 if (!self->readable)
632 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000633
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300634 n = _Py_read(self->fd, buffer->buf, buffer->len);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100635 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100636 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100637
638 if (n == -1) {
639 if (err == EAGAIN) {
640 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000641 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100642 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000643 return NULL;
644 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000645
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000646 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000647}
648
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000649static size_t
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100650new_buffersize(fileio *self, size_t currentsize)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000651{
Antoine Pitroua3f44572012-04-17 13:50:58 +0200652 size_t addend;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100653
Nadeem Vawdad41a98b2011-10-13 13:34:16 +0200654 /* Expand the buffer by an amount proportional to the current size,
Antoine Pitroua3f44572012-04-17 13:50:58 +0200655 giving us amortized linear-time behavior. For bigger sizes, use a
656 less-than-double growth factor to avoid excessive allocation. */
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100657 assert(currentsize <= PY_SSIZE_T_MAX);
Antoine Pitroua3f44572012-04-17 13:50:58 +0200658 if (currentsize > 65536)
659 addend = currentsize >> 3;
660 else
661 addend = 256 + currentsize;
662 if (addend < SMALLCHUNK)
663 /* Avoid tiny read() calls. */
664 addend = SMALLCHUNK;
665 return addend + currentsize;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000666}
667
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300668/*[clinic input]
669_io.FileIO.readall
670
671Read all data from the file, returned as bytes.
672
673In non-blocking mode, returns as much as is immediately available,
674or None if no data is available. Return an empty bytes object at EOF.
675[clinic start generated code]*/
676
Guido van Rossum7165cb12007-07-10 06:54:34 +0000677static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300678_io_FileIO_readall_impl(fileio *self)
679/*[clinic end generated code: output=faa0292b213b4022 input=dbdc137f55602834]*/
Guido van Rossum7165cb12007-07-10 06:54:34 +0000680{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200681 struct _Py_stat_struct status;
Victor Stinnera2a64772011-10-11 22:45:02 +0200682 Py_off_t pos, end;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000683 PyObject *result;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100684 Py_ssize_t bytes_read = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100685 Py_ssize_t n;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100686 size_t bufsize;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000687
Victor Stinnerb79f28c2011-05-25 22:09:03 +0200688 if (self->fd < 0)
689 return err_closed();
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000690
Steve Dower8fc89802015-04-12 00:26:27 -0400691 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +0200692#ifdef MS_WINDOWS
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200693 pos = _lseeki64(self->fd, 0L, SEEK_CUR);
694#else
695 pos = lseek(self->fd, 0L, SEEK_CUR);
696#endif
Steve Dower8fc89802015-04-12 00:26:27 -0400697 _Py_END_SUPPRESS_IPH
698
Victor Stinnere134a7f2015-03-30 10:09:31 +0200699 if (_Py_fstat_noraise(self->fd, &status) == 0)
700 end = status.st_size;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200701 else
Victor Stinnera2a64772011-10-11 22:45:02 +0200702 end = (Py_off_t)-1;
Christian Heimesa872de52008-12-05 08:26:55 +0000703
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100704 if (end > 0 && end >= pos && pos >= 0 && end - pos < PY_SSIZE_T_MAX) {
705 /* This is probably a real file, so we try to allocate a
706 buffer one byte larger than the rest of the file. If the
707 calculation is right then we should get EOF without having
708 to enlarge the buffer. */
709 bufsize = (size_t)(end - pos + 1);
710 } else {
711 bufsize = SMALLCHUNK;
712 }
713
714 result = PyBytes_FromStringAndSize(NULL, bufsize);
715 if (result == NULL)
716 return NULL;
717
718 while (1) {
719 if (bytes_read >= (Py_ssize_t)bufsize) {
720 bufsize = new_buffersize(self, bytes_read);
721 if (bufsize > PY_SSIZE_T_MAX || bufsize <= 0) {
722 PyErr_SetString(PyExc_OverflowError,
723 "unbounded read returned more bytes "
Serhiy Storchakab817b772015-04-10 02:18:44 +0300724 "than a Python bytes object can hold");
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100725 Py_DECREF(result);
726 return NULL;
727 }
728
729 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
730 if (_PyBytes_Resize(&result, bufsize) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000731 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000732 }
733 }
Victor Stinner9672da72015-03-04 18:40:10 +0100734
Victor Stinner66aab0c2015-03-19 22:53:20 +0100735 n = _Py_read(self->fd,
736 PyBytes_AS_STRING(result) + bytes_read,
737 bufsize - bytes_read);
Victor Stinner9672da72015-03-04 18:40:10 +0100738
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000739 if (n == 0)
740 break;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100741 if (n == -1) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000742 if (errno == EAGAIN) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100743 PyErr_Clear();
Victor Stinnere10920f2014-07-02 22:59:31 +0200744 if (bytes_read > 0)
745 break;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000746 Py_DECREF(result);
747 Py_RETURN_NONE;
748 }
749 Py_DECREF(result);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000750 return NULL;
751 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100752 bytes_read += n;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200753 pos += n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000754 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000755
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100756 if (PyBytes_GET_SIZE(result) > bytes_read) {
757 if (_PyBytes_Resize(&result, bytes_read) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000758 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000759 }
760 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000761}
762
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300763/*[clinic input]
764_io.FileIO.read
Serhiy Storchaka762bf402017-03-30 09:15:31 +0300765 size: Py_ssize_t(accept={int, NoneType}) = -1
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300766 /
767
768Read at most size bytes, returned as bytes.
769
770Only makes one system call, so less data may be returned than requested.
771In non-blocking mode, returns None if no data is available.
772Return an empty bytes object at EOF.
773[clinic start generated code]*/
774
Guido van Rossuma9e20242007-03-08 00:43:48 +0000775static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300776_io_FileIO_read_impl(fileio *self, Py_ssize_t size)
Serhiy Storchaka762bf402017-03-30 09:15:31 +0300777/*[clinic end generated code: output=42528d39dd0ca641 input=bec9a2c704ddcbc9]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000778{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000779 char *ptr;
780 Py_ssize_t n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000781 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000782
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000783 if (self->fd < 0)
784 return err_closed();
785 if (!self->readable)
786 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000787
Victor Stinner66aab0c2015-03-19 22:53:20 +0100788 if (size < 0)
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300789 return _io_FileIO_readall_impl(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000790
Victor Stinner14b9b112013-06-25 00:37:25 +0200791#ifdef MS_WINDOWS
Victor Stinner66aab0c2015-03-19 22:53:20 +0100792 /* On Windows, the count parameter of read() is an int */
Victor Stinnerc655a722011-07-05 11:31:49 +0200793 if (size > INT_MAX)
794 size = INT_MAX;
795#endif
Victor Stinner66aab0c2015-03-19 22:53:20 +0100796
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000797 bytes = PyBytes_FromStringAndSize(NULL, size);
798 if (bytes == NULL)
799 return NULL;
800 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000801
Victor Stinner66aab0c2015-03-19 22:53:20 +0100802 n = _Py_read(self->fd, ptr, size);
803 if (n == -1) {
804 /* copy errno because Py_DECREF() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100805 int err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000806 Py_DECREF(bytes);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100807 if (err == EAGAIN) {
808 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000809 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100810 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000811 return NULL;
812 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000813
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000814 if (n != size) {
815 if (_PyBytes_Resize(&bytes, n) < 0) {
Victor Stinner85c761d2013-07-16 21:36:02 +0200816 Py_CLEAR(bytes);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000817 return NULL;
818 }
819 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000820
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000821 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000822}
823
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300824/*[clinic input]
825_io.FileIO.write
826 b: Py_buffer
827 /
828
Martin Panter6bb91f32016-05-28 00:41:57 +0000829Write buffer b to file, return number of bytes written.
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300830
831Only makes one system call, so not all of the data may be written.
832The number of bytes actually written is returned. In non-blocking mode,
833returns None if the write would block.
834[clinic start generated code]*/
835
Guido van Rossuma9e20242007-03-08 00:43:48 +0000836static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300837_io_FileIO_write_impl(fileio *self, Py_buffer *b)
Martin Panter6bb91f32016-05-28 00:41:57 +0000838/*[clinic end generated code: output=b4059db3d363a2f7 input=6e7908b36f0ce74f]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000839{
Victor Stinner66aab0c2015-03-19 22:53:20 +0100840 Py_ssize_t n;
841 int err;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000842
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000843 if (self->fd < 0)
844 return err_closed();
845 if (!self->writable)
846 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000847
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300848 n = _Py_write(self->fd, b->buf, b->len);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100849 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100850 err = errno;
Martin v. Löwis423be952008-08-13 15:53:07 +0000851
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000852 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100853 if (err == EAGAIN) {
854 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000855 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100856 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000857 return NULL;
858 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000859
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000860 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000861}
862
Guido van Rossum53807da2007-04-10 19:01:47 +0000863/* XXX Windows support below is likely incomplete */
864
Guido van Rossum53807da2007-04-10 19:01:47 +0000865/* Cribbed from posix_lseek() */
866static PyObject *
Victor Stinner99970732017-05-02 15:10:39 +0200867portable_lseek(fileio *self, PyObject *posobj, int whence)
Guido van Rossum53807da2007-04-10 19:01:47 +0000868{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000869 Py_off_t pos, res;
Victor Stinner99970732017-05-02 15:10:39 +0200870 int fd = self->fd;
Guido van Rossum53807da2007-04-10 19:01:47 +0000871
872#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000873 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
874 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000875#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000876 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000877#endif
878#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000879 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000880#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000881#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000882 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000883#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000884 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000885#endif /* SEEK_SET */
886
Victor Stinner99970732017-05-02 15:10:39 +0200887 if (posobj == NULL) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000888 pos = 0;
Victor Stinner99970732017-05-02 15:10:39 +0200889 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000890 else {
891 if(PyFloat_Check(posobj)) {
892 PyErr_SetString(PyExc_TypeError, "an integer is required");
893 return NULL;
894 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000895#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000896 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000897#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000898 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000899#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000900 if (PyErr_Occurred())
901 return NULL;
902 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000903
Steve Dower940f33a2016-09-08 11:21:54 -0700904 Py_BEGIN_ALLOW_THREADS
905 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +0200906#ifdef MS_WINDOWS
Steve Dower940f33a2016-09-08 11:21:54 -0700907 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000908#else
Steve Dower940f33a2016-09-08 11:21:54 -0700909 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000910#endif
Steve Dower940f33a2016-09-08 11:21:54 -0700911 _Py_END_SUPPRESS_IPH
912 Py_END_ALLOW_THREADS
Victor Stinner99970732017-05-02 15:10:39 +0200913
914 if (self->seekable < 0) {
915 self->seekable = (res >= 0);
916 }
917
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000918 if (res < 0)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300919 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000920
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000921#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000922 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000923#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000924 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000925#endif
926}
927
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300928/*[clinic input]
929_io.FileIO.seek
930 pos: object
931 whence: int = 0
932 /
Guido van Rossuma9e20242007-03-08 00:43:48 +0000933
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300934Move to new file position and return the file position.
935
936Argument offset is a byte count. Optional argument whence defaults to
937SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values
938are SEEK_CUR or 1 (move relative to current position, positive or negative),
939and SEEK_END or 2 (move relative to end of file, usually negative, although
940many platforms allow seeking beyond the end of a file).
941
942Note that not all file objects are seekable.
943[clinic start generated code]*/
944
945static PyObject *
946_io_FileIO_seek_impl(fileio *self, PyObject *pos, int whence)
947/*[clinic end generated code: output=c976acdf054e6655 input=0439194b0774d454]*/
948{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000949 if (self->fd < 0)
950 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000951
Victor Stinner99970732017-05-02 15:10:39 +0200952 return portable_lseek(self, pos, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000953}
954
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300955/*[clinic input]
956_io.FileIO.tell
957
958Current file position.
959
960Can raise OSError for non seekable files.
961[clinic start generated code]*/
962
Guido van Rossuma9e20242007-03-08 00:43:48 +0000963static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300964_io_FileIO_tell_impl(fileio *self)
965/*[clinic end generated code: output=ffe2147058809d0b input=807e24ead4cec2f9]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000966{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000967 if (self->fd < 0)
968 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000969
Victor Stinner99970732017-05-02 15:10:39 +0200970 return portable_lseek(self, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000971}
972
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000973#ifdef HAVE_FTRUNCATE
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300974/*[clinic input]
975_io.FileIO.truncate
976 size as posobj: object = NULL
977 /
978
979Truncate the file to at most size bytes and return the truncated size.
980
981Size defaults to the current file position, as returned by tell().
982The current file position is changed to the value of size.
983[clinic start generated code]*/
984
Guido van Rossuma9e20242007-03-08 00:43:48 +0000985static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300986_io_FileIO_truncate_impl(fileio *self, PyObject *posobj)
987/*[clinic end generated code: output=e49ca7a916c176fa input=9026af44686b7318]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000988{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000989 Py_off_t pos;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000990 int ret;
991 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000992
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000993 fd = self->fd;
994 if (fd < 0)
995 return err_closed();
996 if (!self->writable)
997 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000998
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000999 if (posobj == Py_None || posobj == NULL) {
1000 /* Get the current position. */
Victor Stinner99970732017-05-02 15:10:39 +02001001 posobj = portable_lseek(self, NULL, 1);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001002 if (posobj == NULL)
1003 return NULL;
1004 }
1005 else {
1006 Py_INCREF(posobj);
1007 }
Guido van Rossum53807da2007-04-10 19:01:47 +00001008
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001009#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001010 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001011#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001012 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001013#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001014 if (PyErr_Occurred()){
1015 Py_DECREF(posobj);
1016 return NULL;
1017 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001018
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001019 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04001020 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001021 errno = 0;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07001022#ifdef MS_WINDOWS
1023 ret = _chsize_s(fd, pos);
1024#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001025 ret = ftruncate(fd, pos);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07001026#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04001027 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001028 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001029
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001030 if (ret != 0) {
1031 Py_DECREF(posobj);
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001032 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001033 return NULL;
1034 }
Guido van Rossuma9e20242007-03-08 00:43:48 +00001035
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001036 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001037}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001038#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +00001039
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001040static const char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001041mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +00001042{
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001043 if (self->created) {
1044 if (self->readable)
1045 return "xb+";
1046 else
1047 return "xb";
1048 }
Antoine Pitroue93b63b2013-09-04 20:46:33 +02001049 if (self->appending) {
1050 if (self->readable)
1051 return "ab+";
1052 else
1053 return "ab";
1054 }
1055 else if (self->readable) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001056 if (self->writable)
1057 return "rb+";
1058 else
1059 return "rb";
1060 }
1061 else
1062 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +00001063}
Guido van Rossuma9e20242007-03-08 00:43:48 +00001064
1065static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001066fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001067{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001068 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001069
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001070 if (self->fd < 0)
1071 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +00001072
Martin v. Löwis767046a2011-10-14 15:35:36 +02001073 nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001074 if (nameobj == NULL) {
1075 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1076 PyErr_Clear();
1077 else
1078 return NULL;
Robert Collins933430a2014-10-18 13:32:43 +13001079 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +02001080 "<_io.FileIO fd=%d mode='%s' closefd=%s>",
1081 self->fd, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001082 }
1083 else {
Serhiy Storchakaa5af6e12017-03-19 19:25:29 +02001084 int status = Py_ReprEnter((PyObject *)self);
1085 res = NULL;
1086 if (status == 0) {
1087 res = PyUnicode_FromFormat(
1088 "<_io.FileIO name=%R mode='%s' closefd=%s>",
1089 nameobj, mode_string(self), self->closefd ? "True" : "False");
1090 Py_ReprLeave((PyObject *)self);
1091 }
1092 else if (status > 0) {
1093 PyErr_Format(PyExc_RuntimeError,
1094 "reentrant call inside %s.__repr__",
1095 Py_TYPE(self)->tp_name);
1096 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001097 Py_DECREF(nameobj);
1098 }
1099 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001100}
1101
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001102/*[clinic input]
1103_io.FileIO.isatty
1104
1105True if the file is connected to a TTY device.
1106[clinic start generated code]*/
1107
Guido van Rossuma9e20242007-03-08 00:43:48 +00001108static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001109_io_FileIO_isatty_impl(fileio *self)
1110/*[clinic end generated code: output=932c39924e9a8070 input=cd94ca1f5e95e843]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +00001111{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001112 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +00001113
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001114 if (self->fd < 0)
1115 return err_closed();
1116 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001117 _Py_BEGIN_SUPPRESS_IPH
Steve Dower940f33a2016-09-08 11:21:54 -07001118 res = isatty(self->fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001119 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001120 Py_END_ALLOW_THREADS
1121 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001122}
1123
Antoine Pitrou243757e2010-11-05 21:15:39 +00001124static PyObject *
1125fileio_getstate(fileio *self)
1126{
1127 PyErr_Format(PyExc_TypeError,
1128 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
1129 return NULL;
1130}
1131
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001132#include "clinic/fileio.c.h"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001133
1134static PyMethodDef fileio_methods[] = {
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001135 _IO_FILEIO_READ_METHODDEF
1136 _IO_FILEIO_READALL_METHODDEF
1137 _IO_FILEIO_READINTO_METHODDEF
1138 _IO_FILEIO_WRITE_METHODDEF
1139 _IO_FILEIO_SEEK_METHODDEF
1140 _IO_FILEIO_TELL_METHODDEF
1141 _IO_FILEIO_TRUNCATE_METHODDEF
1142 _IO_FILEIO_CLOSE_METHODDEF
1143 _IO_FILEIO_SEEKABLE_METHODDEF
1144 _IO_FILEIO_READABLE_METHODDEF
1145 _IO_FILEIO_WRITABLE_METHODDEF
1146 _IO_FILEIO_FILENO_METHODDEF
1147 _IO_FILEIO_ISATTY_METHODDEF
Antoine Pitroue033e062010-10-29 10:38:18 +00001148 {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
Antoine Pitrou243757e2010-11-05 21:15:39 +00001149 {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001150 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001151};
1152
Guido van Rossum53807da2007-04-10 19:01:47 +00001153/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1154
Guido van Rossumb0428152007-04-08 17:44:42 +00001155static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001156get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001157{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001158 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001159}
1160
1161static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001162get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001163{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001164 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001165}
1166
1167static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001168get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001169{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001170 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001171}
1172
1173static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001174 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1175 {"closefd", (getter)get_closefd, NULL,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001176 "True if the file descriptor will be closed by close()."},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001177 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1178 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001179};
1180
Antoine Pitrou796564c2013-07-30 19:59:21 +02001181static PyMemberDef fileio_members[] = {
Antoine Pitroude687222014-06-29 20:07:28 -04001182 {"_blksize", T_UINT, offsetof(fileio, blksize), 0},
Antoine Pitrou796564c2013-07-30 19:59:21 +02001183 {"_finalizing", T_BOOL, offsetof(fileio, finalizing), 0},
1184 {NULL}
1185};
1186
Guido van Rossuma9e20242007-03-08 00:43:48 +00001187PyTypeObject PyFileIO_Type = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001188 PyVarObject_HEAD_INIT(NULL, 0)
1189 "_io.FileIO",
1190 sizeof(fileio),
1191 0,
1192 (destructor)fileio_dealloc, /* tp_dealloc */
1193 0, /* tp_print */
1194 0, /* tp_getattr */
1195 0, /* tp_setattr */
1196 0, /* tp_reserved */
1197 (reprfunc)fileio_repr, /* tp_repr */
1198 0, /* tp_as_number */
1199 0, /* tp_as_sequence */
1200 0, /* tp_as_mapping */
1201 0, /* tp_hash */
1202 0, /* tp_call */
1203 0, /* tp_str */
1204 PyObject_GenericGetAttr, /* tp_getattro */
1205 0, /* tp_setattro */
1206 0, /* tp_as_buffer */
1207 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +02001208 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001209 _io_FileIO___init____doc__, /* tp_doc */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001210 (traverseproc)fileio_traverse, /* tp_traverse */
1211 (inquiry)fileio_clear, /* tp_clear */
1212 0, /* tp_richcompare */
1213 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1214 0, /* tp_iter */
1215 0, /* tp_iternext */
1216 fileio_methods, /* tp_methods */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001217 fileio_members, /* tp_members */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001218 fileio_getsetlist, /* tp_getset */
1219 0, /* tp_base */
1220 0, /* tp_dict */
1221 0, /* tp_descr_get */
1222 0, /* tp_descr_set */
1223 offsetof(fileio, dict), /* tp_dictoffset */
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001224 _io_FileIO___init__, /* tp_init */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001225 PyType_GenericAlloc, /* tp_alloc */
1226 fileio_new, /* tp_new */
1227 PyObject_GC_Del, /* tp_free */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001228 0, /* tp_is_gc */
1229 0, /* tp_bases */
1230 0, /* tp_mro */
1231 0, /* tp_cache */
1232 0, /* tp_subclasses */
1233 0, /* tp_weaklist */
1234 0, /* tp_del */
1235 0, /* tp_version_tag */
1236 0, /* tp_finalize */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001237};