blob: b6755b851dca8ed0c4d9a3e1dc7978ddb7793dcc [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))
Victor Stinner8c663fd2017-11-08 14:44:44 -080044#error "unreasonable BUFSIZ > 64 MiB defined"
Christian Heimesa872de52008-12-05 08:26:55 +000045#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
Steve Dowereacee982017-02-04 14:38:11 -0800274 if (!PyUnicode_FSDecoder(nameobj, &stringobj)) {
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300275 return -1;
276 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300277 widename = PyUnicode_AsUnicode(stringobj);
Steve Dowereacee982017-02-04 14:38:11 -0800278 if (widename == NULL)
279 return -1;
280#else
Antoine Pitrou13348842012-01-29 18:36:34 +0100281 if (!PyUnicode_FSConverter(nameobj, &stringobj)) {
282 return -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000283 }
Antoine Pitrou13348842012-01-29 18:36:34 +0100284 name = PyBytes_AS_STRING(stringobj);
Steve Dowereacee982017-02-04 14:38:11 -0800285#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000286 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000287
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000288 s = mode;
289 while (*s) {
290 switch (*s++) {
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100291 case 'x':
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000292 if (rwa) {
293 bad_mode:
294 PyErr_SetString(PyExc_ValueError,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100295 "Must have exactly one of create/read/write/append "
Georg Brandl28928ae2010-10-21 13:45:52 +0000296 "mode and at most one plus");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000297 goto error;
298 }
299 rwa = 1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100300 self->created = 1;
301 self->writable = 1;
302 flags |= O_EXCL | O_CREAT;
303 break;
304 case 'r':
305 if (rwa)
306 goto bad_mode;
307 rwa = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000308 self->readable = 1;
309 break;
310 case 'w':
311 if (rwa)
312 goto bad_mode;
313 rwa = 1;
314 self->writable = 1;
315 flags |= O_CREAT | O_TRUNC;
316 break;
317 case 'a':
318 if (rwa)
319 goto bad_mode;
320 rwa = 1;
321 self->writable = 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200322 self->appending = 1;
323 flags |= O_APPEND | O_CREAT;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000324 break;
325 case 'b':
326 break;
327 case '+':
328 if (plus)
329 goto bad_mode;
330 self->readable = self->writable = 1;
331 plus = 1;
332 break;
333 default:
334 PyErr_Format(PyExc_ValueError,
335 "invalid mode: %.200s", mode);
336 goto error;
337 }
338 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000339
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000340 if (!rwa)
341 goto bad_mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000342
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000343 if (self->readable && self->writable)
344 flags |= O_RDWR;
345 else if (self->readable)
346 flags |= O_RDONLY;
347 else
348 flags |= O_WRONLY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000349
350#ifdef O_BINARY
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000351 flags |= O_BINARY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000352#endif
353
Victor Stinnerdaf45552013-08-28 00:53:59 +0200354#ifdef MS_WINDOWS
355 flags |= O_NOINHERIT;
356#elif defined(O_CLOEXEC)
357 flags |= O_CLOEXEC;
358#endif
Walter Dörwald0e411482007-06-06 16:55:38 +0000359
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000360 if (fd >= 0) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000361 self->fd = fd;
362 self->closefd = closefd;
363 }
364 else {
365 self->closefd = 1;
366 if (!closefd) {
367 PyErr_SetString(PyExc_ValueError,
368 "Cannot use closefd=False with file name");
369 goto error;
370 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000371
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000372 errno = 0;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200373 if (opener == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000374 do {
375 Py_BEGIN_ALLOW_THREADS
Thomas Helleraf2be262007-07-12 11:03:13 +0000376#ifdef MS_WINDOWS
Steve Dowereacee982017-02-04 14:38:11 -0800377 self->fd = _wopen(widename, flags, 0666);
378#else
379 self->fd = open(name, flags, 0666);
Thomas Helleraf2be262007-07-12 11:03:13 +0000380#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000381 Py_END_ALLOW_THREADS
382 } while (self->fd < 0 && errno == EINTR &&
383 !(async_err = PyErr_CheckSignals()));
Victor Stinner9672da72015-03-04 18:40:10 +0100384
385 if (async_err)
386 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200387 }
388 else {
389 PyObject *fdobj;
390
391#ifndef MS_WINDOWS
392 /* the opener may clear the atomic flag */
393 atomic_flag_works = NULL;
394#endif
395
396 fdobj = PyObject_CallFunction(opener, "Oi", nameobj, flags);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200397 if (fdobj == NULL)
398 goto error;
399 if (!PyLong_Check(fdobj)) {
400 Py_DECREF(fdobj);
401 PyErr_SetString(PyExc_TypeError,
402 "expected integer from opener");
403 goto error;
404 }
405
Serhiy Storchaka78980432013-01-15 01:12:17 +0200406 self->fd = _PyLong_AsInt(fdobj);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200407 Py_DECREF(fdobj);
Barry Warsaw480e2852016-06-08 17:47:26 -0400408 if (self->fd < 0) {
409 if (!PyErr_Occurred()) {
Barry Warsaw118598a2016-06-08 17:54:43 -0400410 /* The opener returned a negative but didn't set an
411 exception. See issue #27066 */
Barry Warsaw480e2852016-06-08 17:47:26 -0400412 PyErr_Format(PyExc_ValueError,
413 "opener returned %d", self->fd);
414 }
Ross Lagerwall59142db2011-10-31 20:34:46 +0200415 goto error;
416 }
417 }
418
Hynek Schlawack7f59fd72012-06-22 09:32:22 +0200419 fd_is_own = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000420 if (self->fd < 0) {
Victor Stinner9672da72015-03-04 18:40:10 +0100421 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000422 goto error;
423 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200424
425#ifndef MS_WINDOWS
426 if (_Py_set_inheritable(self->fd, 0, atomic_flag_works) < 0)
427 goto error;
428#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000429 }
Antoine Pitroude687222014-06-29 20:07:28 -0400430
431 self->blksize = DEFAULT_BUFFER_SIZE;
Martin Panter0bb62b12015-12-06 03:15:05 +0000432 Py_BEGIN_ALLOW_THREADS
433 fstat_result = _Py_fstat_noraise(self->fd, &fdfstat);
434 Py_END_ALLOW_THREADS
435 if (fstat_result < 0) {
Martin Panter49d3db92015-12-06 11:12:15 +0000436 /* Tolerate fstat() errors other than EBADF. See Issue #25717, where
437 an anonymous file on a Virtual Box shared folder filesystem would
438 raise ENOENT. */
Martin Panter0bb62b12015-12-06 03:15:05 +0000439#ifdef MS_WINDOWS
440 if (GetLastError() == ERROR_INVALID_HANDLE) {
441 PyErr_SetFromWindowsErr(0);
442#else
443 if (errno == EBADF) {
444 PyErr_SetFromErrno(PyExc_OSError);
445#endif
446 goto error;
447 }
Antoine Pitroude687222014-06-29 20:07:28 -0400448 }
Martin Panter0bb62b12015-12-06 03:15:05 +0000449 else {
450#if defined(S_ISDIR) && defined(EISDIR)
451 /* On Unix, open will succeed for directories.
452 In Python, there should be no file objects referring to
453 directories, so we need a check. */
454 if (S_ISDIR(fdfstat.st_mode)) {
455 errno = EISDIR;
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300456 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
Martin Panter0bb62b12015-12-06 03:15:05 +0000457 goto error;
458 }
Antoine Pitroude687222014-06-29 20:07:28 -0400459#endif /* defined(S_ISDIR) */
460#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin Panter0bb62b12015-12-06 03:15:05 +0000461 if (fdfstat.st_blksize > 1)
462 self->blksize = fdfstat.st_blksize;
Antoine Pitroude687222014-06-29 20:07:28 -0400463#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Martin Panter0bb62b12015-12-06 03:15:05 +0000464 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000465
Victor Stinner89e34362011-01-07 18:47:22 +0000466#if defined(MS_WINDOWS) || defined(__CYGWIN__)
467 /* don't translate newlines (\r\n <=> \n) */
468 _setmode(self->fd, O_BINARY);
469#endif
470
Victor Stinnerd9d04192013-11-06 23:50:10 +0100471 if (_PyObject_SetAttrId((PyObject *)self, &PyId_name, nameobj) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000472 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000473
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200474 if (self->appending) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000475 /* For consistent behaviour, we explicitly seek to the
476 end of file (otherwise, it might be done only on the
477 first write()). */
Victor Stinner99970732017-05-02 15:10:39 +0200478 PyObject *pos = portable_lseek(self, NULL, 2);
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200479 if (pos == NULL)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000480 goto error;
481 Py_DECREF(pos);
482 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000483
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000484 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000485
486 error:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000487 ret = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200488 if (!fd_is_own)
489 self->fd = -1;
Benjamin Petersonbbb04122010-10-30 23:16:28 +0000490 if (self->fd >= 0)
491 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000492
Guido van Rossuma9e20242007-03-08 00:43:48 +0000493 done:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000494 Py_CLEAR(stringobj);
495 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000496}
497
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000498static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000499fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000500{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000501 Py_VISIT(self->dict);
502 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000503}
504
505static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000506fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000507{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000508 Py_CLEAR(self->dict);
509 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000510}
511
Guido van Rossuma9e20242007-03-08 00:43:48 +0000512static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000513fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000514{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200515 self->finalizing = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000516 if (_PyIOBase_finalize((PyObject *) self) < 0)
517 return;
518 _PyObject_GC_UNTRACK(self);
519 if (self->weakreflist != NULL)
520 PyObject_ClearWeakRefs((PyObject *) self);
521 Py_CLEAR(self->dict);
522 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000523}
524
525static PyObject *
526err_closed(void)
527{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000528 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
529 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000530}
531
532static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200533err_mode(const char *action)
Guido van Rossum53807da2007-04-10 19:01:47 +0000534{
Antoine Pitrou712cb732013-12-21 15:51:54 +0100535 _PyIO_State *state = IO_STATE();
536 if (state != NULL)
537 PyErr_Format(state->unsupported_operation,
538 "File not open for %s", action);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000539 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000540}
541
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300542/*[clinic input]
543_io.FileIO.fileno
544
545Return the underlying file descriptor (an integer).
546[clinic start generated code]*/
547
Guido van Rossum53807da2007-04-10 19:01:47 +0000548static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300549_io_FileIO_fileno_impl(fileio *self)
550/*[clinic end generated code: output=a9626ce5398ece90 input=0b9b2de67335ada3]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000551{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000552 if (self->fd < 0)
553 return err_closed();
554 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000555}
556
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300557/*[clinic input]
558_io.FileIO.readable
559
560True if file was opened in a read mode.
561[clinic start generated code]*/
562
Guido van Rossuma9e20242007-03-08 00:43:48 +0000563static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300564_io_FileIO_readable_impl(fileio *self)
565/*[clinic end generated code: output=640744a6150fe9ba input=a3fdfed6eea721c5]*/
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 PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000570}
571
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300572/*[clinic input]
573_io.FileIO.writable
574
575True if file was opened in a write 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_writable_impl(fileio *self)
580/*[clinic end generated code: output=96cefc5446e89977 input=c204a808ca2e1748]*/
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->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000585}
586
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300587/*[clinic input]
588_io.FileIO.seekable
589
590True if file supports random-access.
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_seekable_impl(fileio *self)
595/*[clinic end generated code: output=47909ca0a42e9287 input=c8e5554d2fd63c7f]*/
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 if (self->seekable < 0) {
Victor Stinner99970732017-05-02 15:10:39 +0200600 /* portable_lseek() sets the seekable attribute */
601 PyObject *pos = portable_lseek(self, NULL, SEEK_CUR);
602 assert(self->seekable >= 0);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000603 if (pos == NULL) {
604 PyErr_Clear();
Victor Stinner99970732017-05-02 15:10:39 +0200605 }
606 else {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000607 Py_DECREF(pos);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000608 }
609 }
610 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000611}
612
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300613/*[clinic input]
614_io.FileIO.readinto
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700615 buffer: Py_buffer(accept={rwbuffer})
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300616 /
617
618Same as RawIOBase.readinto().
619[clinic start generated code]*/
620
Guido van Rossuma9e20242007-03-08 00:43:48 +0000621static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300622_io_FileIO_readinto_impl(fileio *self, Py_buffer *buffer)
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700623/*[clinic end generated code: output=b01a5a22c8415cb4 input=4721d7b68b154eaf]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000624{
Victor Stinner66aab0c2015-03-19 22:53:20 +0100625 Py_ssize_t n;
626 int err;
Guido van Rossum53807da2007-04-10 19:01:47 +0000627
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000628 if (self->fd < 0)
629 return err_closed();
630 if (!self->readable)
631 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000632
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300633 n = _Py_read(self->fd, buffer->buf, buffer->len);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100634 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100635 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100636
637 if (n == -1) {
638 if (err == EAGAIN) {
639 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000640 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100641 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000642 return NULL;
643 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000644
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000645 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000646}
647
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000648static size_t
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100649new_buffersize(fileio *self, size_t currentsize)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000650{
Antoine Pitroua3f44572012-04-17 13:50:58 +0200651 size_t addend;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100652
Nadeem Vawdad41a98b2011-10-13 13:34:16 +0200653 /* Expand the buffer by an amount proportional to the current size,
Antoine Pitroua3f44572012-04-17 13:50:58 +0200654 giving us amortized linear-time behavior. For bigger sizes, use a
655 less-than-double growth factor to avoid excessive allocation. */
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100656 assert(currentsize <= PY_SSIZE_T_MAX);
Antoine Pitroua3f44572012-04-17 13:50:58 +0200657 if (currentsize > 65536)
658 addend = currentsize >> 3;
659 else
660 addend = 256 + currentsize;
661 if (addend < SMALLCHUNK)
662 /* Avoid tiny read() calls. */
663 addend = SMALLCHUNK;
664 return addend + currentsize;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000665}
666
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300667/*[clinic input]
668_io.FileIO.readall
669
670Read all data from the file, returned as bytes.
671
672In non-blocking mode, returns as much as is immediately available,
673or None if no data is available. Return an empty bytes object at EOF.
674[clinic start generated code]*/
675
Guido van Rossum7165cb12007-07-10 06:54:34 +0000676static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300677_io_FileIO_readall_impl(fileio *self)
678/*[clinic end generated code: output=faa0292b213b4022 input=dbdc137f55602834]*/
Guido van Rossum7165cb12007-07-10 06:54:34 +0000679{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200680 struct _Py_stat_struct status;
Victor Stinnera2a64772011-10-11 22:45:02 +0200681 Py_off_t pos, end;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000682 PyObject *result;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100683 Py_ssize_t bytes_read = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100684 Py_ssize_t n;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100685 size_t bufsize;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000686
Victor Stinnerb79f28c2011-05-25 22:09:03 +0200687 if (self->fd < 0)
688 return err_closed();
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000689
Steve Dower8fc89802015-04-12 00:26:27 -0400690 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +0200691#ifdef MS_WINDOWS
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200692 pos = _lseeki64(self->fd, 0L, SEEK_CUR);
693#else
694 pos = lseek(self->fd, 0L, SEEK_CUR);
695#endif
Steve Dower8fc89802015-04-12 00:26:27 -0400696 _Py_END_SUPPRESS_IPH
697
Victor Stinnere134a7f2015-03-30 10:09:31 +0200698 if (_Py_fstat_noraise(self->fd, &status) == 0)
699 end = status.st_size;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200700 else
Victor Stinnera2a64772011-10-11 22:45:02 +0200701 end = (Py_off_t)-1;
Christian Heimesa872de52008-12-05 08:26:55 +0000702
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100703 if (end > 0 && end >= pos && pos >= 0 && end - pos < PY_SSIZE_T_MAX) {
704 /* This is probably a real file, so we try to allocate a
705 buffer one byte larger than the rest of the file. If the
706 calculation is right then we should get EOF without having
707 to enlarge the buffer. */
708 bufsize = (size_t)(end - pos + 1);
709 } else {
710 bufsize = SMALLCHUNK;
711 }
712
713 result = PyBytes_FromStringAndSize(NULL, bufsize);
714 if (result == NULL)
715 return NULL;
716
717 while (1) {
718 if (bytes_read >= (Py_ssize_t)bufsize) {
719 bufsize = new_buffersize(self, bytes_read);
720 if (bufsize > PY_SSIZE_T_MAX || bufsize <= 0) {
721 PyErr_SetString(PyExc_OverflowError,
722 "unbounded read returned more bytes "
Serhiy Storchakab817b772015-04-10 02:18:44 +0300723 "than a Python bytes object can hold");
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100724 Py_DECREF(result);
725 return NULL;
726 }
727
728 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
729 if (_PyBytes_Resize(&result, bufsize) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000730 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000731 }
732 }
Victor Stinner9672da72015-03-04 18:40:10 +0100733
Victor Stinner66aab0c2015-03-19 22:53:20 +0100734 n = _Py_read(self->fd,
735 PyBytes_AS_STRING(result) + bytes_read,
736 bufsize - bytes_read);
Victor Stinner9672da72015-03-04 18:40:10 +0100737
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000738 if (n == 0)
739 break;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100740 if (n == -1) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000741 if (errno == EAGAIN) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100742 PyErr_Clear();
Victor Stinnere10920f2014-07-02 22:59:31 +0200743 if (bytes_read > 0)
744 break;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000745 Py_DECREF(result);
746 Py_RETURN_NONE;
747 }
748 Py_DECREF(result);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000749 return NULL;
750 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100751 bytes_read += n;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200752 pos += n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000753 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000754
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100755 if (PyBytes_GET_SIZE(result) > bytes_read) {
756 if (_PyBytes_Resize(&result, bytes_read) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000757 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000758 }
759 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000760}
761
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300762/*[clinic input]
763_io.FileIO.read
Serhiy Storchaka762bf402017-03-30 09:15:31 +0300764 size: Py_ssize_t(accept={int, NoneType}) = -1
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300765 /
766
767Read at most size bytes, returned as bytes.
768
769Only makes one system call, so less data may be returned than requested.
770In non-blocking mode, returns None if no data is available.
771Return an empty bytes object at EOF.
772[clinic start generated code]*/
773
Guido van Rossuma9e20242007-03-08 00:43:48 +0000774static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300775_io_FileIO_read_impl(fileio *self, Py_ssize_t size)
Serhiy Storchaka762bf402017-03-30 09:15:31 +0300776/*[clinic end generated code: output=42528d39dd0ca641 input=bec9a2c704ddcbc9]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000777{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000778 char *ptr;
779 Py_ssize_t n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000780 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000781
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000782 if (self->fd < 0)
783 return err_closed();
784 if (!self->readable)
785 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000786
Victor Stinner66aab0c2015-03-19 22:53:20 +0100787 if (size < 0)
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300788 return _io_FileIO_readall_impl(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000789
Victor Stinner14b9b112013-06-25 00:37:25 +0200790#ifdef MS_WINDOWS
Victor Stinner66aab0c2015-03-19 22:53:20 +0100791 /* On Windows, the count parameter of read() is an int */
Victor Stinnerc655a722011-07-05 11:31:49 +0200792 if (size > INT_MAX)
793 size = INT_MAX;
794#endif
Victor Stinner66aab0c2015-03-19 22:53:20 +0100795
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000796 bytes = PyBytes_FromStringAndSize(NULL, size);
797 if (bytes == NULL)
798 return NULL;
799 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000800
Victor Stinner66aab0c2015-03-19 22:53:20 +0100801 n = _Py_read(self->fd, ptr, size);
802 if (n == -1) {
803 /* copy errno because Py_DECREF() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100804 int err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000805 Py_DECREF(bytes);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100806 if (err == EAGAIN) {
807 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000808 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100809 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000810 return NULL;
811 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000812
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000813 if (n != size) {
814 if (_PyBytes_Resize(&bytes, n) < 0) {
Victor Stinner85c761d2013-07-16 21:36:02 +0200815 Py_CLEAR(bytes);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000816 return NULL;
817 }
818 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000819
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000820 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000821}
822
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300823/*[clinic input]
824_io.FileIO.write
825 b: Py_buffer
826 /
827
Martin Panter6bb91f32016-05-28 00:41:57 +0000828Write buffer b to file, return number of bytes written.
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300829
830Only makes one system call, so not all of the data may be written.
831The number of bytes actually written is returned. In non-blocking mode,
832returns None if the write would block.
833[clinic start generated code]*/
834
Guido van Rossuma9e20242007-03-08 00:43:48 +0000835static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300836_io_FileIO_write_impl(fileio *self, Py_buffer *b)
Martin Panter6bb91f32016-05-28 00:41:57 +0000837/*[clinic end generated code: output=b4059db3d363a2f7 input=6e7908b36f0ce74f]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000838{
Victor Stinner66aab0c2015-03-19 22:53:20 +0100839 Py_ssize_t n;
840 int err;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000841
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000842 if (self->fd < 0)
843 return err_closed();
844 if (!self->writable)
845 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000846
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300847 n = _Py_write(self->fd, b->buf, b->len);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100848 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100849 err = errno;
Martin v. Löwis423be952008-08-13 15:53:07 +0000850
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000851 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100852 if (err == EAGAIN) {
853 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000854 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100855 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000856 return NULL;
857 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000858
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000859 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000860}
861
Guido van Rossum53807da2007-04-10 19:01:47 +0000862/* XXX Windows support below is likely incomplete */
863
Guido van Rossum53807da2007-04-10 19:01:47 +0000864/* Cribbed from posix_lseek() */
865static PyObject *
Victor Stinner99970732017-05-02 15:10:39 +0200866portable_lseek(fileio *self, PyObject *posobj, int whence)
Guido van Rossum53807da2007-04-10 19:01:47 +0000867{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000868 Py_off_t pos, res;
Victor Stinner99970732017-05-02 15:10:39 +0200869 int fd = self->fd;
Guido van Rossum53807da2007-04-10 19:01:47 +0000870
871#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000872 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
873 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000874#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000875 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000876#endif
877#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000878 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000879#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000880#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000881 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000882#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000883 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000884#endif /* SEEK_SET */
885
Victor Stinner99970732017-05-02 15:10:39 +0200886 if (posobj == NULL) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000887 pos = 0;
Victor Stinner99970732017-05-02 15:10:39 +0200888 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000889 else {
890 if(PyFloat_Check(posobj)) {
891 PyErr_SetString(PyExc_TypeError, "an integer is required");
892 return NULL;
893 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000894#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000895 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000896#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000897 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000898#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000899 if (PyErr_Occurred())
900 return NULL;
901 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000902
Steve Dower940f33a2016-09-08 11:21:54 -0700903 Py_BEGIN_ALLOW_THREADS
904 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +0200905#ifdef MS_WINDOWS
Steve Dower940f33a2016-09-08 11:21:54 -0700906 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000907#else
Steve Dower940f33a2016-09-08 11:21:54 -0700908 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000909#endif
Steve Dower940f33a2016-09-08 11:21:54 -0700910 _Py_END_SUPPRESS_IPH
911 Py_END_ALLOW_THREADS
Victor Stinner99970732017-05-02 15:10:39 +0200912
913 if (self->seekable < 0) {
914 self->seekable = (res >= 0);
915 }
916
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000917 if (res < 0)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300918 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000919
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000920#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000921 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000922#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000923 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000924#endif
925}
926
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300927/*[clinic input]
928_io.FileIO.seek
929 pos: object
930 whence: int = 0
931 /
Guido van Rossuma9e20242007-03-08 00:43:48 +0000932
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300933Move to new file position and return the file position.
934
935Argument offset is a byte count. Optional argument whence defaults to
936SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values
937are SEEK_CUR or 1 (move relative to current position, positive or negative),
938and SEEK_END or 2 (move relative to end of file, usually negative, although
939many platforms allow seeking beyond the end of a file).
940
941Note that not all file objects are seekable.
942[clinic start generated code]*/
943
944static PyObject *
945_io_FileIO_seek_impl(fileio *self, PyObject *pos, int whence)
946/*[clinic end generated code: output=c976acdf054e6655 input=0439194b0774d454]*/
947{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000948 if (self->fd < 0)
949 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000950
Victor Stinner99970732017-05-02 15:10:39 +0200951 return portable_lseek(self, pos, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000952}
953
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300954/*[clinic input]
955_io.FileIO.tell
956
957Current file position.
958
959Can raise OSError for non seekable files.
960[clinic start generated code]*/
961
Guido van Rossuma9e20242007-03-08 00:43:48 +0000962static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300963_io_FileIO_tell_impl(fileio *self)
964/*[clinic end generated code: output=ffe2147058809d0b input=807e24ead4cec2f9]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000965{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000966 if (self->fd < 0)
967 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000968
Victor Stinner99970732017-05-02 15:10:39 +0200969 return portable_lseek(self, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000970}
971
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000972#ifdef HAVE_FTRUNCATE
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300973/*[clinic input]
974_io.FileIO.truncate
975 size as posobj: object = NULL
976 /
977
978Truncate the file to at most size bytes and return the truncated size.
979
980Size defaults to the current file position, as returned by tell().
981The current file position is changed to the value of size.
982[clinic start generated code]*/
983
Guido van Rossuma9e20242007-03-08 00:43:48 +0000984static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300985_io_FileIO_truncate_impl(fileio *self, PyObject *posobj)
986/*[clinic end generated code: output=e49ca7a916c176fa input=9026af44686b7318]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000987{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000988 Py_off_t pos;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000989 int ret;
990 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000991
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000992 fd = self->fd;
993 if (fd < 0)
994 return err_closed();
995 if (!self->writable)
996 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000997
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000998 if (posobj == Py_None || posobj == NULL) {
999 /* Get the current position. */
Victor Stinner99970732017-05-02 15:10:39 +02001000 posobj = portable_lseek(self, NULL, 1);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001001 if (posobj == NULL)
1002 return NULL;
1003 }
1004 else {
1005 Py_INCREF(posobj);
1006 }
Guido van Rossum53807da2007-04-10 19:01:47 +00001007
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001008#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001009 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001010#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001011 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001012#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001013 if (PyErr_Occurred()){
1014 Py_DECREF(posobj);
1015 return NULL;
1016 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001017
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001018 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04001019 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001020 errno = 0;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07001021#ifdef MS_WINDOWS
1022 ret = _chsize_s(fd, pos);
1023#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001024 ret = ftruncate(fd, pos);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07001025#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04001026 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001027 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001028
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001029 if (ret != 0) {
1030 Py_DECREF(posobj);
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001031 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001032 return NULL;
1033 }
Guido van Rossuma9e20242007-03-08 00:43:48 +00001034
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001035 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001036}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001037#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +00001038
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001039static const char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001040mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +00001041{
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001042 if (self->created) {
1043 if (self->readable)
1044 return "xb+";
1045 else
1046 return "xb";
1047 }
Antoine Pitroue93b63b2013-09-04 20:46:33 +02001048 if (self->appending) {
1049 if (self->readable)
1050 return "ab+";
1051 else
1052 return "ab";
1053 }
1054 else if (self->readable) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001055 if (self->writable)
1056 return "rb+";
1057 else
1058 return "rb";
1059 }
1060 else
1061 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +00001062}
Guido van Rossuma9e20242007-03-08 00:43:48 +00001063
1064static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001065fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001066{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001067 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001068
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001069 if (self->fd < 0)
1070 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +00001071
Martin v. Löwis767046a2011-10-14 15:35:36 +02001072 nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001073 if (nameobj == NULL) {
1074 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1075 PyErr_Clear();
1076 else
1077 return NULL;
Robert Collins933430a2014-10-18 13:32:43 +13001078 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +02001079 "<_io.FileIO fd=%d mode='%s' closefd=%s>",
1080 self->fd, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001081 }
1082 else {
Serhiy Storchakaa5af6e12017-03-19 19:25:29 +02001083 int status = Py_ReprEnter((PyObject *)self);
1084 res = NULL;
1085 if (status == 0) {
1086 res = PyUnicode_FromFormat(
1087 "<_io.FileIO name=%R mode='%s' closefd=%s>",
1088 nameobj, mode_string(self), self->closefd ? "True" : "False");
1089 Py_ReprLeave((PyObject *)self);
1090 }
1091 else if (status > 0) {
1092 PyErr_Format(PyExc_RuntimeError,
1093 "reentrant call inside %s.__repr__",
1094 Py_TYPE(self)->tp_name);
1095 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001096 Py_DECREF(nameobj);
1097 }
1098 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001099}
1100
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001101/*[clinic input]
1102_io.FileIO.isatty
1103
1104True if the file is connected to a TTY device.
1105[clinic start generated code]*/
1106
Guido van Rossuma9e20242007-03-08 00:43:48 +00001107static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001108_io_FileIO_isatty_impl(fileio *self)
1109/*[clinic end generated code: output=932c39924e9a8070 input=cd94ca1f5e95e843]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +00001110{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001111 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +00001112
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001113 if (self->fd < 0)
1114 return err_closed();
1115 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001116 _Py_BEGIN_SUPPRESS_IPH
Steve Dower940f33a2016-09-08 11:21:54 -07001117 res = isatty(self->fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001118 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001119 Py_END_ALLOW_THREADS
1120 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001121}
1122
Antoine Pitrou243757e2010-11-05 21:15:39 +00001123static PyObject *
1124fileio_getstate(fileio *self)
1125{
1126 PyErr_Format(PyExc_TypeError,
1127 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
1128 return NULL;
1129}
1130
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001131#include "clinic/fileio.c.h"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001132
1133static PyMethodDef fileio_methods[] = {
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001134 _IO_FILEIO_READ_METHODDEF
1135 _IO_FILEIO_READALL_METHODDEF
1136 _IO_FILEIO_READINTO_METHODDEF
1137 _IO_FILEIO_WRITE_METHODDEF
1138 _IO_FILEIO_SEEK_METHODDEF
1139 _IO_FILEIO_TELL_METHODDEF
1140 _IO_FILEIO_TRUNCATE_METHODDEF
1141 _IO_FILEIO_CLOSE_METHODDEF
1142 _IO_FILEIO_SEEKABLE_METHODDEF
1143 _IO_FILEIO_READABLE_METHODDEF
1144 _IO_FILEIO_WRITABLE_METHODDEF
1145 _IO_FILEIO_FILENO_METHODDEF
1146 _IO_FILEIO_ISATTY_METHODDEF
Antoine Pitroue033e062010-10-29 10:38:18 +00001147 {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
Antoine Pitrou243757e2010-11-05 21:15:39 +00001148 {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001149 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001150};
1151
Guido van Rossum53807da2007-04-10 19:01:47 +00001152/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1153
Guido van Rossumb0428152007-04-08 17:44:42 +00001154static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001155get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001156{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001157 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001158}
1159
1160static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001161get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001162{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001163 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001164}
1165
1166static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001167get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001168{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001169 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001170}
1171
1172static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001173 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1174 {"closefd", (getter)get_closefd, NULL,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001175 "True if the file descriptor will be closed by close()."},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001176 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1177 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001178};
1179
Antoine Pitrou796564c2013-07-30 19:59:21 +02001180static PyMemberDef fileio_members[] = {
Antoine Pitroude687222014-06-29 20:07:28 -04001181 {"_blksize", T_UINT, offsetof(fileio, blksize), 0},
Antoine Pitrou796564c2013-07-30 19:59:21 +02001182 {"_finalizing", T_BOOL, offsetof(fileio, finalizing), 0},
1183 {NULL}
1184};
1185
Guido van Rossuma9e20242007-03-08 00:43:48 +00001186PyTypeObject PyFileIO_Type = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001187 PyVarObject_HEAD_INIT(NULL, 0)
1188 "_io.FileIO",
1189 sizeof(fileio),
1190 0,
1191 (destructor)fileio_dealloc, /* tp_dealloc */
1192 0, /* tp_print */
1193 0, /* tp_getattr */
1194 0, /* tp_setattr */
1195 0, /* tp_reserved */
1196 (reprfunc)fileio_repr, /* tp_repr */
1197 0, /* tp_as_number */
1198 0, /* tp_as_sequence */
1199 0, /* tp_as_mapping */
1200 0, /* tp_hash */
1201 0, /* tp_call */
1202 0, /* tp_str */
1203 PyObject_GenericGetAttr, /* tp_getattro */
1204 0, /* tp_setattro */
1205 0, /* tp_as_buffer */
1206 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +02001207 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001208 _io_FileIO___init____doc__, /* tp_doc */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001209 (traverseproc)fileio_traverse, /* tp_traverse */
1210 (inquiry)fileio_clear, /* tp_clear */
1211 0, /* tp_richcompare */
1212 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1213 0, /* tp_iter */
1214 0, /* tp_iternext */
1215 fileio_methods, /* tp_methods */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001216 fileio_members, /* tp_members */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001217 fileio_getsetlist, /* tp_getset */
1218 0, /* tp_base */
1219 0, /* tp_dict */
1220 0, /* tp_descr_get */
1221 0, /* tp_descr_set */
1222 offsetof(fileio, dict), /* tp_dictoffset */
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001223 _io_FileIO___init__, /* tp_init */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001224 PyType_GenericAlloc, /* tp_alloc */
1225 fileio_new, /* tp_new */
1226 PyObject_GC_Del, /* tp_free */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001227 0, /* tp_is_gc */
1228 0, /* tp_bases */
1229 0, /* tp_mro */
1230 0, /* tp_cache */
1231 0, /* tp_subclasses */
1232 0, /* tp_weaklist */
1233 0, /* tp_del */
1234 0, /* tp_version_tag */
1235 0, /* tp_finalize */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001236};