blob: 6854a44e2de2bf7c4531ea670b490a905db4c9ad [file] [log] [blame]
Guido van Rossuma9e20242007-03-08 00:43:48 +00001/* Author: Daniel Stutzbach */
2
3#define PY_SSIZE_T_CLEAN
4#include "Python.h"
Antoine Pitroue033e062010-10-29 10:38:18 +00005#include "structmember.h"
Benjamin Peterson2614cda2010-03-21 22:36:19 +00006#ifdef HAVE_SYS_TYPES_H
Guido van Rossuma9e20242007-03-08 00:43:48 +00007#include <sys/types.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +00008#endif
9#ifdef HAVE_SYS_STAT_H
Guido van Rossuma9e20242007-03-08 00:43:48 +000010#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000011#endif
Steve Dowerbfce0f92016-12-28 15:41:09 -080012#ifdef HAVE_IO_H
13#include <io.h>
14#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000015#ifdef HAVE_FCNTL_H
Guido van Rossuma9e20242007-03-08 00:43:48 +000016#include <fcntl.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000017#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +000018#include <stddef.h> /* For offsetof */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000019#include "_iomodule.h"
Guido van Rossuma9e20242007-03-08 00:43:48 +000020
21/*
22 * Known likely problems:
23 *
24 * - Files larger then 2**32-1
25 * - Files with unicode filenames
26 * - Passing numbers greater than 2**32-1 when an integer is expected
27 * - Making it work on Windows and other oddball platforms
28 *
29 * To Do:
30 *
31 * - autoconfify header file inclusion
Guido van Rossuma9e20242007-03-08 00:43:48 +000032 */
33
34#ifdef MS_WINDOWS
35/* can simulate truncate with Win32 API functions; see file_truncate */
Thomas Hellerfdeee3a2007-07-12 11:21:36 +000036#define HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +000037#define WIN32_LEAN_AND_MEAN
38#include <windows.h>
39#endif
40
Christian Heimesa872de52008-12-05 08:26:55 +000041#if BUFSIZ < (8*1024)
42#define SMALLCHUNK (8*1024)
43#elif (BUFSIZ >= (2 << 25))
44#error "unreasonable BUFSIZ > 64MB defined"
45#else
46#define SMALLCHUNK BUFSIZ
47#endif
48
Serhiy Storchakaf24131f2015-04-16 11:19:43 +030049/*[clinic input]
50module _io
51class _io.FileIO "fileio *" "&PyFileIO_Type"
52[clinic start generated code]*/
53/*[clinic end generated code: output=da39a3ee5e6b4b0d input=1c77708b41fda70c]*/
54
55/*[python input]
56class io_ssize_t_converter(CConverter):
57 type = 'Py_ssize_t'
58 converter = '_PyIO_ConvertSsize_t'
59[python start generated code]*/
60/*[python end generated code: output=da39a3ee5e6b4b0d input=d0a811d3cbfd1b33]*/
61
Guido van Rossuma9e20242007-03-08 00:43:48 +000062typedef struct {
Antoine Pitrouae4b4722010-05-05 16:31:07 +000063 PyObject_HEAD
64 int fd;
Charles-François Natalidc3044c2012-01-09 22:40:02 +010065 unsigned int created : 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000066 unsigned int readable : 1;
67 unsigned int writable : 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +020068 unsigned int appending : 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000069 signed int seekable : 2; /* -1 means unknown */
70 unsigned int closefd : 1;
Antoine Pitrou796564c2013-07-30 19:59:21 +020071 char finalizing;
Antoine Pitroude687222014-06-29 20:07:28 -040072 unsigned int blksize;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000073 PyObject *weakreflist;
74 PyObject *dict;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000075} fileio;
Guido van Rossuma9e20242007-03-08 00:43:48 +000076
Collin Winteraf334382007-03-08 21:46:15 +000077PyTypeObject PyFileIO_Type;
78
Victor Stinnerd9d04192013-11-06 23:50:10 +010079_Py_IDENTIFIER(name);
80
Guido van Rossuma9e20242007-03-08 00:43:48 +000081#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
82
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000083int
84_PyFileIO_closed(PyObject *self)
85{
Antoine Pitrouae4b4722010-05-05 16:31:07 +000086 return ((fileio *)self)->fd < 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000087}
Antoine Pitrou08838b62009-01-21 00:55:13 +000088
Antoine Pitroue033e062010-10-29 10:38:18 +000089/* Because this can call arbitrary code, it shouldn't be called when
90 the refcount is 0 (that is, not directly from tp_dealloc unless
91 the refcount has been temporarily re-incremented). */
92static PyObject *
93fileio_dealloc_warn(fileio *self, PyObject *source)
94{
95 if (self->fd >= 0 && self->closefd) {
96 PyObject *exc, *val, *tb;
97 PyErr_Fetch(&exc, &val, &tb);
Victor Stinner914cde82016-03-19 01:03:51 +010098 if (PyErr_ResourceWarning(source, 1, "unclosed file %R", source)) {
Antoine Pitroue033e062010-10-29 10:38:18 +000099 /* Spurious errors can appear at shutdown */
100 if (PyErr_ExceptionMatches(PyExc_Warning))
101 PyErr_WriteUnraisable((PyObject *) self);
102 }
103 PyErr_Restore(exc, val, tb);
104 }
105 Py_RETURN_NONE;
106}
107
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000108static PyObject *
109portable_lseek(int fd, PyObject *posobj, int whence);
110
Antoine Pitroua28fcfd2009-03-13 23:42:55 +0000111static PyObject *portable_lseek(int fd, PyObject *posobj, int whence);
112
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +0000113/* Returns 0 on success, -1 with exception set on failure. */
Neal Norwitz88b44da2007-08-12 17:23:54 +0000114static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000115internal_close(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000116{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000117 int err = 0;
118 int save_errno = 0;
119 if (self->fd >= 0) {
120 int fd = self->fd;
121 self->fd = -1;
122 /* fd is accessible and someone else may have closed it */
Steve Dower940f33a2016-09-08 11:21:54 -0700123 Py_BEGIN_ALLOW_THREADS
124 _Py_BEGIN_SUPPRESS_IPH
125 err = close(fd);
126 if (err < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000127 save_errno = errno;
Steve Dower940f33a2016-09-08 11:21:54 -0700128 _Py_END_SUPPRESS_IPH
129 Py_END_ALLOW_THREADS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000130 }
131 if (err < 0) {
132 errno = save_errno;
133 PyErr_SetFromErrno(PyExc_IOError);
134 return -1;
135 }
136 return 0;
Neal Norwitz88b44da2007-08-12 17:23:54 +0000137}
138
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300139/*[clinic input]
140_io.FileIO.close
141
142Close the file.
143
144A closed file cannot be used for further I/O operations. close() may be
145called more than once without error.
146[clinic start generated code]*/
147
Neal Norwitz88b44da2007-08-12 17:23:54 +0000148static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300149_io_FileIO_close_impl(fileio *self)
150/*[clinic end generated code: output=7737a319ef3bad0b input=f35231760d54a522]*/
Neal Norwitz88b44da2007-08-12 17:23:54 +0000151{
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200152 PyObject *res;
153 PyObject *exc, *val, *tb;
154 int rc;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200155 _Py_IDENTIFIER(close);
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200156 res = _PyObject_CallMethodId((PyObject*)&PyRawIOBase_Type,
157 &PyId_close, "O", self);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000158 if (!self->closefd) {
159 self->fd = -1;
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200160 return res;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000161 }
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200162 if (res == NULL)
163 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200164 if (self->finalizing) {
Antoine Pitroue033e062010-10-29 10:38:18 +0000165 PyObject *r = fileio_dealloc_warn(self, (PyObject *) self);
166 if (r)
167 Py_DECREF(r);
168 else
169 PyErr_Clear();
170 }
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200171 rc = internal_close(self);
172 if (res == NULL)
173 _PyErr_ChainExceptions(exc, val, tb);
174 if (rc < 0)
175 Py_CLEAR(res);
176 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000177}
178
179static PyObject *
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000180fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000181{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000182 fileio *self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000183
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000184 assert(type != NULL && type->tp_alloc != NULL);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000185
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000186 self = (fileio *) type->tp_alloc(type, 0);
187 if (self != NULL) {
188 self->fd = -1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100189 self->created = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000190 self->readable = 0;
191 self->writable = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200192 self->appending = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000193 self->seekable = -1;
Antoine Pitroude687222014-06-29 20:07:28 -0400194 self->blksize = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000195 self->closefd = 1;
196 self->weakreflist = NULL;
197 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000198
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000199 return (PyObject *) self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000200}
201
Victor Stinnerdaf45552013-08-28 00:53:59 +0200202#ifdef O_CLOEXEC
203extern int _Py_open_cloexec_works;
204#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000205
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300206/*[clinic input]
207_io.FileIO.__init__
208 file as nameobj: object
209 mode: str = "r"
210 closefd: int(c_default="1") = True
211 opener: object = None
212
213Open a file.
214
215The mode can be 'r' (default), 'w', 'x' or 'a' for reading,
216writing, exclusive creation or appending. The file will be created if it
217doesn't exist when opened for writing or appending; it will be truncated
218when opened for writing. A FileExistsError will be raised if it already
219exists when opened for creating. Opening a file for creating implies
220writing so this mode behaves in a similar way to 'w'.Add a '+' to the mode
221to allow simultaneous reading and writing. A custom opener can be used by
222passing a callable as *opener*. The underlying file descriptor for the file
223object is then obtained by calling opener with (*name*, *flags*).
224*opener* must return an open file descriptor (passing os.open as *opener*
225results in functionality similar to passing None).
226[clinic start generated code]*/
227
Guido van Rossuma9e20242007-03-08 00:43:48 +0000228static int
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300229_io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
230 int closefd, PyObject *opener)
231/*[clinic end generated code: output=23413f68e6484bbd input=193164e293d6c097]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000232{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000233 const char *name = NULL;
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300234 PyObject *stringobj = NULL;
235 const char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000236#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000237 Py_UNICODE *widename = NULL;
Thomas Helleraf2be262007-07-12 11:03:13 +0000238#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000239 int ret = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200240 int rwa = 0, plus = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000241 int flags = 0;
242 int fd = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200243 int fd_is_own = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200244#ifdef O_CLOEXEC
245 int *atomic_flag_works = &_Py_open_cloexec_works;
246#elif !defined(MS_WINDOWS)
247 int *atomic_flag_works = NULL;
248#endif
Steve Dowerf2f373f2015-02-21 08:44:05 -0800249 struct _Py_stat_struct fdfstat;
Martin Panter0bb62b12015-12-06 03:15:05 +0000250 int fstat_result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000251 int async_err = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000252
Christian Heimes82adeff2015-04-16 17:21:54 +0200253 assert(PyFileIO_Check(self));
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000254 if (self->fd >= 0) {
Hynek Schlawack2cc71562012-05-25 10:05:53 +0200255 if (self->closefd) {
256 /* Have to close the existing file first. */
257 if (internal_close(self) < 0)
258 return -1;
259 }
260 else
261 self->fd = -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000262 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000263
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000264 if (PyFloat_Check(nameobj)) {
265 PyErr_SetString(PyExc_TypeError,
266 "integer argument expected, got float");
267 return -1;
268 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000269
Serhiy Storchaka78980432013-01-15 01:12:17 +0200270 fd = _PyLong_AsInt(nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000271 if (fd < 0) {
272 if (!PyErr_Occurred()) {
273 PyErr_SetString(PyExc_ValueError,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +0300274 "negative file descriptor");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000275 return -1;
276 }
277 PyErr_Clear();
278 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000279
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000280#ifdef MS_WINDOWS
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200281 if (PyUnicode_Check(nameobj)) {
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300282 Py_ssize_t length;
283 widename = PyUnicode_AsUnicodeAndSize(nameobj, &length);
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200284 if (widename == NULL)
285 return -1;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300286 if (wcslen(widename) != length) {
Serhiy Storchaka7e9d1d12015-04-20 10:12:28 +0300287 PyErr_SetString(PyExc_ValueError, "embedded null character");
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300288 return -1;
289 }
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200290 } else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000291#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000292 if (fd < 0)
293 {
Antoine Pitrou13348842012-01-29 18:36:34 +0100294 if (!PyUnicode_FSConverter(nameobj, &stringobj)) {
295 return -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000296 }
Antoine Pitrou13348842012-01-29 18:36:34 +0100297 name = PyBytes_AS_STRING(stringobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000298 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000299
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000300 s = mode;
301 while (*s) {
302 switch (*s++) {
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100303 case 'x':
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000304 if (rwa) {
305 bad_mode:
306 PyErr_SetString(PyExc_ValueError,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100307 "Must have exactly one of create/read/write/append "
Georg Brandl28928ae2010-10-21 13:45:52 +0000308 "mode and at most one plus");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000309 goto error;
310 }
311 rwa = 1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100312 self->created = 1;
313 self->writable = 1;
314 flags |= O_EXCL | O_CREAT;
315 break;
316 case 'r':
317 if (rwa)
318 goto bad_mode;
319 rwa = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000320 self->readable = 1;
321 break;
322 case 'w':
323 if (rwa)
324 goto bad_mode;
325 rwa = 1;
326 self->writable = 1;
327 flags |= O_CREAT | O_TRUNC;
328 break;
329 case 'a':
330 if (rwa)
331 goto bad_mode;
332 rwa = 1;
333 self->writable = 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200334 self->appending = 1;
335 flags |= O_APPEND | O_CREAT;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000336 break;
337 case 'b':
338 break;
339 case '+':
340 if (plus)
341 goto bad_mode;
342 self->readable = self->writable = 1;
343 plus = 1;
344 break;
345 default:
346 PyErr_Format(PyExc_ValueError,
347 "invalid mode: %.200s", mode);
348 goto error;
349 }
350 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000351
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000352 if (!rwa)
353 goto bad_mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000354
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000355 if (self->readable && self->writable)
356 flags |= O_RDWR;
357 else if (self->readable)
358 flags |= O_RDONLY;
359 else
360 flags |= O_WRONLY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000361
362#ifdef O_BINARY
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000363 flags |= O_BINARY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000364#endif
365
Victor Stinnerdaf45552013-08-28 00:53:59 +0200366#ifdef MS_WINDOWS
367 flags |= O_NOINHERIT;
368#elif defined(O_CLOEXEC)
369 flags |= O_CLOEXEC;
370#endif
Walter Dörwald0e411482007-06-06 16:55:38 +0000371
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000372 if (fd >= 0) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000373 self->fd = fd;
374 self->closefd = closefd;
375 }
376 else {
377 self->closefd = 1;
378 if (!closefd) {
379 PyErr_SetString(PyExc_ValueError,
380 "Cannot use closefd=False with file name");
381 goto error;
382 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000383
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000384 errno = 0;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200385 if (opener == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000386 do {
387 Py_BEGIN_ALLOW_THREADS
Thomas Helleraf2be262007-07-12 11:03:13 +0000388#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000389 if (widename != NULL)
390 self->fd = _wopen(widename, flags, 0666);
391 else
Thomas Helleraf2be262007-07-12 11:03:13 +0000392#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000393 self->fd = open(name, flags, 0666);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000394 Py_END_ALLOW_THREADS
395 } while (self->fd < 0 && errno == EINTR &&
396 !(async_err = PyErr_CheckSignals()));
Victor Stinner9672da72015-03-04 18:40:10 +0100397
398 if (async_err)
399 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200400 }
401 else {
402 PyObject *fdobj;
403
404#ifndef MS_WINDOWS
405 /* the opener may clear the atomic flag */
406 atomic_flag_works = NULL;
407#endif
408
409 fdobj = PyObject_CallFunction(opener, "Oi", nameobj, flags);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200410 if (fdobj == NULL)
411 goto error;
412 if (!PyLong_Check(fdobj)) {
413 Py_DECREF(fdobj);
414 PyErr_SetString(PyExc_TypeError,
415 "expected integer from opener");
416 goto error;
417 }
418
Serhiy Storchaka78980432013-01-15 01:12:17 +0200419 self->fd = _PyLong_AsInt(fdobj);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200420 Py_DECREF(fdobj);
Barry Warsaw480e2852016-06-08 17:47:26 -0400421 if (self->fd < 0) {
422 if (!PyErr_Occurred()) {
Barry Warsaw118598a2016-06-08 17:54:43 -0400423 /* The opener returned a negative but didn't set an
424 exception. See issue #27066 */
Barry Warsaw480e2852016-06-08 17:47:26 -0400425 PyErr_Format(PyExc_ValueError,
426 "opener returned %d", self->fd);
427 }
Ross Lagerwall59142db2011-10-31 20:34:46 +0200428 goto error;
429 }
430 }
431
Hynek Schlawack7f59fd72012-06-22 09:32:22 +0200432 fd_is_own = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000433 if (self->fd < 0) {
Victor Stinner9672da72015-03-04 18:40:10 +0100434 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000435 goto error;
436 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200437
438#ifndef MS_WINDOWS
439 if (_Py_set_inheritable(self->fd, 0, atomic_flag_works) < 0)
440 goto error;
441#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000442 }
Antoine Pitroude687222014-06-29 20:07:28 -0400443
444 self->blksize = DEFAULT_BUFFER_SIZE;
Martin Panter0bb62b12015-12-06 03:15:05 +0000445 Py_BEGIN_ALLOW_THREADS
446 fstat_result = _Py_fstat_noraise(self->fd, &fdfstat);
447 Py_END_ALLOW_THREADS
448 if (fstat_result < 0) {
Martin Panter49d3db92015-12-06 11:12:15 +0000449 /* Tolerate fstat() errors other than EBADF. See Issue #25717, where
450 an anonymous file on a Virtual Box shared folder filesystem would
451 raise ENOENT. */
Martin Panter0bb62b12015-12-06 03:15:05 +0000452#ifdef MS_WINDOWS
453 if (GetLastError() == ERROR_INVALID_HANDLE) {
454 PyErr_SetFromWindowsErr(0);
455#else
456 if (errno == EBADF) {
457 PyErr_SetFromErrno(PyExc_OSError);
458#endif
459 goto error;
460 }
Antoine Pitroude687222014-06-29 20:07:28 -0400461 }
Martin Panter0bb62b12015-12-06 03:15:05 +0000462 else {
463#if defined(S_ISDIR) && defined(EISDIR)
464 /* On Unix, open will succeed for directories.
465 In Python, there should be no file objects referring to
466 directories, so we need a check. */
467 if (S_ISDIR(fdfstat.st_mode)) {
468 errno = EISDIR;
469 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);
470 goto error;
471 }
Antoine Pitroude687222014-06-29 20:07:28 -0400472#endif /* defined(S_ISDIR) */
473#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin Panter0bb62b12015-12-06 03:15:05 +0000474 if (fdfstat.st_blksize > 1)
475 self->blksize = fdfstat.st_blksize;
Antoine Pitroude687222014-06-29 20:07:28 -0400476#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Martin Panter0bb62b12015-12-06 03:15:05 +0000477 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000478
Victor Stinner89e34362011-01-07 18:47:22 +0000479#if defined(MS_WINDOWS) || defined(__CYGWIN__)
480 /* don't translate newlines (\r\n <=> \n) */
481 _setmode(self->fd, O_BINARY);
482#endif
483
Victor Stinnerd9d04192013-11-06 23:50:10 +0100484 if (_PyObject_SetAttrId((PyObject *)self, &PyId_name, nameobj) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000485 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000486
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200487 if (self->appending) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000488 /* For consistent behaviour, we explicitly seek to the
489 end of file (otherwise, it might be done only on the
490 first write()). */
491 PyObject *pos = portable_lseek(self->fd, NULL, 2);
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200492 if (pos == NULL)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000493 goto error;
494 Py_DECREF(pos);
495 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000496
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000497 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000498
499 error:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000500 ret = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200501 if (!fd_is_own)
502 self->fd = -1;
Benjamin Petersonbbb04122010-10-30 23:16:28 +0000503 if (self->fd >= 0)
504 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000505
Guido van Rossuma9e20242007-03-08 00:43:48 +0000506 done:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000507 Py_CLEAR(stringobj);
508 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000509}
510
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000511static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000512fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000513{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000514 Py_VISIT(self->dict);
515 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000516}
517
518static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000519fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000520{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000521 Py_CLEAR(self->dict);
522 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000523}
524
Guido van Rossuma9e20242007-03-08 00:43:48 +0000525static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000526fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000527{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200528 self->finalizing = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000529 if (_PyIOBase_finalize((PyObject *) self) < 0)
530 return;
531 _PyObject_GC_UNTRACK(self);
532 if (self->weakreflist != NULL)
533 PyObject_ClearWeakRefs((PyObject *) self);
534 Py_CLEAR(self->dict);
535 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000536}
537
538static PyObject *
539err_closed(void)
540{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000541 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
542 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000543}
544
545static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200546err_mode(const char *action)
Guido van Rossum53807da2007-04-10 19:01:47 +0000547{
Antoine Pitrou712cb732013-12-21 15:51:54 +0100548 _PyIO_State *state = IO_STATE();
549 if (state != NULL)
550 PyErr_Format(state->unsupported_operation,
551 "File not open for %s", action);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000552 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000553}
554
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300555/*[clinic input]
556_io.FileIO.fileno
557
558Return the underlying file descriptor (an integer).
559[clinic start generated code]*/
560
Guido van Rossum53807da2007-04-10 19:01:47 +0000561static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300562_io_FileIO_fileno_impl(fileio *self)
563/*[clinic end generated code: output=a9626ce5398ece90 input=0b9b2de67335ada3]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000564{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000565 if (self->fd < 0)
566 return err_closed();
567 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000568}
569
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300570/*[clinic input]
571_io.FileIO.readable
572
573True if file was opened in a read mode.
574[clinic start generated code]*/
575
Guido van Rossuma9e20242007-03-08 00:43:48 +0000576static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300577_io_FileIO_readable_impl(fileio *self)
578/*[clinic end generated code: output=640744a6150fe9ba input=a3fdfed6eea721c5]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000579{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000580 if (self->fd < 0)
581 return err_closed();
582 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000583}
584
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300585/*[clinic input]
586_io.FileIO.writable
587
588True if file was opened in a write mode.
589[clinic start generated code]*/
590
Guido van Rossuma9e20242007-03-08 00:43:48 +0000591static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300592_io_FileIO_writable_impl(fileio *self)
593/*[clinic end generated code: output=96cefc5446e89977 input=c204a808ca2e1748]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000594{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000595 if (self->fd < 0)
596 return err_closed();
597 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000598}
599
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300600/*[clinic input]
601_io.FileIO.seekable
602
603True if file supports random-access.
604[clinic start generated code]*/
605
Guido van Rossuma9e20242007-03-08 00:43:48 +0000606static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300607_io_FileIO_seekable_impl(fileio *self)
608/*[clinic end generated code: output=47909ca0a42e9287 input=c8e5554d2fd63c7f]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000609{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000610 if (self->fd < 0)
611 return err_closed();
612 if (self->seekable < 0) {
613 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
614 if (pos == NULL) {
615 PyErr_Clear();
616 self->seekable = 0;
617 } else {
618 Py_DECREF(pos);
619 self->seekable = 1;
620 }
621 }
622 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000623}
624
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300625/*[clinic input]
626_io.FileIO.readinto
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700627 buffer: Py_buffer(accept={rwbuffer})
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300628 /
629
630Same as RawIOBase.readinto().
631[clinic start generated code]*/
632
Guido van Rossuma9e20242007-03-08 00:43:48 +0000633static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300634_io_FileIO_readinto_impl(fileio *self, Py_buffer *buffer)
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700635/*[clinic end generated code: output=b01a5a22c8415cb4 input=4721d7b68b154eaf]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000636{
Victor Stinner66aab0c2015-03-19 22:53:20 +0100637 Py_ssize_t n;
638 int err;
Guido van Rossum53807da2007-04-10 19:01:47 +0000639
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000640 if (self->fd < 0)
641 return err_closed();
642 if (!self->readable)
643 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000644
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300645 n = _Py_read(self->fd, buffer->buf, buffer->len);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100646 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100647 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100648
649 if (n == -1) {
650 if (err == EAGAIN) {
651 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000652 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100653 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000654 return NULL;
655 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000656
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000657 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000658}
659
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000660static size_t
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100661new_buffersize(fileio *self, size_t currentsize)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000662{
Antoine Pitroua3f44572012-04-17 13:50:58 +0200663 size_t addend;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100664
Nadeem Vawdad41a98b2011-10-13 13:34:16 +0200665 /* Expand the buffer by an amount proportional to the current size,
Antoine Pitroua3f44572012-04-17 13:50:58 +0200666 giving us amortized linear-time behavior. For bigger sizes, use a
667 less-than-double growth factor to avoid excessive allocation. */
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100668 assert(currentsize <= PY_SSIZE_T_MAX);
Antoine Pitroua3f44572012-04-17 13:50:58 +0200669 if (currentsize > 65536)
670 addend = currentsize >> 3;
671 else
672 addend = 256 + currentsize;
673 if (addend < SMALLCHUNK)
674 /* Avoid tiny read() calls. */
675 addend = SMALLCHUNK;
676 return addend + currentsize;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000677}
678
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300679/*[clinic input]
680_io.FileIO.readall
681
682Read all data from the file, returned as bytes.
683
684In non-blocking mode, returns as much as is immediately available,
685or None if no data is available. Return an empty bytes object at EOF.
686[clinic start generated code]*/
687
Guido van Rossum7165cb12007-07-10 06:54:34 +0000688static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300689_io_FileIO_readall_impl(fileio *self)
690/*[clinic end generated code: output=faa0292b213b4022 input=dbdc137f55602834]*/
Guido van Rossum7165cb12007-07-10 06:54:34 +0000691{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200692 struct _Py_stat_struct status;
Victor Stinnera2a64772011-10-11 22:45:02 +0200693 Py_off_t pos, end;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000694 PyObject *result;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100695 Py_ssize_t bytes_read = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100696 Py_ssize_t n;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100697 size_t bufsize;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000698
Victor Stinnerb79f28c2011-05-25 22:09:03 +0200699 if (self->fd < 0)
700 return err_closed();
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000701
Steve Dower8fc89802015-04-12 00:26:27 -0400702 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +0200703#ifdef MS_WINDOWS
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200704 pos = _lseeki64(self->fd, 0L, SEEK_CUR);
705#else
706 pos = lseek(self->fd, 0L, SEEK_CUR);
707#endif
Steve Dower8fc89802015-04-12 00:26:27 -0400708 _Py_END_SUPPRESS_IPH
709
Victor Stinnere134a7f2015-03-30 10:09:31 +0200710 if (_Py_fstat_noraise(self->fd, &status) == 0)
711 end = status.st_size;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200712 else
Victor Stinnera2a64772011-10-11 22:45:02 +0200713 end = (Py_off_t)-1;
Christian Heimesa872de52008-12-05 08:26:55 +0000714
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100715 if (end > 0 && end >= pos && pos >= 0 && end - pos < PY_SSIZE_T_MAX) {
716 /* This is probably a real file, so we try to allocate a
717 buffer one byte larger than the rest of the file. If the
718 calculation is right then we should get EOF without having
719 to enlarge the buffer. */
720 bufsize = (size_t)(end - pos + 1);
721 } else {
722 bufsize = SMALLCHUNK;
723 }
724
725 result = PyBytes_FromStringAndSize(NULL, bufsize);
726 if (result == NULL)
727 return NULL;
728
729 while (1) {
730 if (bytes_read >= (Py_ssize_t)bufsize) {
731 bufsize = new_buffersize(self, bytes_read);
732 if (bufsize > PY_SSIZE_T_MAX || bufsize <= 0) {
733 PyErr_SetString(PyExc_OverflowError,
734 "unbounded read returned more bytes "
Serhiy Storchakab817b772015-04-10 02:18:44 +0300735 "than a Python bytes object can hold");
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100736 Py_DECREF(result);
737 return NULL;
738 }
739
740 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
741 if (_PyBytes_Resize(&result, bufsize) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000742 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000743 }
744 }
Victor Stinner9672da72015-03-04 18:40:10 +0100745
Victor Stinner66aab0c2015-03-19 22:53:20 +0100746 n = _Py_read(self->fd,
747 PyBytes_AS_STRING(result) + bytes_read,
748 bufsize - bytes_read);
Victor Stinner9672da72015-03-04 18:40:10 +0100749
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000750 if (n == 0)
751 break;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100752 if (n == -1) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000753 if (errno == EAGAIN) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100754 PyErr_Clear();
Victor Stinnere10920f2014-07-02 22:59:31 +0200755 if (bytes_read > 0)
756 break;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000757 Py_DECREF(result);
758 Py_RETURN_NONE;
759 }
760 Py_DECREF(result);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000761 return NULL;
762 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100763 bytes_read += n;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200764 pos += n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000765 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000766
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100767 if (PyBytes_GET_SIZE(result) > bytes_read) {
768 if (_PyBytes_Resize(&result, bytes_read) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000769 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000770 }
771 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000772}
773
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300774/*[clinic input]
775_io.FileIO.read
776 size: io_ssize_t = -1
777 /
778
779Read at most size bytes, returned as bytes.
780
781Only makes one system call, so less data may be returned than requested.
782In non-blocking mode, returns None if no data is available.
783Return an empty bytes object at EOF.
784[clinic start generated code]*/
785
Guido van Rossuma9e20242007-03-08 00:43:48 +0000786static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300787_io_FileIO_read_impl(fileio *self, Py_ssize_t size)
788/*[clinic end generated code: output=42528d39dd0ca641 input=5c6caa5490c13a9b]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000789{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000790 char *ptr;
791 Py_ssize_t n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000792 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000793
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000794 if (self->fd < 0)
795 return err_closed();
796 if (!self->readable)
797 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000798
Victor Stinner66aab0c2015-03-19 22:53:20 +0100799 if (size < 0)
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300800 return _io_FileIO_readall_impl(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000801
Victor Stinner14b9b112013-06-25 00:37:25 +0200802#ifdef MS_WINDOWS
Victor Stinner66aab0c2015-03-19 22:53:20 +0100803 /* On Windows, the count parameter of read() is an int */
Victor Stinnerc655a722011-07-05 11:31:49 +0200804 if (size > INT_MAX)
805 size = INT_MAX;
806#endif
Victor Stinner66aab0c2015-03-19 22:53:20 +0100807
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000808 bytes = PyBytes_FromStringAndSize(NULL, size);
809 if (bytes == NULL)
810 return NULL;
811 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000812
Victor Stinner66aab0c2015-03-19 22:53:20 +0100813 n = _Py_read(self->fd, ptr, size);
814 if (n == -1) {
815 /* copy errno because Py_DECREF() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100816 int err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000817 Py_DECREF(bytes);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100818 if (err == EAGAIN) {
819 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000820 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100821 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000822 return NULL;
823 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000824
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000825 if (n != size) {
826 if (_PyBytes_Resize(&bytes, n) < 0) {
Victor Stinner85c761d2013-07-16 21:36:02 +0200827 Py_CLEAR(bytes);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000828 return NULL;
829 }
830 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000831
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000832 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000833}
834
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300835/*[clinic input]
836_io.FileIO.write
837 b: Py_buffer
838 /
839
Martin Panter6bb91f32016-05-28 00:41:57 +0000840Write buffer b to file, return number of bytes written.
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300841
842Only makes one system call, so not all of the data may be written.
843The number of bytes actually written is returned. In non-blocking mode,
844returns None if the write would block.
845[clinic start generated code]*/
846
Guido van Rossuma9e20242007-03-08 00:43:48 +0000847static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300848_io_FileIO_write_impl(fileio *self, Py_buffer *b)
Martin Panter6bb91f32016-05-28 00:41:57 +0000849/*[clinic end generated code: output=b4059db3d363a2f7 input=6e7908b36f0ce74f]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000850{
Victor Stinner66aab0c2015-03-19 22:53:20 +0100851 Py_ssize_t n;
852 int err;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000853
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000854 if (self->fd < 0)
855 return err_closed();
856 if (!self->writable)
857 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000858
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300859 n = _Py_write(self->fd, b->buf, b->len);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100860 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100861 err = errno;
Martin v. Löwis423be952008-08-13 15:53:07 +0000862
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000863 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100864 if (err == EAGAIN) {
865 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000866 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100867 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000868 return NULL;
869 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000870
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000871 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000872}
873
Guido van Rossum53807da2007-04-10 19:01:47 +0000874/* XXX Windows support below is likely incomplete */
875
Guido van Rossum53807da2007-04-10 19:01:47 +0000876/* Cribbed from posix_lseek() */
877static PyObject *
878portable_lseek(int fd, PyObject *posobj, int whence)
879{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000880 Py_off_t pos, res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000881
882#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000883 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
884 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000885#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000886 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000887#endif
888#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000889 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000890#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000891#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000892 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000893#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000894 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000895#endif /* SEEK_SET */
896
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000897 if (posobj == NULL)
898 pos = 0;
899 else {
900 if(PyFloat_Check(posobj)) {
901 PyErr_SetString(PyExc_TypeError, "an integer is required");
902 return NULL;
903 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000904#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000905 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000906#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000907 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000908#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000909 if (PyErr_Occurred())
910 return NULL;
911 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000912
Steve Dower940f33a2016-09-08 11:21:54 -0700913 Py_BEGIN_ALLOW_THREADS
914 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +0200915#ifdef MS_WINDOWS
Steve Dower940f33a2016-09-08 11:21:54 -0700916 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000917#else
Steve Dower940f33a2016-09-08 11:21:54 -0700918 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000919#endif
Steve Dower940f33a2016-09-08 11:21:54 -0700920 _Py_END_SUPPRESS_IPH
921 Py_END_ALLOW_THREADS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000922 if (res < 0)
923 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000924
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000925#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000926 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000927#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000928 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000929#endif
930}
931
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300932/*[clinic input]
933_io.FileIO.seek
934 pos: object
935 whence: int = 0
936 /
Guido van Rossuma9e20242007-03-08 00:43:48 +0000937
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300938Move to new file position and return the file position.
939
940Argument offset is a byte count. Optional argument whence defaults to
941SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values
942are SEEK_CUR or 1 (move relative to current position, positive or negative),
943and SEEK_END or 2 (move relative to end of file, usually negative, although
944many platforms allow seeking beyond the end of a file).
945
946Note that not all file objects are seekable.
947[clinic start generated code]*/
948
949static PyObject *
950_io_FileIO_seek_impl(fileio *self, PyObject *pos, int whence)
951/*[clinic end generated code: output=c976acdf054e6655 input=0439194b0774d454]*/
952{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000953 if (self->fd < 0)
954 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000955
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300956 return portable_lseek(self->fd, pos, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000957}
958
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300959/*[clinic input]
960_io.FileIO.tell
961
962Current file position.
963
964Can raise OSError for non seekable files.
965[clinic start generated code]*/
966
Guido van Rossuma9e20242007-03-08 00:43:48 +0000967static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300968_io_FileIO_tell_impl(fileio *self)
969/*[clinic end generated code: output=ffe2147058809d0b input=807e24ead4cec2f9]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000970{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000971 if (self->fd < 0)
972 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000973
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000974 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000975}
976
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000977#ifdef HAVE_FTRUNCATE
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300978/*[clinic input]
979_io.FileIO.truncate
980 size as posobj: object = NULL
981 /
982
983Truncate the file to at most size bytes and return the truncated size.
984
985Size defaults to the current file position, as returned by tell().
986The current file position is changed to the value of size.
987[clinic start generated code]*/
988
Guido van Rossuma9e20242007-03-08 00:43:48 +0000989static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300990_io_FileIO_truncate_impl(fileio *self, PyObject *posobj)
991/*[clinic end generated code: output=e49ca7a916c176fa input=9026af44686b7318]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000992{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000993 Py_off_t pos;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000994 int ret;
995 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000996
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000997 fd = self->fd;
998 if (fd < 0)
999 return err_closed();
1000 if (!self->writable)
1001 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001002
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001003 if (posobj == Py_None || posobj == NULL) {
1004 /* Get the current position. */
1005 posobj = portable_lseek(fd, NULL, 1);
1006 if (posobj == NULL)
1007 return NULL;
1008 }
1009 else {
1010 Py_INCREF(posobj);
1011 }
Guido van Rossum53807da2007-04-10 19:01:47 +00001012
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001013#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001014 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001015#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001016 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001017#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001018 if (PyErr_Occurred()){
1019 Py_DECREF(posobj);
1020 return NULL;
1021 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001022
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001023 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04001024 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001025 errno = 0;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07001026#ifdef MS_WINDOWS
1027 ret = _chsize_s(fd, pos);
1028#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001029 ret = ftruncate(fd, pos);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07001030#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04001031 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001032 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001033
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001034 if (ret != 0) {
1035 Py_DECREF(posobj);
1036 PyErr_SetFromErrno(PyExc_IOError);
1037 return NULL;
1038 }
Guido van Rossuma9e20242007-03-08 00:43:48 +00001039
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001040 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001041}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001042#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +00001043
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001044static const char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001045mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +00001046{
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001047 if (self->created) {
1048 if (self->readable)
1049 return "xb+";
1050 else
1051 return "xb";
1052 }
Antoine Pitroue93b63b2013-09-04 20:46:33 +02001053 if (self->appending) {
1054 if (self->readable)
1055 return "ab+";
1056 else
1057 return "ab";
1058 }
1059 else if (self->readable) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001060 if (self->writable)
1061 return "rb+";
1062 else
1063 return "rb";
1064 }
1065 else
1066 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +00001067}
Guido van Rossuma9e20242007-03-08 00:43:48 +00001068
1069static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001070fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001071{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001072 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001073
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001074 if (self->fd < 0)
1075 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +00001076
Martin v. Löwis767046a2011-10-14 15:35:36 +02001077 nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001078 if (nameobj == NULL) {
1079 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1080 PyErr_Clear();
1081 else
1082 return NULL;
Robert Collins933430a2014-10-18 13:32:43 +13001083 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +02001084 "<_io.FileIO fd=%d mode='%s' closefd=%s>",
1085 self->fd, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001086 }
1087 else {
Robert Collins933430a2014-10-18 13:32:43 +13001088 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +02001089 "<_io.FileIO name=%R mode='%s' closefd=%s>",
1090 nameobj, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001091 Py_DECREF(nameobj);
1092 }
1093 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001094}
1095
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001096/*[clinic input]
1097_io.FileIO.isatty
1098
1099True if the file is connected to a TTY device.
1100[clinic start generated code]*/
1101
Guido van Rossuma9e20242007-03-08 00:43:48 +00001102static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001103_io_FileIO_isatty_impl(fileio *self)
1104/*[clinic end generated code: output=932c39924e9a8070 input=cd94ca1f5e95e843]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +00001105{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001106 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +00001107
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001108 if (self->fd < 0)
1109 return err_closed();
1110 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001111 _Py_BEGIN_SUPPRESS_IPH
Steve Dower940f33a2016-09-08 11:21:54 -07001112 res = isatty(self->fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001113 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001114 Py_END_ALLOW_THREADS
1115 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001116}
1117
Antoine Pitrou243757e2010-11-05 21:15:39 +00001118static PyObject *
1119fileio_getstate(fileio *self)
1120{
1121 PyErr_Format(PyExc_TypeError,
1122 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
1123 return NULL;
1124}
1125
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001126#include "clinic/fileio.c.h"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001127
1128static PyMethodDef fileio_methods[] = {
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001129 _IO_FILEIO_READ_METHODDEF
1130 _IO_FILEIO_READALL_METHODDEF
1131 _IO_FILEIO_READINTO_METHODDEF
1132 _IO_FILEIO_WRITE_METHODDEF
1133 _IO_FILEIO_SEEK_METHODDEF
1134 _IO_FILEIO_TELL_METHODDEF
1135 _IO_FILEIO_TRUNCATE_METHODDEF
1136 _IO_FILEIO_CLOSE_METHODDEF
1137 _IO_FILEIO_SEEKABLE_METHODDEF
1138 _IO_FILEIO_READABLE_METHODDEF
1139 _IO_FILEIO_WRITABLE_METHODDEF
1140 _IO_FILEIO_FILENO_METHODDEF
1141 _IO_FILEIO_ISATTY_METHODDEF
Antoine Pitroue033e062010-10-29 10:38:18 +00001142 {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
Antoine Pitrou243757e2010-11-05 21:15:39 +00001143 {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001144 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001145};
1146
Guido van Rossum53807da2007-04-10 19:01:47 +00001147/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1148
Guido van Rossumb0428152007-04-08 17:44:42 +00001149static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001150get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001151{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001152 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001153}
1154
1155static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001156get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001157{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001158 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001159}
1160
1161static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001162get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001163{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001164 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001165}
1166
1167static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001168 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1169 {"closefd", (getter)get_closefd, NULL,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001170 "True if the file descriptor will be closed by close()."},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001171 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1172 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001173};
1174
Antoine Pitrou796564c2013-07-30 19:59:21 +02001175static PyMemberDef fileio_members[] = {
Antoine Pitroude687222014-06-29 20:07:28 -04001176 {"_blksize", T_UINT, offsetof(fileio, blksize), 0},
Antoine Pitrou796564c2013-07-30 19:59:21 +02001177 {"_finalizing", T_BOOL, offsetof(fileio, finalizing), 0},
1178 {NULL}
1179};
1180
Guido van Rossuma9e20242007-03-08 00:43:48 +00001181PyTypeObject PyFileIO_Type = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001182 PyVarObject_HEAD_INIT(NULL, 0)
1183 "_io.FileIO",
1184 sizeof(fileio),
1185 0,
1186 (destructor)fileio_dealloc, /* tp_dealloc */
1187 0, /* tp_print */
1188 0, /* tp_getattr */
1189 0, /* tp_setattr */
1190 0, /* tp_reserved */
1191 (reprfunc)fileio_repr, /* tp_repr */
1192 0, /* tp_as_number */
1193 0, /* tp_as_sequence */
1194 0, /* tp_as_mapping */
1195 0, /* tp_hash */
1196 0, /* tp_call */
1197 0, /* tp_str */
1198 PyObject_GenericGetAttr, /* tp_getattro */
1199 0, /* tp_setattro */
1200 0, /* tp_as_buffer */
1201 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +02001202 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001203 _io_FileIO___init____doc__, /* tp_doc */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001204 (traverseproc)fileio_traverse, /* tp_traverse */
1205 (inquiry)fileio_clear, /* tp_clear */
1206 0, /* tp_richcompare */
1207 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1208 0, /* tp_iter */
1209 0, /* tp_iternext */
1210 fileio_methods, /* tp_methods */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001211 fileio_members, /* tp_members */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001212 fileio_getsetlist, /* tp_getset */
1213 0, /* tp_base */
1214 0, /* tp_dict */
1215 0, /* tp_descr_get */
1216 0, /* tp_descr_set */
1217 offsetof(fileio, dict), /* tp_dictoffset */
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001218 _io_FileIO___init__, /* tp_init */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001219 PyType_GenericAlloc, /* tp_alloc */
1220 fileio_new, /* tp_new */
1221 PyObject_GC_Del, /* tp_free */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001222 0, /* tp_is_gc */
1223 0, /* tp_bases */
1224 0, /* tp_mro */
1225 0, /* tp_cache */
1226 0, /* tp_subclasses */
1227 0, /* tp_weaklist */
1228 0, /* tp_del */
1229 0, /* tp_version_tag */
1230 0, /* tp_finalize */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001231};