blob: c44423180c92f6f8cb8e86a96cf38c3c17de365d [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;
185 if (!_PyVerify_fd(fd) || (_Py_fstat(fd, &buf) < 0 && errno == EBADF)) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000186 PyObject *exc;
187 char *msg = strerror(EBADF);
188 exc = PyObject_CallFunction(PyExc_OSError, "(is)",
189 EBADF, msg);
190 PyErr_SetObject(PyExc_OSError, exc);
191 Py_XDECREF(exc);
192 return -1;
193 }
Benjamin Peterson806d4022009-01-19 15:11:51 +0000194#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000195 return 0;
Benjamin Peterson806d4022009-01-19 15:11:51 +0000196}
197
Victor Stinnerdaf45552013-08-28 00:53:59 +0200198#ifdef O_CLOEXEC
199extern int _Py_open_cloexec_works;
200#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000201
202static int
203fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
204{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000205 fileio *self = (fileio *) oself;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200206 static char *kwlist[] = {"file", "mode", "closefd", "opener", NULL};
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000207 const char *name = NULL;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200208 PyObject *nameobj, *stringobj = NULL, *opener = Py_None;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000209 char *mode = "r";
210 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000211#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000212 Py_UNICODE *widename = NULL;
Thomas Helleraf2be262007-07-12 11:03:13 +0000213#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000214 int ret = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200215 int rwa = 0, plus = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000216 int flags = 0;
217 int fd = -1;
218 int closefd = 1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200219 int fd_is_own = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200220#ifdef O_CLOEXEC
221 int *atomic_flag_works = &_Py_open_cloexec_works;
222#elif !defined(MS_WINDOWS)
223 int *atomic_flag_works = NULL;
224#endif
Steve Dowerf2f373f2015-02-21 08:44:05 -0800225#if defined(HAVE_FSTAT) || defined(MS_WINDOWS)
226 struct _Py_stat_struct fdfstat;
Antoine Pitroude687222014-06-29 20:07:28 -0400227#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000228 int async_err = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000229
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000230 assert(PyFileIO_Check(oself));
231 if (self->fd >= 0) {
Hynek Schlawack2cc71562012-05-25 10:05:53 +0200232 if (self->closefd) {
233 /* Have to close the existing file first. */
234 if (internal_close(self) < 0)
235 return -1;
236 }
237 else
238 self->fd = -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000239 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000240
Ross Lagerwall59142db2011-10-31 20:34:46 +0200241 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|siO:fileio",
242 kwlist, &nameobj, &mode, &closefd,
243 &opener))
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000244 return -1;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000245
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000246 if (PyFloat_Check(nameobj)) {
247 PyErr_SetString(PyExc_TypeError,
248 "integer argument expected, got float");
249 return -1;
250 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000251
Serhiy Storchaka78980432013-01-15 01:12:17 +0200252 fd = _PyLong_AsInt(nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000253 if (fd < 0) {
254 if (!PyErr_Occurred()) {
255 PyErr_SetString(PyExc_ValueError,
256 "Negative filedescriptor");
257 return -1;
258 }
259 PyErr_Clear();
260 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000261
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000262#ifdef MS_WINDOWS
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200263 if (PyUnicode_Check(nameobj)) {
Antoine Pitrou13348842012-01-29 18:36:34 +0100264 int rv = _PyUnicode_HasNULChars(nameobj);
265 if (rv) {
266 if (rv != -1)
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300267 PyErr_SetString(PyExc_ValueError, "embedded null character");
Antoine Pitrou13348842012-01-29 18:36:34 +0100268 return -1;
269 }
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200270 widename = PyUnicode_AsUnicode(nameobj);
271 if (widename == NULL)
272 return -1;
273 } else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000274#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000275 if (fd < 0)
276 {
Antoine Pitrou13348842012-01-29 18:36:34 +0100277 if (!PyUnicode_FSConverter(nameobj, &stringobj)) {
278 return -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000279 }
Antoine Pitrou13348842012-01-29 18:36:34 +0100280 name = PyBytes_AS_STRING(stringobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000281 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000282
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000283 s = mode;
284 while (*s) {
285 switch (*s++) {
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100286 case 'x':
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000287 if (rwa) {
288 bad_mode:
289 PyErr_SetString(PyExc_ValueError,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100290 "Must have exactly one of create/read/write/append "
Georg Brandl28928ae2010-10-21 13:45:52 +0000291 "mode and at most one plus");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000292 goto error;
293 }
294 rwa = 1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100295 self->created = 1;
296 self->writable = 1;
297 flags |= O_EXCL | O_CREAT;
298 break;
299 case 'r':
300 if (rwa)
301 goto bad_mode;
302 rwa = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000303 self->readable = 1;
304 break;
305 case 'w':
306 if (rwa)
307 goto bad_mode;
308 rwa = 1;
309 self->writable = 1;
310 flags |= O_CREAT | O_TRUNC;
311 break;
312 case 'a':
313 if (rwa)
314 goto bad_mode;
315 rwa = 1;
316 self->writable = 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200317 self->appending = 1;
318 flags |= O_APPEND | O_CREAT;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000319 break;
320 case 'b':
321 break;
322 case '+':
323 if (plus)
324 goto bad_mode;
325 self->readable = self->writable = 1;
326 plus = 1;
327 break;
328 default:
329 PyErr_Format(PyExc_ValueError,
330 "invalid mode: %.200s", mode);
331 goto error;
332 }
333 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000334
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000335 if (!rwa)
336 goto bad_mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000337
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000338 if (self->readable && self->writable)
339 flags |= O_RDWR;
340 else if (self->readable)
341 flags |= O_RDONLY;
342 else
343 flags |= O_WRONLY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000344
345#ifdef O_BINARY
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000346 flags |= O_BINARY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000347#endif
348
Victor Stinnerdaf45552013-08-28 00:53:59 +0200349#ifdef MS_WINDOWS
350 flags |= O_NOINHERIT;
351#elif defined(O_CLOEXEC)
352 flags |= O_CLOEXEC;
353#endif
Walter Dörwald0e411482007-06-06 16:55:38 +0000354
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000355 if (fd >= 0) {
356 if (check_fd(fd))
357 goto error;
358 self->fd = fd;
359 self->closefd = closefd;
360 }
361 else {
362 self->closefd = 1;
363 if (!closefd) {
364 PyErr_SetString(PyExc_ValueError,
365 "Cannot use closefd=False with file name");
366 goto error;
367 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000368
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000369 errno = 0;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200370 if (opener == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000371 do {
372 Py_BEGIN_ALLOW_THREADS
Thomas Helleraf2be262007-07-12 11:03:13 +0000373#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000374 if (widename != NULL)
375 self->fd = _wopen(widename, flags, 0666);
376 else
Thomas Helleraf2be262007-07-12 11:03:13 +0000377#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000378 self->fd = open(name, flags, 0666);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000379 Py_END_ALLOW_THREADS
380 } while (self->fd < 0 && errno == EINTR &&
381 !(async_err = PyErr_CheckSignals()));
Victor Stinner9672da72015-03-04 18:40:10 +0100382
383 if (async_err)
384 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200385 }
386 else {
387 PyObject *fdobj;
388
389#ifndef MS_WINDOWS
390 /* the opener may clear the atomic flag */
391 atomic_flag_works = NULL;
392#endif
393
394 fdobj = PyObject_CallFunction(opener, "Oi", nameobj, flags);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200395 if (fdobj == NULL)
396 goto error;
397 if (!PyLong_Check(fdobj)) {
398 Py_DECREF(fdobj);
399 PyErr_SetString(PyExc_TypeError,
400 "expected integer from opener");
401 goto error;
402 }
403
Serhiy Storchaka78980432013-01-15 01:12:17 +0200404 self->fd = _PyLong_AsInt(fdobj);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200405 Py_DECREF(fdobj);
406 if (self->fd == -1) {
407 goto error;
408 }
409 }
410
Hynek Schlawack7f59fd72012-06-22 09:32:22 +0200411 fd_is_own = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000412 if (self->fd < 0) {
Victor Stinner9672da72015-03-04 18:40:10 +0100413 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000414 goto error;
415 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200416
417#ifndef MS_WINDOWS
418 if (_Py_set_inheritable(self->fd, 0, atomic_flag_works) < 0)
419 goto error;
420#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000421 }
Antoine Pitroude687222014-06-29 20:07:28 -0400422
423 self->blksize = DEFAULT_BUFFER_SIZE;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800424#if defined(HAVE_FSTAT) || defined(MS_WINDOWS)
425 if (_Py_fstat(self->fd, &fdfstat) < 0) {
426 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrou9235b252012-07-06 18:48:24 +0200427 goto error;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800428 }
Antoine Pitroude687222014-06-29 20:07:28 -0400429#if defined(S_ISDIR) && defined(EISDIR)
430 /* On Unix, open will succeed for directories.
431 In Python, there should be no file objects referring to
432 directories, so we need a check. */
433 if (S_ISDIR(fdfstat.st_mode)) {
434 errno = EISDIR;
435 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);
436 goto error;
437 }
438#endif /* defined(S_ISDIR) */
439#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
440 if (fdfstat.st_blksize > 1)
441 self->blksize = fdfstat.st_blksize;
442#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Steve Dowerf2f373f2015-02-21 08:44:05 -0800443#endif /* HAVE_FSTAT || MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000444
Victor Stinner89e34362011-01-07 18:47:22 +0000445#if defined(MS_WINDOWS) || defined(__CYGWIN__)
446 /* don't translate newlines (\r\n <=> \n) */
447 _setmode(self->fd, O_BINARY);
448#endif
449
Victor Stinnerd9d04192013-11-06 23:50:10 +0100450 if (_PyObject_SetAttrId((PyObject *)self, &PyId_name, nameobj) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000451 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000452
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200453 if (self->appending) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000454 /* For consistent behaviour, we explicitly seek to the
455 end of file (otherwise, it might be done only on the
456 first write()). */
457 PyObject *pos = portable_lseek(self->fd, NULL, 2);
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200458 if (pos == NULL)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000459 goto error;
460 Py_DECREF(pos);
461 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000462
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000463 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000464
465 error:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000466 ret = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200467 if (!fd_is_own)
468 self->fd = -1;
Benjamin Petersonbbb04122010-10-30 23:16:28 +0000469 if (self->fd >= 0)
470 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000471
Guido van Rossuma9e20242007-03-08 00:43:48 +0000472 done:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000473 Py_CLEAR(stringobj);
474 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000475}
476
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000477static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000478fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000479{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000480 Py_VISIT(self->dict);
481 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000482}
483
484static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000485fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000486{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000487 Py_CLEAR(self->dict);
488 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000489}
490
Guido van Rossuma9e20242007-03-08 00:43:48 +0000491static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000492fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000493{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200494 self->finalizing = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000495 if (_PyIOBase_finalize((PyObject *) self) < 0)
496 return;
497 _PyObject_GC_UNTRACK(self);
498 if (self->weakreflist != NULL)
499 PyObject_ClearWeakRefs((PyObject *) self);
500 Py_CLEAR(self->dict);
501 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000502}
503
504static PyObject *
505err_closed(void)
506{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000507 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
508 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000509}
510
511static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000512err_mode(char *action)
513{
Antoine Pitrou712cb732013-12-21 15:51:54 +0100514 _PyIO_State *state = IO_STATE();
515 if (state != NULL)
516 PyErr_Format(state->unsupported_operation,
517 "File not open for %s", action);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000518 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000519}
520
521static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000522fileio_fileno(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000523{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000524 if (self->fd < 0)
525 return err_closed();
526 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000527}
528
529static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000530fileio_readable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000531{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000532 if (self->fd < 0)
533 return err_closed();
534 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000535}
536
537static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000538fileio_writable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000539{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000540 if (self->fd < 0)
541 return err_closed();
542 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000543}
544
545static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000546fileio_seekable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000547{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000548 if (self->fd < 0)
549 return err_closed();
550 if (self->seekable < 0) {
551 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
552 if (pos == NULL) {
553 PyErr_Clear();
554 self->seekable = 0;
555 } else {
556 Py_DECREF(pos);
557 self->seekable = 1;
558 }
559 }
560 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000561}
562
563static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000564fileio_readinto(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000565{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000566 Py_buffer pbuf;
Victor Stinnere6edec22011-01-04 00:29:35 +0000567 Py_ssize_t n, len;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000568 int err, async_err = 0;
Guido van Rossum53807da2007-04-10 19:01:47 +0000569
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000570 if (self->fd < 0)
571 return err_closed();
572 if (!self->readable)
573 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000574
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000575 if (!PyArg_ParseTuple(args, "w*", &pbuf))
576 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000577
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000578 if (_PyVerify_fd(self->fd)) {
Victor Stinnere6edec22011-01-04 00:29:35 +0000579 len = pbuf.len;
Victor Stinner9672da72015-03-04 18:40:10 +0100580#ifdef MS_WINDOWS
581 if (len > INT_MAX)
582 len = INT_MAX;
583#endif
584
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000585 do {
586 Py_BEGIN_ALLOW_THREADS
587 errno = 0;
Victor Stinner14b9b112013-06-25 00:37:25 +0200588#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000589 n = read(self->fd, pbuf.buf, (int)len);
Victor Stinnere6edec22011-01-04 00:29:35 +0000590#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000591 n = read(self->fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +0000592#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000593 Py_END_ALLOW_THREADS
594 } while (n < 0 && errno == EINTR &&
595 !(async_err = PyErr_CheckSignals()));
Victor Stinner9672da72015-03-04 18:40:10 +0100596
597 if (async_err)
598 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000599 } else
600 n = -1;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100601 err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000602 PyBuffer_Release(&pbuf);
603 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100604 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000605 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100606 errno = err;
Victor Stinner9672da72015-03-04 18:40:10 +0100607 PyErr_SetFromErrno(PyExc_IOError);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000608 return NULL;
609 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000610
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000611 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000612}
613
Steve Dowerf2f373f2015-02-21 08:44:05 -0800614#if defined(HAVE_FSTAT) || defined(MS_WINDOWS)
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100615
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000616static size_t
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100617new_buffersize(fileio *self, size_t currentsize)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000618{
Antoine Pitroua3f44572012-04-17 13:50:58 +0200619 size_t addend;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100620
Nadeem Vawdad41a98b2011-10-13 13:34:16 +0200621 /* Expand the buffer by an amount proportional to the current size,
Antoine Pitroua3f44572012-04-17 13:50:58 +0200622 giving us amortized linear-time behavior. For bigger sizes, use a
623 less-than-double growth factor to avoid excessive allocation. */
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100624 assert(currentsize <= PY_SSIZE_T_MAX);
Antoine Pitroua3f44572012-04-17 13:50:58 +0200625 if (currentsize > 65536)
626 addend = currentsize >> 3;
627 else
628 addend = 256 + currentsize;
629 if (addend < SMALLCHUNK)
630 /* Avoid tiny read() calls. */
631 addend = SMALLCHUNK;
632 return addend + currentsize;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000633}
634
Guido van Rossum7165cb12007-07-10 06:54:34 +0000635static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000636fileio_readall(fileio *self)
Guido van Rossum7165cb12007-07-10 06:54:34 +0000637{
Steve Dowerf2f373f2015-02-21 08:44:05 -0800638 struct _Py_stat_struct st;
Victor Stinnera2a64772011-10-11 22:45:02 +0200639 Py_off_t pos, end;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000640 PyObject *result;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100641 Py_ssize_t bytes_read = 0;
Victor Stinner9672da72015-03-04 18:40:10 +0100642 Py_ssize_t len, n;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100643 size_t bufsize;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000644 int async_err = 0;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000645
Victor Stinnerb79f28c2011-05-25 22:09:03 +0200646 if (self->fd < 0)
647 return err_closed();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000648 if (!_PyVerify_fd(self->fd))
649 return PyErr_SetFromErrno(PyExc_IOError);
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000650
Victor Stinner14b9b112013-06-25 00:37:25 +0200651#ifdef MS_WINDOWS
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200652 pos = _lseeki64(self->fd, 0L, SEEK_CUR);
653#else
654 pos = lseek(self->fd, 0L, SEEK_CUR);
655#endif
Steve Dowerf2f373f2015-02-21 08:44:05 -0800656 if (_Py_fstat(self->fd, &st) == 0)
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200657 end = st.st_size;
658 else
Victor Stinnera2a64772011-10-11 22:45:02 +0200659 end = (Py_off_t)-1;
Christian Heimesa872de52008-12-05 08:26:55 +0000660
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100661 if (end > 0 && end >= pos && pos >= 0 && end - pos < PY_SSIZE_T_MAX) {
662 /* This is probably a real file, so we try to allocate a
663 buffer one byte larger than the rest of the file. If the
664 calculation is right then we should get EOF without having
665 to enlarge the buffer. */
666 bufsize = (size_t)(end - pos + 1);
667 } else {
668 bufsize = SMALLCHUNK;
669 }
670
671 result = PyBytes_FromStringAndSize(NULL, bufsize);
672 if (result == NULL)
673 return NULL;
674
675 while (1) {
676 if (bytes_read >= (Py_ssize_t)bufsize) {
677 bufsize = new_buffersize(self, bytes_read);
678 if (bufsize > PY_SSIZE_T_MAX || bufsize <= 0) {
679 PyErr_SetString(PyExc_OverflowError,
680 "unbounded read returned more bytes "
Victor Stinner3e269392013-05-18 00:38:43 +0200681 "than a Python string can hold");
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100682 Py_DECREF(result);
683 return NULL;
684 }
685
686 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
687 if (_PyBytes_Resize(&result, bufsize) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000688 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000689 }
690 }
Victor Stinner9672da72015-03-04 18:40:10 +0100691
692 len = bufsize - bytes_read;
693#ifdef MS_WINDOWS
694 if (len > INT_MAX)
695 len = INT_MAX;
696#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000697 do {
698 Py_BEGIN_ALLOW_THREADS
699 errno = 0;
Victor Stinner14b9b112013-06-25 00:37:25 +0200700#ifdef MS_WINDOWS
Victor Stinner9672da72015-03-04 18:40:10 +0100701 n = read(self->fd, PyBytes_AS_STRING(result) + bytes_read, (int)len);
Victor Stinnerc44057d2013-01-03 03:33:21 +0100702#else
Victor Stinner9672da72015-03-04 18:40:10 +0100703 n = read(self->fd, PyBytes_AS_STRING(result) + bytes_read, len);
Victor Stinnerc44057d2013-01-03 03:33:21 +0100704#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000705 Py_END_ALLOW_THREADS
706 } while (n < 0 && errno == EINTR &&
707 !(async_err = PyErr_CheckSignals()));
Victor Stinner9672da72015-03-04 18:40:10 +0100708
709 if (async_err)
710 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000711 if (n == 0)
712 break;
713 if (n < 0) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000714 if (errno == EAGAIN) {
Victor Stinnere10920f2014-07-02 22:59:31 +0200715 if (bytes_read > 0)
716 break;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000717 Py_DECREF(result);
718 Py_RETURN_NONE;
719 }
720 Py_DECREF(result);
Victor Stinner9672da72015-03-04 18:40:10 +0100721 PyErr_SetFromErrno(PyExc_IOError);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000722 return NULL;
723 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100724 bytes_read += n;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200725 pos += n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000726 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000727
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100728 if (PyBytes_GET_SIZE(result) > bytes_read) {
729 if (_PyBytes_Resize(&result, bytes_read) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000730 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000731 }
732 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000733}
734
Steve Dowerf2f373f2015-02-21 08:44:05 -0800735#else
736
737static PyObject *
738fileio_readall(fileio *self)
739{
740 _Py_IDENTIFIER(readall);
741 return _PyObject_CallMethodId((PyObject*)&PyRawIOBase_Type,
742 &PyId_readall, "O", self);
743}
744
745#endif /* HAVE_FSTAT || MS_WINDOWS */
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100746
Guido van Rossuma9e20242007-03-08 00:43:48 +0000747static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000748fileio_read(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000749{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000750 char *ptr;
751 Py_ssize_t n;
752 Py_ssize_t size = -1;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000753 int async_err = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000754 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000755
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000756 if (self->fd < 0)
757 return err_closed();
758 if (!self->readable)
759 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000760
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000761 if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size))
762 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000763
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000764 if (size < 0) {
765 return fileio_readall(self);
766 }
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000767
Victor Stinner14b9b112013-06-25 00:37:25 +0200768#ifdef MS_WINDOWS
Victor Stinnerc655a722011-07-05 11:31:49 +0200769 if (size > INT_MAX)
770 size = INT_MAX;
771#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000772 bytes = PyBytes_FromStringAndSize(NULL, size);
773 if (bytes == NULL)
774 return NULL;
775 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000776
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000777 if (_PyVerify_fd(self->fd)) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000778 do {
779 Py_BEGIN_ALLOW_THREADS
780 errno = 0;
Victor Stinner14b9b112013-06-25 00:37:25 +0200781#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000782 n = read(self->fd, ptr, (int)size);
Victor Stinnerc655a722011-07-05 11:31:49 +0200783#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000784 n = read(self->fd, ptr, size);
Victor Stinnerc655a722011-07-05 11:31:49 +0200785#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000786 Py_END_ALLOW_THREADS
787 } while (n < 0 && errno == EINTR &&
788 !(async_err = PyErr_CheckSignals()));
Victor Stinner9672da72015-03-04 18:40:10 +0100789
790 if (async_err)
791 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000792 } else
793 n = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000794
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000795 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100796 int err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000797 Py_DECREF(bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100798 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000799 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100800 errno = err;
Victor Stinner9672da72015-03-04 18:40:10 +0100801 PyErr_SetFromErrno(PyExc_IOError);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000802 return NULL;
803 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000804
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000805 if (n != size) {
806 if (_PyBytes_Resize(&bytes, n) < 0) {
Victor Stinner85c761d2013-07-16 21:36:02 +0200807 Py_CLEAR(bytes);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000808 return NULL;
809 }
810 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000811
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000812 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000813}
814
815static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000816fileio_write(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000817{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000818 Py_buffer pbuf;
Victor Stinnere6edec22011-01-04 00:29:35 +0000819 Py_ssize_t n, len;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000820 int err, async_err = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000821
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000822 if (self->fd < 0)
823 return err_closed();
824 if (!self->writable)
825 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000826
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000827 if (!PyArg_ParseTuple(args, "y*", &pbuf))
828 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000829
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000830 if (_PyVerify_fd(self->fd)) {
Victor Stinner9672da72015-03-04 18:40:10 +0100831 len = pbuf.len;
832#ifdef MS_WINDOWS
833 if (len > 32767 && isatty(self->fd)) {
834 /* Issue #11395: the Windows console returns an error (12: not
835 enough space error) on writing into stdout if stdout mode is
836 binary and the length is greater than 66,000 bytes (or less,
837 depending on heap usage). */
838 len = 32767;
839 } else if (len > INT_MAX)
840 len = INT_MAX;
841#endif
842
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000843 do {
844 Py_BEGIN_ALLOW_THREADS
845 errno = 0;
Victor Stinner14b9b112013-06-25 00:37:25 +0200846#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000847 n = write(self->fd, pbuf.buf, (int)len);
Victor Stinnere6edec22011-01-04 00:29:35 +0000848#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000849 n = write(self->fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +0000850#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +0000851 Py_END_ALLOW_THREADS
852 } while (n < 0 && errno == EINTR &&
853 !(async_err = PyErr_CheckSignals()));
Victor Stinner9672da72015-03-04 18:40:10 +0100854
855 if (async_err)
856 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000857 } else
858 n = -1;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100859 err = errno;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000860
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000861 PyBuffer_Release(&pbuf);
Martin v. Löwis423be952008-08-13 15:53:07 +0000862
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000863 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100864 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000865 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100866 errno = err;
Victor Stinner9672da72015-03-04 18:40:10 +0100867 PyErr_SetFromErrno(PyExc_IOError);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000868 return NULL;
869 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000870
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000871 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000872}
873
Guido van Rossum53807da2007-04-10 19:01:47 +0000874/* XXX Windows support below is likely incomplete */
875
Guido van Rossum53807da2007-04-10 19:01:47 +0000876/* Cribbed from posix_lseek() */
877static PyObject *
878portable_lseek(int fd, PyObject *posobj, int whence)
879{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000880 Py_off_t pos, res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000881
882#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000883 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
884 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000885#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000886 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000887#endif
888#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000889 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000890#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000891#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000892 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000893#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000894 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000895#endif /* SEEK_SET */
896
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000897 if (posobj == NULL)
898 pos = 0;
899 else {
900 if(PyFloat_Check(posobj)) {
901 PyErr_SetString(PyExc_TypeError, "an integer is required");
902 return NULL;
903 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000904#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000905 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000906#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000907 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000908#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000909 if (PyErr_Occurred())
910 return NULL;
911 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000912
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000913 if (_PyVerify_fd(fd)) {
914 Py_BEGIN_ALLOW_THREADS
Victor Stinner14b9b112013-06-25 00:37:25 +0200915#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000916 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000917#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000918 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000919#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000920 Py_END_ALLOW_THREADS
921 } else
922 res = -1;
923 if (res < 0)
924 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000925
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000926#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000927 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000928#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000929 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000930#endif
931}
932
Guido van Rossuma9e20242007-03-08 00:43:48 +0000933static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000934fileio_seek(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000935{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000936 PyObject *posobj;
937 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000938
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000939 if (self->fd < 0)
940 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000941
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000942 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
943 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000944
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000945 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000946}
947
948static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000949fileio_tell(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000950{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000951 if (self->fd < 0)
952 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000953
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000954 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000955}
956
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000957#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000958static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000959fileio_truncate(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000960{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000961 PyObject *posobj = NULL; /* the new size wanted by the user */
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000962#ifndef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000963 Py_off_t pos;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000964#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000965 int ret;
966 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000967
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000968 fd = self->fd;
969 if (fd < 0)
970 return err_closed();
971 if (!self->writable)
972 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000973
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000974 if (!PyArg_ParseTuple(args, "|O", &posobj))
975 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000976
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000977 if (posobj == Py_None || posobj == NULL) {
978 /* Get the current position. */
979 posobj = portable_lseek(fd, NULL, 1);
980 if (posobj == NULL)
981 return NULL;
982 }
983 else {
984 Py_INCREF(posobj);
985 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000986
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000987#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000988 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
989 so don't even try using it. */
990 {
991 PyObject *oldposobj, *tempposobj;
992 HANDLE hFile;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000993
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000994 /* we save the file pointer position */
995 oldposobj = portable_lseek(fd, NULL, 1);
996 if (oldposobj == NULL) {
997 Py_DECREF(posobj);
998 return NULL;
999 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +00001000
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001001 /* we then move to the truncation position */
1002 tempposobj = portable_lseek(fd, posobj, 0);
1003 if (tempposobj == NULL) {
1004 Py_DECREF(oldposobj);
1005 Py_DECREF(posobj);
1006 return NULL;
1007 }
1008 Py_DECREF(tempposobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001009
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001010 /* Truncate. Note that this may grow the file! */
1011 Py_BEGIN_ALLOW_THREADS
1012 errno = 0;
1013 hFile = (HANDLE)_get_osfhandle(fd);
1014 ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
1015 if (ret == 0) {
1016 ret = SetEndOfFile(hFile) == 0;
1017 if (ret)
1018 errno = EACCES;
1019 }
1020 Py_END_ALLOW_THREADS
1021
1022 /* we restore the file pointer position in any case */
1023 tempposobj = portable_lseek(fd, oldposobj, 0);
1024 Py_DECREF(oldposobj);
1025 if (tempposobj == NULL) {
1026 Py_DECREF(posobj);
1027 return NULL;
1028 }
1029 Py_DECREF(tempposobj);
1030 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +00001031#else
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001032
1033#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001034 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001035#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001036 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001037#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001038 if (PyErr_Occurred()){
1039 Py_DECREF(posobj);
1040 return NULL;
1041 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001042
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001043 Py_BEGIN_ALLOW_THREADS
1044 errno = 0;
1045 ret = ftruncate(fd, pos);
1046 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001047
Thomas Hellerfdeee3a2007-07-12 11:21:36 +00001048#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001049
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001050 if (ret != 0) {
1051 Py_DECREF(posobj);
1052 PyErr_SetFromErrno(PyExc_IOError);
1053 return NULL;
1054 }
Guido van Rossuma9e20242007-03-08 00:43:48 +00001055
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001056 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001057}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001058#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +00001059
1060static char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001061mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +00001062{
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001063 if (self->created) {
1064 if (self->readable)
1065 return "xb+";
1066 else
1067 return "xb";
1068 }
Antoine Pitroue93b63b2013-09-04 20:46:33 +02001069 if (self->appending) {
1070 if (self->readable)
1071 return "ab+";
1072 else
1073 return "ab";
1074 }
1075 else if (self->readable) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001076 if (self->writable)
1077 return "rb+";
1078 else
1079 return "rb";
1080 }
1081 else
1082 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +00001083}
Guido van Rossuma9e20242007-03-08 00:43:48 +00001084
1085static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001086fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001087{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001088 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001089
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001090 if (self->fd < 0)
1091 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +00001092
Martin v. Löwis767046a2011-10-14 15:35:36 +02001093 nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001094 if (nameobj == NULL) {
1095 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1096 PyErr_Clear();
1097 else
1098 return NULL;
Robert Collins933430a2014-10-18 13:32:43 +13001099 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +02001100 "<_io.FileIO fd=%d mode='%s' closefd=%s>",
1101 self->fd, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001102 }
1103 else {
Robert Collins933430a2014-10-18 13:32:43 +13001104 res = PyUnicode_FromFormat(
Serhiy Storchaka4954f9f2014-12-02 23:39:56 +02001105 "<_io.FileIO name=%R mode='%s' closefd=%s>",
1106 nameobj, mode_string(self), self->closefd ? "True" : "False");
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001107 Py_DECREF(nameobj);
1108 }
1109 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001110}
1111
1112static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001113fileio_isatty(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001114{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001115 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +00001116
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001117 if (self->fd < 0)
1118 return err_closed();
1119 Py_BEGIN_ALLOW_THREADS
1120 res = isatty(self->fd);
1121 Py_END_ALLOW_THREADS
1122 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001123}
1124
Antoine Pitrou243757e2010-11-05 21:15:39 +00001125static PyObject *
1126fileio_getstate(fileio *self)
1127{
1128 PyErr_Format(PyExc_TypeError,
1129 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
1130 return NULL;
1131}
1132
Guido van Rossuma9e20242007-03-08 00:43:48 +00001133
1134PyDoc_STRVAR(fileio_doc,
Ross Lagerwall59142db2011-10-31 20:34:46 +02001135"file(name: str[, mode: str][, opener: None]) -> file IO object\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001136"\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001137"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 +01001138"writing, exclusive creation or appending. The file will be created if it\n"
1139"doesn't exist when opened for writing or appending; it will be truncated\n"
1140"when opened for writing. A `FileExistsError` will be raised if it already\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001141"exists when opened for creating. Opening a file for creating implies\n"
1142"writing so this mode behaves in a similar way to 'w'.Add a '+' to the mode\n"
1143"to allow simultaneous reading and writing. A custom opener can be used by\n"
1144"passing a callable as *opener*. The underlying file descriptor for the file\n"
Ross Lagerwall59142db2011-10-31 20:34:46 +02001145"object is then obtained by calling opener with (*name*, *flags*).\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001146"*opener* must return an open file descriptor (passing os.open as *opener*\n"
1147"results in functionality similar to passing None).");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001148
1149PyDoc_STRVAR(read_doc,
1150"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
1151"\n"
1152"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +00001153"In non-blocking mode, returns None if no data is available.\n"
1154"On end-of-file, returns ''.");
1155
1156PyDoc_STRVAR(readall_doc,
1157"readall() -> bytes. read all data from the file, returned as bytes.\n"
1158"\n"
1159"In non-blocking mode, returns as much as is immediately available,\n"
1160"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001161
1162PyDoc_STRVAR(write_doc,
1163"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
1164"\n"
1165"Only makes one system call, so not all of the data may be written.\n"
1166"The number of bytes actually written is returned.");
1167
1168PyDoc_STRVAR(fileno_doc,
1169"fileno() -> int. \"file descriptor\".\n"
1170"\n"
1171"This is needed for lower-level file interfaces, such the fcntl module.");
1172
1173PyDoc_STRVAR(seek_doc,
Berker Peksagb87630c2014-09-24 12:43:29 +03001174"seek(offset: int[, whence: int]) -> int. Move to new file position and\n"
1175"return the file position.\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001176"\n"
1177"Argument offset is a byte count. Optional argument whence defaults to\n"
1178"0 (offset from start of file, offset should be >= 0); other values are 1\n"
1179"(move relative to current position, positive or negative), and 2 (move\n"
1180"relative to end of file, usually negative, although many platforms allow\n"
1181"seeking beyond the end of a file)."
1182"\n"
1183"Note that not all file objects are seekable.");
1184
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001185#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +00001186PyDoc_STRVAR(truncate_doc,
Berker Peksagb87630c2014-09-24 12:43:29 +03001187"truncate([size: int]) -> int. Truncate the file to at most size bytes\n"
1188"and return the truncated size.\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001189"\n"
Berker Peksagb87630c2014-09-24 12:43:29 +03001190"Size defaults to the current file position, as returned by tell().\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +00001191"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001192#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +00001193
1194PyDoc_STRVAR(tell_doc,
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001195"tell() -> int. Current file position");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001196
1197PyDoc_STRVAR(readinto_doc,
Benjamin Peterson9a8082f2009-03-05 00:55:56 +00001198"readinto() -> Same as RawIOBase.readinto().");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001199
1200PyDoc_STRVAR(close_doc,
1201"close() -> None. Close the file.\n"
1202"\n"
1203"A closed file cannot be used for further I/O operations. close() may be\n"
1204"called more than once without error. Changes the fileno to -1.");
1205
1206PyDoc_STRVAR(isatty_doc,
1207"isatty() -> bool. True if the file is connected to a tty device.");
1208
Guido van Rossuma9e20242007-03-08 00:43:48 +00001209PyDoc_STRVAR(seekable_doc,
1210"seekable() -> bool. True if file supports random-access.");
1211
1212PyDoc_STRVAR(readable_doc,
1213"readable() -> bool. True if file was opened in a read mode.");
1214
1215PyDoc_STRVAR(writable_doc,
1216"writable() -> bool. True if file was opened in a write mode.");
1217
1218static PyMethodDef fileio_methods[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001219 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
1220 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
1221 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
1222 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
1223 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
1224 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001225#ifdef HAVE_FTRUNCATE
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001226 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001227#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001228 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
1229 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
1230 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
1231 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
1232 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
1233 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Antoine Pitroue033e062010-10-29 10:38:18 +00001234 {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
Antoine Pitrou243757e2010-11-05 21:15:39 +00001235 {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001236 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001237};
1238
Guido van Rossum53807da2007-04-10 19:01:47 +00001239/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1240
Guido van Rossumb0428152007-04-08 17:44:42 +00001241static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001242get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001243{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001244 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001245}
1246
1247static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001248get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001249{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001250 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001251}
1252
1253static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001254get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001255{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001256 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001257}
1258
1259static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001260 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1261 {"closefd", (getter)get_closefd, NULL,
1262 "True if the file descriptor will be closed"},
1263 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1264 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001265};
1266
Antoine Pitrou796564c2013-07-30 19:59:21 +02001267static PyMemberDef fileio_members[] = {
Antoine Pitroude687222014-06-29 20:07:28 -04001268 {"_blksize", T_UINT, offsetof(fileio, blksize), 0},
Antoine Pitrou796564c2013-07-30 19:59:21 +02001269 {"_finalizing", T_BOOL, offsetof(fileio, finalizing), 0},
1270 {NULL}
1271};
1272
Guido van Rossuma9e20242007-03-08 00:43:48 +00001273PyTypeObject PyFileIO_Type = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001274 PyVarObject_HEAD_INIT(NULL, 0)
1275 "_io.FileIO",
1276 sizeof(fileio),
1277 0,
1278 (destructor)fileio_dealloc, /* tp_dealloc */
1279 0, /* tp_print */
1280 0, /* tp_getattr */
1281 0, /* tp_setattr */
1282 0, /* tp_reserved */
1283 (reprfunc)fileio_repr, /* tp_repr */
1284 0, /* tp_as_number */
1285 0, /* tp_as_sequence */
1286 0, /* tp_as_mapping */
1287 0, /* tp_hash */
1288 0, /* tp_call */
1289 0, /* tp_str */
1290 PyObject_GenericGetAttr, /* tp_getattro */
1291 0, /* tp_setattro */
1292 0, /* tp_as_buffer */
1293 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +02001294 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001295 fileio_doc, /* tp_doc */
1296 (traverseproc)fileio_traverse, /* tp_traverse */
1297 (inquiry)fileio_clear, /* tp_clear */
1298 0, /* tp_richcompare */
1299 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1300 0, /* tp_iter */
1301 0, /* tp_iternext */
1302 fileio_methods, /* tp_methods */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001303 fileio_members, /* tp_members */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001304 fileio_getsetlist, /* tp_getset */
1305 0, /* tp_base */
1306 0, /* tp_dict */
1307 0, /* tp_descr_get */
1308 0, /* tp_descr_set */
1309 offsetof(fileio, dict), /* tp_dictoffset */
1310 fileio_init, /* tp_init */
1311 PyType_GenericAlloc, /* tp_alloc */
1312 fileio_new, /* tp_new */
1313 PyObject_GC_Del, /* tp_free */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001314 0, /* tp_is_gc */
1315 0, /* tp_bases */
1316 0, /* tp_mro */
1317 0, /* tp_cache */
1318 0, /* tp_subclasses */
1319 0, /* tp_weaklist */
1320 0, /* tp_del */
1321 0, /* tp_version_tag */
1322 0, /* tp_finalize */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001323};