blob: 74508a7f42869f76c085adfa1c42a59fbcf70ef0 [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 Pitrouae4b4722010-05-05 16:31:07 +000056 PyObject *weakreflist;
57 PyObject *dict;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000058} fileio;
Guido van Rossuma9e20242007-03-08 00:43:48 +000059
Collin Winteraf334382007-03-08 21:46:15 +000060PyTypeObject PyFileIO_Type;
61
Victor Stinnerd9d04192013-11-06 23:50:10 +010062_Py_IDENTIFIER(name);
63
Guido van Rossuma9e20242007-03-08 00:43:48 +000064#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
65
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000066int
67_PyFileIO_closed(PyObject *self)
68{
Antoine Pitrouae4b4722010-05-05 16:31:07 +000069 return ((fileio *)self)->fd < 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000070}
Antoine Pitrou08838b62009-01-21 00:55:13 +000071
Antoine Pitroue033e062010-10-29 10:38:18 +000072/* Because this can call arbitrary code, it shouldn't be called when
73 the refcount is 0 (that is, not directly from tp_dealloc unless
74 the refcount has been temporarily re-incremented). */
75static PyObject *
76fileio_dealloc_warn(fileio *self, PyObject *source)
77{
78 if (self->fd >= 0 && self->closefd) {
79 PyObject *exc, *val, *tb;
80 PyErr_Fetch(&exc, &val, &tb);
81 if (PyErr_WarnFormat(PyExc_ResourceWarning, 1,
82 "unclosed file %R", source)) {
83 /* Spurious errors can appear at shutdown */
84 if (PyErr_ExceptionMatches(PyExc_Warning))
85 PyErr_WriteUnraisable((PyObject *) self);
86 }
87 PyErr_Restore(exc, val, tb);
88 }
89 Py_RETURN_NONE;
90}
91
Antoine Pitrou7fb111b2009-03-04 11:14:01 +000092static PyObject *
93portable_lseek(int fd, PyObject *posobj, int whence);
94
Antoine Pitroua28fcfd2009-03-13 23:42:55 +000095static PyObject *portable_lseek(int fd, PyObject *posobj, int whence);
96
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000097/* Returns 0 on success, -1 with exception set on failure. */
Neal Norwitz88b44da2007-08-12 17:23:54 +000098static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000099internal_close(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000100{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000101 int err = 0;
102 int save_errno = 0;
103 if (self->fd >= 0) {
104 int fd = self->fd;
105 self->fd = -1;
106 /* fd is accessible and someone else may have closed it */
107 if (_PyVerify_fd(fd)) {
108 Py_BEGIN_ALLOW_THREADS
109 err = close(fd);
110 if (err < 0)
111 save_errno = errno;
112 Py_END_ALLOW_THREADS
113 } else {
114 save_errno = errno;
115 err = -1;
116 }
117 }
118 if (err < 0) {
119 errno = save_errno;
120 PyErr_SetFromErrno(PyExc_IOError);
121 return -1;
122 }
123 return 0;
Neal Norwitz88b44da2007-08-12 17:23:54 +0000124}
125
126static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000127fileio_close(fileio *self)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000128{
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200129 PyObject *res;
130 PyObject *exc, *val, *tb;
131 int rc;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200132 _Py_IDENTIFIER(close);
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200133 res = _PyObject_CallMethodId((PyObject*)&PyRawIOBase_Type,
134 &PyId_close, "O", self);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000135 if (!self->closefd) {
136 self->fd = -1;
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200137 return res;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000138 }
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200139 if (res == NULL)
140 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200141 if (self->finalizing) {
Antoine Pitroue033e062010-10-29 10:38:18 +0000142 PyObject *r = fileio_dealloc_warn(self, (PyObject *) self);
143 if (r)
144 Py_DECREF(r);
145 else
146 PyErr_Clear();
147 }
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200148 rc = internal_close(self);
149 if (res == NULL)
150 _PyErr_ChainExceptions(exc, val, tb);
151 if (rc < 0)
152 Py_CLEAR(res);
153 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000154}
155
156static PyObject *
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000157fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000158{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000159 fileio *self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000160
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000161 assert(type != NULL && type->tp_alloc != NULL);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000162
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000163 self = (fileio *) type->tp_alloc(type, 0);
164 if (self != NULL) {
165 self->fd = -1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100166 self->created = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000167 self->readable = 0;
168 self->writable = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200169 self->appending = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000170 self->seekable = -1;
171 self->closefd = 1;
172 self->weakreflist = NULL;
173 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000174
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000175 return (PyObject *) self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000176}
177
178/* On Unix, open will succeed for directories.
179 In Python, there should be no file objects referring to
180 directories, so we need a check. */
181
182static int
Antoine Pitrou9235b252012-07-06 18:48:24 +0200183dircheck(fileio* self, PyObject *nameobj)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000184{
Christian Heimes91e8b812013-06-23 23:51:44 +0200185#if defined(HAVE_FSTAT) && defined(S_ISDIR) && defined(EISDIR)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000186 struct stat buf;
187 if (self->fd < 0)
188 return 0;
189 if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
Antoine Pitrou9235b252012-07-06 18:48:24 +0200190 errno = EISDIR;
191 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000192 return -1;
193 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000194#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000195 return 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000196}
197
Benjamin Peterson806d4022009-01-19 15:11:51 +0000198static int
199check_fd(int fd)
200{
201#if defined(HAVE_FSTAT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000202 struct stat buf;
203 if (!_PyVerify_fd(fd) || (fstat(fd, &buf) < 0 && errno == EBADF)) {
204 PyObject *exc;
205 char *msg = strerror(EBADF);
206 exc = PyObject_CallFunction(PyExc_OSError, "(is)",
207 EBADF, msg);
208 PyErr_SetObject(PyExc_OSError, exc);
209 Py_XDECREF(exc);
210 return -1;
211 }
Benjamin Peterson806d4022009-01-19 15:11:51 +0000212#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000213 return 0;
Benjamin Peterson806d4022009-01-19 15:11:51 +0000214}
215
Victor Stinnerdaf45552013-08-28 00:53:59 +0200216#ifdef O_CLOEXEC
217extern int _Py_open_cloexec_works;
218#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000219
220static int
221fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
222{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000223 fileio *self = (fileio *) oself;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200224 static char *kwlist[] = {"file", "mode", "closefd", "opener", NULL};
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000225 const char *name = NULL;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200226 PyObject *nameobj, *stringobj = NULL, *opener = Py_None;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000227 char *mode = "r";
228 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000229#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000230 Py_UNICODE *widename = NULL;
Thomas Helleraf2be262007-07-12 11:03:13 +0000231#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000232 int ret = 0;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200233 int rwa = 0, plus = 0;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000234 int flags = 0;
235 int fd = -1;
236 int closefd = 1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200237 int fd_is_own = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200238#ifdef O_CLOEXEC
239 int *atomic_flag_works = &_Py_open_cloexec_works;
240#elif !defined(MS_WINDOWS)
241 int *atomic_flag_works = NULL;
242#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000243
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000244 assert(PyFileIO_Check(oself));
245 if (self->fd >= 0) {
Hynek Schlawack2cc71562012-05-25 10:05:53 +0200246 if (self->closefd) {
247 /* Have to close the existing file first. */
248 if (internal_close(self) < 0)
249 return -1;
250 }
251 else
252 self->fd = -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000253 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000254
Ross Lagerwall59142db2011-10-31 20:34:46 +0200255 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|siO:fileio",
256 kwlist, &nameobj, &mode, &closefd,
257 &opener))
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000258 return -1;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000259
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000260 if (PyFloat_Check(nameobj)) {
261 PyErr_SetString(PyExc_TypeError,
262 "integer argument expected, got float");
263 return -1;
264 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000265
Serhiy Storchaka78980432013-01-15 01:12:17 +0200266 fd = _PyLong_AsInt(nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000267 if (fd < 0) {
268 if (!PyErr_Occurred()) {
269 PyErr_SetString(PyExc_ValueError,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +0300270 "negative file descriptor");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000271 return -1;
272 }
273 PyErr_Clear();
274 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000275
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000276#ifdef MS_WINDOWS
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200277 if (PyUnicode_Check(nameobj)) {
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300278 Py_ssize_t length;
279 widename = PyUnicode_AsUnicodeAndSize(nameobj, &length);
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200280 if (widename == NULL)
281 return -1;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300282 if (wcslen(widename) != length) {
283 PyErr_SetString(PyExc_TypeError, "embedded NUL character");
284 return -1;
285 }
Victor Stinnerfe9a8612011-09-29 23:19:04 +0200286 } else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000287#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000288 if (fd < 0)
289 {
Antoine Pitrou13348842012-01-29 18:36:34 +0100290 if (!PyUnicode_FSConverter(nameobj, &stringobj)) {
291 return -1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000292 }
Antoine Pitrou13348842012-01-29 18:36:34 +0100293 name = PyBytes_AS_STRING(stringobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000294 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000295
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000296 s = mode;
297 while (*s) {
298 switch (*s++) {
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100299 case 'x':
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000300 if (rwa) {
301 bad_mode:
302 PyErr_SetString(PyExc_ValueError,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100303 "Must have exactly one of create/read/write/append "
Georg Brandl28928ae2010-10-21 13:45:52 +0000304 "mode and at most one plus");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000305 goto error;
306 }
307 rwa = 1;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100308 self->created = 1;
309 self->writable = 1;
310 flags |= O_EXCL | O_CREAT;
311 break;
312 case 'r':
313 if (rwa)
314 goto bad_mode;
315 rwa = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000316 self->readable = 1;
317 break;
318 case 'w':
319 if (rwa)
320 goto bad_mode;
321 rwa = 1;
322 self->writable = 1;
323 flags |= O_CREAT | O_TRUNC;
324 break;
325 case 'a':
326 if (rwa)
327 goto bad_mode;
328 rwa = 1;
329 self->writable = 1;
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200330 self->appending = 1;
331 flags |= O_APPEND | O_CREAT;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000332 break;
333 case 'b':
334 break;
335 case '+':
336 if (plus)
337 goto bad_mode;
338 self->readable = self->writable = 1;
339 plus = 1;
340 break;
341 default:
342 PyErr_Format(PyExc_ValueError,
343 "invalid mode: %.200s", mode);
344 goto error;
345 }
346 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000347
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000348 if (!rwa)
349 goto bad_mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000350
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000351 if (self->readable && self->writable)
352 flags |= O_RDWR;
353 else if (self->readable)
354 flags |= O_RDONLY;
355 else
356 flags |= O_WRONLY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000357
358#ifdef O_BINARY
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000359 flags |= O_BINARY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000360#endif
361
Victor Stinnerdaf45552013-08-28 00:53:59 +0200362#ifdef MS_WINDOWS
363 flags |= O_NOINHERIT;
364#elif defined(O_CLOEXEC)
365 flags |= O_CLOEXEC;
366#endif
Walter Dörwald0e411482007-06-06 16:55:38 +0000367
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000368 if (fd >= 0) {
369 if (check_fd(fd))
370 goto error;
371 self->fd = fd;
372 self->closefd = closefd;
373 }
374 else {
375 self->closefd = 1;
376 if (!closefd) {
377 PyErr_SetString(PyExc_ValueError,
378 "Cannot use closefd=False with file name");
379 goto error;
380 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000381
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000382 errno = 0;
Ross Lagerwall59142db2011-10-31 20:34:46 +0200383 if (opener == Py_None) {
384 Py_BEGIN_ALLOW_THREADS
Thomas Helleraf2be262007-07-12 11:03:13 +0000385#ifdef MS_WINDOWS
Ross Lagerwall59142db2011-10-31 20:34:46 +0200386 if (widename != NULL)
387 self->fd = _wopen(widename, flags, 0666);
388 else
Thomas Helleraf2be262007-07-12 11:03:13 +0000389#endif
Ross Lagerwall59142db2011-10-31 20:34:46 +0200390 self->fd = open(name, flags, 0666);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200391
Ross Lagerwall59142db2011-10-31 20:34:46 +0200392 Py_END_ALLOW_THREADS
Victor Stinnerdaf45552013-08-28 00:53:59 +0200393 }
394 else {
395 PyObject *fdobj;
396
397#ifndef MS_WINDOWS
398 /* the opener may clear the atomic flag */
399 atomic_flag_works = NULL;
400#endif
401
402 fdobj = PyObject_CallFunction(opener, "Oi", nameobj, flags);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200403 if (fdobj == NULL)
404 goto error;
405 if (!PyLong_Check(fdobj)) {
406 Py_DECREF(fdobj);
407 PyErr_SetString(PyExc_TypeError,
408 "expected integer from opener");
409 goto error;
410 }
411
Serhiy Storchaka78980432013-01-15 01:12:17 +0200412 self->fd = _PyLong_AsInt(fdobj);
Ross Lagerwall59142db2011-10-31 20:34:46 +0200413 Py_DECREF(fdobj);
414 if (self->fd == -1) {
415 goto error;
416 }
417 }
418
Hynek Schlawack7f59fd72012-06-22 09:32:22 +0200419 fd_is_own = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000420 if (self->fd < 0) {
Victor Stinner292c8352012-10-30 02:17:38 +0100421 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000422 goto error;
423 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200424
425#ifndef MS_WINDOWS
426 if (_Py_set_inheritable(self->fd, 0, atomic_flag_works) < 0)
427 goto error;
428#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000429 }
Antoine Pitrou9235b252012-07-06 18:48:24 +0200430 if (dircheck(self, nameobj) < 0)
431 goto error;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000432
Victor Stinner89e34362011-01-07 18:47:22 +0000433#if defined(MS_WINDOWS) || defined(__CYGWIN__)
434 /* don't translate newlines (\r\n <=> \n) */
435 _setmode(self->fd, O_BINARY);
436#endif
437
Victor Stinnerd9d04192013-11-06 23:50:10 +0100438 if (_PyObject_SetAttrId((PyObject *)self, &PyId_name, nameobj) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000439 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000440
Antoine Pitroue93b63b2013-09-04 20:46:33 +0200441 if (self->appending) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000442 /* For consistent behaviour, we explicitly seek to the
443 end of file (otherwise, it might be done only on the
444 first write()). */
445 PyObject *pos = portable_lseek(self->fd, NULL, 2);
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200446 if (pos == NULL)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000447 goto error;
448 Py_DECREF(pos);
449 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000450
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000451 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000452
453 error:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000454 ret = -1;
Hynek Schlawack9ed8b4e2012-06-21 20:20:25 +0200455 if (!fd_is_own)
456 self->fd = -1;
Benjamin Petersonbbb04122010-10-30 23:16:28 +0000457 if (self->fd >= 0)
458 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000459
Guido van Rossuma9e20242007-03-08 00:43:48 +0000460 done:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000461 Py_CLEAR(stringobj);
462 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000463}
464
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000465static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000466fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000467{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000468 Py_VISIT(self->dict);
469 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000470}
471
472static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000473fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000474{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000475 Py_CLEAR(self->dict);
476 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000477}
478
Guido van Rossuma9e20242007-03-08 00:43:48 +0000479static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000480fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000481{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200482 self->finalizing = 1;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000483 if (_PyIOBase_finalize((PyObject *) self) < 0)
484 return;
485 _PyObject_GC_UNTRACK(self);
486 if (self->weakreflist != NULL)
487 PyObject_ClearWeakRefs((PyObject *) self);
488 Py_CLEAR(self->dict);
489 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000490}
491
492static PyObject *
493err_closed(void)
494{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000495 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
496 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000497}
498
499static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000500err_mode(char *action)
501{
Antoine Pitrou712cb732013-12-21 15:51:54 +0100502 _PyIO_State *state = IO_STATE();
503 if (state != NULL)
504 PyErr_Format(state->unsupported_operation,
505 "File not open for %s", action);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000506 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000507}
508
509static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000510fileio_fileno(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000511{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000512 if (self->fd < 0)
513 return err_closed();
514 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000515}
516
517static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000518fileio_readable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000519{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000520 if (self->fd < 0)
521 return err_closed();
522 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000523}
524
525static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000526fileio_writable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000527{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000528 if (self->fd < 0)
529 return err_closed();
530 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000531}
532
533static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000534fileio_seekable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000535{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000536 if (self->fd < 0)
537 return err_closed();
538 if (self->seekable < 0) {
539 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
540 if (pos == NULL) {
541 PyErr_Clear();
542 self->seekable = 0;
543 } else {
544 Py_DECREF(pos);
545 self->seekable = 1;
546 }
547 }
548 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000549}
550
551static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000552fileio_readinto(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000553{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000554 Py_buffer pbuf;
Victor Stinnere6edec22011-01-04 00:29:35 +0000555 Py_ssize_t n, len;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100556 int err;
Guido van Rossum53807da2007-04-10 19:01:47 +0000557
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000558 if (self->fd < 0)
559 return err_closed();
560 if (!self->readable)
561 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000562
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000563 if (!PyArg_ParseTuple(args, "w*", &pbuf))
564 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000565
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000566 if (_PyVerify_fd(self->fd)) {
Victor Stinnere6edec22011-01-04 00:29:35 +0000567 len = pbuf.len;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000568 Py_BEGIN_ALLOW_THREADS
569 errno = 0;
Victor Stinner14b9b112013-06-25 00:37:25 +0200570#ifdef MS_WINDOWS
Victor Stinnere6edec22011-01-04 00:29:35 +0000571 if (len > INT_MAX)
572 len = INT_MAX;
573 n = read(self->fd, pbuf.buf, (int)len);
574#else
Victor Stinner72344792011-01-11 00:04:12 +0000575 n = read(self->fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +0000576#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000577 Py_END_ALLOW_THREADS
578 } else
579 n = -1;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100580 err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000581 PyBuffer_Release(&pbuf);
582 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100583 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000584 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100585 errno = err;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000586 PyErr_SetFromErrno(PyExc_IOError);
587 return NULL;
588 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000589
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000590 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000591}
592
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100593#ifndef HAVE_FSTAT
594
595static PyObject *
596fileio_readall(fileio *self)
597{
598 _Py_IDENTIFIER(readall);
599 return _PyObject_CallMethodId((PyObject*)&PyRawIOBase_Type,
600 &PyId_readall, "O", self);
601}
602
603#else
604
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000605static size_t
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100606new_buffersize(fileio *self, size_t currentsize)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000607{
Antoine Pitroua3f44572012-04-17 13:50:58 +0200608 size_t addend;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100609
Nadeem Vawdad41a98b2011-10-13 13:34:16 +0200610 /* Expand the buffer by an amount proportional to the current size,
Antoine Pitroua3f44572012-04-17 13:50:58 +0200611 giving us amortized linear-time behavior. For bigger sizes, use a
612 less-than-double growth factor to avoid excessive allocation. */
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100613 assert(currentsize <= PY_SSIZE_T_MAX);
Antoine Pitroua3f44572012-04-17 13:50:58 +0200614 if (currentsize > 65536)
615 addend = currentsize >> 3;
616 else
617 addend = 256 + currentsize;
618 if (addend < SMALLCHUNK)
619 /* Avoid tiny read() calls. */
620 addend = SMALLCHUNK;
621 return addend + currentsize;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000622}
623
Guido van Rossum7165cb12007-07-10 06:54:34 +0000624static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000625fileio_readall(fileio *self)
Guido van Rossum7165cb12007-07-10 06:54:34 +0000626{
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200627 struct stat st;
Victor Stinnera2a64772011-10-11 22:45:02 +0200628 Py_off_t pos, end;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000629 PyObject *result;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100630 Py_ssize_t bytes_read = 0;
Victor Stinnerc44057d2013-01-03 03:33:21 +0100631 Py_ssize_t n;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100632 size_t bufsize;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000633
Victor Stinnerb79f28c2011-05-25 22:09:03 +0200634 if (self->fd < 0)
635 return err_closed();
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000636 if (!_PyVerify_fd(self->fd))
637 return PyErr_SetFromErrno(PyExc_IOError);
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000638
Victor Stinner14b9b112013-06-25 00:37:25 +0200639#ifdef MS_WINDOWS
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200640 pos = _lseeki64(self->fd, 0L, SEEK_CUR);
641#else
642 pos = lseek(self->fd, 0L, SEEK_CUR);
643#endif
644 if (fstat(self->fd, &st) == 0)
645 end = st.st_size;
646 else
Victor Stinnera2a64772011-10-11 22:45:02 +0200647 end = (Py_off_t)-1;
Christian Heimesa872de52008-12-05 08:26:55 +0000648
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100649 if (end > 0 && end >= pos && pos >= 0 && end - pos < PY_SSIZE_T_MAX) {
650 /* This is probably a real file, so we try to allocate a
651 buffer one byte larger than the rest of the file. If the
652 calculation is right then we should get EOF without having
653 to enlarge the buffer. */
654 bufsize = (size_t)(end - pos + 1);
655 } else {
656 bufsize = SMALLCHUNK;
657 }
658
659 result = PyBytes_FromStringAndSize(NULL, bufsize);
660 if (result == NULL)
661 return NULL;
662
663 while (1) {
664 if (bytes_read >= (Py_ssize_t)bufsize) {
665 bufsize = new_buffersize(self, bytes_read);
666 if (bufsize > PY_SSIZE_T_MAX || bufsize <= 0) {
667 PyErr_SetString(PyExc_OverflowError,
668 "unbounded read returned more bytes "
Serhiy Storchakab817b772015-04-10 02:18:44 +0300669 "than a Python bytes object can hold");
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100670 Py_DECREF(result);
671 return NULL;
672 }
673
674 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
675 if (_PyBytes_Resize(&result, bufsize) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000676 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000677 }
678 }
679 Py_BEGIN_ALLOW_THREADS
680 errno = 0;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100681 n = bufsize - bytes_read;
Victor Stinner14b9b112013-06-25 00:37:25 +0200682#ifdef MS_WINDOWS
Victor Stinnerc44057d2013-01-03 03:33:21 +0100683 if (n > INT_MAX)
684 n = INT_MAX;
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100685 n = read(self->fd, PyBytes_AS_STRING(result) + bytes_read, (int)n);
Victor Stinnerc44057d2013-01-03 03:33:21 +0100686#else
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100687 n = read(self->fd, PyBytes_AS_STRING(result) + bytes_read, n);
Victor Stinnerc44057d2013-01-03 03:33:21 +0100688#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000689 Py_END_ALLOW_THREADS
690 if (n == 0)
691 break;
692 if (n < 0) {
Gregory P. Smith51359922012-06-23 23:55:39 -0700693 if (errno == EINTR) {
694 if (PyErr_CheckSignals()) {
695 Py_DECREF(result);
696 return NULL;
697 }
698 continue;
699 }
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000700 if (errno == EAGAIN) {
Victor Stinnere10920f2014-07-02 22:59:31 +0200701 if (bytes_read > 0)
702 break;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000703 Py_DECREF(result);
704 Py_RETURN_NONE;
705 }
706 Py_DECREF(result);
707 PyErr_SetFromErrno(PyExc_IOError);
708 return NULL;
709 }
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100710 bytes_read += n;
Victor Stinnere9d44ccb2011-05-26 00:16:44 +0200711 pos += n;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000712 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000713
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100714 if (PyBytes_GET_SIZE(result) > bytes_read) {
715 if (_PyBytes_Resize(&result, bytes_read) < 0)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000716 return NULL;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000717 }
718 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000719}
720
Richard Oudkerkaf7260e2013-05-17 23:34:42 +0100721#endif /* HAVE_FSTAT */
722
Guido van Rossuma9e20242007-03-08 00:43:48 +0000723static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000724fileio_read(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000725{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000726 char *ptr;
727 Py_ssize_t n;
728 Py_ssize_t size = -1;
729 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000730
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000731 if (self->fd < 0)
732 return err_closed();
733 if (!self->readable)
734 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000735
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000736 if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size))
737 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000738
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000739 if (size < 0) {
740 return fileio_readall(self);
741 }
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000742
Victor Stinner14b9b112013-06-25 00:37:25 +0200743#ifdef MS_WINDOWS
Victor Stinnerc655a722011-07-05 11:31:49 +0200744 if (size > INT_MAX)
745 size = INT_MAX;
746#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000747 bytes = PyBytes_FromStringAndSize(NULL, size);
748 if (bytes == NULL)
749 return NULL;
750 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000751
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000752 if (_PyVerify_fd(self->fd)) {
753 Py_BEGIN_ALLOW_THREADS
754 errno = 0;
Victor Stinner14b9b112013-06-25 00:37:25 +0200755#ifdef MS_WINDOWS
Victor Stinnerc655a722011-07-05 11:31:49 +0200756 n = read(self->fd, ptr, (int)size);
757#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000758 n = read(self->fd, ptr, size);
Victor Stinnerc655a722011-07-05 11:31:49 +0200759#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000760 Py_END_ALLOW_THREADS
761 } else
762 n = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000763
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000764 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100765 int err = errno;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000766 Py_DECREF(bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100767 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000768 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100769 errno = err;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000770 PyErr_SetFromErrno(PyExc_IOError);
771 return NULL;
772 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000773
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000774 if (n != size) {
775 if (_PyBytes_Resize(&bytes, n) < 0) {
Victor Stinner85c761d2013-07-16 21:36:02 +0200776 Py_CLEAR(bytes);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000777 return NULL;
778 }
779 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000780
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000781 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000782}
783
784static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000785fileio_write(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000786{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000787 Py_buffer pbuf;
Victor Stinnere6edec22011-01-04 00:29:35 +0000788 Py_ssize_t n, len;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100789 int err;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000790
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000791 if (self->fd < 0)
792 return err_closed();
793 if (!self->writable)
794 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000795
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000796 if (!PyArg_ParseTuple(args, "y*", &pbuf))
797 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000798
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000799 if (_PyVerify_fd(self->fd)) {
800 Py_BEGIN_ALLOW_THREADS
801 errno = 0;
Victor Stinnere6edec22011-01-04 00:29:35 +0000802 len = pbuf.len;
Victor Stinner14b9b112013-06-25 00:37:25 +0200803#ifdef MS_WINDOWS
Victor Stinnere0daff12011-03-20 23:36:35 +0100804 if (len > 32767 && isatty(self->fd)) {
805 /* Issue #11395: the Windows console returns an error (12: not
806 enough space error) on writing into stdout if stdout mode is
807 binary and the length is greater than 66,000 bytes (or less,
808 depending on heap usage). */
809 len = 32767;
810 }
811 else if (len > INT_MAX)
Victor Stinnere6edec22011-01-04 00:29:35 +0000812 len = INT_MAX;
813 n = write(self->fd, pbuf.buf, (int)len);
814#else
Victor Stinner72344792011-01-11 00:04:12 +0000815 n = write(self->fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +0000816#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000817 Py_END_ALLOW_THREADS
818 } else
819 n = -1;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100820 err = errno;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000821
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000822 PyBuffer_Release(&pbuf);
Martin v. Löwis423be952008-08-13 15:53:07 +0000823
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000824 if (n < 0) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100825 if (err == EAGAIN)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000826 Py_RETURN_NONE;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100827 errno = err;
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000828 PyErr_SetFromErrno(PyExc_IOError);
829 return NULL;
830 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000831
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000832 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000833}
834
Guido van Rossum53807da2007-04-10 19:01:47 +0000835/* XXX Windows support below is likely incomplete */
836
Guido van Rossum53807da2007-04-10 19:01:47 +0000837/* Cribbed from posix_lseek() */
838static PyObject *
839portable_lseek(int fd, PyObject *posobj, int whence)
840{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000841 Py_off_t pos, res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000842
843#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000844 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
845 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000846#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000847 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000848#endif
849#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000850 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000851#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000852#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000853 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000854#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000855 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000856#endif /* SEEK_SET */
857
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000858 if (posobj == NULL)
859 pos = 0;
860 else {
861 if(PyFloat_Check(posobj)) {
862 PyErr_SetString(PyExc_TypeError, "an integer is required");
863 return NULL;
864 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000865#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000866 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000867#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000868 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000869#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000870 if (PyErr_Occurred())
871 return NULL;
872 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000873
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000874 if (_PyVerify_fd(fd)) {
875 Py_BEGIN_ALLOW_THREADS
Victor Stinner14b9b112013-06-25 00:37:25 +0200876#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000877 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000878#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000879 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000880#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000881 Py_END_ALLOW_THREADS
882 } else
883 res = -1;
884 if (res < 0)
885 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000886
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000887#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000888 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000889#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000890 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000891#endif
892}
893
Guido van Rossuma9e20242007-03-08 00:43:48 +0000894static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000895fileio_seek(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000896{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000897 PyObject *posobj;
898 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000899
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000900 if (self->fd < 0)
901 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000902
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000903 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
904 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000905
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000906 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000907}
908
909static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000910fileio_tell(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000911{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000912 if (self->fd < 0)
913 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000914
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000915 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000916}
917
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000918#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000919static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000920fileio_truncate(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000921{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000922 PyObject *posobj = NULL; /* the new size wanted by the user */
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000923#ifndef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000924 Py_off_t pos;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000925#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000926 int ret;
927 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000928
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000929 fd = self->fd;
930 if (fd < 0)
931 return err_closed();
932 if (!self->writable)
933 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000934
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000935 if (!PyArg_ParseTuple(args, "|O", &posobj))
936 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000937
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000938 if (posobj == Py_None || posobj == NULL) {
939 /* Get the current position. */
940 posobj = portable_lseek(fd, NULL, 1);
941 if (posobj == NULL)
942 return NULL;
943 }
944 else {
945 Py_INCREF(posobj);
946 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000947
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000948#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000949 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
950 so don't even try using it. */
951 {
952 PyObject *oldposobj, *tempposobj;
953 HANDLE hFile;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000954
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000955 /* we save the file pointer position */
956 oldposobj = portable_lseek(fd, NULL, 1);
957 if (oldposobj == NULL) {
958 Py_DECREF(posobj);
959 return NULL;
960 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000961
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000962 /* we then move to the truncation position */
963 tempposobj = portable_lseek(fd, posobj, 0);
964 if (tempposobj == NULL) {
965 Py_DECREF(oldposobj);
966 Py_DECREF(posobj);
967 return NULL;
968 }
969 Py_DECREF(tempposobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000970
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000971 /* Truncate. Note that this may grow the file! */
972 Py_BEGIN_ALLOW_THREADS
973 errno = 0;
974 hFile = (HANDLE)_get_osfhandle(fd);
975 ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
976 if (ret == 0) {
977 ret = SetEndOfFile(hFile) == 0;
978 if (ret)
979 errno = EACCES;
980 }
981 Py_END_ALLOW_THREADS
982
983 /* we restore the file pointer position in any case */
984 tempposobj = portable_lseek(fd, oldposobj, 0);
985 Py_DECREF(oldposobj);
986 if (tempposobj == NULL) {
987 Py_DECREF(posobj);
988 return NULL;
989 }
990 Py_DECREF(tempposobj);
991 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000992#else
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000993
994#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000995 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000996#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000997 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000998#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000999 if (PyErr_Occurred()){
1000 Py_DECREF(posobj);
1001 return NULL;
1002 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001003
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001004 Py_BEGIN_ALLOW_THREADS
1005 errno = 0;
1006 ret = ftruncate(fd, pos);
1007 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001008
Thomas Hellerfdeee3a2007-07-12 11:21:36 +00001009#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001010
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001011 if (ret != 0) {
1012 Py_DECREF(posobj);
1013 PyErr_SetFromErrno(PyExc_IOError);
1014 return NULL;
1015 }
Guido van Rossuma9e20242007-03-08 00:43:48 +00001016
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001017 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001018}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001019#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +00001020
1021static char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001022mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +00001023{
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001024 if (self->created) {
1025 if (self->readable)
1026 return "xb+";
1027 else
1028 return "xb";
1029 }
Antoine Pitroue93b63b2013-09-04 20:46:33 +02001030 if (self->appending) {
1031 if (self->readable)
1032 return "ab+";
1033 else
1034 return "ab";
1035 }
1036 else if (self->readable) {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001037 if (self->writable)
1038 return "rb+";
1039 else
1040 return "rb";
1041 }
1042 else
1043 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +00001044}
Guido van Rossuma9e20242007-03-08 00:43:48 +00001045
1046static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001047fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001048{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001049 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001050
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001051 if (self->fd < 0)
1052 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +00001053
Martin v. Löwis767046a2011-10-14 15:35:36 +02001054 nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001055 if (nameobj == NULL) {
1056 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1057 PyErr_Clear();
1058 else
1059 return NULL;
1060 res = PyUnicode_FromFormat("<_io.FileIO fd=%d mode='%s'>",
1061 self->fd, mode_string(self));
1062 }
1063 else {
1064 res = PyUnicode_FromFormat("<_io.FileIO name=%R mode='%s'>",
1065 nameobj, mode_string(self));
1066 Py_DECREF(nameobj);
1067 }
1068 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +00001069}
1070
1071static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001072fileio_isatty(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +00001073{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001074 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +00001075
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001076 if (self->fd < 0)
1077 return err_closed();
1078 Py_BEGIN_ALLOW_THREADS
1079 res = isatty(self->fd);
1080 Py_END_ALLOW_THREADS
1081 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001082}
1083
Antoine Pitrou243757e2010-11-05 21:15:39 +00001084static PyObject *
1085fileio_getstate(fileio *self)
1086{
1087 PyErr_Format(PyExc_TypeError,
1088 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
1089 return NULL;
1090}
1091
Guido van Rossuma9e20242007-03-08 00:43:48 +00001092
1093PyDoc_STRVAR(fileio_doc,
Ross Lagerwall59142db2011-10-31 20:34:46 +02001094"file(name: str[, mode: str][, opener: None]) -> file IO object\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001095"\n"
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001096"Open a file. The mode can be 'r' (default), 'w', 'x' or 'a' for reading,\n"
Charles-François Natalid612de12012-01-14 11:51:00 +01001097"writing, exclusive creation or appending. The file will be created if it\n"
1098"doesn't exist when opened for writing or appending; it will be truncated\n"
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001099"when opened for writing. A FileExistsError will be raised if it already\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001100"exists when opened for creating. Opening a file for creating implies\n"
1101"writing so this mode behaves in a similar way to 'w'.Add a '+' to the mode\n"
1102"to allow simultaneous reading and writing. A custom opener can be used by\n"
1103"passing a callable as *opener*. The underlying file descriptor for the file\n"
Ross Lagerwall59142db2011-10-31 20:34:46 +02001104"object is then obtained by calling opener with (*name*, *flags*).\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +01001105"*opener* must return an open file descriptor (passing os.open as *opener*\n"
1106"results in functionality similar to passing None).");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001107
1108PyDoc_STRVAR(read_doc,
1109"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
1110"\n"
1111"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +00001112"In non-blocking mode, returns None if no data is available.\n"
Serhiy Storchakab817b772015-04-10 02:18:44 +03001113"Return an empty bytes object at EOF.");
Guido van Rossum7165cb12007-07-10 06:54:34 +00001114
1115PyDoc_STRVAR(readall_doc,
1116"readall() -> bytes. read all data from the file, returned as bytes.\n"
1117"\n"
1118"In non-blocking mode, returns as much as is immediately available,\n"
Serhiy Storchakab817b772015-04-10 02:18:44 +03001119"or None if no data is available. Return an empty bytes object at EOF.");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001120
1121PyDoc_STRVAR(write_doc,
1122"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
1123"\n"
1124"Only makes one system call, so not all of the data may be written.\n"
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001125"The number of bytes actually written is returned. In non-blocking mode,\n"
1126"returns None if the write would block."
1127);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001128
1129PyDoc_STRVAR(fileno_doc,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001130"fileno() -> int. Return the underlying file descriptor (an integer).");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001131
1132PyDoc_STRVAR(seek_doc,
Berker Peksagb87630c2014-09-24 12:43:29 +03001133"seek(offset: int[, whence: int]) -> int. Move to new file position and\n"
1134"return the file position.\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001135"\n"
1136"Argument offset is a byte count. Optional argument whence defaults to\n"
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001137"SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values\n"
1138"are SEEK_CUR or 1 (move relative to current position, positive or negative),\n"
1139"and SEEK_END or 2 (move relative to end of file, usually negative, although\n"
1140"many platforms allow seeking beyond the end of a file).\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001141"\n"
1142"Note that not all file objects are seekable.");
1143
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001144#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +00001145PyDoc_STRVAR(truncate_doc,
Berker Peksagb87630c2014-09-24 12:43:29 +03001146"truncate([size: int]) -> int. Truncate the file to at most size bytes\n"
1147"and return the truncated size.\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001148"\n"
Berker Peksagb87630c2014-09-24 12:43:29 +03001149"Size defaults to the current file position, as returned by tell().\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +00001150"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001151#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +00001152
1153PyDoc_STRVAR(tell_doc,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001154"tell() -> int. Current file position.\n"
1155"\n"
1156"Can raise OSError for non seekable files."
1157);
Guido van Rossuma9e20242007-03-08 00:43:48 +00001158
1159PyDoc_STRVAR(readinto_doc,
Benjamin Peterson9a8082f2009-03-05 00:55:56 +00001160"readinto() -> Same as RawIOBase.readinto().");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001161
1162PyDoc_STRVAR(close_doc,
1163"close() -> None. Close the file.\n"
1164"\n"
1165"A closed file cannot be used for further I/O operations. close() may be\n"
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001166"called more than once without error.");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001167
1168PyDoc_STRVAR(isatty_doc,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001169"isatty() -> bool. True if the file is connected to a TTY device.");
Guido van Rossuma9e20242007-03-08 00:43:48 +00001170
Guido van Rossuma9e20242007-03-08 00:43:48 +00001171PyDoc_STRVAR(seekable_doc,
1172"seekable() -> bool. True if file supports random-access.");
1173
1174PyDoc_STRVAR(readable_doc,
1175"readable() -> bool. True if file was opened in a read mode.");
1176
1177PyDoc_STRVAR(writable_doc,
1178"writable() -> bool. True if file was opened in a write mode.");
1179
1180static PyMethodDef fileio_methods[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001181 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
1182 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
1183 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
1184 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
1185 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
1186 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001187#ifdef HAVE_FTRUNCATE
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001188 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001189#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001190 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
1191 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
1192 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
1193 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
1194 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
1195 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Antoine Pitroue033e062010-10-29 10:38:18 +00001196 {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
Antoine Pitrou243757e2010-11-05 21:15:39 +00001197 {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001198 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001199};
1200
Guido van Rossum53807da2007-04-10 19:01:47 +00001201/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1202
Guido van Rossumb0428152007-04-08 17:44:42 +00001203static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001204get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001205{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001206 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001207}
1208
1209static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001210get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001211{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001212 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001213}
1214
1215static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001216get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001217{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001218 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001219}
1220
1221static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001222 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1223 {"closefd", (getter)get_closefd, NULL,
Serhiy Storchaka3d2279f2015-04-10 16:08:43 +03001224 "True if the file descriptor will be closed by close()."},
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001225 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1226 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001227};
1228
Antoine Pitrou796564c2013-07-30 19:59:21 +02001229static PyMemberDef fileio_members[] = {
1230 {"_finalizing", T_BOOL, offsetof(fileio, finalizing), 0},
1231 {NULL}
1232};
1233
Guido van Rossuma9e20242007-03-08 00:43:48 +00001234PyTypeObject PyFileIO_Type = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001235 PyVarObject_HEAD_INIT(NULL, 0)
1236 "_io.FileIO",
1237 sizeof(fileio),
1238 0,
1239 (destructor)fileio_dealloc, /* tp_dealloc */
1240 0, /* tp_print */
1241 0, /* tp_getattr */
1242 0, /* tp_setattr */
1243 0, /* tp_reserved */
1244 (reprfunc)fileio_repr, /* tp_repr */
1245 0, /* tp_as_number */
1246 0, /* tp_as_sequence */
1247 0, /* tp_as_mapping */
1248 0, /* tp_hash */
1249 0, /* tp_call */
1250 0, /* tp_str */
1251 PyObject_GenericGetAttr, /* tp_getattro */
1252 0, /* tp_setattro */
1253 0, /* tp_as_buffer */
1254 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +02001255 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001256 fileio_doc, /* tp_doc */
1257 (traverseproc)fileio_traverse, /* tp_traverse */
1258 (inquiry)fileio_clear, /* tp_clear */
1259 0, /* tp_richcompare */
1260 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1261 0, /* tp_iter */
1262 0, /* tp_iternext */
1263 fileio_methods, /* tp_methods */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001264 fileio_members, /* tp_members */
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001265 fileio_getsetlist, /* tp_getset */
1266 0, /* tp_base */
1267 0, /* tp_dict */
1268 0, /* tp_descr_get */
1269 0, /* tp_descr_set */
1270 offsetof(fileio, dict), /* tp_dictoffset */
1271 fileio_init, /* tp_init */
1272 PyType_GenericAlloc, /* tp_alloc */
1273 fileio_new, /* tp_new */
1274 PyObject_GC_Del, /* tp_free */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001275 0, /* tp_is_gc */
1276 0, /* tp_bases */
1277 0, /* tp_mro */
1278 0, /* tp_cache */
1279 0, /* tp_subclasses */
1280 0, /* tp_weaklist */
1281 0, /* tp_del */
1282 0, /* tp_version_tag */
1283 0, /* tp_finalize */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001284};