blob: af93499caed831211c07a389aa09ff4052e10621 [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
Guido van Rossuma9e20242007-03-08 00:43:48 +000046typedef struct {
Antoine Pitrouae4b4722010-05-05 16:31:07 +000047 PyObject_HEAD
48 int fd;
Charles-François Natalidc3044c2012-01-09 22:40:02 +010049 unsigned int created : 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000050 unsigned int readable : 1;
51 unsigned int writable : 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +020052 unsigned int appending : 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000053 signed int seekable : 2; /* -1 means unknown */
54 unsigned int closefd : 1;
Antoine Pitrou796564c2013-07-30 19:59:21 +020055 char finalizing;
Antoine Pitroude687222014-06-29 20:07:28 -040056 unsigned int blksize;
Antoine Pitrouae4b4722010-05-05 16:31:07 +000057 PyObject *weakreflist;
58 PyObject *dict;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000059} fileio;
Guido van Rossuma9e20242007-03-08 00:43:48 +000060
Collin Winteraf334382007-03-08 21:46:15 +000061PyTypeObject PyFileIO_Type;
62
Victor Stinnerd9d04192013-11-06 23:50:10 +010063_Py_IDENTIFIER(name);
64
Guido van Rossuma9e20242007-03-08 00:43:48 +000065#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
66
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000067int
68_PyFileIO_closed(PyObject *self)
69{
Antoine Pitrouae4b4722010-05-05 16:31:07 +000070 return ((fileio *)self)->fd < 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000071}
Antoine Pitrou08838b62009-01-21 00:55:13 +000072
Antoine Pitroue033e062010-10-29 10:38:18 +000073/* Because this can call arbitrary code, it shouldn't be called when
74 the refcount is 0 (that is, not directly from tp_dealloc unless
75 the refcount has been temporarily re-incremented). */
76static PyObject *
77fileio_dealloc_warn(fileio *self, PyObject *source)
78{
79 if (self->fd >= 0 && self->closefd) {
80 PyObject *exc, *val, *tb;
81 PyErr_Fetch(&exc, &val, &tb);
82 if (PyErr_WarnFormat(PyExc_ResourceWarning, 1,
83 "unclosed file %R", source)) {
84 /* Spurious errors can appear at shutdown */
85 if (PyErr_ExceptionMatches(PyExc_Warning))
86 PyErr_WriteUnraisable((PyObject *) self);
87 }
88 PyErr_Restore(exc, val, tb);
89 }
90 Py_RETURN_NONE;
91}
92
Antoine Pitrou7fb111b2009-03-04 11:14:01 +000093static PyObject *
94portable_lseek(int fd, PyObject *posobj, int whence);
95
Antoine Pitroua28fcfd2009-03-13 23:42:55 +000096static PyObject *portable_lseek(int fd, PyObject *posobj, int whence);
97
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000098/* Returns 0 on success, -1 with exception set on failure. */
Neal Norwitz88b44da2007-08-12 17:23:54 +000099static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000100internal_close(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000101{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000102 int err = 0;
103 int save_errno = 0;
104 if (self->fd >= 0) {
105 int fd = self->fd;
106 self->fd = -1;
107 /* fd is accessible and someone else may have closed it */
108 if (_PyVerify_fd(fd)) {
109 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -0400110 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000111 err = close(fd);
112 if (err < 0)
113 save_errno = errno;
Steve Dower8fc89802015-04-12 00:26:27 -0400114 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000115 Py_END_ALLOW_THREADS
116 } else {
117 save_errno = errno;
118 err = -1;
119 }
120 }
121 if (err < 0) {
122 errno = save_errno;
123 PyErr_SetFromErrno(PyExc_IOError);
124 return -1;
125 }
126 return 0;
Neal Norwitz88b44da2007-08-12 17:23:54 +0000127}
128
129static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000130fileio_close(fileio *self)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000131{
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200132 PyObject *res;
133 PyObject *exc, *val, *tb;
134 int rc;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200135 _Py_IDENTIFIER(close);
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200136 res = _PyObject_CallMethodId((PyObject*)&PyRawIOBase_Type,
137 &PyId_close, "O", self);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000138 if (!self->closefd) {
139 self->fd = -1;
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200140 return res;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000141 }
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200142 if (res == NULL)
143 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200144 if (self->finalizing) {
Antoine Pitroue033e062010-10-29 10:38:18 +0000145 PyObject *r = fileio_dealloc_warn(self, (PyObject *) self);
146 if (r)
147 Py_DECREF(r);
148 else
149 PyErr_Clear();
150 }
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200151 rc = internal_close(self);
152 if (res == NULL)
153 _PyErr_ChainExceptions(exc, val, tb);
154 if (rc < 0)
155 Py_CLEAR(res);
156 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000157}
158
159static PyObject *
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000160fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000161{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000162 fileio *self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000163
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000164 assert(type != NULL && type->tp_alloc != NULL);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000165
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000166 self = (fileio *) type->tp_alloc(type, 0);
167 if (self != NULL) {
168 self->fd = -1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100169 self->created = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000170 self->readable = 0;
171 self->writable = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200172 self->appending = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000173 self->seekable = -1;
Antoine Pitroude687222014-06-29 20:07:28 -0400174 self->blksize = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000175 self->closefd = 1;
176 self->weakreflist = NULL;
177 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000178
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000179 return (PyObject *) self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000180}
181
Victor Stinnerdaf45552013-08-28 00:53:59 +0200182#ifdef O_CLOEXEC
183extern int _Py_open_cloexec_works;
184#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000185
186static int
187fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
188{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000189 fileio *self = (fileio *) oself;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200190 static char *kwlist[] = {"file", "mode", "closefd", "opener", NULL};
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000191 const char *name = NULL;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200192 PyObject *nameobj, *stringobj = NULL, *opener = Py_None;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000193 char *mode = "r";
194 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000195#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000196 Py_UNICODE *widename = NULL;
Thomas Helleraf2be262007-07-12 11:03:13 +0000197#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000198 int ret = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200199 int rwa = 0, plus = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000200 int flags = 0;
201 int fd = -1;
202 int closefd = 1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200203 int fd_is_own = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200204#ifdef O_CLOEXEC
205 int *atomic_flag_works = &_Py_open_cloexec_works;
206#elif !defined(MS_WINDOWS)
207 int *atomic_flag_works = NULL;
208#endif
Steve Dowerf2f373f2015-02-21 08:44:05 -0800209 struct _Py_stat_struct fdfstat;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000210 int async_err = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000211
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000212 assert(PyFileIO_Check(oself));
213 if (self->fd >= 0) {
Hynek Schlawack2cc71562012-05-25 10:05:53 +0200214 if (self->closefd) {
215 /* Have to close the existing file first. */
216 if (internal_close(self) < 0)
217 return -1;
218 }
219 else
220 self->fd = -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000221 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000222
Ross Lagerwall59142db2011-10-31 20:34:46 +0200223 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|siO:fileio",
224 kwlist, &nameobj, &mode, &closefd,
225 &opener))
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000226 return -1;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000227
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000228 if (PyFloat_Check(nameobj)) {
229 PyErr_SetString(PyExc_TypeError,
230 "integer argument expected, got float");
231 return -1;
232 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000233
Serhiy Storchaka78980432013-01-15 01:12:17 +0200234 fd = _PyLong_AsInt(nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000235 if (fd < 0) {
236 if (!PyErr_Occurred()) {
237 PyErr_SetString(PyExc_ValueError,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +0300238 "negative file descriptor");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000239 return -1;
240 }
241 PyErr_Clear();
242 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000243
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000244#ifdef MS_WINDOWS
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200245 if (PyUnicode_Check(nameobj)) {
Antoine Pitrou13348842012-01-29 18:36:34 +0100246 int rv = _PyUnicode_HasNULChars(nameobj);
247 if (rv) {
248 if (rv != -1)
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300249 PyErr_SetString(PyExc_ValueError, "embedded null character");
Antoine Pitrou13348842012-01-29 18:36:34 +0100250 return -1;
251 }
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200252 widename = PyUnicode_AsUnicode(nameobj);
253 if (widename == NULL)
254 return -1;
255 } else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000256#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000257 if (fd < 0)
258 {
Antoine Pitrou13348842012-01-29 18:36:34 +0100259 if (!PyUnicode_FSConverter(nameobj, &stringobj)) {
260 return -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000261 }
Antoine Pitrou13348842012-01-29 18:36:34 +0100262 name = PyBytes_AS_STRING(stringobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000263 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000264
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000265 s = mode;
266 while (*s) {
267 switch (*s++) {
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100268 case 'x':
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000269 if (rwa) {
270 bad_mode:
271 PyErr_SetString(PyExc_ValueError,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100272 "Must have exactly one of create/read/write/append "
Georg Brandl28928ae2010-10-21 13:45:52 +0000273 "mode and at most one plus");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000274 goto error;
275 }
276 rwa = 1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100277 self->created = 1;
278 self->writable = 1;
279 flags |= O_EXCL | O_CREAT;
280 break;
281 case 'r':
282 if (rwa)
283 goto bad_mode;
284 rwa = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000285 self->readable = 1;
286 break;
287 case 'w':
288 if (rwa)
289 goto bad_mode;
290 rwa = 1;
291 self->writable = 1;
292 flags |= O_CREAT | O_TRUNC;
293 break;
294 case 'a':
295 if (rwa)
296 goto bad_mode;
297 rwa = 1;
298 self->writable = 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200299 self->appending = 1;
300 flags |= O_APPEND | O_CREAT;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000301 break;
302 case 'b':
303 break;
304 case '+':
305 if (plus)
306 goto bad_mode;
307 self->readable = self->writable = 1;
308 plus = 1;
309 break;
310 default:
311 PyErr_Format(PyExc_ValueError,
312 "invalid mode: %.200s", mode);
313 goto error;
314 }
315 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000316
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000317 if (!rwa)
318 goto bad_mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000319
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000320 if (self->readable && self->writable)
321 flags |= O_RDWR;
322 else if (self->readable)
323 flags |= O_RDONLY;
324 else
325 flags |= O_WRONLY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000326
327#ifdef O_BINARY
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000328 flags |= O_BINARY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000329#endif
330
Victor Stinnerdaf45552013-08-28 00:53:59 +0200331#ifdef MS_WINDOWS
332 flags |= O_NOINHERIT;
333#elif defined(O_CLOEXEC)
334 flags |= O_CLOEXEC;
335#endif
Walter Dörwald0e411482007-06-06 16:55:38 +0000336
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000337 if (fd >= 0) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000338 self->fd = fd;
339 self->closefd = closefd;
340 }
341 else {
342 self->closefd = 1;
343 if (!closefd) {
344 PyErr_SetString(PyExc_ValueError,
345 "Cannot use closefd=False with file name");
346 goto error;
347 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000348
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000349 errno = 0;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200350 if (opener == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000351 do {
352 Py_BEGIN_ALLOW_THREADS
Thomas Helleraf2be262007-07-12 11:03:13 +0000353#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000354 if (widename != NULL)
355 self->fd = _wopen(widename, flags, 0666);
356 else
Thomas Helleraf2be262007-07-12 11:03:13 +0000357#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000358 self->fd = open(name, flags, 0666);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000359 Py_END_ALLOW_THREADS
360 } while (self->fd < 0 && errno == EINTR &&
361 !(async_err = PyErr_CheckSignals()));
Victor Stinner9672da72015-03-04 18:40:10 +0100362
363 if (async_err)
364 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200365 }
366 else {
367 PyObject *fdobj;
368
369#ifndef MS_WINDOWS
370 /* the opener may clear the atomic flag */
371 atomic_flag_works = NULL;
372#endif
373
374 fdobj = PyObject_CallFunction(opener, "Oi", nameobj, flags);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200375 if (fdobj == NULL)
376 goto error;
377 if (!PyLong_Check(fdobj)) {
378 Py_DECREF(fdobj);
379 PyErr_SetString(PyExc_TypeError,
380 "expected integer from opener");
381 goto error;
382 }
383
Serhiy Storchaka78980432013-01-15 01:12:17 +0200384 self->fd = _PyLong_AsInt(fdobj);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200385 Py_DECREF(fdobj);
386 if (self->fd == -1) {
387 goto error;
388 }
389 }
390
Hynek Schlawack7f59fd72012-06-22 09:32:22 +0200391 fd_is_own = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000392 if (self->fd < 0) {
Victor Stinner9672da72015-03-04 18:40:10 +0100393 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000394 goto error;
395 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200396
397#ifndef MS_WINDOWS
398 if (_Py_set_inheritable(self->fd, 0, atomic_flag_works) < 0)
399 goto error;
400#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000401 }
Antoine Pitroude687222014-06-29 20:07:28 -0400402
403 self->blksize = DEFAULT_BUFFER_SIZE;
Victor Stinnere134a7f2015-03-30 10:09:31 +0200404 if (_Py_fstat(self->fd, &fdfstat) < 0)
Antoine Pitrou9235b252012-07-06 18:48:24 +0200405 goto error;
Antoine Pitroude687222014-06-29 20:07:28 -0400406#if defined(S_ISDIR) && defined(EISDIR)
407 /* On Unix, open will succeed for directories.
408 In Python, there should be no file objects referring to
409 directories, so we need a check. */
410 if (S_ISDIR(fdfstat.st_mode)) {
411 errno = EISDIR;
412 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);
413 goto error;
414 }
415#endif /* defined(S_ISDIR) */
416#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
417 if (fdfstat.st_blksize > 1)
418 self->blksize = fdfstat.st_blksize;
419#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000420
Victor Stinner89e34362011-01-07 18:47:22 +0000421#if defined(MS_WINDOWS) || defined(__CYGWIN__)
422 /* don't translate newlines (\r\n <=> \n) */
423 _setmode(self->fd, O_BINARY);
424#endif
425
Victor Stinnerd9d04192013-11-06 23:50:10 +0100426 if (_PyObject_SetAttrId((PyObject *)self, &PyId_name, nameobj) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000427 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000428
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200429 if (self->appending) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000430 /* For consistent behaviour, we explicitly seek to the
431 end of file (otherwise, it might be done only on the
432 first write()). */
433 PyObject *pos = portable_lseek(self->fd, NULL, 2);
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200434 if (pos == NULL)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000435 goto error;
436 Py_DECREF(pos);
437 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000438
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000439 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000440
441 error:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000442 ret = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200443 if (!fd_is_own)
444 self->fd = -1;
Benjamin Petersonbbb04122010-10-30 23:16:28 +0000445 if (self->fd >= 0)
446 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000447
Guido van Rossuma9e20242007-03-08 00:43:48 +0000448 done:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000449 Py_CLEAR(stringobj);
450 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000451}
452
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000453static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000454fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000455{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000456 Py_VISIT(self->dict);
457 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000458}
459
460static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000461fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000462{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000463 Py_CLEAR(self->dict);
464 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000465}
466
Guido van Rossuma9e20242007-03-08 00:43:48 +0000467static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000468fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000469{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200470 self->finalizing = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000471 if (_PyIOBase_finalize((PyObject *) self) < 0)
472 return;
473 _PyObject_GC_UNTRACK(self);
474 if (self->weakreflist != NULL)
475 PyObject_ClearWeakRefs((PyObject *) self);
476 Py_CLEAR(self->dict);
477 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000478}
479
480static PyObject *
481err_closed(void)
482{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000483 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
484 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000485}
486
487static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000488err_mode(char *action)
489{
Antoine Pitrou712cb732013-12-21 15:51:54 +0100490 _PyIO_State *state = IO_STATE();
491 if (state != NULL)
492 PyErr_Format(state->unsupported_operation,
493 "File not open for %s", action);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000494 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000495}
496
497static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000498fileio_fileno(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000499{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000500 if (self->fd < 0)
501 return err_closed();
502 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000503}
504
505static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000506fileio_readable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000507{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000508 if (self->fd < 0)
509 return err_closed();
510 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000511}
512
513static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000514fileio_writable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000515{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000516 if (self->fd < 0)
517 return err_closed();
518 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000519}
520
521static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000522fileio_seekable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000523{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000524 if (self->fd < 0)
525 return err_closed();
526 if (self->seekable < 0) {
527 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
528 if (pos == NULL) {
529 PyErr_Clear();
530 self->seekable = 0;
531 } else {
532 Py_DECREF(pos);
533 self->seekable = 1;
534 }
535 }
536 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000537}
538
539static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000540fileio_readinto(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000541{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000542 Py_buffer pbuf;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100543 Py_ssize_t n;
544 int err;
Guido van Rossum53807da2007-04-10 19:01:47 +0000545
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000546 if (self->fd < 0)
547 return err_closed();
548 if (!self->readable)
549 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000550
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000551 if (!PyArg_ParseTuple(args, "w*", &pbuf))
552 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000553
Victor Stinner66aab0c2015-03-19 22:53:20 +0100554 n = _Py_read(self->fd, pbuf.buf, pbuf.len);
555 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100556 err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000557 PyBuffer_Release(&pbuf);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100558
559 if (n == -1) {
560 if (err == EAGAIN) {
561 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000562 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100563 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000564 return NULL;
565 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000566
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000567 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000568}
569
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000570static size_t
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100571new_buffersize(fileio *self, size_t currentsize)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000572{
Antoine Pitroua3f44572012-04-17 13:50:58 +0200573 size_t addend;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100574
Nadeem Vawdad41a98b2011-10-13 13:34:16 +0200575 /* Expand the buffer by an amount proportional to the current size,
Antoine Pitroua3f44572012-04-17 13:50:58 +0200576 giving us amortized linear-time behavior. For bigger sizes, use a
577 less-than-double growth factor to avoid excessive allocation. */
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100578 assert(currentsize <= PY_SSIZE_T_MAX);
Antoine Pitroua3f44572012-04-17 13:50:58 +0200579 if (currentsize > 65536)
580 addend = currentsize >> 3;
581 else
582 addend = 256 + currentsize;
583 if (addend < SMALLCHUNK)
584 /* Avoid tiny read() calls. */
585 addend = SMALLCHUNK;
586 return addend + currentsize;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000587}
588
Guido van Rossum7165cb12007-07-10 06:54:34 +0000589static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000590fileio_readall(fileio *self)
Guido van Rossum7165cb12007-07-10 06:54:34 +0000591{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200592 struct _Py_stat_struct status;
Victor Stinnera2a64772011-10-11 22:45:02 +0200593 Py_off_t pos, end;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000594 PyObject *result;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100595 Py_ssize_t bytes_read = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100596 Py_ssize_t n;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100597 size_t bufsize;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000598
Victor Stinnerb79f28c2011-05-25 22:09:03 +0200599 if (self->fd < 0)
600 return err_closed();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000601 if (!_PyVerify_fd(self->fd))
602 return PyErr_SetFromErrno(PyExc_IOError);
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000603
Steve Dower8fc89802015-04-12 00:26:27 -0400604 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +0200605#ifdef MS_WINDOWS
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200606 pos = _lseeki64(self->fd, 0L, SEEK_CUR);
607#else
608 pos = lseek(self->fd, 0L, SEEK_CUR);
609#endif
Steve Dower8fc89802015-04-12 00:26:27 -0400610 _Py_END_SUPPRESS_IPH
611
Victor Stinnere134a7f2015-03-30 10:09:31 +0200612 if (_Py_fstat_noraise(self->fd, &status) == 0)
613 end = status.st_size;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200614 else
Victor Stinnera2a64772011-10-11 22:45:02 +0200615 end = (Py_off_t)-1;
Christian Heimesa872de52008-12-05 08:26:55 +0000616
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100617 if (end > 0 && end >= pos && pos >= 0 && end - pos < PY_SSIZE_T_MAX) {
618 /* This is probably a real file, so we try to allocate a
619 buffer one byte larger than the rest of the file. If the
620 calculation is right then we should get EOF without having
621 to enlarge the buffer. */
622 bufsize = (size_t)(end - pos + 1);
623 } else {
624 bufsize = SMALLCHUNK;
625 }
626
627 result = PyBytes_FromStringAndSize(NULL, bufsize);
628 if (result == NULL)
629 return NULL;
630
631 while (1) {
632 if (bytes_read >= (Py_ssize_t)bufsize) {
633 bufsize = new_buffersize(self, bytes_read);
634 if (bufsize > PY_SSIZE_T_MAX || bufsize <= 0) {
635 PyErr_SetString(PyExc_OverflowError,
636 "unbounded read returned more bytes "
Serhiy Storchakab817b772015-04-10 02:18:44 +0300637 "than a Python bytes object can hold");
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100638 Py_DECREF(result);
639 return NULL;
640 }
641
642 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
643 if (_PyBytes_Resize(&result, bufsize) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000644 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000645 }
646 }
Victor Stinner9672da72015-03-04 18:40:10 +0100647
Victor Stinner66aab0c2015-03-19 22:53:20 +0100648 n = _Py_read(self->fd,
649 PyBytes_AS_STRING(result) + bytes_read,
650 bufsize - bytes_read);
Victor Stinner9672da72015-03-04 18:40:10 +0100651
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000652 if (n == 0)
653 break;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100654 if (n == -1) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000655 if (errno == EAGAIN) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100656 PyErr_Clear();
Victor Stinnere10920f2014-07-02 22:59:31 +0200657 if (bytes_read > 0)
658 break;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000659 Py_DECREF(result);
660 Py_RETURN_NONE;
661 }
662 Py_DECREF(result);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000663 return NULL;
664 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100665 bytes_read += n;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200666 pos += n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000667 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000668
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100669 if (PyBytes_GET_SIZE(result) > bytes_read) {
670 if (_PyBytes_Resize(&result, bytes_read) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000671 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000672 }
673 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000674}
675
Guido van Rossuma9e20242007-03-08 00:43:48 +0000676static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000677fileio_read(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000678{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000679 char *ptr;
680 Py_ssize_t n;
681 Py_ssize_t size = -1;
682 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000683
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000684 if (self->fd < 0)
685 return err_closed();
686 if (!self->readable)
687 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000688
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000689 if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size))
690 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000691
Victor Stinner66aab0c2015-03-19 22:53:20 +0100692 if (size < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000693 return fileio_readall(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000694
Victor Stinner14b9b112013-06-25 00:37:25 +0200695#ifdef MS_WINDOWS
Victor Stinner66aab0c2015-03-19 22:53:20 +0100696 /* On Windows, the count parameter of read() is an int */
Victor Stinnerc655a722011-07-05 11:31:49 +0200697 if (size > INT_MAX)
698 size = INT_MAX;
699#endif
Victor Stinner66aab0c2015-03-19 22:53:20 +0100700
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000701 bytes = PyBytes_FromStringAndSize(NULL, size);
702 if (bytes == NULL)
703 return NULL;
704 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000705
Victor Stinner66aab0c2015-03-19 22:53:20 +0100706 n = _Py_read(self->fd, ptr, size);
707 if (n == -1) {
708 /* copy errno because Py_DECREF() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100709 int err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000710 Py_DECREF(bytes);
Victor Stinner66aab0c2015-03-19 22:53:20 +0100711 if (err == EAGAIN) {
712 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000713 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100714 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000715 return NULL;
716 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000717
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000718 if (n != size) {
719 if (_PyBytes_Resize(&bytes, n) < 0) {
Victor Stinner85c761d2013-07-16 21:36:02 +0200720 Py_CLEAR(bytes);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000721 return NULL;
722 }
723 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000724
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000725 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000726}
727
728static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000729fileio_write(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000730{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000731 Py_buffer pbuf;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100732 Py_ssize_t n;
733 int err;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000734
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000735 if (self->fd < 0)
736 return err_closed();
737 if (!self->writable)
738 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000739
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000740 if (!PyArg_ParseTuple(args, "y*", &pbuf))
741 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000742
Victor Stinner66aab0c2015-03-19 22:53:20 +0100743 n = _Py_write(self->fd, pbuf.buf, pbuf.len);
744 /* copy errno because PyBuffer_Release() can indirectly modify it */
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100745 err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000746 PyBuffer_Release(&pbuf);
Martin v. Löwis423be952008-08-13 15:53:07 +0000747
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000748 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100749 if (err == EAGAIN) {
750 PyErr_Clear();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000751 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100752 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000753 return NULL;
754 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000755
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000756 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000757}
758
Guido van Rossum53807da2007-04-10 19:01:47 +0000759/* XXX Windows support below is likely incomplete */
760
Guido van Rossum53807da2007-04-10 19:01:47 +0000761/* Cribbed from posix_lseek() */
762static PyObject *
763portable_lseek(int fd, PyObject *posobj, int whence)
764{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000765 Py_off_t pos, res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000766
767#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000768 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
769 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000770#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000771 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000772#endif
773#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000774 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000775#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000776#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000777 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000778#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000779 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000780#endif /* SEEK_SET */
781
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000782 if (posobj == NULL)
783 pos = 0;
784 else {
785 if(PyFloat_Check(posobj)) {
786 PyErr_SetString(PyExc_TypeError, "an integer is required");
787 return NULL;
788 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000789#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000790 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000791#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000792 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000793#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000794 if (PyErr_Occurred())
795 return NULL;
796 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000797
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000798 if (_PyVerify_fd(fd)) {
799 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -0400800 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +0200801#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000802 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000803#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000804 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000805#endif
Steve Dower8fc89802015-04-12 00:26:27 -0400806 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000807 Py_END_ALLOW_THREADS
808 } else
809 res = -1;
810 if (res < 0)
811 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000812
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000813#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000814 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000815#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000816 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000817#endif
818}
819
Guido van Rossuma9e20242007-03-08 00:43:48 +0000820static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000821fileio_seek(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000822{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000823 PyObject *posobj;
824 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000825
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000826 if (self->fd < 0)
827 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000828
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000829 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
830 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000831
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000832 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000833}
834
835static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000836fileio_tell(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000837{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000838 if (self->fd < 0)
839 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000840
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000841 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000842}
843
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000844#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000845static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000846fileio_truncate(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000847{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000848 PyObject *posobj = NULL; /* the new size wanted by the user */
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000849 Py_off_t pos;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000850 int ret;
851 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000852
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000853 fd = self->fd;
854 if (fd < 0)
855 return err_closed();
856 if (!self->writable)
857 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000858
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000859 if (!PyArg_ParseTuple(args, "|O", &posobj))
860 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000861
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000862 if (posobj == Py_None || posobj == NULL) {
863 /* Get the current position. */
864 posobj = portable_lseek(fd, NULL, 1);
865 if (posobj == NULL)
866 return NULL;
867 }
868 else {
869 Py_INCREF(posobj);
870 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000871
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000872#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000873 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000874#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000875 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000876#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000877 if (PyErr_Occurred()){
878 Py_DECREF(posobj);
879 return NULL;
880 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000881
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000882 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -0400883 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000884 errno = 0;
Steve Dowerfe0a41a2015-03-20 19:50:46 -0700885#ifdef MS_WINDOWS
886 ret = _chsize_s(fd, pos);
887#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000888 ret = ftruncate(fd, pos);
Steve Dowerfe0a41a2015-03-20 19:50:46 -0700889#endif
Steve Dowera1c7e722015-04-12 00:26:43 -0400890 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000891 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000892
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000893 if (ret != 0) {
894 Py_DECREF(posobj);
895 PyErr_SetFromErrno(PyExc_IOError);
896 return NULL;
897 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000898
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000899 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000900}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000901#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +0000902
903static char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000904mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +0000905{
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100906 if (self->created) {
907 if (self->readable)
908 return "xb+";
909 else
910 return "xb";
911 }
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200912 if (self->appending) {
913 if (self->readable)
914 return "ab+";
915 else
916 return "ab";
917 }
918 else if (self->readable) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000919 if (self->writable)
920 return "rb+";
921 else
922 return "rb";
923 }
924 else
925 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000926}
Guido van Rossuma9e20242007-03-08 00:43:48 +0000927
928static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000929fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000930{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000931 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000932
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000933 if (self->fd < 0)
934 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +0000935
Martin v. Löwis767046a2011-10-14 15:35:36 +0200936 nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000937 if (nameobj == NULL) {
938 if (PyErr_ExceptionMatches(PyExc_AttributeError))
939 PyErr_Clear();
940 else
941 return NULL;
Robert Collins933430a2014-10-18 13:32:43 +1300942 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +0200943 "<_io.FileIO fd=%d mode='%s' closefd=%s>",
944 self->fd, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000945 }
946 else {
Robert Collins933430a2014-10-18 13:32:43 +1300947 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +0200948 "<_io.FileIO name=%R mode='%s' closefd=%s>",
949 nameobj, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000950 Py_DECREF(nameobj);
951 }
952 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000953}
954
955static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000956fileio_isatty(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000957{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000958 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000959
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000960 if (self->fd < 0)
961 return err_closed();
962 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -0400963 _Py_BEGIN_SUPPRESS_IPH
964 if (_PyVerify_fd(self->fd))
965 res = isatty(self->fd);
966 else
967 res = 0;
968 _Py_END_SUPPRESS_IPH
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000969 Py_END_ALLOW_THREADS
970 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000971}
972
Antoine Pitrou243757e2010-11-05 21:15:39 +0000973static PyObject *
974fileio_getstate(fileio *self)
975{
976 PyErr_Format(PyExc_TypeError,
977 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
978 return NULL;
979}
980
Guido van Rossuma9e20242007-03-08 00:43:48 +0000981
982PyDoc_STRVAR(fileio_doc,
Ross Lagerwall59142db2011-10-31 20:34:46 +0200983"file(name: str[, mode: str][, opener: None]) -> file IO object\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +0000984"\n"
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +0300985"Open a file. The mode can be 'r' (default), 'w', 'x' or 'a' for reading,\n"
Charles-François Natalid612de12012-01-14 11:51:00 +0100986"writing, exclusive creation or appending. The file will be created if it\n"
987"doesn't exist when opened for writing or appending; it will be truncated\n"
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +0300988"when opened for writing. A FileExistsError will be raised if it already\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100989"exists when opened for creating. Opening a file for creating implies\n"
990"writing so this mode behaves in a similar way to 'w'.Add a '+' to the mode\n"
991"to allow simultaneous reading and writing. A custom opener can be used by\n"
992"passing a callable as *opener*. The underlying file descriptor for the file\n"
Ross Lagerwall59142db2011-10-31 20:34:46 +0200993"object is then obtained by calling opener with (*name*, *flags*).\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100994"*opener* must return an open file descriptor (passing os.open as *opener*\n"
995"results in functionality similar to passing None).");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000996
997PyDoc_STRVAR(read_doc,
998"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
999"\n"
1000"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +00001001"In non-blocking mode, returns None if no data is available.\n"
Serhiy Storchakab817b772015-04-10 02:18:44 +03001002"Return an empty bytes object at EOF.");
Guido van Rossum7165cb12007-07-10 06:54:34 +00001003
1004PyDoc_STRVAR(readall_doc,
1005"readall() -> bytes. read all data from the file, returned as bytes.\n"
1006"\n"
1007"In non-blocking mode, returns as much as is immediately available,\n"
Serhiy Storchakab817b772015-04-10 02:18:44 +03001008"or None if no data is available. Return an empty bytes object at EOF.");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001009
1010PyDoc_STRVAR(write_doc,
1011"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
1012"\n"
1013"Only makes one system call, so not all of the data may be written.\n"
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001014"The number of bytes actually written is returned. In non-blocking mode,\n"
1015"returns None if the write would block."
1016);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001017
1018PyDoc_STRVAR(fileno_doc,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001019"fileno() -> int. Return the underlying file descriptor (an integer).");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001020
1021PyDoc_STRVAR(seek_doc,
Berker Peksagb87630c2014-09-24 12:43:29 +03001022"seek(offset: int[, whence: int]) -> int. Move to new file position and\n"
1023"return the file position.\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001024"\n"
1025"Argument offset is a byte count. Optional argument whence defaults to\n"
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001026"SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values\n"
1027"are SEEK_CUR or 1 (move relative to current position, positive or negative),\n"
1028"and SEEK_END or 2 (move relative to end of file, usually negative, although\n"
1029"many platforms allow seeking beyond the end of a file).\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001030"\n"
1031"Note that not all file objects are seekable.");
1032
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001033#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +00001034PyDoc_STRVAR(truncate_doc,
Berker Peksagb87630c2014-09-24 12:43:29 +03001035"truncate([size: int]) -> int. Truncate the file to at most size bytes\n"
1036"and return the truncated size.\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001037"\n"
Berker Peksagb87630c2014-09-24 12:43:29 +03001038"Size defaults to the current file position, as returned by tell().\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +00001039"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001040#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +00001041
1042PyDoc_STRVAR(tell_doc,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001043"tell() -> int. Current file position.\n"
1044"\n"
1045"Can raise OSError for non seekable files."
1046);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001047
1048PyDoc_STRVAR(readinto_doc,
Benjamin Peterson9a8082f2009-03-05 00:55:56 +00001049"readinto() -> Same as RawIOBase.readinto().");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001050
1051PyDoc_STRVAR(close_doc,
1052"close() -> None. Close the file.\n"
1053"\n"
1054"A closed file cannot be used for further I/O operations. close() may be\n"
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001055"called more than once without error.");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001056
1057PyDoc_STRVAR(isatty_doc,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001058"isatty() -> bool. True if the file is connected to a TTY device.");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001059
Guido van Rossuma9e20242007-03-08 00:43:48 +00001060PyDoc_STRVAR(seekable_doc,
1061"seekable() -> bool. True if file supports random-access.");
1062
1063PyDoc_STRVAR(readable_doc,
1064"readable() -> bool. True if file was opened in a read mode.");
1065
1066PyDoc_STRVAR(writable_doc,
1067"writable() -> bool. True if file was opened in a write mode.");
1068
1069static PyMethodDef fileio_methods[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001070 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
1071 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
1072 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
1073 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
1074 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
1075 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001076#ifdef HAVE_FTRUNCATE
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001077 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001078#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001079 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
1080 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
1081 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
1082 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
1083 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
1084 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Antoine Pitroue033e062010-10-29 10:38:18 +00001085 {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
Antoine Pitrou243757e2010-11-05 21:15:39 +00001086 {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001087 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001088};
1089
Guido van Rossum53807da2007-04-10 19:01:47 +00001090/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1091
Guido van Rossumb0428152007-04-08 17:44:42 +00001092static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001093get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001094{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001095 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001096}
1097
1098static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001099get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001100{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001101 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001102}
1103
1104static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001105get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001106{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001107 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001108}
1109
1110static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001111 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1112 {"closefd", (getter)get_closefd, NULL,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001113 "True if the file descriptor will be closed by close()."},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001114 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1115 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001116};
1117
Antoine Pitrou796564c2013-07-30 19:59:21 +02001118static PyMemberDef fileio_members[] = {
Antoine Pitroude687222014-06-29 20:07:28 -04001119 {"_blksize", T_UINT, offsetof(fileio, blksize), 0},
Antoine Pitrou796564c2013-07-30 19:59:21 +02001120 {"_finalizing", T_BOOL, offsetof(fileio, finalizing), 0},
1121 {NULL}
1122};
1123
Guido van Rossuma9e20242007-03-08 00:43:48 +00001124PyTypeObject PyFileIO_Type = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001125 PyVarObject_HEAD_INIT(NULL, 0)
1126 "_io.FileIO",
1127 sizeof(fileio),
1128 0,
1129 (destructor)fileio_dealloc, /* tp_dealloc */
1130 0, /* tp_print */
1131 0, /* tp_getattr */
1132 0, /* tp_setattr */
1133 0, /* tp_reserved */
1134 (reprfunc)fileio_repr, /* tp_repr */
1135 0, /* tp_as_number */
1136 0, /* tp_as_sequence */
1137 0, /* tp_as_mapping */
1138 0, /* tp_hash */
1139 0, /* tp_call */
1140 0, /* tp_str */
1141 PyObject_GenericGetAttr, /* tp_getattro */
1142 0, /* tp_setattro */
1143 0, /* tp_as_buffer */
1144 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +02001145 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001146 fileio_doc, /* tp_doc */
1147 (traverseproc)fileio_traverse, /* tp_traverse */
1148 (inquiry)fileio_clear, /* tp_clear */
1149 0, /* tp_richcompare */
1150 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1151 0, /* tp_iter */
1152 0, /* tp_iternext */
1153 fileio_methods, /* tp_methods */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001154 fileio_members, /* tp_members */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001155 fileio_getsetlist, /* tp_getset */
1156 0, /* tp_base */
1157 0, /* tp_dict */
1158 0, /* tp_descr_get */
1159 0, /* tp_descr_set */
1160 offsetof(fileio, dict), /* tp_dictoffset */
1161 fileio_init, /* tp_init */
1162 PyType_GenericAlloc, /* tp_alloc */
1163 fileio_new, /* tp_new */
1164 PyObject_GC_Del, /* tp_free */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001165 0, /* tp_is_gc */
1166 0, /* tp_bases */
1167 0, /* tp_mro */
1168 0, /* tp_cache */
1169 0, /* tp_subclasses */
1170 0, /* tp_weaklist */
1171 0, /* tp_del */
1172 0, /* tp_version_tag */
1173 0, /* tp_finalize */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001174};