blob: 038b2c6459544e6234df193de68f757eb6d45d9e [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
110 err = close(fd);
111 if (err < 0)
112 save_errno = errno;
113 Py_END_ALLOW_THREADS
114 } else {
115 save_errno = errno;
116 err = -1;
117 }
118 }
119 if (err < 0) {
120 errno = save_errno;
121 PyErr_SetFromErrno(PyExc_IOError);
122 return -1;
123 }
124 return 0;
Neal Norwitz88b44da2007-08-12 17:23:54 +0000125}
126
127static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000128fileio_close(fileio *self)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000129{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200130 _Py_IDENTIFIER(close);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000131 if (!self->closefd) {
132 self->fd = -1;
133 Py_RETURN_NONE;
134 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200135 if (self->finalizing) {
Antoine Pitroue033e062010-10-29 10:38:18 +0000136 PyObject *r = fileio_dealloc_warn(self, (PyObject *) self);
137 if (r)
138 Py_DECREF(r);
139 else
140 PyErr_Clear();
141 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000142 errno = internal_close(self);
143 if (errno < 0)
144 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000145
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200146 return _PyObject_CallMethodId((PyObject*)&PyRawIOBase_Type,
147 &PyId_close, "O", self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000148}
149
150static PyObject *
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000151fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000152{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000153 fileio *self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000154
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000155 assert(type != NULL && type->tp_alloc != NULL);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000156
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000157 self = (fileio *) type->tp_alloc(type, 0);
158 if (self != NULL) {
159 self->fd = -1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100160 self->created = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000161 self->readable = 0;
162 self->writable = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200163 self->appending = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000164 self->seekable = -1;
Antoine Pitroude687222014-06-29 20:07:28 -0400165 self->blksize = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000166 self->closefd = 1;
167 self->weakreflist = NULL;
168 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000169
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000170 return (PyObject *) self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000171}
172
Benjamin Peterson806d4022009-01-19 15:11:51 +0000173static int
174check_fd(int fd)
175{
176#if defined(HAVE_FSTAT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000177 struct stat buf;
178 if (!_PyVerify_fd(fd) || (fstat(fd, &buf) < 0 && errno == EBADF)) {
179 PyObject *exc;
180 char *msg = strerror(EBADF);
181 exc = PyObject_CallFunction(PyExc_OSError, "(is)",
182 EBADF, msg);
183 PyErr_SetObject(PyExc_OSError, exc);
184 Py_XDECREF(exc);
185 return -1;
186 }
Benjamin Peterson806d4022009-01-19 15:11:51 +0000187#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000188 return 0;
Benjamin Peterson806d4022009-01-19 15:11:51 +0000189}
190
Victor Stinnerdaf45552013-08-28 00:53:59 +0200191#ifdef O_CLOEXEC
192extern int _Py_open_cloexec_works;
193#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000194
195static int
196fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
197{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000198 fileio *self = (fileio *) oself;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200199 static char *kwlist[] = {"file", "mode", "closefd", "opener", NULL};
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000200 const char *name = NULL;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200201 PyObject *nameobj, *stringobj = NULL, *opener = Py_None;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000202 char *mode = "r";
203 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000204#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000205 Py_UNICODE *widename = NULL;
Thomas Helleraf2be262007-07-12 11:03:13 +0000206#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000207 int ret = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200208 int rwa = 0, plus = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000209 int flags = 0;
210 int fd = -1;
211 int closefd = 1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200212 int fd_is_own = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200213#ifdef O_CLOEXEC
214 int *atomic_flag_works = &_Py_open_cloexec_works;
215#elif !defined(MS_WINDOWS)
216 int *atomic_flag_works = NULL;
217#endif
Antoine Pitroude687222014-06-29 20:07:28 -0400218#ifdef HAVE_FSTAT
219 struct stat fdfstat;
220#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000221
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000222 assert(PyFileIO_Check(oself));
223 if (self->fd >= 0) {
Hynek Schlawack2cc71562012-05-25 10:05:53 +0200224 if (self->closefd) {
225 /* Have to close the existing file first. */
226 if (internal_close(self) < 0)
227 return -1;
228 }
229 else
230 self->fd = -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000231 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000232
Ross Lagerwall59142db2011-10-31 20:34:46 +0200233 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|siO:fileio",
234 kwlist, &nameobj, &mode, &closefd,
235 &opener))
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000236 return -1;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000237
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000238 if (PyFloat_Check(nameobj)) {
239 PyErr_SetString(PyExc_TypeError,
240 "integer argument expected, got float");
241 return -1;
242 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000243
Serhiy Storchaka78980432013-01-15 01:12:17 +0200244 fd = _PyLong_AsInt(nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000245 if (fd < 0) {
246 if (!PyErr_Occurred()) {
247 PyErr_SetString(PyExc_ValueError,
248 "Negative filedescriptor");
249 return -1;
250 }
251 PyErr_Clear();
252 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000253
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000254#ifdef MS_WINDOWS
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200255 if (PyUnicode_Check(nameobj)) {
Antoine Pitrou13348842012-01-29 18:36:34 +0100256 int rv = _PyUnicode_HasNULChars(nameobj);
257 if (rv) {
258 if (rv != -1)
259 PyErr_SetString(PyExc_TypeError, "embedded NUL character");
260 return -1;
261 }
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200262 widename = PyUnicode_AsUnicode(nameobj);
263 if (widename == NULL)
264 return -1;
265 } else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000266#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000267 if (fd < 0)
268 {
Antoine Pitrou13348842012-01-29 18:36:34 +0100269 if (!PyUnicode_FSConverter(nameobj, &stringobj)) {
270 return -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000271 }
Antoine Pitrou13348842012-01-29 18:36:34 +0100272 name = PyBytes_AS_STRING(stringobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000273 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000274
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000275 s = mode;
276 while (*s) {
277 switch (*s++) {
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100278 case 'x':
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000279 if (rwa) {
280 bad_mode:
281 PyErr_SetString(PyExc_ValueError,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100282 "Must have exactly one of create/read/write/append "
Georg Brandl28928ae2010-10-21 13:45:52 +0000283 "mode and at most one plus");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000284 goto error;
285 }
286 rwa = 1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100287 self->created = 1;
288 self->writable = 1;
289 flags |= O_EXCL | O_CREAT;
290 break;
291 case 'r':
292 if (rwa)
293 goto bad_mode;
294 rwa = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000295 self->readable = 1;
296 break;
297 case 'w':
298 if (rwa)
299 goto bad_mode;
300 rwa = 1;
301 self->writable = 1;
302 flags |= O_CREAT | O_TRUNC;
303 break;
304 case 'a':
305 if (rwa)
306 goto bad_mode;
307 rwa = 1;
308 self->writable = 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200309 self->appending = 1;
310 flags |= O_APPEND | O_CREAT;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000311 break;
312 case 'b':
313 break;
314 case '+':
315 if (plus)
316 goto bad_mode;
317 self->readable = self->writable = 1;
318 plus = 1;
319 break;
320 default:
321 PyErr_Format(PyExc_ValueError,
322 "invalid mode: %.200s", mode);
323 goto error;
324 }
325 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000326
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000327 if (!rwa)
328 goto bad_mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000329
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000330 if (self->readable && self->writable)
331 flags |= O_RDWR;
332 else if (self->readable)
333 flags |= O_RDONLY;
334 else
335 flags |= O_WRONLY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000336
337#ifdef O_BINARY
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000338 flags |= O_BINARY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000339#endif
340
Victor Stinnerdaf45552013-08-28 00:53:59 +0200341#ifdef MS_WINDOWS
342 flags |= O_NOINHERIT;
343#elif defined(O_CLOEXEC)
344 flags |= O_CLOEXEC;
345#endif
Walter Dörwald0e411482007-06-06 16:55:38 +0000346
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000347 if (fd >= 0) {
348 if (check_fd(fd))
349 goto error;
350 self->fd = fd;
351 self->closefd = closefd;
352 }
353 else {
354 self->closefd = 1;
355 if (!closefd) {
356 PyErr_SetString(PyExc_ValueError,
357 "Cannot use closefd=False with file name");
358 goto error;
359 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000360
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000361 errno = 0;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200362 if (opener == Py_None) {
363 Py_BEGIN_ALLOW_THREADS
Thomas Helleraf2be262007-07-12 11:03:13 +0000364#ifdef MS_WINDOWS
Ross Lagerwall59142db2011-10-31 20:34:46 +0200365 if (widename != NULL)
366 self->fd = _wopen(widename, flags, 0666);
367 else
Thomas Helleraf2be262007-07-12 11:03:13 +0000368#endif
Ross Lagerwall59142db2011-10-31 20:34:46 +0200369 self->fd = open(name, flags, 0666);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200370
Ross Lagerwall59142db2011-10-31 20:34:46 +0200371 Py_END_ALLOW_THREADS
Victor Stinnerdaf45552013-08-28 00:53:59 +0200372 }
373 else {
374 PyObject *fdobj;
375
376#ifndef MS_WINDOWS
377 /* the opener may clear the atomic flag */
378 atomic_flag_works = NULL;
379#endif
380
381 fdobj = PyObject_CallFunction(opener, "Oi", nameobj, flags);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200382 if (fdobj == NULL)
383 goto error;
384 if (!PyLong_Check(fdobj)) {
385 Py_DECREF(fdobj);
386 PyErr_SetString(PyExc_TypeError,
387 "expected integer from opener");
388 goto error;
389 }
390
Serhiy Storchaka78980432013-01-15 01:12:17 +0200391 self->fd = _PyLong_AsInt(fdobj);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200392 Py_DECREF(fdobj);
393 if (self->fd == -1) {
394 goto error;
395 }
396 }
397
Hynek Schlawack7f59fd72012-06-22 09:32:22 +0200398 fd_is_own = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000399 if (self->fd < 0) {
Victor Stinner292c8352012-10-30 02:17:38 +0100400 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000401 goto error;
402 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200403
404#ifndef MS_WINDOWS
405 if (_Py_set_inheritable(self->fd, 0, atomic_flag_works) < 0)
406 goto error;
407#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000408 }
Antoine Pitroude687222014-06-29 20:07:28 -0400409
410 self->blksize = DEFAULT_BUFFER_SIZE;
411#ifdef HAVE_FSTAT
412 if (fstat(self->fd, &fdfstat) < 0)
Antoine Pitrou9235b252012-07-06 18:48:24 +0200413 goto error;
Antoine Pitroude687222014-06-29 20:07:28 -0400414#if defined(S_ISDIR) && defined(EISDIR)
415 /* On Unix, open will succeed for directories.
416 In Python, there should be no file objects referring to
417 directories, so we need a check. */
418 if (S_ISDIR(fdfstat.st_mode)) {
419 errno = EISDIR;
420 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);
421 goto error;
422 }
423#endif /* defined(S_ISDIR) */
424#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
425 if (fdfstat.st_blksize > 1)
426 self->blksize = fdfstat.st_blksize;
427#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
428#endif /* HAVE_FSTAT */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000429
Victor Stinner89e34362011-01-07 18:47:22 +0000430#if defined(MS_WINDOWS) || defined(__CYGWIN__)
431 /* don't translate newlines (\r\n <=> \n) */
432 _setmode(self->fd, O_BINARY);
433#endif
434
Victor Stinnerd9d04192013-11-06 23:50:10 +0100435 if (_PyObject_SetAttrId((PyObject *)self, &PyId_name, nameobj) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000436 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000437
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200438 if (self->appending) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000439 /* For consistent behaviour, we explicitly seek to the
440 end of file (otherwise, it might be done only on the
441 first write()). */
442 PyObject *pos = portable_lseek(self->fd, NULL, 2);
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200443 if (pos == NULL)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000444 goto error;
445 Py_DECREF(pos);
446 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000447
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000448 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000449
450 error:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000451 ret = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200452 if (!fd_is_own)
453 self->fd = -1;
Benjamin Petersonbbb04122010-10-30 23:16:28 +0000454 if (self->fd >= 0)
455 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000456
Guido van Rossuma9e20242007-03-08 00:43:48 +0000457 done:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000458 Py_CLEAR(stringobj);
459 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000460}
461
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000462static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000463fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000464{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000465 Py_VISIT(self->dict);
466 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000467}
468
469static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000470fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000471{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000472 Py_CLEAR(self->dict);
473 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000474}
475
Guido van Rossuma9e20242007-03-08 00:43:48 +0000476static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000477fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000478{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200479 self->finalizing = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000480 if (_PyIOBase_finalize((PyObject *) self) < 0)
481 return;
482 _PyObject_GC_UNTRACK(self);
483 if (self->weakreflist != NULL)
484 PyObject_ClearWeakRefs((PyObject *) self);
485 Py_CLEAR(self->dict);
486 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000487}
488
489static PyObject *
490err_closed(void)
491{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000492 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
493 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000494}
495
496static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000497err_mode(char *action)
498{
Antoine Pitrou712cb732013-12-21 15:51:54 +0100499 _PyIO_State *state = IO_STATE();
500 if (state != NULL)
501 PyErr_Format(state->unsupported_operation,
502 "File not open for %s", action);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000503 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000504}
505
506static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000507fileio_fileno(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000508{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000509 if (self->fd < 0)
510 return err_closed();
511 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000512}
513
514static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000515fileio_readable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000516{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000517 if (self->fd < 0)
518 return err_closed();
519 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000520}
521
522static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000523fileio_writable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000524{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000525 if (self->fd < 0)
526 return err_closed();
527 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000528}
529
530static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000531fileio_seekable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000532{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000533 if (self->fd < 0)
534 return err_closed();
535 if (self->seekable < 0) {
536 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
537 if (pos == NULL) {
538 PyErr_Clear();
539 self->seekable = 0;
540 } else {
541 Py_DECREF(pos);
542 self->seekable = 1;
543 }
544 }
545 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000546}
547
548static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000549fileio_readinto(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000550{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000551 Py_buffer pbuf;
Victor Stinnere6edec22011-01-04 00:29:35 +0000552 Py_ssize_t n, len;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100553 int err;
Guido van Rossum53807da2007-04-10 19:01:47 +0000554
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000555 if (self->fd < 0)
556 return err_closed();
557 if (!self->readable)
558 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000559
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000560 if (!PyArg_ParseTuple(args, "w*", &pbuf))
561 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000562
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000563 if (_PyVerify_fd(self->fd)) {
Victor Stinnere6edec22011-01-04 00:29:35 +0000564 len = pbuf.len;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000565 Py_BEGIN_ALLOW_THREADS
566 errno = 0;
Victor Stinner14b9b112013-06-25 00:37:25 +0200567#ifdef MS_WINDOWS
Victor Stinnere6edec22011-01-04 00:29:35 +0000568 if (len > INT_MAX)
569 len = INT_MAX;
570 n = read(self->fd, pbuf.buf, (int)len);
571#else
Victor Stinner72344792011-01-11 00:04:12 +0000572 n = read(self->fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +0000573#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000574 Py_END_ALLOW_THREADS
575 } else
576 n = -1;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100577 err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000578 PyBuffer_Release(&pbuf);
579 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100580 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000581 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100582 errno = err;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000583 PyErr_SetFromErrno(PyExc_IOError);
584 return NULL;
585 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000586
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000587 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000588}
589
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100590#ifndef HAVE_FSTAT
591
592static PyObject *
593fileio_readall(fileio *self)
594{
595 _Py_IDENTIFIER(readall);
596 return _PyObject_CallMethodId((PyObject*)&PyRawIOBase_Type,
597 &PyId_readall, "O", self);
598}
599
600#else
601
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000602static size_t
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100603new_buffersize(fileio *self, size_t currentsize)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000604{
Antoine Pitroua3f44572012-04-17 13:50:58 +0200605 size_t addend;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100606
Nadeem Vawdad41a98b2011-10-13 13:34:16 +0200607 /* Expand the buffer by an amount proportional to the current size,
Antoine Pitroua3f44572012-04-17 13:50:58 +0200608 giving us amortized linear-time behavior. For bigger sizes, use a
609 less-than-double growth factor to avoid excessive allocation. */
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100610 assert(currentsize <= PY_SSIZE_T_MAX);
Antoine Pitroua3f44572012-04-17 13:50:58 +0200611 if (currentsize > 65536)
612 addend = currentsize >> 3;
613 else
614 addend = 256 + currentsize;
615 if (addend < SMALLCHUNK)
616 /* Avoid tiny read() calls. */
617 addend = SMALLCHUNK;
618 return addend + currentsize;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000619}
620
Guido van Rossum7165cb12007-07-10 06:54:34 +0000621static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000622fileio_readall(fileio *self)
Guido van Rossum7165cb12007-07-10 06:54:34 +0000623{
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200624 struct stat st;
Victor Stinnera2a64772011-10-11 22:45:02 +0200625 Py_off_t pos, end;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000626 PyObject *result;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100627 Py_ssize_t bytes_read = 0;
Victor Stinnerc44057d2013-01-03 03:33:21 +0100628 Py_ssize_t n;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100629 size_t bufsize;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000630
Victor Stinnerb79f28c2011-05-25 22:09:03 +0200631 if (self->fd < 0)
632 return err_closed();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000633 if (!_PyVerify_fd(self->fd))
634 return PyErr_SetFromErrno(PyExc_IOError);
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000635
Victor Stinner14b9b112013-06-25 00:37:25 +0200636#ifdef MS_WINDOWS
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200637 pos = _lseeki64(self->fd, 0L, SEEK_CUR);
638#else
639 pos = lseek(self->fd, 0L, SEEK_CUR);
640#endif
641 if (fstat(self->fd, &st) == 0)
642 end = st.st_size;
643 else
Victor Stinnera2a64772011-10-11 22:45:02 +0200644 end = (Py_off_t)-1;
Christian Heimesa872de52008-12-05 08:26:55 +0000645
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100646 if (end > 0 && end >= pos && pos >= 0 && end - pos < PY_SSIZE_T_MAX) {
647 /* This is probably a real file, so we try to allocate a
648 buffer one byte larger than the rest of the file. If the
649 calculation is right then we should get EOF without having
650 to enlarge the buffer. */
651 bufsize = (size_t)(end - pos + 1);
652 } else {
653 bufsize = SMALLCHUNK;
654 }
655
656 result = PyBytes_FromStringAndSize(NULL, bufsize);
657 if (result == NULL)
658 return NULL;
659
660 while (1) {
661 if (bytes_read >= (Py_ssize_t)bufsize) {
662 bufsize = new_buffersize(self, bytes_read);
663 if (bufsize > PY_SSIZE_T_MAX || bufsize <= 0) {
664 PyErr_SetString(PyExc_OverflowError,
665 "unbounded read returned more bytes "
Victor Stinner3e269392013-05-18 00:38:43 +0200666 "than a Python string can hold");
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100667 Py_DECREF(result);
668 return NULL;
669 }
670
671 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
672 if (_PyBytes_Resize(&result, bufsize) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000673 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000674 }
675 }
676 Py_BEGIN_ALLOW_THREADS
677 errno = 0;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100678 n = bufsize - bytes_read;
Victor Stinner14b9b112013-06-25 00:37:25 +0200679#ifdef MS_WINDOWS
Victor Stinnerc44057d2013-01-03 03:33:21 +0100680 if (n > INT_MAX)
681 n = INT_MAX;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100682 n = read(self->fd, PyBytes_AS_STRING(result) + bytes_read, (int)n);
Victor Stinnerc44057d2013-01-03 03:33:21 +0100683#else
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100684 n = read(self->fd, PyBytes_AS_STRING(result) + bytes_read, n);
Victor Stinnerc44057d2013-01-03 03:33:21 +0100685#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000686 Py_END_ALLOW_THREADS
687 if (n == 0)
688 break;
689 if (n < 0) {
Gregory P. Smith51359922012-06-23 23:55:39 -0700690 if (errno == EINTR) {
691 if (PyErr_CheckSignals()) {
692 Py_DECREF(result);
693 return NULL;
694 }
695 continue;
696 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100697 if (bytes_read > 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000698 break;
699 if (errno == EAGAIN) {
700 Py_DECREF(result);
701 Py_RETURN_NONE;
702 }
703 Py_DECREF(result);
704 PyErr_SetFromErrno(PyExc_IOError);
705 return NULL;
706 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100707 bytes_read += n;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200708 pos += n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000709 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000710
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100711 if (PyBytes_GET_SIZE(result) > bytes_read) {
712 if (_PyBytes_Resize(&result, bytes_read) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000713 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000714 }
715 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000716}
717
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100718#endif /* HAVE_FSTAT */
719
Guido van Rossuma9e20242007-03-08 00:43:48 +0000720static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000721fileio_read(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000722{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000723 char *ptr;
724 Py_ssize_t n;
725 Py_ssize_t size = -1;
726 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000727
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000728 if (self->fd < 0)
729 return err_closed();
730 if (!self->readable)
731 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000732
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000733 if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size))
734 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000735
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000736 if (size < 0) {
737 return fileio_readall(self);
738 }
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000739
Victor Stinner14b9b112013-06-25 00:37:25 +0200740#ifdef MS_WINDOWS
Victor Stinnerc655a722011-07-05 11:31:49 +0200741 if (size > INT_MAX)
742 size = INT_MAX;
743#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000744 bytes = PyBytes_FromStringAndSize(NULL, size);
745 if (bytes == NULL)
746 return NULL;
747 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000748
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000749 if (_PyVerify_fd(self->fd)) {
750 Py_BEGIN_ALLOW_THREADS
751 errno = 0;
Victor Stinner14b9b112013-06-25 00:37:25 +0200752#ifdef MS_WINDOWS
Victor Stinnerc655a722011-07-05 11:31:49 +0200753 n = read(self->fd, ptr, (int)size);
754#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000755 n = read(self->fd, ptr, size);
Victor Stinnerc655a722011-07-05 11:31:49 +0200756#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000757 Py_END_ALLOW_THREADS
758 } else
759 n = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000760
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000761 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100762 int err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000763 Py_DECREF(bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100764 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000765 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100766 errno = err;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000767 PyErr_SetFromErrno(PyExc_IOError);
768 return NULL;
769 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000770
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000771 if (n != size) {
772 if (_PyBytes_Resize(&bytes, n) < 0) {
Victor Stinner85c761d2013-07-16 21:36:02 +0200773 Py_CLEAR(bytes);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000774 return NULL;
775 }
776 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000777
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000778 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000779}
780
781static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000782fileio_write(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000783{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000784 Py_buffer pbuf;
Victor Stinnere6edec22011-01-04 00:29:35 +0000785 Py_ssize_t n, len;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100786 int err;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000787
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000788 if (self->fd < 0)
789 return err_closed();
790 if (!self->writable)
791 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000792
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000793 if (!PyArg_ParseTuple(args, "y*", &pbuf))
794 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000795
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000796 if (_PyVerify_fd(self->fd)) {
797 Py_BEGIN_ALLOW_THREADS
798 errno = 0;
Victor Stinnere6edec22011-01-04 00:29:35 +0000799 len = pbuf.len;
Victor Stinner14b9b112013-06-25 00:37:25 +0200800#ifdef MS_WINDOWS
Victor Stinnere0daff12011-03-20 23:36:35 +0100801 if (len > 32767 && isatty(self->fd)) {
802 /* Issue #11395: the Windows console returns an error (12: not
803 enough space error) on writing into stdout if stdout mode is
804 binary and the length is greater than 66,000 bytes (or less,
805 depending on heap usage). */
806 len = 32767;
807 }
808 else if (len > INT_MAX)
Victor Stinnere6edec22011-01-04 00:29:35 +0000809 len = INT_MAX;
810 n = write(self->fd, pbuf.buf, (int)len);
811#else
Victor Stinner72344792011-01-11 00:04:12 +0000812 n = write(self->fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +0000813#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000814 Py_END_ALLOW_THREADS
815 } else
816 n = -1;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100817 err = errno;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000818
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000819 PyBuffer_Release(&pbuf);
Martin v. Löwis423be952008-08-13 15:53:07 +0000820
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000821 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100822 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000823 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100824 errno = err;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000825 PyErr_SetFromErrno(PyExc_IOError);
826 return NULL;
827 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000828
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000829 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000830}
831
Guido van Rossum53807da2007-04-10 19:01:47 +0000832/* XXX Windows support below is likely incomplete */
833
Guido van Rossum53807da2007-04-10 19:01:47 +0000834/* Cribbed from posix_lseek() */
835static PyObject *
836portable_lseek(int fd, PyObject *posobj, int whence)
837{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000838 Py_off_t pos, res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000839
840#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000841 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
842 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000843#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000844 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000845#endif
846#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000847 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000848#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000849#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000850 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000851#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000852 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000853#endif /* SEEK_SET */
854
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000855 if (posobj == NULL)
856 pos = 0;
857 else {
858 if(PyFloat_Check(posobj)) {
859 PyErr_SetString(PyExc_TypeError, "an integer is required");
860 return NULL;
861 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000862#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000863 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000864#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000865 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000866#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000867 if (PyErr_Occurred())
868 return NULL;
869 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000870
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000871 if (_PyVerify_fd(fd)) {
872 Py_BEGIN_ALLOW_THREADS
Victor Stinner14b9b112013-06-25 00:37:25 +0200873#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000874 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000875#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000876 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000877#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000878 Py_END_ALLOW_THREADS
879 } else
880 res = -1;
881 if (res < 0)
882 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000883
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000884#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000885 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000886#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000887 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000888#endif
889}
890
Guido van Rossuma9e20242007-03-08 00:43:48 +0000891static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000892fileio_seek(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000893{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000894 PyObject *posobj;
895 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000896
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000897 if (self->fd < 0)
898 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000899
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000900 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
901 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000902
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000903 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000904}
905
906static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000907fileio_tell(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000908{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000909 if (self->fd < 0)
910 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000911
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000912 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000913}
914
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000915#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000916static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000917fileio_truncate(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000918{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000919 PyObject *posobj = NULL; /* the new size wanted by the user */
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000920#ifndef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000921 Py_off_t pos;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000922#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000923 int ret;
924 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000925
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000926 fd = self->fd;
927 if (fd < 0)
928 return err_closed();
929 if (!self->writable)
930 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000931
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000932 if (!PyArg_ParseTuple(args, "|O", &posobj))
933 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000934
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000935 if (posobj == Py_None || posobj == NULL) {
936 /* Get the current position. */
937 posobj = portable_lseek(fd, NULL, 1);
938 if (posobj == NULL)
939 return NULL;
940 }
941 else {
942 Py_INCREF(posobj);
943 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000944
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000945#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000946 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
947 so don't even try using it. */
948 {
949 PyObject *oldposobj, *tempposobj;
950 HANDLE hFile;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000951
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000952 /* we save the file pointer position */
953 oldposobj = portable_lseek(fd, NULL, 1);
954 if (oldposobj == NULL) {
955 Py_DECREF(posobj);
956 return NULL;
957 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000958
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000959 /* we then move to the truncation position */
960 tempposobj = portable_lseek(fd, posobj, 0);
961 if (tempposobj == NULL) {
962 Py_DECREF(oldposobj);
963 Py_DECREF(posobj);
964 return NULL;
965 }
966 Py_DECREF(tempposobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000967
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000968 /* Truncate. Note that this may grow the file! */
969 Py_BEGIN_ALLOW_THREADS
970 errno = 0;
971 hFile = (HANDLE)_get_osfhandle(fd);
972 ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
973 if (ret == 0) {
974 ret = SetEndOfFile(hFile) == 0;
975 if (ret)
976 errno = EACCES;
977 }
978 Py_END_ALLOW_THREADS
979
980 /* we restore the file pointer position in any case */
981 tempposobj = portable_lseek(fd, oldposobj, 0);
982 Py_DECREF(oldposobj);
983 if (tempposobj == NULL) {
984 Py_DECREF(posobj);
985 return NULL;
986 }
987 Py_DECREF(tempposobj);
988 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000989#else
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000990
991#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000992 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000993#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000994 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000995#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000996 if (PyErr_Occurred()){
997 Py_DECREF(posobj);
998 return NULL;
999 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001000
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001001 Py_BEGIN_ALLOW_THREADS
1002 errno = 0;
1003 ret = ftruncate(fd, pos);
1004 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001005
Thomas Hellerfdeee3a2007-07-12 11:21:36 +00001006#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001007
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001008 if (ret != 0) {
1009 Py_DECREF(posobj);
1010 PyErr_SetFromErrno(PyExc_IOError);
1011 return NULL;
1012 }
Guido van Rossuma9e20242007-03-08 00:43:48 +00001013
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001014 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001015}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001016#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +00001017
1018static char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001019mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +00001020{
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001021 if (self->created) {
1022 if (self->readable)
1023 return "xb+";
1024 else
1025 return "xb";
1026 }
Antoine Pitroue93b63b2013-09-04 20:46:33 +02001027 if (self->appending) {
1028 if (self->readable)
1029 return "ab+";
1030 else
1031 return "ab";
1032 }
1033 else if (self->readable) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001034 if (self->writable)
1035 return "rb+";
1036 else
1037 return "rb";
1038 }
1039 else
1040 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +00001041}
Guido van Rossuma9e20242007-03-08 00:43:48 +00001042
1043static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001044fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001045{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001046 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001047
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001048 if (self->fd < 0)
1049 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +00001050
Martin v. Löwis767046a2011-10-14 15:35:36 +02001051 nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001052 if (nameobj == NULL) {
1053 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1054 PyErr_Clear();
1055 else
1056 return NULL;
1057 res = PyUnicode_FromFormat("<_io.FileIO fd=%d mode='%s'>",
1058 self->fd, mode_string(self));
1059 }
1060 else {
1061 res = PyUnicode_FromFormat("<_io.FileIO name=%R mode='%s'>",
1062 nameobj, mode_string(self));
1063 Py_DECREF(nameobj);
1064 }
1065 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001066}
1067
1068static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001069fileio_isatty(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001070{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001071 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +00001072
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001073 if (self->fd < 0)
1074 return err_closed();
1075 Py_BEGIN_ALLOW_THREADS
1076 res = isatty(self->fd);
1077 Py_END_ALLOW_THREADS
1078 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001079}
1080
Antoine Pitrou243757e2010-11-05 21:15:39 +00001081static PyObject *
1082fileio_getstate(fileio *self)
1083{
1084 PyErr_Format(PyExc_TypeError,
1085 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
1086 return NULL;
1087}
1088
Guido van Rossuma9e20242007-03-08 00:43:48 +00001089
1090PyDoc_STRVAR(fileio_doc,
Ross Lagerwall59142db2011-10-31 20:34:46 +02001091"file(name: str[, mode: str][, opener: None]) -> file IO object\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001092"\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001093"Open a file. The mode can be 'r', 'w', 'x' or 'a' for reading (default),\n"
Charles-François Natalid612de12012-01-14 11:51:00 +01001094"writing, exclusive creation or appending. The file will be created if it\n"
1095"doesn't exist when opened for writing or appending; it will be truncated\n"
1096"when opened for writing. A `FileExistsError` will be raised if it already\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001097"exists when opened for creating. Opening a file for creating implies\n"
1098"writing so this mode behaves in a similar way to 'w'.Add a '+' to the mode\n"
1099"to allow simultaneous reading and writing. A custom opener can be used by\n"
1100"passing a callable as *opener*. The underlying file descriptor for the file\n"
Ross Lagerwall59142db2011-10-31 20:34:46 +02001101"object is then obtained by calling opener with (*name*, *flags*).\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001102"*opener* must return an open file descriptor (passing os.open as *opener*\n"
1103"results in functionality similar to passing None).");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001104
1105PyDoc_STRVAR(read_doc,
1106"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
1107"\n"
1108"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +00001109"In non-blocking mode, returns None if no data is available.\n"
1110"On end-of-file, returns ''.");
1111
1112PyDoc_STRVAR(readall_doc,
1113"readall() -> bytes. read all data from the file, returned as bytes.\n"
1114"\n"
1115"In non-blocking mode, returns as much as is immediately available,\n"
1116"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001117
1118PyDoc_STRVAR(write_doc,
1119"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
1120"\n"
1121"Only makes one system call, so not all of the data may be written.\n"
1122"The number of bytes actually written is returned.");
1123
1124PyDoc_STRVAR(fileno_doc,
1125"fileno() -> int. \"file descriptor\".\n"
1126"\n"
1127"This is needed for lower-level file interfaces, such the fcntl module.");
1128
1129PyDoc_STRVAR(seek_doc,
1130"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
1131"\n"
1132"Argument offset is a byte count. Optional argument whence defaults to\n"
1133"0 (offset from start of file, offset should be >= 0); other values are 1\n"
1134"(move relative to current position, positive or negative), and 2 (move\n"
1135"relative to end of file, usually negative, although many platforms allow\n"
1136"seeking beyond the end of a file)."
1137"\n"
1138"Note that not all file objects are seekable.");
1139
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001140#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +00001141PyDoc_STRVAR(truncate_doc,
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001142"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001143"\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +00001144"Size defaults to the current file position, as returned by tell()."
1145"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001146#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +00001147
1148PyDoc_STRVAR(tell_doc,
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001149"tell() -> int. Current file position");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001150
1151PyDoc_STRVAR(readinto_doc,
Benjamin Peterson9a8082f2009-03-05 00:55:56 +00001152"readinto() -> Same as RawIOBase.readinto().");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001153
1154PyDoc_STRVAR(close_doc,
1155"close() -> None. Close the file.\n"
1156"\n"
1157"A closed file cannot be used for further I/O operations. close() may be\n"
1158"called more than once without error. Changes the fileno to -1.");
1159
1160PyDoc_STRVAR(isatty_doc,
1161"isatty() -> bool. True if the file is connected to a tty device.");
1162
Guido van Rossuma9e20242007-03-08 00:43:48 +00001163PyDoc_STRVAR(seekable_doc,
1164"seekable() -> bool. True if file supports random-access.");
1165
1166PyDoc_STRVAR(readable_doc,
1167"readable() -> bool. True if file was opened in a read mode.");
1168
1169PyDoc_STRVAR(writable_doc,
1170"writable() -> bool. True if file was opened in a write mode.");
1171
1172static PyMethodDef fileio_methods[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001173 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
1174 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
1175 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
1176 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
1177 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
1178 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001179#ifdef HAVE_FTRUNCATE
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001180 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001181#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001182 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
1183 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
1184 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
1185 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
1186 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
1187 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Antoine Pitroue033e062010-10-29 10:38:18 +00001188 {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
Antoine Pitrou243757e2010-11-05 21:15:39 +00001189 {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001190 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001191};
1192
Guido van Rossum53807da2007-04-10 19:01:47 +00001193/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1194
Guido van Rossumb0428152007-04-08 17:44:42 +00001195static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001196get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001197{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001198 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001199}
1200
1201static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001202get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001203{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001204 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001205}
1206
1207static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001208get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001209{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001210 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001211}
1212
1213static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001214 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1215 {"closefd", (getter)get_closefd, NULL,
1216 "True if the file descriptor will be closed"},
1217 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1218 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001219};
1220
Antoine Pitrou796564c2013-07-30 19:59:21 +02001221static PyMemberDef fileio_members[] = {
Antoine Pitroude687222014-06-29 20:07:28 -04001222 {"_blksize", T_UINT, offsetof(fileio, blksize), 0},
Antoine Pitrou796564c2013-07-30 19:59:21 +02001223 {"_finalizing", T_BOOL, offsetof(fileio, finalizing), 0},
1224 {NULL}
1225};
1226
Guido van Rossuma9e20242007-03-08 00:43:48 +00001227PyTypeObject PyFileIO_Type = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001228 PyVarObject_HEAD_INIT(NULL, 0)
1229 "_io.FileIO",
1230 sizeof(fileio),
1231 0,
1232 (destructor)fileio_dealloc, /* tp_dealloc */
1233 0, /* tp_print */
1234 0, /* tp_getattr */
1235 0, /* tp_setattr */
1236 0, /* tp_reserved */
1237 (reprfunc)fileio_repr, /* tp_repr */
1238 0, /* tp_as_number */
1239 0, /* tp_as_sequence */
1240 0, /* tp_as_mapping */
1241 0, /* tp_hash */
1242 0, /* tp_call */
1243 0, /* tp_str */
1244 PyObject_GenericGetAttr, /* tp_getattro */
1245 0, /* tp_setattro */
1246 0, /* tp_as_buffer */
1247 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +02001248 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001249 fileio_doc, /* tp_doc */
1250 (traverseproc)fileio_traverse, /* tp_traverse */
1251 (inquiry)fileio_clear, /* tp_clear */
1252 0, /* tp_richcompare */
1253 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1254 0, /* tp_iter */
1255 0, /* tp_iternext */
1256 fileio_methods, /* tp_methods */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001257 fileio_members, /* tp_members */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001258 fileio_getsetlist, /* tp_getset */
1259 0, /* tp_base */
1260 0, /* tp_dict */
1261 0, /* tp_descr_get */
1262 0, /* tp_descr_set */
1263 offsetof(fileio, dict), /* tp_dictoffset */
1264 fileio_init, /* tp_init */
1265 PyType_GenericAlloc, /* tp_alloc */
1266 fileio_new, /* tp_new */
1267 PyObject_GC_Del, /* tp_free */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001268 0, /* tp_is_gc */
1269 0, /* tp_bases */
1270 0, /* tp_mro */
1271 0, /* tp_cache */
1272 0, /* tp_subclasses */
1273 0, /* tp_weaklist */
1274 0, /* tp_del */
1275 0, /* tp_version_tag */
1276 0, /* tp_finalize */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001277};