blob: dbd604a1168907baf3bc9986875f325a42f88a39 [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);
424 if (self->fd == -1) {
425 goto error;
426 }
427 }
428
Hynek Schlawack7f59fd72012-06-22 09:32:22 +0200429 fd_is_own = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000430 if (self->fd < 0) {
Victor Stinner9672da72015-03-04 18:40:10 +0100431 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000432 goto error;
433 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200434
435#ifndef MS_WINDOWS
436 if (_Py_set_inheritable(self->fd, 0, atomic_flag_works) < 0)
437 goto error;
438#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000439 }
Antoine Pitroude687222014-06-29 20:07:28 -0400440
441 self->blksize = DEFAULT_BUFFER_SIZE;
Martin Panter0bb62b12015-12-06 03:15:05 +0000442 Py_BEGIN_ALLOW_THREADS
443 fstat_result = _Py_fstat_noraise(self->fd, &fdfstat);
444 Py_END_ALLOW_THREADS
445 if (fstat_result < 0) {
Martin Panter49d3db92015-12-06 11:12:15 +0000446 /* Tolerate fstat() errors other than EBADF. See Issue #25717, where
447 an anonymous file on a Virtual Box shared folder filesystem would
448 raise ENOENT. */
Martin Panter0bb62b12015-12-06 03:15:05 +0000449#ifdef MS_WINDOWS
450 if (GetLastError() == ERROR_INVALID_HANDLE) {
451 PyErr_SetFromWindowsErr(0);
452#else
453 if (errno == EBADF) {
454 PyErr_SetFromErrno(PyExc_OSError);
455#endif
456 goto error;
457 }
Antoine Pitroude687222014-06-29 20:07:28 -0400458 }
Martin Panter0bb62b12015-12-06 03:15:05 +0000459 else {
460#if defined(S_ISDIR) && defined(EISDIR)
461 /* On Unix, open will succeed for directories.
462 In Python, there should be no file objects referring to
463 directories, so we need a check. */
464 if (S_ISDIR(fdfstat.st_mode)) {
465 errno = EISDIR;
466 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);
467 goto error;
468 }
Antoine Pitroude687222014-06-29 20:07:28 -0400469#endif /* defined(S_ISDIR) */
470#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin Panter0bb62b12015-12-06 03:15:05 +0000471 if (fdfstat.st_blksize > 1)
472 self->blksize = fdfstat.st_blksize;
Antoine Pitroude687222014-06-29 20:07:28 -0400473#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Martin Panter0bb62b12015-12-06 03:15:05 +0000474 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000475
Victor Stinner89e34362011-01-07 18:47:22 +0000476#if defined(MS_WINDOWS) || defined(__CYGWIN__)
477 /* don't translate newlines (\r\n <=> \n) */
478 _setmode(self->fd, O_BINARY);
479#endif
480
Victor Stinnerd9d04192013-11-06 23:50:10 +0100481 if (_PyObject_SetAttrId((PyObject *)self, &PyId_name, nameobj) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000482 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000483
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200484 if (self->appending) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000485 /* For consistent behaviour, we explicitly seek to the
486 end of file (otherwise, it might be done only on the
487 first write()). */
488 PyObject *pos = portable_lseek(self->fd, NULL, 2);
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200489 if (pos == NULL)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000490 goto error;
491 Py_DECREF(pos);
492 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000493
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000494 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000495
496 error:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000497 ret = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200498 if (!fd_is_own)
499 self->fd = -1;
Benjamin Petersonbbb04122010-10-30 23:16:28 +0000500 if (self->fd >= 0)
501 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000502
Guido van Rossuma9e20242007-03-08 00:43:48 +0000503 done:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000504 Py_CLEAR(stringobj);
505 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000506}
507
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000508static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000509fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000510{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000511 Py_VISIT(self->dict);
512 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000513}
514
515static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000516fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000517{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000518 Py_CLEAR(self->dict);
519 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000520}
521
Guido van Rossuma9e20242007-03-08 00:43:48 +0000522static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000523fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000524{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200525 self->finalizing = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000526 if (_PyIOBase_finalize((PyObject *) self) < 0)
527 return;
528 _PyObject_GC_UNTRACK(self);
529 if (self->weakreflist != NULL)
530 PyObject_ClearWeakRefs((PyObject *) self);
531 Py_CLEAR(self->dict);
532 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000533}
534
535static PyObject *
536err_closed(void)
537{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000538 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
539 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000540}
541
542static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000543err_mode(char *action)
544{
Antoine Pitrou712cb732013-12-21 15:51:54 +0100545 _PyIO_State *state = IO_STATE();
546 if (state != NULL)
547 PyErr_Format(state->unsupported_operation,
548 "File not open for %s", action);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000549 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000550}
551
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300552/*[clinic input]
553_io.FileIO.fileno
554
555Return the underlying file descriptor (an integer).
556[clinic start generated code]*/
557
Guido van Rossum53807da2007-04-10 19:01:47 +0000558static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300559_io_FileIO_fileno_impl(fileio *self)
560/*[clinic end generated code: output=a9626ce5398ece90 input=0b9b2de67335ada3]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000561{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000562 if (self->fd < 0)
563 return err_closed();
564 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000565}
566
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300567/*[clinic input]
568_io.FileIO.readable
569
570True if file was opened in a read mode.
571[clinic start generated code]*/
572
Guido van Rossuma9e20242007-03-08 00:43:48 +0000573static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300574_io_FileIO_readable_impl(fileio *self)
575/*[clinic end generated code: output=640744a6150fe9ba input=a3fdfed6eea721c5]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000576{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000577 if (self->fd < 0)
578 return err_closed();
579 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000580}
581
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300582/*[clinic input]
583_io.FileIO.writable
584
585True if file was opened in a write mode.
586[clinic start generated code]*/
587
Guido van Rossuma9e20242007-03-08 00:43:48 +0000588static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300589_io_FileIO_writable_impl(fileio *self)
590/*[clinic end generated code: output=96cefc5446e89977 input=c204a808ca2e1748]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000591{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000592 if (self->fd < 0)
593 return err_closed();
594 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000595}
596
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300597/*[clinic input]
598_io.FileIO.seekable
599
600True if file supports random-access.
601[clinic start generated code]*/
602
Guido van Rossuma9e20242007-03-08 00:43:48 +0000603static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300604_io_FileIO_seekable_impl(fileio *self)
605/*[clinic end generated code: output=47909ca0a42e9287 input=c8e5554d2fd63c7f]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000606{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000607 if (self->fd < 0)
608 return err_closed();
609 if (self->seekable < 0) {
610 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
611 if (pos == NULL) {
612 PyErr_Clear();
613 self->seekable = 0;
614 } else {
615 Py_DECREF(pos);
616 self->seekable = 1;
617 }
618 }
619 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000620}
621
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300622/*[clinic input]
623_io.FileIO.readinto
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700624 buffer: Py_buffer(accept={rwbuffer})
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300625 /
626
627Same as RawIOBase.readinto().
628[clinic start generated code]*/
629
Guido van Rossuma9e20242007-03-08 00:43:48 +0000630static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300631_io_FileIO_readinto_impl(fileio *self, Py_buffer *buffer)
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700632/*[clinic end generated code: output=b01a5a22c8415cb4 input=4721d7b68b154eaf]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000633{
Victor Stinner66aab0c2015-03-19 22:53:20 +0100634 Py_ssize_t n;
635 int err;
Guido van Rossum53807da2007-04-10 19:01:47 +0000636
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000637 if (self->fd < 0)
638 return err_closed();
639 if (!self->readable)
640 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000641
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300642 n = _Py_read(self->fd, buffer->buf, buffer->len);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100643 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100644 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100645
646 if (n == -1) {
647 if (err == EAGAIN) {
648 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000649 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100650 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000651 return NULL;
652 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000653
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000654 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000655}
656
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000657static size_t
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100658new_buffersize(fileio *self, size_t currentsize)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000659{
Antoine Pitroua3f44572012-04-17 13:50:58 +0200660 size_t addend;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100661
Nadeem Vawdad41a98b2011-10-13 13:34:16 +0200662 /* Expand the buffer by an amount proportional to the current size,
Antoine Pitroua3f44572012-04-17 13:50:58 +0200663 giving us amortized linear-time behavior. For bigger sizes, use a
664 less-than-double growth factor to avoid excessive allocation. */
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100665 assert(currentsize <= PY_SSIZE_T_MAX);
Antoine Pitroua3f44572012-04-17 13:50:58 +0200666 if (currentsize > 65536)
667 addend = currentsize >> 3;
668 else
669 addend = 256 + currentsize;
670 if (addend < SMALLCHUNK)
671 /* Avoid tiny read() calls. */
672 addend = SMALLCHUNK;
673 return addend + currentsize;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000674}
675
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300676/*[clinic input]
677_io.FileIO.readall
678
679Read all data from the file, returned as bytes.
680
681In non-blocking mode, returns as much as is immediately available,
682or None if no data is available. Return an empty bytes object at EOF.
683[clinic start generated code]*/
684
Guido van Rossum7165cb12007-07-10 06:54:34 +0000685static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300686_io_FileIO_readall_impl(fileio *self)
687/*[clinic end generated code: output=faa0292b213b4022 input=dbdc137f55602834]*/
Guido van Rossum7165cb12007-07-10 06:54:34 +0000688{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200689 struct _Py_stat_struct status;
Victor Stinnera2a64772011-10-11 22:45:02 +0200690 Py_off_t pos, end;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000691 PyObject *result;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100692 Py_ssize_t bytes_read = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100693 Py_ssize_t n;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100694 size_t bufsize;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000695
Victor Stinnerb79f28c2011-05-25 22:09:03 +0200696 if (self->fd < 0)
697 return err_closed();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000698 if (!_PyVerify_fd(self->fd))
699 return PyErr_SetFromErrno(PyExc_IOError);
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000700
Steve Dower8fc89802015-04-12 00:26:27 -0400701 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +0200702#ifdef MS_WINDOWS
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200703 pos = _lseeki64(self->fd, 0L, SEEK_CUR);
704#else
705 pos = lseek(self->fd, 0L, SEEK_CUR);
706#endif
Steve Dower8fc89802015-04-12 00:26:27 -0400707 _Py_END_SUPPRESS_IPH
708
Victor Stinnere134a7f2015-03-30 10:09:31 +0200709 if (_Py_fstat_noraise(self->fd, &status) == 0)
710 end = status.st_size;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200711 else
Victor Stinnera2a64772011-10-11 22:45:02 +0200712 end = (Py_off_t)-1;
Christian Heimesa872de52008-12-05 08:26:55 +0000713
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100714 if (end > 0 && end >= pos && pos >= 0 && end - pos < PY_SSIZE_T_MAX) {
715 /* This is probably a real file, so we try to allocate a
716 buffer one byte larger than the rest of the file. If the
717 calculation is right then we should get EOF without having
718 to enlarge the buffer. */
719 bufsize = (size_t)(end - pos + 1);
720 } else {
721 bufsize = SMALLCHUNK;
722 }
723
724 result = PyBytes_FromStringAndSize(NULL, bufsize);
725 if (result == NULL)
726 return NULL;
727
728 while (1) {
729 if (bytes_read >= (Py_ssize_t)bufsize) {
730 bufsize = new_buffersize(self, bytes_read);
731 if (bufsize > PY_SSIZE_T_MAX || bufsize <= 0) {
732 PyErr_SetString(PyExc_OverflowError,
733 "unbounded read returned more bytes "
Serhiy Storchakab817b772015-04-10 02:18:44 +0300734 "than a Python bytes object can hold");
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100735 Py_DECREF(result);
736 return NULL;
737 }
738
739 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
740 if (_PyBytes_Resize(&result, bufsize) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000741 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000742 }
743 }
Victor Stinner9672da72015-03-04 18:40:10 +0100744
Victor Stinner66aab0c2015-03-19 22:53:20 +0100745 n = _Py_read(self->fd,
746 PyBytes_AS_STRING(result) + bytes_read,
747 bufsize - bytes_read);
Victor Stinner9672da72015-03-04 18:40:10 +0100748
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000749 if (n == 0)
750 break;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100751 if (n == -1) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000752 if (errno == EAGAIN) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100753 PyErr_Clear();
Victor Stinnere10920f2014-07-02 22:59:31 +0200754 if (bytes_read > 0)
755 break;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000756 Py_DECREF(result);
757 Py_RETURN_NONE;
758 }
759 Py_DECREF(result);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000760 return NULL;
761 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100762 bytes_read += n;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200763 pos += n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000764 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000765
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100766 if (PyBytes_GET_SIZE(result) > bytes_read) {
767 if (_PyBytes_Resize(&result, bytes_read) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000768 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000769 }
770 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000771}
772
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300773/*[clinic input]
774_io.FileIO.read
775 size: io_ssize_t = -1
776 /
777
778Read at most size bytes, returned as bytes.
779
780Only makes one system call, so less data may be returned than requested.
781In non-blocking mode, returns None if no data is available.
782Return an empty bytes object at EOF.
783[clinic start generated code]*/
784
Guido van Rossuma9e20242007-03-08 00:43:48 +0000785static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300786_io_FileIO_read_impl(fileio *self, Py_ssize_t size)
787/*[clinic end generated code: output=42528d39dd0ca641 input=5c6caa5490c13a9b]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000788{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000789 char *ptr;
790 Py_ssize_t n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000791 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000792
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000793 if (self->fd < 0)
794 return err_closed();
795 if (!self->readable)
796 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000797
Victor Stinner66aab0c2015-03-19 22:53:20 +0100798 if (size < 0)
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300799 return _io_FileIO_readall_impl(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000800
Victor Stinner14b9b112013-06-25 00:37:25 +0200801#ifdef MS_WINDOWS
Victor Stinner66aab0c2015-03-19 22:53:20 +0100802 /* On Windows, the count parameter of read() is an int */
Victor Stinnerc655a722011-07-05 11:31:49 +0200803 if (size > INT_MAX)
804 size = INT_MAX;
805#endif
Victor Stinner66aab0c2015-03-19 22:53:20 +0100806
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000807 bytes = PyBytes_FromStringAndSize(NULL, size);
808 if (bytes == NULL)
809 return NULL;
810 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000811
Victor Stinner66aab0c2015-03-19 22:53:20 +0100812 n = _Py_read(self->fd, ptr, size);
813 if (n == -1) {
814 /* copy errno because Py_DECREF() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100815 int err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000816 Py_DECREF(bytes);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100817 if (err == EAGAIN) {
818 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000819 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100820 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000821 return NULL;
822 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000823
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000824 if (n != size) {
825 if (_PyBytes_Resize(&bytes, n) < 0) {
Victor Stinner85c761d2013-07-16 21:36:02 +0200826 Py_CLEAR(bytes);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000827 return NULL;
828 }
829 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000830
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000831 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000832}
833
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300834/*[clinic input]
835_io.FileIO.write
836 b: Py_buffer
837 /
838
839Write bytes b to file, return number written.
840
841Only makes one system call, so not all of the data may be written.
842The number of bytes actually written is returned. In non-blocking mode,
843returns None if the write would block.
844[clinic start generated code]*/
845
Guido van Rossuma9e20242007-03-08 00:43:48 +0000846static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300847_io_FileIO_write_impl(fileio *self, Py_buffer *b)
848/*[clinic end generated code: output=b4059db3d363a2f7 input=ffbd8834f447ac31]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000849{
Victor Stinner66aab0c2015-03-19 22:53:20 +0100850 Py_ssize_t n;
851 int err;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000852
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000853 if (self->fd < 0)
854 return err_closed();
855 if (!self->writable)
856 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000857
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300858 n = _Py_write(self->fd, b->buf, b->len);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100859 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100860 err = errno;
Martin v. Löwis423be952008-08-13 15:53:07 +0000861
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000862 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100863 if (err == EAGAIN) {
864 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000865 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100866 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000867 return NULL;
868 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000869
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000870 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000871}
872
Guido van Rossum53807da2007-04-10 19:01:47 +0000873/* XXX Windows support below is likely incomplete */
874
Guido van Rossum53807da2007-04-10 19:01:47 +0000875/* Cribbed from posix_lseek() */
876static PyObject *
877portable_lseek(int fd, PyObject *posobj, int whence)
878{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000879 Py_off_t pos, res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000880
881#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000882 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
883 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000884#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000885 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000886#endif
887#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000888 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000889#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000890#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000891 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000892#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000893 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000894#endif /* SEEK_SET */
895
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000896 if (posobj == NULL)
897 pos = 0;
898 else {
899 if(PyFloat_Check(posobj)) {
900 PyErr_SetString(PyExc_TypeError, "an integer is required");
901 return NULL;
902 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000903#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000904 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000905#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000906 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000907#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000908 if (PyErr_Occurred())
909 return NULL;
910 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000911
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000912 if (_PyVerify_fd(fd)) {
913 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -0400914 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +0200915#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000916 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000917#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000918 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000919#endif
Steve Dower8fc89802015-04-12 00:26:27 -0400920 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000921 Py_END_ALLOW_THREADS
922 } else
923 res = -1;
924 if (res < 0)
925 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000926
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000927#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000928 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000929#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000930 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000931#endif
932}
933
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300934/*[clinic input]
935_io.FileIO.seek
936 pos: object
937 whence: int = 0
938 /
Guido van Rossuma9e20242007-03-08 00:43:48 +0000939
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300940Move to new file position and return the file position.
941
942Argument offset is a byte count. Optional argument whence defaults to
943SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values
944are SEEK_CUR or 1 (move relative to current position, positive or negative),
945and SEEK_END or 2 (move relative to end of file, usually negative, although
946many platforms allow seeking beyond the end of a file).
947
948Note that not all file objects are seekable.
949[clinic start generated code]*/
950
951static PyObject *
952_io_FileIO_seek_impl(fileio *self, PyObject *pos, int whence)
953/*[clinic end generated code: output=c976acdf054e6655 input=0439194b0774d454]*/
954{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000955 if (self->fd < 0)
956 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000957
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300958 return portable_lseek(self->fd, pos, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000959}
960
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300961/*[clinic input]
962_io.FileIO.tell
963
964Current file position.
965
966Can raise OSError for non seekable files.
967[clinic start generated code]*/
968
Guido van Rossuma9e20242007-03-08 00:43:48 +0000969static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300970_io_FileIO_tell_impl(fileio *self)
971/*[clinic end generated code: output=ffe2147058809d0b input=807e24ead4cec2f9]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000972{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000973 if (self->fd < 0)
974 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000975
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000976 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000977}
978
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000979#ifdef HAVE_FTRUNCATE
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300980/*[clinic input]
981_io.FileIO.truncate
982 size as posobj: object = NULL
983 /
984
985Truncate the file to at most size bytes and return the truncated size.
986
987Size defaults to the current file position, as returned by tell().
988The current file position is changed to the value of size.
989[clinic start generated code]*/
990
Guido van Rossuma9e20242007-03-08 00:43:48 +0000991static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300992_io_FileIO_truncate_impl(fileio *self, PyObject *posobj)
993/*[clinic end generated code: output=e49ca7a916c176fa input=9026af44686b7318]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000994{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000995 Py_off_t pos;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000996 int ret;
997 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000998
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000999 fd = self->fd;
1000 if (fd < 0)
1001 return err_closed();
1002 if (!self->writable)
1003 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001004
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001005 if (posobj == Py_None || posobj == NULL) {
1006 /* Get the current position. */
1007 posobj = portable_lseek(fd, NULL, 1);
1008 if (posobj == NULL)
1009 return NULL;
1010 }
1011 else {
1012 Py_INCREF(posobj);
1013 }
Guido van Rossum53807da2007-04-10 19:01:47 +00001014
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001015#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001016 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001017#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001018 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001019#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001020 if (PyErr_Occurred()){
1021 Py_DECREF(posobj);
1022 return NULL;
1023 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001024
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001025 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04001026 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001027 errno = 0;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07001028#ifdef MS_WINDOWS
1029 ret = _chsize_s(fd, pos);
1030#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001031 ret = ftruncate(fd, pos);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07001032#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04001033 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001034 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001035
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001036 if (ret != 0) {
1037 Py_DECREF(posobj);
1038 PyErr_SetFromErrno(PyExc_IOError);
1039 return NULL;
1040 }
Guido van Rossuma9e20242007-03-08 00:43:48 +00001041
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001042 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001043}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001044#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +00001045
1046static char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001047mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +00001048{
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001049 if (self->created) {
1050 if (self->readable)
1051 return "xb+";
1052 else
1053 return "xb";
1054 }
Antoine Pitroue93b63b2013-09-04 20:46:33 +02001055 if (self->appending) {
1056 if (self->readable)
1057 return "ab+";
1058 else
1059 return "ab";
1060 }
1061 else if (self->readable) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001062 if (self->writable)
1063 return "rb+";
1064 else
1065 return "rb";
1066 }
1067 else
1068 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +00001069}
Guido van Rossuma9e20242007-03-08 00:43:48 +00001070
1071static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001072fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001073{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001074 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001075
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001076 if (self->fd < 0)
1077 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +00001078
Martin v. Löwis767046a2011-10-14 15:35:36 +02001079 nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001080 if (nameobj == NULL) {
1081 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1082 PyErr_Clear();
1083 else
1084 return NULL;
Robert Collins933430a2014-10-18 13:32:43 +13001085 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +02001086 "<_io.FileIO fd=%d mode='%s' closefd=%s>",
1087 self->fd, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001088 }
1089 else {
Robert Collins933430a2014-10-18 13:32:43 +13001090 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +02001091 "<_io.FileIO name=%R mode='%s' closefd=%s>",
1092 nameobj, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001093 Py_DECREF(nameobj);
1094 }
1095 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001096}
1097
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001098/*[clinic input]
1099_io.FileIO.isatty
1100
1101True if the file is connected to a TTY device.
1102[clinic start generated code]*/
1103
Guido van Rossuma9e20242007-03-08 00:43:48 +00001104static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001105_io_FileIO_isatty_impl(fileio *self)
1106/*[clinic end generated code: output=932c39924e9a8070 input=cd94ca1f5e95e843]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +00001107{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001108 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +00001109
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001110 if (self->fd < 0)
1111 return err_closed();
1112 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001113 _Py_BEGIN_SUPPRESS_IPH
1114 if (_PyVerify_fd(self->fd))
1115 res = isatty(self->fd);
1116 else
1117 res = 0;
1118 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001119 Py_END_ALLOW_THREADS
1120 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001121}
1122
Antoine Pitrou243757e2010-11-05 21:15:39 +00001123static PyObject *
1124fileio_getstate(fileio *self)
1125{
1126 PyErr_Format(PyExc_TypeError,
1127 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
1128 return NULL;
1129}
1130
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001131#include "clinic/fileio.c.h"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001132
1133static PyMethodDef fileio_methods[] = {
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001134 _IO_FILEIO_READ_METHODDEF
1135 _IO_FILEIO_READALL_METHODDEF
1136 _IO_FILEIO_READINTO_METHODDEF
1137 _IO_FILEIO_WRITE_METHODDEF
1138 _IO_FILEIO_SEEK_METHODDEF
1139 _IO_FILEIO_TELL_METHODDEF
1140 _IO_FILEIO_TRUNCATE_METHODDEF
1141 _IO_FILEIO_CLOSE_METHODDEF
1142 _IO_FILEIO_SEEKABLE_METHODDEF
1143 _IO_FILEIO_READABLE_METHODDEF
1144 _IO_FILEIO_WRITABLE_METHODDEF
1145 _IO_FILEIO_FILENO_METHODDEF
1146 _IO_FILEIO_ISATTY_METHODDEF
Antoine Pitroue033e062010-10-29 10:38:18 +00001147 {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
Antoine Pitrou243757e2010-11-05 21:15:39 +00001148 {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001149 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001150};
1151
Guido van Rossum53807da2007-04-10 19:01:47 +00001152/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1153
Guido van Rossumb0428152007-04-08 17:44:42 +00001154static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001155get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001156{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001157 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001158}
1159
1160static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001161get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001162{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001163 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001164}
1165
1166static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001167get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001168{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001169 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001170}
1171
1172static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001173 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1174 {"closefd", (getter)get_closefd, NULL,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001175 "True if the file descriptor will be closed by close()."},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001176 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1177 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001178};
1179
Antoine Pitrou796564c2013-07-30 19:59:21 +02001180static PyMemberDef fileio_members[] = {
Antoine Pitroude687222014-06-29 20:07:28 -04001181 {"_blksize", T_UINT, offsetof(fileio, blksize), 0},
Antoine Pitrou796564c2013-07-30 19:59:21 +02001182 {"_finalizing", T_BOOL, offsetof(fileio, finalizing), 0},
1183 {NULL}
1184};
1185
Guido van Rossuma9e20242007-03-08 00:43:48 +00001186PyTypeObject PyFileIO_Type = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001187 PyVarObject_HEAD_INIT(NULL, 0)
1188 "_io.FileIO",
1189 sizeof(fileio),
1190 0,
1191 (destructor)fileio_dealloc, /* tp_dealloc */
1192 0, /* tp_print */
1193 0, /* tp_getattr */
1194 0, /* tp_setattr */
1195 0, /* tp_reserved */
1196 (reprfunc)fileio_repr, /* tp_repr */
1197 0, /* tp_as_number */
1198 0, /* tp_as_sequence */
1199 0, /* tp_as_mapping */
1200 0, /* tp_hash */
1201 0, /* tp_call */
1202 0, /* tp_str */
1203 PyObject_GenericGetAttr, /* tp_getattro */
1204 0, /* tp_setattro */
1205 0, /* tp_as_buffer */
1206 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +02001207 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001208 _io_FileIO___init____doc__, /* tp_doc */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001209 (traverseproc)fileio_traverse, /* tp_traverse */
1210 (inquiry)fileio_clear, /* tp_clear */
1211 0, /* tp_richcompare */
1212 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1213 0, /* tp_iter */
1214 0, /* tp_iternext */
1215 fileio_methods, /* tp_methods */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001216 fileio_members, /* tp_members */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001217 fileio_getsetlist, /* tp_getset */
1218 0, /* tp_base */
1219 0, /* tp_dict */
1220 0, /* tp_descr_get */
1221 0, /* tp_descr_set */
1222 offsetof(fileio, dict), /* tp_dictoffset */
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001223 _io_FileIO___init__, /* tp_init */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001224 PyType_GenericAlloc, /* tp_alloc */
1225 fileio_new, /* tp_new */
1226 PyObject_GC_Del, /* tp_free */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001227 0, /* tp_is_gc */
1228 0, /* tp_bases */
1229 0, /* tp_mro */
1230 0, /* tp_cache */
1231 0, /* tp_subclasses */
1232 0, /* tp_weaklist */
1233 0, /* tp_del */
1234 0, /* tp_version_tag */
1235 0, /* tp_finalize */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001236};