blob: 44d51c9faf5d1fcacaa2a3453fdfba0f478395f8 [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;
Nir Soffer6a894812017-12-01 03:18:58 +0200686 int fstat_result;
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
Nir Soffer6a894812017-12-01 03:18:58 +0200691 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -0400692 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +0200693#ifdef MS_WINDOWS
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200694 pos = _lseeki64(self->fd, 0L, SEEK_CUR);
695#else
696 pos = lseek(self->fd, 0L, SEEK_CUR);
697#endif
Steve Dower8fc89802015-04-12 00:26:27 -0400698 _Py_END_SUPPRESS_IPH
Nir Soffer6a894812017-12-01 03:18:58 +0200699 fstat_result = _Py_fstat_noraise(self->fd, &status);
700 Py_END_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -0400701
Nir Soffer6a894812017-12-01 03:18:58 +0200702 if (fstat_result == 0)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200703 end = status.st_size;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200704 else
Victor Stinnera2a64772011-10-11 22:45:02 +0200705 end = (Py_off_t)-1;
Christian Heimesa872de52008-12-05 08:26:55 +0000706
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100707 if (end > 0 && end >= pos && pos >= 0 && end - pos < PY_SSIZE_T_MAX) {
708 /* This is probably a real file, so we try to allocate a
709 buffer one byte larger than the rest of the file. If the
710 calculation is right then we should get EOF without having
711 to enlarge the buffer. */
712 bufsize = (size_t)(end - pos + 1);
713 } else {
714 bufsize = SMALLCHUNK;
715 }
716
717 result = PyBytes_FromStringAndSize(NULL, bufsize);
718 if (result == NULL)
719 return NULL;
720
721 while (1) {
722 if (bytes_read >= (Py_ssize_t)bufsize) {
723 bufsize = new_buffersize(self, bytes_read);
724 if (bufsize > PY_SSIZE_T_MAX || bufsize <= 0) {
725 PyErr_SetString(PyExc_OverflowError,
726 "unbounded read returned more bytes "
Serhiy Storchakab817b772015-04-10 02:18:44 +0300727 "than a Python bytes object can hold");
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100728 Py_DECREF(result);
729 return NULL;
730 }
731
732 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
733 if (_PyBytes_Resize(&result, bufsize) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000734 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000735 }
736 }
Victor Stinner9672da72015-03-04 18:40:10 +0100737
Victor Stinner66aab0c2015-03-19 22:53:20 +0100738 n = _Py_read(self->fd,
739 PyBytes_AS_STRING(result) + bytes_read,
740 bufsize - bytes_read);
Victor Stinner9672da72015-03-04 18:40:10 +0100741
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000742 if (n == 0)
743 break;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100744 if (n == -1) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000745 if (errno == EAGAIN) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100746 PyErr_Clear();
Victor Stinnere10920f2014-07-02 22:59:31 +0200747 if (bytes_read > 0)
748 break;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000749 Py_DECREF(result);
750 Py_RETURN_NONE;
751 }
752 Py_DECREF(result);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000753 return NULL;
754 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100755 bytes_read += n;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200756 pos += n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000757 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000758
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100759 if (PyBytes_GET_SIZE(result) > bytes_read) {
760 if (_PyBytes_Resize(&result, bytes_read) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000761 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000762 }
763 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000764}
765
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300766/*[clinic input]
767_io.FileIO.read
Serhiy Storchaka762bf402017-03-30 09:15:31 +0300768 size: Py_ssize_t(accept={int, NoneType}) = -1
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300769 /
770
771Read at most size bytes, returned as bytes.
772
773Only makes one system call, so less data may be returned than requested.
774In non-blocking mode, returns None if no data is available.
775Return an empty bytes object at EOF.
776[clinic start generated code]*/
777
Guido van Rossuma9e20242007-03-08 00:43:48 +0000778static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300779_io_FileIO_read_impl(fileio *self, Py_ssize_t size)
Serhiy Storchaka762bf402017-03-30 09:15:31 +0300780/*[clinic end generated code: output=42528d39dd0ca641 input=bec9a2c704ddcbc9]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000781{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000782 char *ptr;
783 Py_ssize_t n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000784 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000785
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000786 if (self->fd < 0)
787 return err_closed();
788 if (!self->readable)
789 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000790
Victor Stinner66aab0c2015-03-19 22:53:20 +0100791 if (size < 0)
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300792 return _io_FileIO_readall_impl(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000793
Stéphane Wirtel74a8b6e2018-10-18 01:05:04 +0200794 if (size > _PY_READ_MAX) {
795 size = _PY_READ_MAX;
796 }
Victor Stinner66aab0c2015-03-19 22:53:20 +0100797
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000798 bytes = PyBytes_FromStringAndSize(NULL, size);
799 if (bytes == NULL)
800 return NULL;
801 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000802
Victor Stinner66aab0c2015-03-19 22:53:20 +0100803 n = _Py_read(self->fd, ptr, size);
804 if (n == -1) {
805 /* copy errno because Py_DECREF() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100806 int err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000807 Py_DECREF(bytes);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100808 if (err == EAGAIN) {
809 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000810 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100811 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000812 return NULL;
813 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000814
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000815 if (n != size) {
816 if (_PyBytes_Resize(&bytes, n) < 0) {
Victor Stinner85c761d2013-07-16 21:36:02 +0200817 Py_CLEAR(bytes);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000818 return NULL;
819 }
820 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000821
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000822 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000823}
824
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300825/*[clinic input]
826_io.FileIO.write
827 b: Py_buffer
828 /
829
Martin Panter6bb91f32016-05-28 00:41:57 +0000830Write buffer b to file, return number of bytes written.
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300831
832Only makes one system call, so not all of the data may be written.
833The number of bytes actually written is returned. In non-blocking mode,
834returns None if the write would block.
835[clinic start generated code]*/
836
Guido van Rossuma9e20242007-03-08 00:43:48 +0000837static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300838_io_FileIO_write_impl(fileio *self, Py_buffer *b)
Martin Panter6bb91f32016-05-28 00:41:57 +0000839/*[clinic end generated code: output=b4059db3d363a2f7 input=6e7908b36f0ce74f]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000840{
Victor Stinner66aab0c2015-03-19 22:53:20 +0100841 Py_ssize_t n;
842 int err;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000843
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000844 if (self->fd < 0)
845 return err_closed();
846 if (!self->writable)
847 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000848
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300849 n = _Py_write(self->fd, b->buf, b->len);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100850 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100851 err = errno;
Martin v. Löwis423be952008-08-13 15:53:07 +0000852
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000853 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100854 if (err == EAGAIN) {
855 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000856 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100857 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000858 return NULL;
859 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000860
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000861 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000862}
863
Guido van Rossum53807da2007-04-10 19:01:47 +0000864/* XXX Windows support below is likely incomplete */
865
Guido van Rossum53807da2007-04-10 19:01:47 +0000866/* Cribbed from posix_lseek() */
867static PyObject *
Victor Stinner99970732017-05-02 15:10:39 +0200868portable_lseek(fileio *self, PyObject *posobj, int whence)
Guido van Rossum53807da2007-04-10 19:01:47 +0000869{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000870 Py_off_t pos, res;
Victor Stinner99970732017-05-02 15:10:39 +0200871 int fd = self->fd;
Guido van Rossum53807da2007-04-10 19:01:47 +0000872
873#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000874 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
875 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000876#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000877 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000878#endif
879#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000880 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000881#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000882#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000883 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000884#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000885 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000886#endif /* SEEK_SET */
887
Victor Stinner99970732017-05-02 15:10:39 +0200888 if (posobj == NULL) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000889 pos = 0;
Victor Stinner99970732017-05-02 15:10:39 +0200890 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000891 else {
892 if(PyFloat_Check(posobj)) {
893 PyErr_SetString(PyExc_TypeError, "an integer is required");
894 return NULL;
895 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000896#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000897 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000898#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000899 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000900#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000901 if (PyErr_Occurred())
902 return NULL;
903 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000904
Steve Dower940f33a2016-09-08 11:21:54 -0700905 Py_BEGIN_ALLOW_THREADS
906 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +0200907#ifdef MS_WINDOWS
Steve Dower940f33a2016-09-08 11:21:54 -0700908 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000909#else
Steve Dower940f33a2016-09-08 11:21:54 -0700910 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000911#endif
Steve Dower940f33a2016-09-08 11:21:54 -0700912 _Py_END_SUPPRESS_IPH
913 Py_END_ALLOW_THREADS
Victor Stinner99970732017-05-02 15:10:39 +0200914
915 if (self->seekable < 0) {
916 self->seekable = (res >= 0);
917 }
918
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000919 if (res < 0)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300920 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000921
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000922#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000923 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000924#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000925 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000926#endif
927}
928
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300929/*[clinic input]
930_io.FileIO.seek
931 pos: object
932 whence: int = 0
933 /
Guido van Rossuma9e20242007-03-08 00:43:48 +0000934
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300935Move to new file position and return the file position.
936
937Argument offset is a byte count. Optional argument whence defaults to
938SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values
939are SEEK_CUR or 1 (move relative to current position, positive or negative),
940and SEEK_END or 2 (move relative to end of file, usually negative, although
941many platforms allow seeking beyond the end of a file).
942
943Note that not all file objects are seekable.
944[clinic start generated code]*/
945
946static PyObject *
947_io_FileIO_seek_impl(fileio *self, PyObject *pos, int whence)
948/*[clinic end generated code: output=c976acdf054e6655 input=0439194b0774d454]*/
949{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000950 if (self->fd < 0)
951 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000952
Victor Stinner99970732017-05-02 15:10:39 +0200953 return portable_lseek(self, pos, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000954}
955
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300956/*[clinic input]
957_io.FileIO.tell
958
959Current file position.
960
961Can raise OSError for non seekable files.
962[clinic start generated code]*/
963
Guido van Rossuma9e20242007-03-08 00:43:48 +0000964static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300965_io_FileIO_tell_impl(fileio *self)
966/*[clinic end generated code: output=ffe2147058809d0b input=807e24ead4cec2f9]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000967{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000968 if (self->fd < 0)
969 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000970
Victor Stinner99970732017-05-02 15:10:39 +0200971 return portable_lseek(self, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000972}
973
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000974#ifdef HAVE_FTRUNCATE
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300975/*[clinic input]
976_io.FileIO.truncate
977 size as posobj: object = NULL
978 /
979
980Truncate the file to at most size bytes and return the truncated size.
981
982Size defaults to the current file position, as returned by tell().
983The current file position is changed to the value of size.
984[clinic start generated code]*/
985
Guido van Rossuma9e20242007-03-08 00:43:48 +0000986static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300987_io_FileIO_truncate_impl(fileio *self, PyObject *posobj)
988/*[clinic end generated code: output=e49ca7a916c176fa input=9026af44686b7318]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000989{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000990 Py_off_t pos;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000991 int ret;
992 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000993
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000994 fd = self->fd;
995 if (fd < 0)
996 return err_closed();
997 if (!self->writable)
998 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000999
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001000 if (posobj == Py_None || posobj == NULL) {
1001 /* Get the current position. */
Victor Stinner99970732017-05-02 15:10:39 +02001002 posobj = portable_lseek(self, NULL, 1);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001003 if (posobj == NULL)
1004 return NULL;
1005 }
1006 else {
1007 Py_INCREF(posobj);
1008 }
Guido van Rossum53807da2007-04-10 19:01:47 +00001009
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001010#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001011 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001012#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001013 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001014#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001015 if (PyErr_Occurred()){
1016 Py_DECREF(posobj);
1017 return NULL;
1018 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001019
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001020 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04001021 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001022 errno = 0;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07001023#ifdef MS_WINDOWS
1024 ret = _chsize_s(fd, pos);
1025#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001026 ret = ftruncate(fd, pos);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07001027#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04001028 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001029 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001030
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001031 if (ret != 0) {
1032 Py_DECREF(posobj);
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001033 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001034 return NULL;
1035 }
Guido van Rossuma9e20242007-03-08 00:43:48 +00001036
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001037 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001038}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001039#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +00001040
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001041static const char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001042mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +00001043{
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001044 if (self->created) {
1045 if (self->readable)
1046 return "xb+";
1047 else
1048 return "xb";
1049 }
Antoine Pitroue93b63b2013-09-04 20:46:33 +02001050 if (self->appending) {
1051 if (self->readable)
1052 return "ab+";
1053 else
1054 return "ab";
1055 }
1056 else if (self->readable) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001057 if (self->writable)
1058 return "rb+";
1059 else
1060 return "rb";
1061 }
1062 else
1063 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +00001064}
Guido van Rossuma9e20242007-03-08 00:43:48 +00001065
1066static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001067fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001068{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001069 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001070
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001071 if (self->fd < 0)
1072 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +00001073
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001074 if (_PyObject_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) {
1075 return NULL;
1076 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001077 if (nameobj == 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 *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301124fileio_getstate(fileio *self, PyObject *Py_UNUSED(ignored))
Antoine Pitrou243757e2010-11-05 21:15:39 +00001125{
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};