blob: 919cf502dca4ac1df8ff8b0b15ffab4483dfeabc [file] [log] [blame]
Guido van Rossuma9e20242007-03-08 00:43:48 +00001/* Author: Daniel Stutzbach */
2
3#define PY_SSIZE_T_CLEAN
4#include "Python.h"
Antoine Pitroue033e062010-10-29 10:38:18 +00005#include "structmember.h"
Benjamin Peterson2614cda2010-03-21 22:36:19 +00006#ifdef HAVE_SYS_TYPES_H
Guido van Rossuma9e20242007-03-08 00:43:48 +00007#include <sys/types.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +00008#endif
9#ifdef HAVE_SYS_STAT_H
Guido van Rossuma9e20242007-03-08 00:43:48 +000010#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000011#endif
12#ifdef HAVE_FCNTL_H
Guido van Rossuma9e20242007-03-08 00:43:48 +000013#include <fcntl.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000014#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +000015#include <stddef.h> /* For offsetof */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000016#include "_iomodule.h"
Guido van Rossuma9e20242007-03-08 00:43:48 +000017
18/*
19 * Known likely problems:
20 *
21 * - Files larger then 2**32-1
22 * - Files with unicode filenames
23 * - Passing numbers greater than 2**32-1 when an integer is expected
24 * - Making it work on Windows and other oddball platforms
25 *
26 * To Do:
27 *
28 * - autoconfify header file inclusion
Guido van Rossuma9e20242007-03-08 00:43:48 +000029 */
30
31#ifdef MS_WINDOWS
32/* can simulate truncate with Win32 API functions; see file_truncate */
Thomas Hellerfdeee3a2007-07-12 11:21:36 +000033#define HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +000034#define WIN32_LEAN_AND_MEAN
35#include <windows.h>
36#endif
37
Christian Heimesa872de52008-12-05 08:26:55 +000038#if BUFSIZ < (8*1024)
39#define SMALLCHUNK (8*1024)
40#elif (BUFSIZ >= (2 << 25))
41#error "unreasonable BUFSIZ > 64MB defined"
42#else
43#define SMALLCHUNK BUFSIZ
44#endif
45
Serhiy Storchakaf24131f2015-04-16 11:19:43 +030046/*[clinic input]
47module _io
48class _io.FileIO "fileio *" "&PyFileIO_Type"
49[clinic start generated code]*/
50/*[clinic end generated code: output=da39a3ee5e6b4b0d input=1c77708b41fda70c]*/
51
52/*[python input]
53class io_ssize_t_converter(CConverter):
54 type = 'Py_ssize_t'
55 converter = '_PyIO_ConvertSsize_t'
56[python start generated code]*/
57/*[python end generated code: output=da39a3ee5e6b4b0d input=d0a811d3cbfd1b33]*/
58
Guido van Rossuma9e20242007-03-08 00:43:48 +000059typedef struct {
Antoine Pitrouae4b4722010-05-05 16:31:07 +000060 PyObject_HEAD
61 int fd;
Charles-François Natalidc3044c2012-01-09 22:40:02 +010062 unsigned int created : 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000063 unsigned int readable : 1;
64 unsigned int writable : 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +020065 unsigned int appending : 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000066 signed int seekable : 2; /* -1 means unknown */
67 unsigned int closefd : 1;
Antoine Pitrou796564c2013-07-30 19:59:21 +020068 char finalizing;
Antoine Pitroude687222014-06-29 20:07:28 -040069 unsigned int blksize;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000070 PyObject *weakreflist;
71 PyObject *dict;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000072} fileio;
Guido van Rossuma9e20242007-03-08 00:43:48 +000073
Collin Winteraf334382007-03-08 21:46:15 +000074PyTypeObject PyFileIO_Type;
75
Victor Stinnerd9d04192013-11-06 23:50:10 +010076_Py_IDENTIFIER(name);
77
Guido van Rossuma9e20242007-03-08 00:43:48 +000078#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
79
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000080int
81_PyFileIO_closed(PyObject *self)
82{
Antoine Pitrouae4b4722010-05-05 16:31:07 +000083 return ((fileio *)self)->fd < 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000084}
Antoine Pitrou08838b62009-01-21 00:55:13 +000085
Antoine Pitroue033e062010-10-29 10:38:18 +000086/* Because this can call arbitrary code, it shouldn't be called when
87 the refcount is 0 (that is, not directly from tp_dealloc unless
88 the refcount has been temporarily re-incremented). */
89static PyObject *
90fileio_dealloc_warn(fileio *self, PyObject *source)
91{
92 if (self->fd >= 0 && self->closefd) {
93 PyObject *exc, *val, *tb;
94 PyErr_Fetch(&exc, &val, &tb);
95 if (PyErr_WarnFormat(PyExc_ResourceWarning, 1,
96 "unclosed file %R", source)) {
97 /* Spurious errors can appear at shutdown */
98 if (PyErr_ExceptionMatches(PyExc_Warning))
99 PyErr_WriteUnraisable((PyObject *) self);
100 }
101 PyErr_Restore(exc, val, tb);
102 }
103 Py_RETURN_NONE;
104}
105
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000106static PyObject *
107portable_lseek(int fd, PyObject *posobj, int whence);
108
Antoine Pitroua28fcfd2009-03-13 23:42:55 +0000109static PyObject *portable_lseek(int fd, PyObject *posobj, int whence);
110
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +0000111/* Returns 0 on success, -1 with exception set on failure. */
Neal Norwitz88b44da2007-08-12 17:23:54 +0000112static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000113internal_close(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000114{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000115 int err = 0;
116 int save_errno = 0;
117 if (self->fd >= 0) {
118 int fd = self->fd;
119 self->fd = -1;
120 /* fd is accessible and someone else may have closed it */
121 if (_PyVerify_fd(fd)) {
122 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -0400123 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000124 err = close(fd);
125 if (err < 0)
126 save_errno = errno;
Steve Dower8fc89802015-04-12 00:26:27 -0400127 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000128 Py_END_ALLOW_THREADS
129 } else {
130 save_errno = errno;
131 err = -1;
132 }
133 }
134 if (err < 0) {
135 errno = save_errno;
136 PyErr_SetFromErrno(PyExc_IOError);
137 return -1;
138 }
139 return 0;
Neal Norwitz88b44da2007-08-12 17:23:54 +0000140}
141
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300142/*[clinic input]
143_io.FileIO.close
144
145Close the file.
146
147A closed file cannot be used for further I/O operations. close() may be
148called more than once without error.
149[clinic start generated code]*/
150
Neal Norwitz88b44da2007-08-12 17:23:54 +0000151static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300152_io_FileIO_close_impl(fileio *self)
153/*[clinic end generated code: output=7737a319ef3bad0b input=f35231760d54a522]*/
Neal Norwitz88b44da2007-08-12 17:23:54 +0000154{
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200155 PyObject *res;
156 PyObject *exc, *val, *tb;
157 int rc;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200158 _Py_IDENTIFIER(close);
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200159 res = _PyObject_CallMethodId((PyObject*)&PyRawIOBase_Type,
160 &PyId_close, "O", self);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000161 if (!self->closefd) {
162 self->fd = -1;
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200163 return res;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000164 }
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200165 if (res == NULL)
166 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200167 if (self->finalizing) {
Antoine Pitroue033e062010-10-29 10:38:18 +0000168 PyObject *r = fileio_dealloc_warn(self, (PyObject *) self);
169 if (r)
170 Py_DECREF(r);
171 else
172 PyErr_Clear();
173 }
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200174 rc = internal_close(self);
175 if (res == NULL)
176 _PyErr_ChainExceptions(exc, val, tb);
177 if (rc < 0)
178 Py_CLEAR(res);
179 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000180}
181
182static PyObject *
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000183fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000184{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000185 fileio *self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000186
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000187 assert(type != NULL && type->tp_alloc != NULL);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000188
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000189 self = (fileio *) type->tp_alloc(type, 0);
190 if (self != NULL) {
191 self->fd = -1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100192 self->created = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000193 self->readable = 0;
194 self->writable = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200195 self->appending = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000196 self->seekable = -1;
Antoine Pitroude687222014-06-29 20:07:28 -0400197 self->blksize = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000198 self->closefd = 1;
199 self->weakreflist = NULL;
200 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000201
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000202 return (PyObject *) self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000203}
204
Victor Stinnerdaf45552013-08-28 00:53:59 +0200205#ifdef O_CLOEXEC
206extern int _Py_open_cloexec_works;
207#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000208
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300209/*[clinic input]
210_io.FileIO.__init__
211 file as nameobj: object
212 mode: str = "r"
213 closefd: int(c_default="1") = True
214 opener: object = None
215
216Open a file.
217
218The mode can be 'r' (default), 'w', 'x' or 'a' for reading,
219writing, exclusive creation or appending. The file will be created if it
220doesn't exist when opened for writing or appending; it will be truncated
221when opened for writing. A FileExistsError will be raised if it already
222exists when opened for creating. Opening a file for creating implies
223writing so this mode behaves in a similar way to 'w'.Add a '+' to the mode
224to allow simultaneous reading and writing. A custom opener can be used by
225passing a callable as *opener*. The underlying file descriptor for the file
226object is then obtained by calling opener with (*name*, *flags*).
227*opener* must return an open file descriptor (passing os.open as *opener*
228results in functionality similar to passing None).
229[clinic start generated code]*/
230
Guido van Rossuma9e20242007-03-08 00:43:48 +0000231static int
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300232_io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
233 int closefd, PyObject *opener)
234/*[clinic end generated code: output=23413f68e6484bbd input=193164e293d6c097]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000235{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000236 const char *name = NULL;
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300237 PyObject *stringobj = NULL;
238 const char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000239#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000240 Py_UNICODE *widename = NULL;
Thomas Helleraf2be262007-07-12 11:03:13 +0000241#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000242 int ret = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200243 int rwa = 0, plus = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000244 int flags = 0;
245 int fd = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200246 int fd_is_own = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200247#ifdef O_CLOEXEC
248 int *atomic_flag_works = &_Py_open_cloexec_works;
249#elif !defined(MS_WINDOWS)
250 int *atomic_flag_works = NULL;
251#endif
Steve Dowerf2f373f2015-02-21 08:44:05 -0800252 struct _Py_stat_struct fdfstat;
Martin Panter0bb62b12015-12-06 03:15:05 +0000253 int fstat_result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000254 int async_err = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000255
Christian Heimes82adeff2015-04-16 17:21:54 +0200256 assert(PyFileIO_Check(self));
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000257 if (self->fd >= 0) {
Hynek Schlawack2cc71562012-05-25 10:05:53 +0200258 if (self->closefd) {
259 /* Have to close the existing file first. */
260 if (internal_close(self) < 0)
261 return -1;
262 }
263 else
264 self->fd = -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000265 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000266
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000267 if (PyFloat_Check(nameobj)) {
268 PyErr_SetString(PyExc_TypeError,
269 "integer argument expected, got float");
270 return -1;
271 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000272
Serhiy Storchaka78980432013-01-15 01:12:17 +0200273 fd = _PyLong_AsInt(nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000274 if (fd < 0) {
275 if (!PyErr_Occurred()) {
276 PyErr_SetString(PyExc_ValueError,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +0300277 "negative file descriptor");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000278 return -1;
279 }
280 PyErr_Clear();
281 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000282
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000283#ifdef MS_WINDOWS
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200284 if (PyUnicode_Check(nameobj)) {
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300285 Py_ssize_t length;
286 widename = PyUnicode_AsUnicodeAndSize(nameobj, &length);
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200287 if (widename == NULL)
288 return -1;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300289 if (wcslen(widename) != length) {
Serhiy Storchaka7e9d1d12015-04-20 10:12:28 +0300290 PyErr_SetString(PyExc_ValueError, "embedded null character");
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300291 return -1;
292 }
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200293 } else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000294#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000295 if (fd < 0)
296 {
Antoine Pitrou13348842012-01-29 18:36:34 +0100297 if (!PyUnicode_FSConverter(nameobj, &stringobj)) {
298 return -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000299 }
Antoine Pitrou13348842012-01-29 18:36:34 +0100300 name = PyBytes_AS_STRING(stringobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000301 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000302
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000303 s = mode;
304 while (*s) {
305 switch (*s++) {
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100306 case 'x':
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000307 if (rwa) {
308 bad_mode:
309 PyErr_SetString(PyExc_ValueError,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100310 "Must have exactly one of create/read/write/append "
Georg Brandl28928ae2010-10-21 13:45:52 +0000311 "mode and at most one plus");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000312 goto error;
313 }
314 rwa = 1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100315 self->created = 1;
316 self->writable = 1;
317 flags |= O_EXCL | O_CREAT;
318 break;
319 case 'r':
320 if (rwa)
321 goto bad_mode;
322 rwa = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000323 self->readable = 1;
324 break;
325 case 'w':
326 if (rwa)
327 goto bad_mode;
328 rwa = 1;
329 self->writable = 1;
330 flags |= O_CREAT | O_TRUNC;
331 break;
332 case 'a':
333 if (rwa)
334 goto bad_mode;
335 rwa = 1;
336 self->writable = 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200337 self->appending = 1;
338 flags |= O_APPEND | O_CREAT;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000339 break;
340 case 'b':
341 break;
342 case '+':
343 if (plus)
344 goto bad_mode;
345 self->readable = self->writable = 1;
346 plus = 1;
347 break;
348 default:
349 PyErr_Format(PyExc_ValueError,
350 "invalid mode: %.200s", mode);
351 goto error;
352 }
353 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000354
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000355 if (!rwa)
356 goto bad_mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000357
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000358 if (self->readable && self->writable)
359 flags |= O_RDWR;
360 else if (self->readable)
361 flags |= O_RDONLY;
362 else
363 flags |= O_WRONLY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000364
365#ifdef O_BINARY
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000366 flags |= O_BINARY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000367#endif
368
Victor Stinnerdaf45552013-08-28 00:53:59 +0200369#ifdef MS_WINDOWS
370 flags |= O_NOINHERIT;
371#elif defined(O_CLOEXEC)
372 flags |= O_CLOEXEC;
373#endif
Walter Dörwald0e411482007-06-06 16:55:38 +0000374
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000375 if (fd >= 0) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000376 self->fd = fd;
377 self->closefd = closefd;
378 }
379 else {
380 self->closefd = 1;
381 if (!closefd) {
382 PyErr_SetString(PyExc_ValueError,
383 "Cannot use closefd=False with file name");
384 goto error;
385 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000386
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000387 errno = 0;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200388 if (opener == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000389 do {
390 Py_BEGIN_ALLOW_THREADS
Thomas Helleraf2be262007-07-12 11:03:13 +0000391#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000392 if (widename != NULL)
393 self->fd = _wopen(widename, flags, 0666);
394 else
Thomas Helleraf2be262007-07-12 11:03:13 +0000395#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000396 self->fd = open(name, flags, 0666);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000397 Py_END_ALLOW_THREADS
398 } while (self->fd < 0 && errno == EINTR &&
399 !(async_err = PyErr_CheckSignals()));
Victor Stinner9672da72015-03-04 18:40:10 +0100400
401 if (async_err)
402 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200403 }
404 else {
405 PyObject *fdobj;
406
407#ifndef MS_WINDOWS
408 /* the opener may clear the atomic flag */
409 atomic_flag_works = NULL;
410#endif
411
412 fdobj = PyObject_CallFunction(opener, "Oi", nameobj, flags);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200413 if (fdobj == NULL)
414 goto error;
415 if (!PyLong_Check(fdobj)) {
416 Py_DECREF(fdobj);
417 PyErr_SetString(PyExc_TypeError,
418 "expected integer from opener");
419 goto error;
420 }
421
Serhiy Storchaka78980432013-01-15 01:12:17 +0200422 self->fd = _PyLong_AsInt(fdobj);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200423 Py_DECREF(fdobj);
Barry Warsaw480e2852016-06-08 17:47:26 -0400424 if (self->fd < 0) {
425 if (!PyErr_Occurred()) {
Barry Warsawefe72562016-06-08 17:55:49 -0400426 /* The opener returned a negative but didn't set an
427 exception. See issue #27066 */
Barry Warsaw480e2852016-06-08 17:47:26 -0400428 PyErr_Format(PyExc_ValueError,
429 "opener returned %d", self->fd);
430 }
Ross Lagerwall59142db2011-10-31 20:34:46 +0200431 goto error;
432 }
433 }
434
Hynek Schlawack7f59fd72012-06-22 09:32:22 +0200435 fd_is_own = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000436 if (self->fd < 0) {
Victor Stinner9672da72015-03-04 18:40:10 +0100437 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000438 goto error;
439 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200440
441#ifndef MS_WINDOWS
442 if (_Py_set_inheritable(self->fd, 0, atomic_flag_works) < 0)
443 goto error;
444#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000445 }
Antoine Pitroude687222014-06-29 20:07:28 -0400446
447 self->blksize = DEFAULT_BUFFER_SIZE;
Martin Panter0bb62b12015-12-06 03:15:05 +0000448 Py_BEGIN_ALLOW_THREADS
449 fstat_result = _Py_fstat_noraise(self->fd, &fdfstat);
450 Py_END_ALLOW_THREADS
451 if (fstat_result < 0) {
Martin Panter49d3db92015-12-06 11:12:15 +0000452 /* Tolerate fstat() errors other than EBADF. See Issue #25717, where
453 an anonymous file on a Virtual Box shared folder filesystem would
454 raise ENOENT. */
Martin Panter0bb62b12015-12-06 03:15:05 +0000455#ifdef MS_WINDOWS
456 if (GetLastError() == ERROR_INVALID_HANDLE) {
457 PyErr_SetFromWindowsErr(0);
458#else
459 if (errno == EBADF) {
460 PyErr_SetFromErrno(PyExc_OSError);
461#endif
462 goto error;
463 }
Antoine Pitroude687222014-06-29 20:07:28 -0400464 }
Martin Panter0bb62b12015-12-06 03:15:05 +0000465 else {
466#if defined(S_ISDIR) && defined(EISDIR)
467 /* On Unix, open will succeed for directories.
468 In Python, there should be no file objects referring to
469 directories, so we need a check. */
470 if (S_ISDIR(fdfstat.st_mode)) {
471 errno = EISDIR;
472 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);
473 goto error;
474 }
Antoine Pitroude687222014-06-29 20:07:28 -0400475#endif /* defined(S_ISDIR) */
476#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin Panter0bb62b12015-12-06 03:15:05 +0000477 if (fdfstat.st_blksize > 1)
478 self->blksize = fdfstat.st_blksize;
Antoine Pitroude687222014-06-29 20:07:28 -0400479#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Martin Panter0bb62b12015-12-06 03:15:05 +0000480 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000481
Victor Stinner89e34362011-01-07 18:47:22 +0000482#if defined(MS_WINDOWS) || defined(__CYGWIN__)
483 /* don't translate newlines (\r\n <=> \n) */
484 _setmode(self->fd, O_BINARY);
485#endif
486
Victor Stinnerd9d04192013-11-06 23:50:10 +0100487 if (_PyObject_SetAttrId((PyObject *)self, &PyId_name, nameobj) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000488 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000489
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200490 if (self->appending) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000491 /* For consistent behaviour, we explicitly seek to the
492 end of file (otherwise, it might be done only on the
493 first write()). */
494 PyObject *pos = portable_lseek(self->fd, NULL, 2);
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200495 if (pos == NULL)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000496 goto error;
497 Py_DECREF(pos);
498 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000499
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000500 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000501
502 error:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000503 ret = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200504 if (!fd_is_own)
505 self->fd = -1;
Benjamin Petersonbbb04122010-10-30 23:16:28 +0000506 if (self->fd >= 0)
507 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000508
Guido van Rossuma9e20242007-03-08 00:43:48 +0000509 done:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000510 Py_CLEAR(stringobj);
511 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000512}
513
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000514static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000515fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000516{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000517 Py_VISIT(self->dict);
518 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000519}
520
521static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000522fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000523{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000524 Py_CLEAR(self->dict);
525 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000526}
527
Guido van Rossuma9e20242007-03-08 00:43:48 +0000528static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000529fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000530{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200531 self->finalizing = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000532 if (_PyIOBase_finalize((PyObject *) self) < 0)
533 return;
534 _PyObject_GC_UNTRACK(self);
535 if (self->weakreflist != NULL)
536 PyObject_ClearWeakRefs((PyObject *) self);
537 Py_CLEAR(self->dict);
538 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000539}
540
541static PyObject *
542err_closed(void)
543{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000544 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
545 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000546}
547
548static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000549err_mode(char *action)
550{
Antoine Pitrou712cb732013-12-21 15:51:54 +0100551 _PyIO_State *state = IO_STATE();
552 if (state != NULL)
553 PyErr_Format(state->unsupported_operation,
554 "File not open for %s", action);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000555 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000556}
557
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300558/*[clinic input]
559_io.FileIO.fileno
560
561Return the underlying file descriptor (an integer).
562[clinic start generated code]*/
563
Guido van Rossum53807da2007-04-10 19:01:47 +0000564static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300565_io_FileIO_fileno_impl(fileio *self)
566/*[clinic end generated code: output=a9626ce5398ece90 input=0b9b2de67335ada3]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000567{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000568 if (self->fd < 0)
569 return err_closed();
570 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000571}
572
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300573/*[clinic input]
574_io.FileIO.readable
575
576True if file was opened in a read mode.
577[clinic start generated code]*/
578
Guido van Rossuma9e20242007-03-08 00:43:48 +0000579static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300580_io_FileIO_readable_impl(fileio *self)
581/*[clinic end generated code: output=640744a6150fe9ba input=a3fdfed6eea721c5]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000582{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000583 if (self->fd < 0)
584 return err_closed();
585 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000586}
587
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300588/*[clinic input]
589_io.FileIO.writable
590
591True if file was opened in a write mode.
592[clinic start generated code]*/
593
Guido van Rossuma9e20242007-03-08 00:43:48 +0000594static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300595_io_FileIO_writable_impl(fileio *self)
596/*[clinic end generated code: output=96cefc5446e89977 input=c204a808ca2e1748]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000597{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000598 if (self->fd < 0)
599 return err_closed();
600 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000601}
602
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300603/*[clinic input]
604_io.FileIO.seekable
605
606True if file supports random-access.
607[clinic start generated code]*/
608
Guido van Rossuma9e20242007-03-08 00:43:48 +0000609static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300610_io_FileIO_seekable_impl(fileio *self)
611/*[clinic end generated code: output=47909ca0a42e9287 input=c8e5554d2fd63c7f]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000612{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000613 if (self->fd < 0)
614 return err_closed();
615 if (self->seekable < 0) {
616 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
617 if (pos == NULL) {
618 PyErr_Clear();
619 self->seekable = 0;
620 } else {
621 Py_DECREF(pos);
622 self->seekable = 1;
623 }
624 }
625 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000626}
627
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300628/*[clinic input]
629_io.FileIO.readinto
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700630 buffer: Py_buffer(accept={rwbuffer})
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300631 /
632
633Same as RawIOBase.readinto().
634[clinic start generated code]*/
635
Guido van Rossuma9e20242007-03-08 00:43:48 +0000636static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300637_io_FileIO_readinto_impl(fileio *self, Py_buffer *buffer)
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700638/*[clinic end generated code: output=b01a5a22c8415cb4 input=4721d7b68b154eaf]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000639{
Victor Stinner66aab0c2015-03-19 22:53:20 +0100640 Py_ssize_t n;
641 int err;
Guido van Rossum53807da2007-04-10 19:01:47 +0000642
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000643 if (self->fd < 0)
644 return err_closed();
645 if (!self->readable)
646 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000647
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300648 n = _Py_read(self->fd, buffer->buf, buffer->len);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100649 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100650 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100651
652 if (n == -1) {
653 if (err == EAGAIN) {
654 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000655 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100656 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000657 return NULL;
658 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000659
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000660 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000661}
662
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000663static size_t
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100664new_buffersize(fileio *self, size_t currentsize)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000665{
Antoine Pitroua3f44572012-04-17 13:50:58 +0200666 size_t addend;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100667
Nadeem Vawdad41a98b2011-10-13 13:34:16 +0200668 /* Expand the buffer by an amount proportional to the current size,
Antoine Pitroua3f44572012-04-17 13:50:58 +0200669 giving us amortized linear-time behavior. For bigger sizes, use a
670 less-than-double growth factor to avoid excessive allocation. */
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100671 assert(currentsize <= PY_SSIZE_T_MAX);
Antoine Pitroua3f44572012-04-17 13:50:58 +0200672 if (currentsize > 65536)
673 addend = currentsize >> 3;
674 else
675 addend = 256 + currentsize;
676 if (addend < SMALLCHUNK)
677 /* Avoid tiny read() calls. */
678 addend = SMALLCHUNK;
679 return addend + currentsize;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000680}
681
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300682/*[clinic input]
683_io.FileIO.readall
684
685Read all data from the file, returned as bytes.
686
687In non-blocking mode, returns as much as is immediately available,
688or None if no data is available. Return an empty bytes object at EOF.
689[clinic start generated code]*/
690
Guido van Rossum7165cb12007-07-10 06:54:34 +0000691static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300692_io_FileIO_readall_impl(fileio *self)
693/*[clinic end generated code: output=faa0292b213b4022 input=dbdc137f55602834]*/
Guido van Rossum7165cb12007-07-10 06:54:34 +0000694{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200695 struct _Py_stat_struct status;
Victor Stinnera2a64772011-10-11 22:45:02 +0200696 Py_off_t pos, end;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000697 PyObject *result;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100698 Py_ssize_t bytes_read = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100699 Py_ssize_t n;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100700 size_t bufsize;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000701
Victor Stinnerb79f28c2011-05-25 22:09:03 +0200702 if (self->fd < 0)
703 return err_closed();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000704 if (!_PyVerify_fd(self->fd))
705 return PyErr_SetFromErrno(PyExc_IOError);
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000706
Steve Dower8fc89802015-04-12 00:26:27 -0400707 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +0200708#ifdef MS_WINDOWS
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200709 pos = _lseeki64(self->fd, 0L, SEEK_CUR);
710#else
711 pos = lseek(self->fd, 0L, SEEK_CUR);
712#endif
Steve Dower8fc89802015-04-12 00:26:27 -0400713 _Py_END_SUPPRESS_IPH
714
Victor Stinnere134a7f2015-03-30 10:09:31 +0200715 if (_Py_fstat_noraise(self->fd, &status) == 0)
716 end = status.st_size;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200717 else
Victor Stinnera2a64772011-10-11 22:45:02 +0200718 end = (Py_off_t)-1;
Christian Heimesa872de52008-12-05 08:26:55 +0000719
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100720 if (end > 0 && end >= pos && pos >= 0 && end - pos < PY_SSIZE_T_MAX) {
721 /* This is probably a real file, so we try to allocate a
722 buffer one byte larger than the rest of the file. If the
723 calculation is right then we should get EOF without having
724 to enlarge the buffer. */
725 bufsize = (size_t)(end - pos + 1);
726 } else {
727 bufsize = SMALLCHUNK;
728 }
729
730 result = PyBytes_FromStringAndSize(NULL, bufsize);
731 if (result == NULL)
732 return NULL;
733
734 while (1) {
735 if (bytes_read >= (Py_ssize_t)bufsize) {
736 bufsize = new_buffersize(self, bytes_read);
737 if (bufsize > PY_SSIZE_T_MAX || bufsize <= 0) {
738 PyErr_SetString(PyExc_OverflowError,
739 "unbounded read returned more bytes "
Serhiy Storchakab817b772015-04-10 02:18:44 +0300740 "than a Python bytes object can hold");
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100741 Py_DECREF(result);
742 return NULL;
743 }
744
745 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
746 if (_PyBytes_Resize(&result, bufsize) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000747 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000748 }
749 }
Victor Stinner9672da72015-03-04 18:40:10 +0100750
Victor Stinner66aab0c2015-03-19 22:53:20 +0100751 n = _Py_read(self->fd,
752 PyBytes_AS_STRING(result) + bytes_read,
753 bufsize - bytes_read);
Victor Stinner9672da72015-03-04 18:40:10 +0100754
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000755 if (n == 0)
756 break;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100757 if (n == -1) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000758 if (errno == EAGAIN) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100759 PyErr_Clear();
Victor Stinnere10920f2014-07-02 22:59:31 +0200760 if (bytes_read > 0)
761 break;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000762 Py_DECREF(result);
763 Py_RETURN_NONE;
764 }
765 Py_DECREF(result);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000766 return NULL;
767 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100768 bytes_read += n;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200769 pos += n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000770 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000771
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100772 if (PyBytes_GET_SIZE(result) > bytes_read) {
773 if (_PyBytes_Resize(&result, bytes_read) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000774 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000775 }
776 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000777}
778
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300779/*[clinic input]
780_io.FileIO.read
781 size: io_ssize_t = -1
782 /
783
784Read at most size bytes, returned as bytes.
785
786Only makes one system call, so less data may be returned than requested.
787In non-blocking mode, returns None if no data is available.
788Return an empty bytes object at EOF.
789[clinic start generated code]*/
790
Guido van Rossuma9e20242007-03-08 00:43:48 +0000791static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300792_io_FileIO_read_impl(fileio *self, Py_ssize_t size)
793/*[clinic end generated code: output=42528d39dd0ca641 input=5c6caa5490c13a9b]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000794{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000795 char *ptr;
796 Py_ssize_t n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000797 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000798
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000799 if (self->fd < 0)
800 return err_closed();
801 if (!self->readable)
802 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000803
Victor Stinner66aab0c2015-03-19 22:53:20 +0100804 if (size < 0)
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300805 return _io_FileIO_readall_impl(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000806
Victor Stinner14b9b112013-06-25 00:37:25 +0200807#ifdef MS_WINDOWS
Victor Stinner66aab0c2015-03-19 22:53:20 +0100808 /* On Windows, the count parameter of read() is an int */
Victor Stinnerc655a722011-07-05 11:31:49 +0200809 if (size > INT_MAX)
810 size = INT_MAX;
811#endif
Victor Stinner66aab0c2015-03-19 22:53:20 +0100812
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000813 bytes = PyBytes_FromStringAndSize(NULL, size);
814 if (bytes == NULL)
815 return NULL;
816 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000817
Victor Stinner66aab0c2015-03-19 22:53:20 +0100818 n = _Py_read(self->fd, ptr, size);
819 if (n == -1) {
820 /* copy errno because Py_DECREF() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100821 int err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000822 Py_DECREF(bytes);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100823 if (err == EAGAIN) {
824 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000825 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100826 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000827 return NULL;
828 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000829
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000830 if (n != size) {
831 if (_PyBytes_Resize(&bytes, n) < 0) {
Victor Stinner85c761d2013-07-16 21:36:02 +0200832 Py_CLEAR(bytes);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000833 return NULL;
834 }
835 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000836
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000837 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000838}
839
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300840/*[clinic input]
841_io.FileIO.write
842 b: Py_buffer
843 /
844
Martin Panter6bb91f32016-05-28 00:41:57 +0000845Write buffer b to file, return number of bytes written.
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300846
847Only makes one system call, so not all of the data may be written.
848The number of bytes actually written is returned. In non-blocking mode,
849returns None if the write would block.
850[clinic start generated code]*/
851
Guido van Rossuma9e20242007-03-08 00:43:48 +0000852static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300853_io_FileIO_write_impl(fileio *self, Py_buffer *b)
Martin Panter6bb91f32016-05-28 00:41:57 +0000854/*[clinic end generated code: output=b4059db3d363a2f7 input=6e7908b36f0ce74f]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000855{
Victor Stinner66aab0c2015-03-19 22:53:20 +0100856 Py_ssize_t n;
857 int err;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000858
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000859 if (self->fd < 0)
860 return err_closed();
861 if (!self->writable)
862 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000863
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300864 n = _Py_write(self->fd, b->buf, b->len);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100865 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100866 err = errno;
Martin v. Löwis423be952008-08-13 15:53:07 +0000867
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000868 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100869 if (err == EAGAIN) {
870 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000871 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100872 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000873 return NULL;
874 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000875
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000876 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000877}
878
Guido van Rossum53807da2007-04-10 19:01:47 +0000879/* XXX Windows support below is likely incomplete */
880
Guido van Rossum53807da2007-04-10 19:01:47 +0000881/* Cribbed from posix_lseek() */
882static PyObject *
883portable_lseek(int fd, PyObject *posobj, int whence)
884{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000885 Py_off_t pos, res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000886
887#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000888 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
889 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000890#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000891 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000892#endif
893#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000894 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000895#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000896#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000897 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000898#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000899 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000900#endif /* SEEK_SET */
901
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000902 if (posobj == NULL)
903 pos = 0;
904 else {
905 if(PyFloat_Check(posobj)) {
906 PyErr_SetString(PyExc_TypeError, "an integer is required");
907 return NULL;
908 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000909#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000910 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000911#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000912 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000913#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000914 if (PyErr_Occurred())
915 return NULL;
916 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000917
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000918 if (_PyVerify_fd(fd)) {
919 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -0400920 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +0200921#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000922 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000923#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000924 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000925#endif
Steve Dower8fc89802015-04-12 00:26:27 -0400926 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000927 Py_END_ALLOW_THREADS
928 } else
929 res = -1;
930 if (res < 0)
931 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000932
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000933#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000934 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000935#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000936 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000937#endif
938}
939
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300940/*[clinic input]
941_io.FileIO.seek
942 pos: object
943 whence: int = 0
944 /
Guido van Rossuma9e20242007-03-08 00:43:48 +0000945
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300946Move to new file position and return the file position.
947
948Argument offset is a byte count. Optional argument whence defaults to
949SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values
950are SEEK_CUR or 1 (move relative to current position, positive or negative),
951and SEEK_END or 2 (move relative to end of file, usually negative, although
952many platforms allow seeking beyond the end of a file).
953
954Note that not all file objects are seekable.
955[clinic start generated code]*/
956
957static PyObject *
958_io_FileIO_seek_impl(fileio *self, PyObject *pos, int whence)
959/*[clinic end generated code: output=c976acdf054e6655 input=0439194b0774d454]*/
960{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000961 if (self->fd < 0)
962 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000963
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300964 return portable_lseek(self->fd, pos, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000965}
966
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300967/*[clinic input]
968_io.FileIO.tell
969
970Current file position.
971
972Can raise OSError for non seekable files.
973[clinic start generated code]*/
974
Guido van Rossuma9e20242007-03-08 00:43:48 +0000975static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300976_io_FileIO_tell_impl(fileio *self)
977/*[clinic end generated code: output=ffe2147058809d0b input=807e24ead4cec2f9]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000978{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000979 if (self->fd < 0)
980 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000981
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000982 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000983}
984
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000985#ifdef HAVE_FTRUNCATE
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300986/*[clinic input]
987_io.FileIO.truncate
988 size as posobj: object = NULL
989 /
990
991Truncate the file to at most size bytes and return the truncated size.
992
993Size defaults to the current file position, as returned by tell().
994The current file position is changed to the value of size.
995[clinic start generated code]*/
996
Guido van Rossuma9e20242007-03-08 00:43:48 +0000997static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300998_io_FileIO_truncate_impl(fileio *self, PyObject *posobj)
999/*[clinic end generated code: output=e49ca7a916c176fa input=9026af44686b7318]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +00001000{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001001 Py_off_t pos;
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001002 int ret;
1003 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001004
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001005 fd = self->fd;
1006 if (fd < 0)
1007 return err_closed();
1008 if (!self->writable)
1009 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001010
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001011 if (posobj == Py_None || posobj == NULL) {
1012 /* Get the current position. */
1013 posobj = portable_lseek(fd, NULL, 1);
1014 if (posobj == NULL)
1015 return NULL;
1016 }
1017 else {
1018 Py_INCREF(posobj);
1019 }
Guido van Rossum53807da2007-04-10 19:01:47 +00001020
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001021#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001022 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001023#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001024 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001025#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001026 if (PyErr_Occurred()){
1027 Py_DECREF(posobj);
1028 return NULL;
1029 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001030
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001031 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04001032 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001033 errno = 0;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07001034#ifdef MS_WINDOWS
1035 ret = _chsize_s(fd, pos);
1036#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001037 ret = ftruncate(fd, pos);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07001038#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04001039 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001040 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001041
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001042 if (ret != 0) {
1043 Py_DECREF(posobj);
1044 PyErr_SetFromErrno(PyExc_IOError);
1045 return NULL;
1046 }
Guido van Rossuma9e20242007-03-08 00:43:48 +00001047
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001048 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001049}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001050#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +00001051
1052static char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001053mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +00001054{
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001055 if (self->created) {
1056 if (self->readable)
1057 return "xb+";
1058 else
1059 return "xb";
1060 }
Antoine Pitroue93b63b2013-09-04 20:46:33 +02001061 if (self->appending) {
1062 if (self->readable)
1063 return "ab+";
1064 else
1065 return "ab";
1066 }
1067 else if (self->readable) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001068 if (self->writable)
1069 return "rb+";
1070 else
1071 return "rb";
1072 }
1073 else
1074 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +00001075}
Guido van Rossuma9e20242007-03-08 00:43:48 +00001076
1077static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001078fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001079{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001080 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001081
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001082 if (self->fd < 0)
1083 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +00001084
Martin v. Löwis767046a2011-10-14 15:35:36 +02001085 nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001086 if (nameobj == NULL) {
1087 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1088 PyErr_Clear();
1089 else
1090 return NULL;
Robert Collins933430a2014-10-18 13:32:43 +13001091 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +02001092 "<_io.FileIO fd=%d mode='%s' closefd=%s>",
1093 self->fd, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001094 }
1095 else {
Robert Collins933430a2014-10-18 13:32:43 +13001096 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +02001097 "<_io.FileIO name=%R mode='%s' closefd=%s>",
1098 nameobj, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001099 Py_DECREF(nameobj);
1100 }
1101 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001102}
1103
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001104/*[clinic input]
1105_io.FileIO.isatty
1106
1107True if the file is connected to a TTY device.
1108[clinic start generated code]*/
1109
Guido van Rossuma9e20242007-03-08 00:43:48 +00001110static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001111_io_FileIO_isatty_impl(fileio *self)
1112/*[clinic end generated code: output=932c39924e9a8070 input=cd94ca1f5e95e843]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +00001113{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001114 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +00001115
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001116 if (self->fd < 0)
1117 return err_closed();
1118 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001119 _Py_BEGIN_SUPPRESS_IPH
1120 if (_PyVerify_fd(self->fd))
1121 res = isatty(self->fd);
1122 else
1123 res = 0;
1124 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001125 Py_END_ALLOW_THREADS
1126 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001127}
1128
Antoine Pitrou243757e2010-11-05 21:15:39 +00001129static PyObject *
1130fileio_getstate(fileio *self)
1131{
1132 PyErr_Format(PyExc_TypeError,
1133 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
1134 return NULL;
1135}
1136
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001137#include "clinic/fileio.c.h"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001138
1139static PyMethodDef fileio_methods[] = {
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001140 _IO_FILEIO_READ_METHODDEF
1141 _IO_FILEIO_READALL_METHODDEF
1142 _IO_FILEIO_READINTO_METHODDEF
1143 _IO_FILEIO_WRITE_METHODDEF
1144 _IO_FILEIO_SEEK_METHODDEF
1145 _IO_FILEIO_TELL_METHODDEF
1146 _IO_FILEIO_TRUNCATE_METHODDEF
1147 _IO_FILEIO_CLOSE_METHODDEF
1148 _IO_FILEIO_SEEKABLE_METHODDEF
1149 _IO_FILEIO_READABLE_METHODDEF
1150 _IO_FILEIO_WRITABLE_METHODDEF
1151 _IO_FILEIO_FILENO_METHODDEF
1152 _IO_FILEIO_ISATTY_METHODDEF
Antoine Pitroue033e062010-10-29 10:38:18 +00001153 {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
Antoine Pitrou243757e2010-11-05 21:15:39 +00001154 {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001155 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001156};
1157
Guido van Rossum53807da2007-04-10 19:01:47 +00001158/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1159
Guido van Rossumb0428152007-04-08 17:44:42 +00001160static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001161get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001162{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001163 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001164}
1165
1166static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001167get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001168{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001169 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001170}
1171
1172static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001173get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001174{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001175 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001176}
1177
1178static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001179 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1180 {"closefd", (getter)get_closefd, NULL,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001181 "True if the file descriptor will be closed by close()."},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001182 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1183 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001184};
1185
Antoine Pitrou796564c2013-07-30 19:59:21 +02001186static PyMemberDef fileio_members[] = {
Antoine Pitroude687222014-06-29 20:07:28 -04001187 {"_blksize", T_UINT, offsetof(fileio, blksize), 0},
Antoine Pitrou796564c2013-07-30 19:59:21 +02001188 {"_finalizing", T_BOOL, offsetof(fileio, finalizing), 0},
1189 {NULL}
1190};
1191
Guido van Rossuma9e20242007-03-08 00:43:48 +00001192PyTypeObject PyFileIO_Type = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001193 PyVarObject_HEAD_INIT(NULL, 0)
1194 "_io.FileIO",
1195 sizeof(fileio),
1196 0,
1197 (destructor)fileio_dealloc, /* tp_dealloc */
1198 0, /* tp_print */
1199 0, /* tp_getattr */
1200 0, /* tp_setattr */
1201 0, /* tp_reserved */
1202 (reprfunc)fileio_repr, /* tp_repr */
1203 0, /* tp_as_number */
1204 0, /* tp_as_sequence */
1205 0, /* tp_as_mapping */
1206 0, /* tp_hash */
1207 0, /* tp_call */
1208 0, /* tp_str */
1209 PyObject_GenericGetAttr, /* tp_getattro */
1210 0, /* tp_setattro */
1211 0, /* tp_as_buffer */
1212 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +02001213 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001214 _io_FileIO___init____doc__, /* tp_doc */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001215 (traverseproc)fileio_traverse, /* tp_traverse */
1216 (inquiry)fileio_clear, /* tp_clear */
1217 0, /* tp_richcompare */
1218 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1219 0, /* tp_iter */
1220 0, /* tp_iternext */
1221 fileio_methods, /* tp_methods */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001222 fileio_members, /* tp_members */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001223 fileio_getsetlist, /* tp_getset */
1224 0, /* tp_base */
1225 0, /* tp_dict */
1226 0, /* tp_descr_get */
1227 0, /* tp_descr_set */
1228 offsetof(fileio, dict), /* tp_dictoffset */
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001229 _io_FileIO___init__, /* tp_init */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001230 PyType_GenericAlloc, /* tp_alloc */
1231 fileio_new, /* tp_new */
1232 PyObject_GC_Del, /* tp_free */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001233 0, /* tp_is_gc */
1234 0, /* tp_bases */
1235 0, /* tp_mro */
1236 0, /* tp_cache */
1237 0, /* tp_subclasses */
1238 0, /* tp_weaklist */
1239 0, /* tp_del */
1240 0, /* tp_version_tag */
1241 0, /* tp_finalize */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001242};