blob: 0f226eafbf1eebe322536268f59aa526251c30cd [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{
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200130 PyObject *res;
131 PyObject *exc, *val, *tb;
132 int rc;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200133 _Py_IDENTIFIER(close);
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200134 res = _PyObject_CallMethodId((PyObject*)&PyRawIOBase_Type,
135 &PyId_close, "O", self);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000136 if (!self->closefd) {
137 self->fd = -1;
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200138 return res;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000139 }
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200140 if (res == NULL)
141 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200142 if (self->finalizing) {
Antoine Pitroue033e062010-10-29 10:38:18 +0000143 PyObject *r = fileio_dealloc_warn(self, (PyObject *) self);
144 if (r)
145 Py_DECREF(r);
146 else
147 PyErr_Clear();
148 }
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200149 rc = internal_close(self);
150 if (res == NULL)
151 _PyErr_ChainExceptions(exc, val, tb);
152 if (rc < 0)
153 Py_CLEAR(res);
154 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000155}
156
157static PyObject *
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000158fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000159{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000160 fileio *self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000161
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000162 assert(type != NULL && type->tp_alloc != NULL);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000163
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000164 self = (fileio *) type->tp_alloc(type, 0);
165 if (self != NULL) {
166 self->fd = -1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100167 self->created = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000168 self->readable = 0;
169 self->writable = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200170 self->appending = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000171 self->seekable = -1;
Antoine Pitroude687222014-06-29 20:07:28 -0400172 self->blksize = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000173 self->closefd = 1;
174 self->weakreflist = NULL;
175 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000176
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000177 return (PyObject *) self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000178}
179
Benjamin Peterson806d4022009-01-19 15:11:51 +0000180static int
181check_fd(int fd)
182{
Steve Dowerf2f373f2015-02-21 08:44:05 -0800183#if defined(HAVE_FSTAT) || defined(MS_WINDOWS)
184 struct _Py_stat_struct buf;
Steve Dower8acde7d2015-03-07 18:14:07 -0800185 if (_Py_fstat(fd, &buf) < 0 &&
186#ifdef MS_WINDOWS
187 GetLastError() == ERROR_INVALID_HANDLE
188#else
189 errno == EBADF
190#endif
191 ) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000192 PyObject *exc;
193 char *msg = strerror(EBADF);
194 exc = PyObject_CallFunction(PyExc_OSError, "(is)",
195 EBADF, msg);
196 PyErr_SetObject(PyExc_OSError, exc);
197 Py_XDECREF(exc);
198 return -1;
199 }
Benjamin Peterson806d4022009-01-19 15:11:51 +0000200#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000201 return 0;
Benjamin Peterson806d4022009-01-19 15:11:51 +0000202}
203
Victor Stinnerdaf45552013-08-28 00:53:59 +0200204#ifdef O_CLOEXEC
205extern int _Py_open_cloexec_works;
206#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000207
208static int
209fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
210{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000211 fileio *self = (fileio *) oself;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200212 static char *kwlist[] = {"file", "mode", "closefd", "opener", NULL};
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000213 const char *name = NULL;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200214 PyObject *nameobj, *stringobj = NULL, *opener = Py_None;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000215 char *mode = "r";
216 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000217#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000218 Py_UNICODE *widename = NULL;
Thomas Helleraf2be262007-07-12 11:03:13 +0000219#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000220 int ret = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200221 int rwa = 0, plus = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000222 int flags = 0;
223 int fd = -1;
224 int closefd = 1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200225 int fd_is_own = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200226#ifdef O_CLOEXEC
227 int *atomic_flag_works = &_Py_open_cloexec_works;
228#elif !defined(MS_WINDOWS)
229 int *atomic_flag_works = NULL;
230#endif
Steve Dowerf2f373f2015-02-21 08:44:05 -0800231#if defined(HAVE_FSTAT) || defined(MS_WINDOWS)
232 struct _Py_stat_struct fdfstat;
Antoine Pitroude687222014-06-29 20:07:28 -0400233#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000234 int async_err = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000235
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000236 assert(PyFileIO_Check(oself));
237 if (self->fd >= 0) {
Hynek Schlawack2cc71562012-05-25 10:05:53 +0200238 if (self->closefd) {
239 /* Have to close the existing file first. */
240 if (internal_close(self) < 0)
241 return -1;
242 }
243 else
244 self->fd = -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000245 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000246
Ross Lagerwall59142db2011-10-31 20:34:46 +0200247 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|siO:fileio",
248 kwlist, &nameobj, &mode, &closefd,
249 &opener))
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000250 return -1;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000251
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000252 if (PyFloat_Check(nameobj)) {
253 PyErr_SetString(PyExc_TypeError,
254 "integer argument expected, got float");
255 return -1;
256 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000257
Serhiy Storchaka78980432013-01-15 01:12:17 +0200258 fd = _PyLong_AsInt(nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000259 if (fd < 0) {
260 if (!PyErr_Occurred()) {
261 PyErr_SetString(PyExc_ValueError,
262 "Negative filedescriptor");
263 return -1;
264 }
265 PyErr_Clear();
266 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000267
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000268#ifdef MS_WINDOWS
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200269 if (PyUnicode_Check(nameobj)) {
Antoine Pitrou13348842012-01-29 18:36:34 +0100270 int rv = _PyUnicode_HasNULChars(nameobj);
271 if (rv) {
272 if (rv != -1)
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300273 PyErr_SetString(PyExc_ValueError, "embedded null character");
Antoine Pitrou13348842012-01-29 18:36:34 +0100274 return -1;
275 }
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200276 widename = PyUnicode_AsUnicode(nameobj);
277 if (widename == NULL)
278 return -1;
279 } else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000280#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000281 if (fd < 0)
282 {
Antoine Pitrou13348842012-01-29 18:36:34 +0100283 if (!PyUnicode_FSConverter(nameobj, &stringobj)) {
284 return -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000285 }
Antoine Pitrou13348842012-01-29 18:36:34 +0100286 name = PyBytes_AS_STRING(stringobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000287 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000288
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000289 s = mode;
290 while (*s) {
291 switch (*s++) {
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100292 case 'x':
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000293 if (rwa) {
294 bad_mode:
295 PyErr_SetString(PyExc_ValueError,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100296 "Must have exactly one of create/read/write/append "
Georg Brandl28928ae2010-10-21 13:45:52 +0000297 "mode and at most one plus");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000298 goto error;
299 }
300 rwa = 1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100301 self->created = 1;
302 self->writable = 1;
303 flags |= O_EXCL | O_CREAT;
304 break;
305 case 'r':
306 if (rwa)
307 goto bad_mode;
308 rwa = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000309 self->readable = 1;
310 break;
311 case 'w':
312 if (rwa)
313 goto bad_mode;
314 rwa = 1;
315 self->writable = 1;
316 flags |= O_CREAT | O_TRUNC;
317 break;
318 case 'a':
319 if (rwa)
320 goto bad_mode;
321 rwa = 1;
322 self->writable = 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200323 self->appending = 1;
324 flags |= O_APPEND | O_CREAT;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000325 break;
326 case 'b':
327 break;
328 case '+':
329 if (plus)
330 goto bad_mode;
331 self->readable = self->writable = 1;
332 plus = 1;
333 break;
334 default:
335 PyErr_Format(PyExc_ValueError,
336 "invalid mode: %.200s", mode);
337 goto error;
338 }
339 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000340
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000341 if (!rwa)
342 goto bad_mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000343
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000344 if (self->readable && self->writable)
345 flags |= O_RDWR;
346 else if (self->readable)
347 flags |= O_RDONLY;
348 else
349 flags |= O_WRONLY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000350
351#ifdef O_BINARY
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000352 flags |= O_BINARY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000353#endif
354
Victor Stinnerdaf45552013-08-28 00:53:59 +0200355#ifdef MS_WINDOWS
356 flags |= O_NOINHERIT;
357#elif defined(O_CLOEXEC)
358 flags |= O_CLOEXEC;
359#endif
Walter Dörwald0e411482007-06-06 16:55:38 +0000360
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000361 if (fd >= 0) {
362 if (check_fd(fd))
363 goto error;
364 self->fd = fd;
365 self->closefd = closefd;
366 }
367 else {
368 self->closefd = 1;
369 if (!closefd) {
370 PyErr_SetString(PyExc_ValueError,
371 "Cannot use closefd=False with file name");
372 goto error;
373 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000374
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000375 errno = 0;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200376 if (opener == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000377 do {
378 Py_BEGIN_ALLOW_THREADS
Thomas Helleraf2be262007-07-12 11:03:13 +0000379#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000380 if (widename != NULL)
381 self->fd = _wopen(widename, flags, 0666);
382 else
Thomas Helleraf2be262007-07-12 11:03:13 +0000383#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000384 self->fd = open(name, flags, 0666);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000385 Py_END_ALLOW_THREADS
386 } while (self->fd < 0 && errno == EINTR &&
387 !(async_err = PyErr_CheckSignals()));
Victor Stinner9672da72015-03-04 18:40:10 +0100388
389 if (async_err)
390 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200391 }
392 else {
393 PyObject *fdobj;
394
395#ifndef MS_WINDOWS
396 /* the opener may clear the atomic flag */
397 atomic_flag_works = NULL;
398#endif
399
400 fdobj = PyObject_CallFunction(opener, "Oi", nameobj, flags);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200401 if (fdobj == NULL)
402 goto error;
403 if (!PyLong_Check(fdobj)) {
404 Py_DECREF(fdobj);
405 PyErr_SetString(PyExc_TypeError,
406 "expected integer from opener");
407 goto error;
408 }
409
Serhiy Storchaka78980432013-01-15 01:12:17 +0200410 self->fd = _PyLong_AsInt(fdobj);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200411 Py_DECREF(fdobj);
412 if (self->fd == -1) {
413 goto error;
414 }
415 }
416
Hynek Schlawack7f59fd72012-06-22 09:32:22 +0200417 fd_is_own = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000418 if (self->fd < 0) {
Victor Stinner9672da72015-03-04 18:40:10 +0100419 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000420 goto error;
421 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200422
423#ifndef MS_WINDOWS
424 if (_Py_set_inheritable(self->fd, 0, atomic_flag_works) < 0)
425 goto error;
426#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000427 }
Antoine Pitroude687222014-06-29 20:07:28 -0400428
429 self->blksize = DEFAULT_BUFFER_SIZE;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800430#if defined(HAVE_FSTAT) || defined(MS_WINDOWS)
431 if (_Py_fstat(self->fd, &fdfstat) < 0) {
432 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrou9235b252012-07-06 18:48:24 +0200433 goto error;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800434 }
Antoine Pitroude687222014-06-29 20:07:28 -0400435#if defined(S_ISDIR) && defined(EISDIR)
436 /* On Unix, open will succeed for directories.
437 In Python, there should be no file objects referring to
438 directories, so we need a check. */
439 if (S_ISDIR(fdfstat.st_mode)) {
440 errno = EISDIR;
441 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);
442 goto error;
443 }
444#endif /* defined(S_ISDIR) */
445#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
446 if (fdfstat.st_blksize > 1)
447 self->blksize = fdfstat.st_blksize;
448#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Steve Dowerf2f373f2015-02-21 08:44:05 -0800449#endif /* HAVE_FSTAT || MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000450
Victor Stinner89e34362011-01-07 18:47:22 +0000451#if defined(MS_WINDOWS) || defined(__CYGWIN__)
452 /* don't translate newlines (\r\n <=> \n) */
453 _setmode(self->fd, O_BINARY);
454#endif
455
Victor Stinnerd9d04192013-11-06 23:50:10 +0100456 if (_PyObject_SetAttrId((PyObject *)self, &PyId_name, nameobj) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000457 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000458
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200459 if (self->appending) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000460 /* For consistent behaviour, we explicitly seek to the
461 end of file (otherwise, it might be done only on the
462 first write()). */
463 PyObject *pos = portable_lseek(self->fd, NULL, 2);
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200464 if (pos == NULL)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000465 goto error;
466 Py_DECREF(pos);
467 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000468
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000469 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000470
471 error:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000472 ret = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200473 if (!fd_is_own)
474 self->fd = -1;
Benjamin Petersonbbb04122010-10-30 23:16:28 +0000475 if (self->fd >= 0)
476 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000477
Guido van Rossuma9e20242007-03-08 00:43:48 +0000478 done:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000479 Py_CLEAR(stringobj);
480 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000481}
482
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000483static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000484fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000485{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000486 Py_VISIT(self->dict);
487 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000488}
489
490static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000491fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000492{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000493 Py_CLEAR(self->dict);
494 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000495}
496
Guido van Rossuma9e20242007-03-08 00:43:48 +0000497static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000498fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000499{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200500 self->finalizing = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000501 if (_PyIOBase_finalize((PyObject *) self) < 0)
502 return;
503 _PyObject_GC_UNTRACK(self);
504 if (self->weakreflist != NULL)
505 PyObject_ClearWeakRefs((PyObject *) self);
506 Py_CLEAR(self->dict);
507 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000508}
509
510static PyObject *
511err_closed(void)
512{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000513 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
514 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000515}
516
517static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000518err_mode(char *action)
519{
Antoine Pitrou712cb732013-12-21 15:51:54 +0100520 _PyIO_State *state = IO_STATE();
521 if (state != NULL)
522 PyErr_Format(state->unsupported_operation,
523 "File not open for %s", action);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000524 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000525}
526
527static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000528fileio_fileno(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000529{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000530 if (self->fd < 0)
531 return err_closed();
532 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000533}
534
535static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000536fileio_readable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000537{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000538 if (self->fd < 0)
539 return err_closed();
540 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000541}
542
543static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000544fileio_writable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000545{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000546 if (self->fd < 0)
547 return err_closed();
548 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000549}
550
551static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000552fileio_seekable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000553{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000554 if (self->fd < 0)
555 return err_closed();
556 if (self->seekable < 0) {
557 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
558 if (pos == NULL) {
559 PyErr_Clear();
560 self->seekable = 0;
561 } else {
562 Py_DECREF(pos);
563 self->seekable = 1;
564 }
565 }
566 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000567}
568
569static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000570fileio_readinto(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000571{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000572 Py_buffer pbuf;
Victor Stinnere6edec22011-01-04 00:29:35 +0000573 Py_ssize_t n, len;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000574 int err, async_err = 0;
Guido van Rossum53807da2007-04-10 19:01:47 +0000575
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000576 if (self->fd < 0)
577 return err_closed();
578 if (!self->readable)
579 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000580
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000581 if (!PyArg_ParseTuple(args, "w*", &pbuf))
582 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000583
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000584 if (_PyVerify_fd(self->fd)) {
Victor Stinnere6edec22011-01-04 00:29:35 +0000585 len = pbuf.len;
Victor Stinner9672da72015-03-04 18:40:10 +0100586#ifdef MS_WINDOWS
587 if (len > INT_MAX)
588 len = INT_MAX;
589#endif
590
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000591 do {
592 Py_BEGIN_ALLOW_THREADS
593 errno = 0;
Victor Stinner14b9b112013-06-25 00:37:25 +0200594#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000595 n = read(self->fd, pbuf.buf, (int)len);
Victor Stinnere6edec22011-01-04 00:29:35 +0000596#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000597 n = read(self->fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +0000598#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000599 Py_END_ALLOW_THREADS
600 } while (n < 0 && errno == EINTR &&
601 !(async_err = PyErr_CheckSignals()));
Victor Stinner9672da72015-03-04 18:40:10 +0100602
603 if (async_err)
604 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000605 } else
606 n = -1;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100607 err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000608 PyBuffer_Release(&pbuf);
609 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100610 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000611 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100612 errno = err;
Victor Stinner9672da72015-03-04 18:40:10 +0100613 PyErr_SetFromErrno(PyExc_IOError);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000614 return NULL;
615 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000616
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000617 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000618}
619
Steve Dowerf2f373f2015-02-21 08:44:05 -0800620#if defined(HAVE_FSTAT) || defined(MS_WINDOWS)
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100621
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000622static size_t
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100623new_buffersize(fileio *self, size_t currentsize)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000624{
Antoine Pitroua3f44572012-04-17 13:50:58 +0200625 size_t addend;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100626
Nadeem Vawdad41a98b2011-10-13 13:34:16 +0200627 /* Expand the buffer by an amount proportional to the current size,
Antoine Pitroua3f44572012-04-17 13:50:58 +0200628 giving us amortized linear-time behavior. For bigger sizes, use a
629 less-than-double growth factor to avoid excessive allocation. */
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100630 assert(currentsize <= PY_SSIZE_T_MAX);
Antoine Pitroua3f44572012-04-17 13:50:58 +0200631 if (currentsize > 65536)
632 addend = currentsize >> 3;
633 else
634 addend = 256 + currentsize;
635 if (addend < SMALLCHUNK)
636 /* Avoid tiny read() calls. */
637 addend = SMALLCHUNK;
638 return addend + currentsize;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000639}
640
Guido van Rossum7165cb12007-07-10 06:54:34 +0000641static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000642fileio_readall(fileio *self)
Guido van Rossum7165cb12007-07-10 06:54:34 +0000643{
Steve Dowerf2f373f2015-02-21 08:44:05 -0800644 struct _Py_stat_struct st;
Victor Stinnera2a64772011-10-11 22:45:02 +0200645 Py_off_t pos, end;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000646 PyObject *result;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100647 Py_ssize_t bytes_read = 0;
Victor Stinner9672da72015-03-04 18:40:10 +0100648 Py_ssize_t len, n;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100649 size_t bufsize;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000650 int async_err = 0;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000651
Victor Stinnerb79f28c2011-05-25 22:09:03 +0200652 if (self->fd < 0)
653 return err_closed();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000654 if (!_PyVerify_fd(self->fd))
655 return PyErr_SetFromErrno(PyExc_IOError);
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000656
Victor Stinner14b9b112013-06-25 00:37:25 +0200657#ifdef MS_WINDOWS
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200658 pos = _lseeki64(self->fd, 0L, SEEK_CUR);
659#else
660 pos = lseek(self->fd, 0L, SEEK_CUR);
661#endif
Steve Dowerf2f373f2015-02-21 08:44:05 -0800662 if (_Py_fstat(self->fd, &st) == 0)
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200663 end = st.st_size;
664 else
Victor Stinnera2a64772011-10-11 22:45:02 +0200665 end = (Py_off_t)-1;
Christian Heimesa872de52008-12-05 08:26:55 +0000666
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100667 if (end > 0 && end >= pos && pos >= 0 && end - pos < PY_SSIZE_T_MAX) {
668 /* This is probably a real file, so we try to allocate a
669 buffer one byte larger than the rest of the file. If the
670 calculation is right then we should get EOF without having
671 to enlarge the buffer. */
672 bufsize = (size_t)(end - pos + 1);
673 } else {
674 bufsize = SMALLCHUNK;
675 }
676
677 result = PyBytes_FromStringAndSize(NULL, bufsize);
678 if (result == NULL)
679 return NULL;
680
681 while (1) {
682 if (bytes_read >= (Py_ssize_t)bufsize) {
683 bufsize = new_buffersize(self, bytes_read);
684 if (bufsize > PY_SSIZE_T_MAX || bufsize <= 0) {
685 PyErr_SetString(PyExc_OverflowError,
686 "unbounded read returned more bytes "
Victor Stinner3e269392013-05-18 00:38:43 +0200687 "than a Python string can hold");
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100688 Py_DECREF(result);
689 return NULL;
690 }
691
692 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
693 if (_PyBytes_Resize(&result, bufsize) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000694 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000695 }
696 }
Victor Stinner9672da72015-03-04 18:40:10 +0100697
698 len = bufsize - bytes_read;
699#ifdef MS_WINDOWS
700 if (len > INT_MAX)
701 len = INT_MAX;
702#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000703 do {
704 Py_BEGIN_ALLOW_THREADS
705 errno = 0;
Victor Stinner14b9b112013-06-25 00:37:25 +0200706#ifdef MS_WINDOWS
Victor Stinner9672da72015-03-04 18:40:10 +0100707 n = read(self->fd, PyBytes_AS_STRING(result) + bytes_read, (int)len);
Victor Stinnerc44057d2013-01-03 03:33:21 +0100708#else
Victor Stinner9672da72015-03-04 18:40:10 +0100709 n = read(self->fd, PyBytes_AS_STRING(result) + bytes_read, len);
Victor Stinnerc44057d2013-01-03 03:33:21 +0100710#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000711 Py_END_ALLOW_THREADS
712 } while (n < 0 && errno == EINTR &&
713 !(async_err = PyErr_CheckSignals()));
Victor Stinner9672da72015-03-04 18:40:10 +0100714
715 if (async_err)
716 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000717 if (n == 0)
718 break;
719 if (n < 0) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000720 if (errno == EAGAIN) {
Victor Stinnere10920f2014-07-02 22:59:31 +0200721 if (bytes_read > 0)
722 break;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000723 Py_DECREF(result);
724 Py_RETURN_NONE;
725 }
726 Py_DECREF(result);
Victor Stinner9672da72015-03-04 18:40:10 +0100727 PyErr_SetFromErrno(PyExc_IOError);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000728 return NULL;
729 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100730 bytes_read += n;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200731 pos += n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000732 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000733
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100734 if (PyBytes_GET_SIZE(result) > bytes_read) {
735 if (_PyBytes_Resize(&result, bytes_read) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000736 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000737 }
738 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000739}
740
Steve Dowerf2f373f2015-02-21 08:44:05 -0800741#else
742
743static PyObject *
744fileio_readall(fileio *self)
745{
746 _Py_IDENTIFIER(readall);
747 return _PyObject_CallMethodId((PyObject*)&PyRawIOBase_Type,
748 &PyId_readall, "O", self);
749}
750
751#endif /* HAVE_FSTAT || MS_WINDOWS */
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100752
Guido van Rossuma9e20242007-03-08 00:43:48 +0000753static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000754fileio_read(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000755{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000756 char *ptr;
757 Py_ssize_t n;
758 Py_ssize_t size = -1;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000759 int async_err = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000760 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000761
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000762 if (self->fd < 0)
763 return err_closed();
764 if (!self->readable)
765 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000766
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000767 if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size))
768 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000769
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000770 if (size < 0) {
771 return fileio_readall(self);
772 }
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000773
Victor Stinner14b9b112013-06-25 00:37:25 +0200774#ifdef MS_WINDOWS
Victor Stinnerc655a722011-07-05 11:31:49 +0200775 if (size > INT_MAX)
776 size = INT_MAX;
777#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000778 bytes = PyBytes_FromStringAndSize(NULL, size);
779 if (bytes == NULL)
780 return NULL;
781 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000782
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000783 if (_PyVerify_fd(self->fd)) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000784 do {
785 Py_BEGIN_ALLOW_THREADS
786 errno = 0;
Victor Stinner14b9b112013-06-25 00:37:25 +0200787#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000788 n = read(self->fd, ptr, (int)size);
Victor Stinnerc655a722011-07-05 11:31:49 +0200789#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000790 n = read(self->fd, ptr, size);
Victor Stinnerc655a722011-07-05 11:31:49 +0200791#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000792 Py_END_ALLOW_THREADS
793 } while (n < 0 && errno == EINTR &&
794 !(async_err = PyErr_CheckSignals()));
Victor Stinner9672da72015-03-04 18:40:10 +0100795
796 if (async_err)
797 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000798 } else
799 n = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000800
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000801 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100802 int err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000803 Py_DECREF(bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100804 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000805 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100806 errno = err;
Victor Stinner9672da72015-03-04 18:40:10 +0100807 PyErr_SetFromErrno(PyExc_IOError);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000808 return NULL;
809 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000810
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000811 if (n != size) {
812 if (_PyBytes_Resize(&bytes, n) < 0) {
Victor Stinner85c761d2013-07-16 21:36:02 +0200813 Py_CLEAR(bytes);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000814 return NULL;
815 }
816 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000817
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000818 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000819}
820
821static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000822fileio_write(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000823{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000824 Py_buffer pbuf;
Victor Stinnere6edec22011-01-04 00:29:35 +0000825 Py_ssize_t n, len;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000826 int err, async_err = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000827
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000828 if (self->fd < 0)
829 return err_closed();
830 if (!self->writable)
831 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000832
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000833 if (!PyArg_ParseTuple(args, "y*", &pbuf))
834 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000835
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000836 if (_PyVerify_fd(self->fd)) {
Victor Stinner9672da72015-03-04 18:40:10 +0100837 len = pbuf.len;
838#ifdef MS_WINDOWS
839 if (len > 32767 && isatty(self->fd)) {
840 /* Issue #11395: the Windows console returns an error (12: not
841 enough space error) on writing into stdout if stdout mode is
842 binary and the length is greater than 66,000 bytes (or less,
843 depending on heap usage). */
844 len = 32767;
845 } else if (len > INT_MAX)
846 len = INT_MAX;
847#endif
848
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000849 do {
850 Py_BEGIN_ALLOW_THREADS
851 errno = 0;
Victor Stinner14b9b112013-06-25 00:37:25 +0200852#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000853 n = write(self->fd, pbuf.buf, (int)len);
Victor Stinnere6edec22011-01-04 00:29:35 +0000854#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000855 n = write(self->fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +0000856#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000857 Py_END_ALLOW_THREADS
858 } while (n < 0 && errno == EINTR &&
859 !(async_err = PyErr_CheckSignals()));
Victor Stinner9672da72015-03-04 18:40:10 +0100860
861 if (async_err)
862 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000863 } else
864 n = -1;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100865 err = errno;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000866
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000867 PyBuffer_Release(&pbuf);
Martin v. Löwis423be952008-08-13 15:53:07 +0000868
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000869 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100870 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000871 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100872 errno = err;
Victor Stinner9672da72015-03-04 18:40:10 +0100873 PyErr_SetFromErrno(PyExc_IOError);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000874 return NULL;
875 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000876
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000877 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000878}
879
Guido van Rossum53807da2007-04-10 19:01:47 +0000880/* XXX Windows support below is likely incomplete */
881
Guido van Rossum53807da2007-04-10 19:01:47 +0000882/* Cribbed from posix_lseek() */
883static PyObject *
884portable_lseek(int fd, PyObject *posobj, int whence)
885{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000886 Py_off_t pos, res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000887
888#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000889 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
890 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000891#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000892 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000893#endif
894#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000895 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000896#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000897#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000898 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000899#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000900 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000901#endif /* SEEK_SET */
902
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000903 if (posobj == NULL)
904 pos = 0;
905 else {
906 if(PyFloat_Check(posobj)) {
907 PyErr_SetString(PyExc_TypeError, "an integer is required");
908 return NULL;
909 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000910#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000911 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000912#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000913 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000914#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000915 if (PyErr_Occurred())
916 return NULL;
917 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000918
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000919 if (_PyVerify_fd(fd)) {
920 Py_BEGIN_ALLOW_THREADS
Victor Stinner14b9b112013-06-25 00:37:25 +0200921#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000922 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000923#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000924 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000925#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000926 Py_END_ALLOW_THREADS
927 } else
928 res = -1;
929 if (res < 0)
930 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000931
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000932#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000933 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000934#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000935 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000936#endif
937}
938
Guido van Rossuma9e20242007-03-08 00:43:48 +0000939static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000940fileio_seek(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000941{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000942 PyObject *posobj;
943 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000944
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000945 if (self->fd < 0)
946 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000947
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000948 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
949 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000950
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000951 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000952}
953
954static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000955fileio_tell(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000956{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000957 if (self->fd < 0)
958 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000959
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000960 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000961}
962
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000963#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000964static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000965fileio_truncate(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000966{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000967 PyObject *posobj = NULL; /* the new size wanted by the user */
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000968#ifndef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000969 Py_off_t pos;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000970#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000971 int ret;
972 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000973
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000974 fd = self->fd;
975 if (fd < 0)
976 return err_closed();
977 if (!self->writable)
978 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000979
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000980 if (!PyArg_ParseTuple(args, "|O", &posobj))
981 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000982
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000983 if (posobj == Py_None || posobj == NULL) {
984 /* Get the current position. */
985 posobj = portable_lseek(fd, NULL, 1);
986 if (posobj == NULL)
987 return NULL;
988 }
989 else {
990 Py_INCREF(posobj);
991 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000992
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000993#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000994 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
995 so don't even try using it. */
996 {
997 PyObject *oldposobj, *tempposobj;
998 HANDLE hFile;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000999
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001000 /* we save the file pointer position */
1001 oldposobj = portable_lseek(fd, NULL, 1);
1002 if (oldposobj == NULL) {
1003 Py_DECREF(posobj);
1004 return NULL;
1005 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +00001006
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001007 /* we then move to the truncation position */
1008 tempposobj = portable_lseek(fd, posobj, 0);
1009 if (tempposobj == NULL) {
1010 Py_DECREF(oldposobj);
1011 Py_DECREF(posobj);
1012 return NULL;
1013 }
1014 Py_DECREF(tempposobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001015
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001016 /* Truncate. Note that this may grow the file! */
1017 Py_BEGIN_ALLOW_THREADS
1018 errno = 0;
1019 hFile = (HANDLE)_get_osfhandle(fd);
1020 ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
1021 if (ret == 0) {
1022 ret = SetEndOfFile(hFile) == 0;
1023 if (ret)
1024 errno = EACCES;
1025 }
1026 Py_END_ALLOW_THREADS
1027
1028 /* we restore the file pointer position in any case */
1029 tempposobj = portable_lseek(fd, oldposobj, 0);
1030 Py_DECREF(oldposobj);
1031 if (tempposobj == NULL) {
1032 Py_DECREF(posobj);
1033 return NULL;
1034 }
1035 Py_DECREF(tempposobj);
1036 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +00001037#else
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001038
1039#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001040 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001041#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001042 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001043#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001044 if (PyErr_Occurred()){
1045 Py_DECREF(posobj);
1046 return NULL;
1047 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001048
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001049 Py_BEGIN_ALLOW_THREADS
1050 errno = 0;
1051 ret = ftruncate(fd, pos);
1052 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001053
Thomas Hellerfdeee3a2007-07-12 11:21:36 +00001054#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001055
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001056 if (ret != 0) {
1057 Py_DECREF(posobj);
1058 PyErr_SetFromErrno(PyExc_IOError);
1059 return NULL;
1060 }
Guido van Rossuma9e20242007-03-08 00:43:48 +00001061
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001062 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001063}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001064#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +00001065
1066static char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001067mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +00001068{
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001069 if (self->created) {
1070 if (self->readable)
1071 return "xb+";
1072 else
1073 return "xb";
1074 }
Antoine Pitroue93b63b2013-09-04 20:46:33 +02001075 if (self->appending) {
1076 if (self->readable)
1077 return "ab+";
1078 else
1079 return "ab";
1080 }
1081 else if (self->readable) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001082 if (self->writable)
1083 return "rb+";
1084 else
1085 return "rb";
1086 }
1087 else
1088 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +00001089}
Guido van Rossuma9e20242007-03-08 00:43:48 +00001090
1091static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001092fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001093{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001094 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001095
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001096 if (self->fd < 0)
1097 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +00001098
Martin v. Löwis767046a2011-10-14 15:35:36 +02001099 nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001100 if (nameobj == NULL) {
1101 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1102 PyErr_Clear();
1103 else
1104 return NULL;
Robert Collins933430a2014-10-18 13:32:43 +13001105 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +02001106 "<_io.FileIO fd=%d mode='%s' closefd=%s>",
1107 self->fd, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001108 }
1109 else {
Robert Collins933430a2014-10-18 13:32:43 +13001110 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +02001111 "<_io.FileIO name=%R mode='%s' closefd=%s>",
1112 nameobj, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001113 Py_DECREF(nameobj);
1114 }
1115 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001116}
1117
1118static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001119fileio_isatty(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001120{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001121 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +00001122
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001123 if (self->fd < 0)
1124 return err_closed();
1125 Py_BEGIN_ALLOW_THREADS
1126 res = isatty(self->fd);
1127 Py_END_ALLOW_THREADS
1128 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001129}
1130
Antoine Pitrou243757e2010-11-05 21:15:39 +00001131static PyObject *
1132fileio_getstate(fileio *self)
1133{
1134 PyErr_Format(PyExc_TypeError,
1135 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
1136 return NULL;
1137}
1138
Guido van Rossuma9e20242007-03-08 00:43:48 +00001139
1140PyDoc_STRVAR(fileio_doc,
Ross Lagerwall59142db2011-10-31 20:34:46 +02001141"file(name: str[, mode: str][, opener: None]) -> file IO object\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001142"\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001143"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 +01001144"writing, exclusive creation or appending. The file will be created if it\n"
1145"doesn't exist when opened for writing or appending; it will be truncated\n"
1146"when opened for writing. A `FileExistsError` will be raised if it already\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001147"exists when opened for creating. Opening a file for creating implies\n"
1148"writing so this mode behaves in a similar way to 'w'.Add a '+' to the mode\n"
1149"to allow simultaneous reading and writing. A custom opener can be used by\n"
1150"passing a callable as *opener*. The underlying file descriptor for the file\n"
Ross Lagerwall59142db2011-10-31 20:34:46 +02001151"object is then obtained by calling opener with (*name*, *flags*).\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001152"*opener* must return an open file descriptor (passing os.open as *opener*\n"
1153"results in functionality similar to passing None).");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001154
1155PyDoc_STRVAR(read_doc,
1156"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
1157"\n"
1158"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +00001159"In non-blocking mode, returns None if no data is available.\n"
1160"On end-of-file, returns ''.");
1161
1162PyDoc_STRVAR(readall_doc,
1163"readall() -> bytes. read all data from the file, returned as bytes.\n"
1164"\n"
1165"In non-blocking mode, returns as much as is immediately available,\n"
1166"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001167
1168PyDoc_STRVAR(write_doc,
1169"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
1170"\n"
1171"Only makes one system call, so not all of the data may be written.\n"
1172"The number of bytes actually written is returned.");
1173
1174PyDoc_STRVAR(fileno_doc,
1175"fileno() -> int. \"file descriptor\".\n"
1176"\n"
1177"This is needed for lower-level file interfaces, such the fcntl module.");
1178
1179PyDoc_STRVAR(seek_doc,
Berker Peksagb87630c2014-09-24 12:43:29 +03001180"seek(offset: int[, whence: int]) -> int. Move to new file position and\n"
1181"return the file position.\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001182"\n"
1183"Argument offset is a byte count. Optional argument whence defaults to\n"
1184"0 (offset from start of file, offset should be >= 0); other values are 1\n"
1185"(move relative to current position, positive or negative), and 2 (move\n"
1186"relative to end of file, usually negative, although many platforms allow\n"
1187"seeking beyond the end of a file)."
1188"\n"
1189"Note that not all file objects are seekable.");
1190
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001191#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +00001192PyDoc_STRVAR(truncate_doc,
Berker Peksagb87630c2014-09-24 12:43:29 +03001193"truncate([size: int]) -> int. Truncate the file to at most size bytes\n"
1194"and return the truncated size.\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001195"\n"
Berker Peksagb87630c2014-09-24 12:43:29 +03001196"Size defaults to the current file position, as returned by tell().\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +00001197"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001198#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +00001199
1200PyDoc_STRVAR(tell_doc,
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001201"tell() -> int. Current file position");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001202
1203PyDoc_STRVAR(readinto_doc,
Benjamin Peterson9a8082f2009-03-05 00:55:56 +00001204"readinto() -> Same as RawIOBase.readinto().");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001205
1206PyDoc_STRVAR(close_doc,
1207"close() -> None. Close the file.\n"
1208"\n"
1209"A closed file cannot be used for further I/O operations. close() may be\n"
1210"called more than once without error. Changes the fileno to -1.");
1211
1212PyDoc_STRVAR(isatty_doc,
1213"isatty() -> bool. True if the file is connected to a tty device.");
1214
Guido van Rossuma9e20242007-03-08 00:43:48 +00001215PyDoc_STRVAR(seekable_doc,
1216"seekable() -> bool. True if file supports random-access.");
1217
1218PyDoc_STRVAR(readable_doc,
1219"readable() -> bool. True if file was opened in a read mode.");
1220
1221PyDoc_STRVAR(writable_doc,
1222"writable() -> bool. True if file was opened in a write mode.");
1223
1224static PyMethodDef fileio_methods[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001225 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
1226 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
1227 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
1228 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
1229 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
1230 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001231#ifdef HAVE_FTRUNCATE
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001232 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001233#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001234 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
1235 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
1236 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
1237 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
1238 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
1239 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Antoine Pitroue033e062010-10-29 10:38:18 +00001240 {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
Antoine Pitrou243757e2010-11-05 21:15:39 +00001241 {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001242 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001243};
1244
Guido van Rossum53807da2007-04-10 19:01:47 +00001245/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1246
Guido van Rossumb0428152007-04-08 17:44:42 +00001247static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001248get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001249{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001250 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001251}
1252
1253static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001254get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001255{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001256 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001257}
1258
1259static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001260get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001261{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001262 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001263}
1264
1265static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001266 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1267 {"closefd", (getter)get_closefd, NULL,
1268 "True if the file descriptor will be closed"},
1269 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1270 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001271};
1272
Antoine Pitrou796564c2013-07-30 19:59:21 +02001273static PyMemberDef fileio_members[] = {
Antoine Pitroude687222014-06-29 20:07:28 -04001274 {"_blksize", T_UINT, offsetof(fileio, blksize), 0},
Antoine Pitrou796564c2013-07-30 19:59:21 +02001275 {"_finalizing", T_BOOL, offsetof(fileio, finalizing), 0},
1276 {NULL}
1277};
1278
Guido van Rossuma9e20242007-03-08 00:43:48 +00001279PyTypeObject PyFileIO_Type = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001280 PyVarObject_HEAD_INIT(NULL, 0)
1281 "_io.FileIO",
1282 sizeof(fileio),
1283 0,
1284 (destructor)fileio_dealloc, /* tp_dealloc */
1285 0, /* tp_print */
1286 0, /* tp_getattr */
1287 0, /* tp_setattr */
1288 0, /* tp_reserved */
1289 (reprfunc)fileio_repr, /* tp_repr */
1290 0, /* tp_as_number */
1291 0, /* tp_as_sequence */
1292 0, /* tp_as_mapping */
1293 0, /* tp_hash */
1294 0, /* tp_call */
1295 0, /* tp_str */
1296 PyObject_GenericGetAttr, /* tp_getattro */
1297 0, /* tp_setattro */
1298 0, /* tp_as_buffer */
1299 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +02001300 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001301 fileio_doc, /* tp_doc */
1302 (traverseproc)fileio_traverse, /* tp_traverse */
1303 (inquiry)fileio_clear, /* tp_clear */
1304 0, /* tp_richcompare */
1305 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1306 0, /* tp_iter */
1307 0, /* tp_iternext */
1308 fileio_methods, /* tp_methods */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001309 fileio_members, /* tp_members */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001310 fileio_getsetlist, /* tp_getset */
1311 0, /* tp_base */
1312 0, /* tp_dict */
1313 0, /* tp_descr_get */
1314 0, /* tp_descr_set */
1315 offsetof(fileio, dict), /* tp_dictoffset */
1316 fileio_init, /* tp_init */
1317 PyType_GenericAlloc, /* tp_alloc */
1318 fileio_new, /* tp_new */
1319 PyObject_GC_Del, /* tp_free */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001320 0, /* tp_is_gc */
1321 0, /* tp_bases */
1322 0, /* tp_mro */
1323 0, /* tp_cache */
1324 0, /* tp_subclasses */
1325 0, /* tp_weaklist */
1326 0, /* tp_del */
1327 0, /* tp_version_tag */
1328 0, /* tp_finalize */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001329};