blob: 5f88d58c45a455b101f947702b524b47475189ff [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) {
446#ifdef MS_WINDOWS
447 if (GetLastError() == ERROR_INVALID_HANDLE) {
448 PyErr_SetFromWindowsErr(0);
449#else
450 if (errno == EBADF) {
451 PyErr_SetFromErrno(PyExc_OSError);
452#endif
453 goto error;
454 }
Antoine Pitroude687222014-06-29 20:07:28 -0400455 }
Martin Panter0bb62b12015-12-06 03:15:05 +0000456 else {
457#if defined(S_ISDIR) && defined(EISDIR)
458 /* On Unix, open will succeed for directories.
459 In Python, there should be no file objects referring to
460 directories, so we need a check. */
461 if (S_ISDIR(fdfstat.st_mode)) {
462 errno = EISDIR;
463 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);
464 goto error;
465 }
Antoine Pitroude687222014-06-29 20:07:28 -0400466#endif /* defined(S_ISDIR) */
467#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin Panter0bb62b12015-12-06 03:15:05 +0000468 if (fdfstat.st_blksize > 1)
469 self->blksize = fdfstat.st_blksize;
Antoine Pitroude687222014-06-29 20:07:28 -0400470#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Martin Panter0bb62b12015-12-06 03:15:05 +0000471 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000472
Victor Stinner89e34362011-01-07 18:47:22 +0000473#if defined(MS_WINDOWS) || defined(__CYGWIN__)
474 /* don't translate newlines (\r\n <=> \n) */
475 _setmode(self->fd, O_BINARY);
476#endif
477
Victor Stinnerd9d04192013-11-06 23:50:10 +0100478 if (_PyObject_SetAttrId((PyObject *)self, &PyId_name, nameobj) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000479 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000480
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200481 if (self->appending) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000482 /* For consistent behaviour, we explicitly seek to the
483 end of file (otherwise, it might be done only on the
484 first write()). */
485 PyObject *pos = portable_lseek(self->fd, NULL, 2);
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200486 if (pos == NULL)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000487 goto error;
488 Py_DECREF(pos);
489 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000490
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000491 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000492
493 error:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000494 ret = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200495 if (!fd_is_own)
496 self->fd = -1;
Benjamin Petersonbbb04122010-10-30 23:16:28 +0000497 if (self->fd >= 0)
498 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000499
Guido van Rossuma9e20242007-03-08 00:43:48 +0000500 done:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000501 Py_CLEAR(stringobj);
502 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000503}
504
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000505static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000506fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000507{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000508 Py_VISIT(self->dict);
509 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000510}
511
512static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000513fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000514{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000515 Py_CLEAR(self->dict);
516 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000517}
518
Guido van Rossuma9e20242007-03-08 00:43:48 +0000519static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000520fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000521{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200522 self->finalizing = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000523 if (_PyIOBase_finalize((PyObject *) self) < 0)
524 return;
525 _PyObject_GC_UNTRACK(self);
526 if (self->weakreflist != NULL)
527 PyObject_ClearWeakRefs((PyObject *) self);
528 Py_CLEAR(self->dict);
529 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000530}
531
532static PyObject *
533err_closed(void)
534{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000535 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
536 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000537}
538
539static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000540err_mode(char *action)
541{
Antoine Pitrou712cb732013-12-21 15:51:54 +0100542 _PyIO_State *state = IO_STATE();
543 if (state != NULL)
544 PyErr_Format(state->unsupported_operation,
545 "File not open for %s", action);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000546 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000547}
548
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300549/*[clinic input]
550_io.FileIO.fileno
551
552Return the underlying file descriptor (an integer).
553[clinic start generated code]*/
554
Guido van Rossum53807da2007-04-10 19:01:47 +0000555static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300556_io_FileIO_fileno_impl(fileio *self)
557/*[clinic end generated code: output=a9626ce5398ece90 input=0b9b2de67335ada3]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000558{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000559 if (self->fd < 0)
560 return err_closed();
561 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000562}
563
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300564/*[clinic input]
565_io.FileIO.readable
566
567True if file was opened in a read mode.
568[clinic start generated code]*/
569
Guido van Rossuma9e20242007-03-08 00:43:48 +0000570static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300571_io_FileIO_readable_impl(fileio *self)
572/*[clinic end generated code: output=640744a6150fe9ba input=a3fdfed6eea721c5]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000573{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000574 if (self->fd < 0)
575 return err_closed();
576 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000577}
578
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300579/*[clinic input]
580_io.FileIO.writable
581
582True if file was opened in a write mode.
583[clinic start generated code]*/
584
Guido van Rossuma9e20242007-03-08 00:43:48 +0000585static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300586_io_FileIO_writable_impl(fileio *self)
587/*[clinic end generated code: output=96cefc5446e89977 input=c204a808ca2e1748]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000588{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000589 if (self->fd < 0)
590 return err_closed();
591 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000592}
593
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300594/*[clinic input]
595_io.FileIO.seekable
596
597True if file supports random-access.
598[clinic start generated code]*/
599
Guido van Rossuma9e20242007-03-08 00:43:48 +0000600static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300601_io_FileIO_seekable_impl(fileio *self)
602/*[clinic end generated code: output=47909ca0a42e9287 input=c8e5554d2fd63c7f]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000603{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000604 if (self->fd < 0)
605 return err_closed();
606 if (self->seekable < 0) {
607 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
608 if (pos == NULL) {
609 PyErr_Clear();
610 self->seekable = 0;
611 } else {
612 Py_DECREF(pos);
613 self->seekable = 1;
614 }
615 }
616 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000617}
618
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300619/*[clinic input]
620_io.FileIO.readinto
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700621 buffer: Py_buffer(accept={rwbuffer})
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300622 /
623
624Same as RawIOBase.readinto().
625[clinic start generated code]*/
626
Guido van Rossuma9e20242007-03-08 00:43:48 +0000627static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300628_io_FileIO_readinto_impl(fileio *self, Py_buffer *buffer)
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700629/*[clinic end generated code: output=b01a5a22c8415cb4 input=4721d7b68b154eaf]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000630{
Victor Stinner66aab0c2015-03-19 22:53:20 +0100631 Py_ssize_t n;
632 int err;
Guido van Rossum53807da2007-04-10 19:01:47 +0000633
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000634 if (self->fd < 0)
635 return err_closed();
636 if (!self->readable)
637 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000638
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300639 n = _Py_read(self->fd, buffer->buf, buffer->len);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100640 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100641 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100642
643 if (n == -1) {
644 if (err == EAGAIN) {
645 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000646 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100647 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000648 return NULL;
649 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000650
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000651 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000652}
653
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000654static size_t
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100655new_buffersize(fileio *self, size_t currentsize)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000656{
Antoine Pitroua3f44572012-04-17 13:50:58 +0200657 size_t addend;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100658
Nadeem Vawdad41a98b2011-10-13 13:34:16 +0200659 /* Expand the buffer by an amount proportional to the current size,
Antoine Pitroua3f44572012-04-17 13:50:58 +0200660 giving us amortized linear-time behavior. For bigger sizes, use a
661 less-than-double growth factor to avoid excessive allocation. */
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100662 assert(currentsize <= PY_SSIZE_T_MAX);
Antoine Pitroua3f44572012-04-17 13:50:58 +0200663 if (currentsize > 65536)
664 addend = currentsize >> 3;
665 else
666 addend = 256 + currentsize;
667 if (addend < SMALLCHUNK)
668 /* Avoid tiny read() calls. */
669 addend = SMALLCHUNK;
670 return addend + currentsize;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000671}
672
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300673/*[clinic input]
674_io.FileIO.readall
675
676Read all data from the file, returned as bytes.
677
678In non-blocking mode, returns as much as is immediately available,
679or None if no data is available. Return an empty bytes object at EOF.
680[clinic start generated code]*/
681
Guido van Rossum7165cb12007-07-10 06:54:34 +0000682static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300683_io_FileIO_readall_impl(fileio *self)
684/*[clinic end generated code: output=faa0292b213b4022 input=dbdc137f55602834]*/
Guido van Rossum7165cb12007-07-10 06:54:34 +0000685{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200686 struct _Py_stat_struct status;
Victor Stinnera2a64772011-10-11 22:45:02 +0200687 Py_off_t pos, end;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000688 PyObject *result;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100689 Py_ssize_t bytes_read = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100690 Py_ssize_t n;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100691 size_t bufsize;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000692
Victor Stinnerb79f28c2011-05-25 22:09:03 +0200693 if (self->fd < 0)
694 return err_closed();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000695 if (!_PyVerify_fd(self->fd))
696 return PyErr_SetFromErrno(PyExc_IOError);
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000697
Steve Dower8fc89802015-04-12 00:26:27 -0400698 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +0200699#ifdef MS_WINDOWS
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200700 pos = _lseeki64(self->fd, 0L, SEEK_CUR);
701#else
702 pos = lseek(self->fd, 0L, SEEK_CUR);
703#endif
Steve Dower8fc89802015-04-12 00:26:27 -0400704 _Py_END_SUPPRESS_IPH
705
Victor Stinnere134a7f2015-03-30 10:09:31 +0200706 if (_Py_fstat_noraise(self->fd, &status) == 0)
707 end = status.st_size;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200708 else
Victor Stinnera2a64772011-10-11 22:45:02 +0200709 end = (Py_off_t)-1;
Christian Heimesa872de52008-12-05 08:26:55 +0000710
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100711 if (end > 0 && end >= pos && pos >= 0 && end - pos < PY_SSIZE_T_MAX) {
712 /* This is probably a real file, so we try to allocate a
713 buffer one byte larger than the rest of the file. If the
714 calculation is right then we should get EOF without having
715 to enlarge the buffer. */
716 bufsize = (size_t)(end - pos + 1);
717 } else {
718 bufsize = SMALLCHUNK;
719 }
720
721 result = PyBytes_FromStringAndSize(NULL, bufsize);
722 if (result == NULL)
723 return NULL;
724
725 while (1) {
726 if (bytes_read >= (Py_ssize_t)bufsize) {
727 bufsize = new_buffersize(self, bytes_read);
728 if (bufsize > PY_SSIZE_T_MAX || bufsize <= 0) {
729 PyErr_SetString(PyExc_OverflowError,
730 "unbounded read returned more bytes "
Serhiy Storchakab817b772015-04-10 02:18:44 +0300731 "than a Python bytes object can hold");
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100732 Py_DECREF(result);
733 return NULL;
734 }
735
736 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
737 if (_PyBytes_Resize(&result, bufsize) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000738 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000739 }
740 }
Victor Stinner9672da72015-03-04 18:40:10 +0100741
Victor Stinner66aab0c2015-03-19 22:53:20 +0100742 n = _Py_read(self->fd,
743 PyBytes_AS_STRING(result) + bytes_read,
744 bufsize - bytes_read);
Victor Stinner9672da72015-03-04 18:40:10 +0100745
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000746 if (n == 0)
747 break;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100748 if (n == -1) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000749 if (errno == EAGAIN) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100750 PyErr_Clear();
Victor Stinnere10920f2014-07-02 22:59:31 +0200751 if (bytes_read > 0)
752 break;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000753 Py_DECREF(result);
754 Py_RETURN_NONE;
755 }
756 Py_DECREF(result);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000757 return NULL;
758 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100759 bytes_read += n;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200760 pos += n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000761 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000762
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100763 if (PyBytes_GET_SIZE(result) > bytes_read) {
764 if (_PyBytes_Resize(&result, bytes_read) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000765 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000766 }
767 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000768}
769
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300770/*[clinic input]
771_io.FileIO.read
772 size: io_ssize_t = -1
773 /
774
775Read at most size bytes, returned as bytes.
776
777Only makes one system call, so less data may be returned than requested.
778In non-blocking mode, returns None if no data is available.
779Return an empty bytes object at EOF.
780[clinic start generated code]*/
781
Guido van Rossuma9e20242007-03-08 00:43:48 +0000782static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300783_io_FileIO_read_impl(fileio *self, Py_ssize_t size)
784/*[clinic end generated code: output=42528d39dd0ca641 input=5c6caa5490c13a9b]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000785{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000786 char *ptr;
787 Py_ssize_t n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000788 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000789
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000790 if (self->fd < 0)
791 return err_closed();
792 if (!self->readable)
793 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000794
Victor Stinner66aab0c2015-03-19 22:53:20 +0100795 if (size < 0)
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300796 return _io_FileIO_readall_impl(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000797
Victor Stinner14b9b112013-06-25 00:37:25 +0200798#ifdef MS_WINDOWS
Victor Stinner66aab0c2015-03-19 22:53:20 +0100799 /* On Windows, the count parameter of read() is an int */
Victor Stinnerc655a722011-07-05 11:31:49 +0200800 if (size > INT_MAX)
801 size = INT_MAX;
802#endif
Victor Stinner66aab0c2015-03-19 22:53:20 +0100803
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000804 bytes = PyBytes_FromStringAndSize(NULL, size);
805 if (bytes == NULL)
806 return NULL;
807 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000808
Victor Stinner66aab0c2015-03-19 22:53:20 +0100809 n = _Py_read(self->fd, ptr, size);
810 if (n == -1) {
811 /* copy errno because Py_DECREF() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100812 int err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000813 Py_DECREF(bytes);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100814 if (err == EAGAIN) {
815 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000816 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100817 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000818 return NULL;
819 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000820
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000821 if (n != size) {
822 if (_PyBytes_Resize(&bytes, n) < 0) {
Victor Stinner85c761d2013-07-16 21:36:02 +0200823 Py_CLEAR(bytes);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000824 return NULL;
825 }
826 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000827
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000828 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000829}
830
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300831/*[clinic input]
832_io.FileIO.write
833 b: Py_buffer
834 /
835
836Write bytes b to file, return number written.
837
838Only makes one system call, so not all of the data may be written.
839The number of bytes actually written is returned. In non-blocking mode,
840returns None if the write would block.
841[clinic start generated code]*/
842
Guido van Rossuma9e20242007-03-08 00:43:48 +0000843static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300844_io_FileIO_write_impl(fileio *self, Py_buffer *b)
845/*[clinic end generated code: output=b4059db3d363a2f7 input=ffbd8834f447ac31]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000846{
Victor Stinner66aab0c2015-03-19 22:53:20 +0100847 Py_ssize_t n;
848 int err;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000849
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000850 if (self->fd < 0)
851 return err_closed();
852 if (!self->writable)
853 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000854
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300855 n = _Py_write(self->fd, b->buf, b->len);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100856 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100857 err = errno;
Martin v. Löwis423be952008-08-13 15:53:07 +0000858
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000859 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100860 if (err == EAGAIN) {
861 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000862 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100863 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000864 return NULL;
865 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000866
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000867 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000868}
869
Guido van Rossum53807da2007-04-10 19:01:47 +0000870/* XXX Windows support below is likely incomplete */
871
Guido van Rossum53807da2007-04-10 19:01:47 +0000872/* Cribbed from posix_lseek() */
873static PyObject *
874portable_lseek(int fd, PyObject *posobj, int whence)
875{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000876 Py_off_t pos, res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000877
878#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000879 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
880 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000881#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000882 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000883#endif
884#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000885 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000886#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000887#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000888 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000889#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000890 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000891#endif /* SEEK_SET */
892
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000893 if (posobj == NULL)
894 pos = 0;
895 else {
896 if(PyFloat_Check(posobj)) {
897 PyErr_SetString(PyExc_TypeError, "an integer is required");
898 return NULL;
899 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000900#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000901 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000902#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000903 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000904#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000905 if (PyErr_Occurred())
906 return NULL;
907 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000908
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000909 if (_PyVerify_fd(fd)) {
910 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -0400911 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +0200912#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000913 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000914#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000915 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000916#endif
Steve Dower8fc89802015-04-12 00:26:27 -0400917 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000918 Py_END_ALLOW_THREADS
919 } else
920 res = -1;
921 if (res < 0)
922 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000923
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000924#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000925 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000926#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000927 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000928#endif
929}
930
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300931/*[clinic input]
932_io.FileIO.seek
933 pos: object
934 whence: int = 0
935 /
Guido van Rossuma9e20242007-03-08 00:43:48 +0000936
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300937Move to new file position and return the file position.
938
939Argument offset is a byte count. Optional argument whence defaults to
940SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values
941are SEEK_CUR or 1 (move relative to current position, positive or negative),
942and SEEK_END or 2 (move relative to end of file, usually negative, although
943many platforms allow seeking beyond the end of a file).
944
945Note that not all file objects are seekable.
946[clinic start generated code]*/
947
948static PyObject *
949_io_FileIO_seek_impl(fileio *self, PyObject *pos, int whence)
950/*[clinic end generated code: output=c976acdf054e6655 input=0439194b0774d454]*/
951{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000952 if (self->fd < 0)
953 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000954
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300955 return portable_lseek(self->fd, pos, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000956}
957
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300958/*[clinic input]
959_io.FileIO.tell
960
961Current file position.
962
963Can raise OSError for non seekable files.
964[clinic start generated code]*/
965
Guido van Rossuma9e20242007-03-08 00:43:48 +0000966static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300967_io_FileIO_tell_impl(fileio *self)
968/*[clinic end generated code: output=ffe2147058809d0b input=807e24ead4cec2f9]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000969{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000970 if (self->fd < 0)
971 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000972
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000973 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000974}
975
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000976#ifdef HAVE_FTRUNCATE
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300977/*[clinic input]
978_io.FileIO.truncate
979 size as posobj: object = NULL
980 /
981
982Truncate the file to at most size bytes and return the truncated size.
983
984Size defaults to the current file position, as returned by tell().
985The current file position is changed to the value of size.
986[clinic start generated code]*/
987
Guido van Rossuma9e20242007-03-08 00:43:48 +0000988static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300989_io_FileIO_truncate_impl(fileio *self, PyObject *posobj)
990/*[clinic end generated code: output=e49ca7a916c176fa input=9026af44686b7318]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +0000991{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000992 Py_off_t pos;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000993 int ret;
994 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000995
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000996 fd = self->fd;
997 if (fd < 0)
998 return err_closed();
999 if (!self->writable)
1000 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001001
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001002 if (posobj == Py_None || posobj == NULL) {
1003 /* Get the current position. */
1004 posobj = portable_lseek(fd, NULL, 1);
1005 if (posobj == NULL)
1006 return NULL;
1007 }
1008 else {
1009 Py_INCREF(posobj);
1010 }
Guido van Rossum53807da2007-04-10 19:01:47 +00001011
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001012#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001013 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001014#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001015 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001016#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001017 if (PyErr_Occurred()){
1018 Py_DECREF(posobj);
1019 return NULL;
1020 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001021
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001022 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04001023 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001024 errno = 0;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07001025#ifdef MS_WINDOWS
1026 ret = _chsize_s(fd, pos);
1027#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001028 ret = ftruncate(fd, pos);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07001029#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04001030 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001031 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001032
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001033 if (ret != 0) {
1034 Py_DECREF(posobj);
1035 PyErr_SetFromErrno(PyExc_IOError);
1036 return NULL;
1037 }
Guido van Rossuma9e20242007-03-08 00:43:48 +00001038
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001039 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001040}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001041#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +00001042
1043static char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001044mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +00001045{
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001046 if (self->created) {
1047 if (self->readable)
1048 return "xb+";
1049 else
1050 return "xb";
1051 }
Antoine Pitroue93b63b2013-09-04 20:46:33 +02001052 if (self->appending) {
1053 if (self->readable)
1054 return "ab+";
1055 else
1056 return "ab";
1057 }
1058 else if (self->readable) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001059 if (self->writable)
1060 return "rb+";
1061 else
1062 return "rb";
1063 }
1064 else
1065 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +00001066}
Guido van Rossuma9e20242007-03-08 00:43:48 +00001067
1068static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001069fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001070{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001071 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001072
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001073 if (self->fd < 0)
1074 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +00001075
Martin v. Löwis767046a2011-10-14 15:35:36 +02001076 nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001077 if (nameobj == NULL) {
1078 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1079 PyErr_Clear();
1080 else
1081 return NULL;
Robert Collins933430a2014-10-18 13:32:43 +13001082 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +02001083 "<_io.FileIO fd=%d mode='%s' closefd=%s>",
1084 self->fd, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001085 }
1086 else {
Robert Collins933430a2014-10-18 13:32:43 +13001087 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +02001088 "<_io.FileIO name=%R mode='%s' closefd=%s>",
1089 nameobj, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001090 Py_DECREF(nameobj);
1091 }
1092 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001093}
1094
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001095/*[clinic input]
1096_io.FileIO.isatty
1097
1098True if the file is connected to a TTY device.
1099[clinic start generated code]*/
1100
Guido van Rossuma9e20242007-03-08 00:43:48 +00001101static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001102_io_FileIO_isatty_impl(fileio *self)
1103/*[clinic end generated code: output=932c39924e9a8070 input=cd94ca1f5e95e843]*/
Guido van Rossuma9e20242007-03-08 00:43:48 +00001104{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001105 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +00001106
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001107 if (self->fd < 0)
1108 return err_closed();
1109 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001110 _Py_BEGIN_SUPPRESS_IPH
1111 if (_PyVerify_fd(self->fd))
1112 res = isatty(self->fd);
1113 else
1114 res = 0;
1115 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001116 Py_END_ALLOW_THREADS
1117 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001118}
1119
Antoine Pitrou243757e2010-11-05 21:15:39 +00001120static PyObject *
1121fileio_getstate(fileio *self)
1122{
1123 PyErr_Format(PyExc_TypeError,
1124 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
1125 return NULL;
1126}
1127
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001128#include "clinic/fileio.c.h"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001129
1130static PyMethodDef fileio_methods[] = {
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001131 _IO_FILEIO_READ_METHODDEF
1132 _IO_FILEIO_READALL_METHODDEF
1133 _IO_FILEIO_READINTO_METHODDEF
1134 _IO_FILEIO_WRITE_METHODDEF
1135 _IO_FILEIO_SEEK_METHODDEF
1136 _IO_FILEIO_TELL_METHODDEF
1137 _IO_FILEIO_TRUNCATE_METHODDEF
1138 _IO_FILEIO_CLOSE_METHODDEF
1139 _IO_FILEIO_SEEKABLE_METHODDEF
1140 _IO_FILEIO_READABLE_METHODDEF
1141 _IO_FILEIO_WRITABLE_METHODDEF
1142 _IO_FILEIO_FILENO_METHODDEF
1143 _IO_FILEIO_ISATTY_METHODDEF
Antoine Pitroue033e062010-10-29 10:38:18 +00001144 {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
Antoine Pitrou243757e2010-11-05 21:15:39 +00001145 {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001146 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001147};
1148
Guido van Rossum53807da2007-04-10 19:01:47 +00001149/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1150
Guido van Rossumb0428152007-04-08 17:44:42 +00001151static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001152get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001153{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001154 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001155}
1156
1157static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001158get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001159{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001160 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001161}
1162
1163static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001164get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001165{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001166 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001167}
1168
1169static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001170 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1171 {"closefd", (getter)get_closefd, NULL,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001172 "True if the file descriptor will be closed by close()."},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001173 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1174 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001175};
1176
Antoine Pitrou796564c2013-07-30 19:59:21 +02001177static PyMemberDef fileio_members[] = {
Antoine Pitroude687222014-06-29 20:07:28 -04001178 {"_blksize", T_UINT, offsetof(fileio, blksize), 0},
Antoine Pitrou796564c2013-07-30 19:59:21 +02001179 {"_finalizing", T_BOOL, offsetof(fileio, finalizing), 0},
1180 {NULL}
1181};
1182
Guido van Rossuma9e20242007-03-08 00:43:48 +00001183PyTypeObject PyFileIO_Type = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001184 PyVarObject_HEAD_INIT(NULL, 0)
1185 "_io.FileIO",
1186 sizeof(fileio),
1187 0,
1188 (destructor)fileio_dealloc, /* tp_dealloc */
1189 0, /* tp_print */
1190 0, /* tp_getattr */
1191 0, /* tp_setattr */
1192 0, /* tp_reserved */
1193 (reprfunc)fileio_repr, /* tp_repr */
1194 0, /* tp_as_number */
1195 0, /* tp_as_sequence */
1196 0, /* tp_as_mapping */
1197 0, /* tp_hash */
1198 0, /* tp_call */
1199 0, /* tp_str */
1200 PyObject_GenericGetAttr, /* tp_getattro */
1201 0, /* tp_setattro */
1202 0, /* tp_as_buffer */
1203 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +02001204 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001205 _io_FileIO___init____doc__, /* tp_doc */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001206 (traverseproc)fileio_traverse, /* tp_traverse */
1207 (inquiry)fileio_clear, /* tp_clear */
1208 0, /* tp_richcompare */
1209 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1210 0, /* tp_iter */
1211 0, /* tp_iternext */
1212 fileio_methods, /* tp_methods */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001213 fileio_members, /* tp_members */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001214 fileio_getsetlist, /* tp_getset */
1215 0, /* tp_base */
1216 0, /* tp_dict */
1217 0, /* tp_descr_get */
1218 0, /* tp_descr_set */
1219 offsetof(fileio, dict), /* tp_dictoffset */
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001220 _io_FileIO___init__, /* tp_init */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001221 PyType_GenericAlloc, /* tp_alloc */
1222 fileio_new, /* tp_new */
1223 PyObject_GC_Del, /* tp_free */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001224 0, /* tp_is_gc */
1225 0, /* tp_bases */
1226 0, /* tp_mro */
1227 0, /* tp_cache */
1228 0, /* tp_subclasses */
1229 0, /* tp_weaklist */
1230 0, /* tp_del */
1231 0, /* tp_version_tag */
1232 0, /* tp_finalize */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001233};