blob: 32f679007aae3b998b8185b54e2526510950c3b7 [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"
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8#include <stddef.h> /* For offsetof */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00009#include "_iomodule.h"
Guido van Rossuma9e20242007-03-08 00:43:48 +000010
11/*
12 * Known likely problems:
13 *
14 * - Files larger then 2**32-1
15 * - Files with unicode filenames
16 * - Passing numbers greater than 2**32-1 when an integer is expected
17 * - Making it work on Windows and other oddball platforms
18 *
19 * To Do:
20 *
21 * - autoconfify header file inclusion
Guido van Rossuma9e20242007-03-08 00:43:48 +000022 */
23
24#ifdef MS_WINDOWS
25/* can simulate truncate with Win32 API functions; see file_truncate */
Thomas Hellerfdeee3a2007-07-12 11:21:36 +000026#define HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +000027#define WIN32_LEAN_AND_MEAN
28#include <windows.h>
29#endif
30
Christian Heimesa872de52008-12-05 08:26:55 +000031#if BUFSIZ < (8*1024)
32#define SMALLCHUNK (8*1024)
33#elif (BUFSIZ >= (2 << 25))
34#error "unreasonable BUFSIZ > 64MB defined"
35#else
36#define SMALLCHUNK BUFSIZ
37#endif
38
39#if SIZEOF_INT < 4
40#define BIGCHUNK (512 * 32)
41#else
42#define BIGCHUNK (512 * 1024)
43#endif
44
Guido van Rossuma9e20242007-03-08 00:43:48 +000045typedef struct {
46 PyObject_HEAD
47 int fd;
Neal Norwitz88b44da2007-08-12 17:23:54 +000048 unsigned readable : 1;
49 unsigned writable : 1;
50 int seekable : 2; /* -1 means unknown */
Guido van Rossum2dced8b2007-10-30 17:27:30 +000051 int closefd : 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +000052 PyObject *weakreflist;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000053 PyObject *dict;
Guido van Rossuma9e20242007-03-08 00:43:48 +000054} PyFileIOObject;
55
Collin Winteraf334382007-03-08 21:46:15 +000056PyTypeObject PyFileIO_Type;
57
Guido van Rossuma9e20242007-03-08 00:43:48 +000058#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
59
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000060int
61_PyFileIO_closed(PyObject *self)
62{
63 return ((PyFileIOObject *)self)->fd < 0;
64}
Antoine Pitrou08838b62009-01-21 00:55:13 +000065
Antoine Pitrou7fb111b2009-03-04 11:14:01 +000066static PyObject *
67portable_lseek(int fd, PyObject *posobj, int whence);
68
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000069/* Returns 0 on success, -1 with exception set on failure. */
Neal Norwitz88b44da2007-08-12 17:23:54 +000070static int
71internal_close(PyFileIOObject *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +000072{
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000073 int err = 0;
Benjamin Peterson7aedf112009-01-19 15:19:46 +000074 int save_errno = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +000075 if (self->fd >= 0) {
Guido van Rossumb0428152007-04-08 17:44:42 +000076 int fd = self->fd;
77 self->fd = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +000078 Py_BEGIN_ALLOW_THREADS
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000079 err = close(fd);
80 if (err < 0)
Neal Norwitz88b44da2007-08-12 17:23:54 +000081 save_errno = errno;
Guido van Rossuma9e20242007-03-08 00:43:48 +000082 Py_END_ALLOW_THREADS
Neal Norwitz88b44da2007-08-12 17:23:54 +000083 }
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000084 if (err < 0) {
85 errno = save_errno;
86 PyErr_SetFromErrno(PyExc_IOError);
87 return -1;
88 }
89 return 0;
Neal Norwitz88b44da2007-08-12 17:23:54 +000090}
91
92static PyObject *
93fileio_close(PyFileIOObject *self)
94{
Guido van Rossum2dced8b2007-10-30 17:27:30 +000095 if (!self->closefd) {
Christian Heimesecc42a22008-11-05 19:30:32 +000096 self->fd = -1;
Guido van Rossum2dced8b2007-10-30 17:27:30 +000097 Py_RETURN_NONE;
98 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000099 errno = internal_close(self);
Antoine Pitrou0ae29cf2009-03-13 22:33:17 +0000100 if (errno < 0)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000101 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000102
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000103 return PyObject_CallMethod((PyObject*)&PyRawIOBase_Type,
104 "close", "O", self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000105}
106
107static PyObject *
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000108fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000109{
110 PyFileIOObject *self;
111
112 assert(type != NULL && type->tp_alloc != NULL);
113
114 self = (PyFileIOObject *) type->tp_alloc(type, 0);
115 if (self != NULL) {
116 self->fd = -1;
Christian Heimesdf32b392008-10-30 21:23:35 +0000117 self->readable = 0;
118 self->writable = 0;
119 self->seekable = -1;
120 self->closefd = 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000121 self->weakreflist = NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000122 }
123
124 return (PyObject *) self;
125}
126
127/* On Unix, open will succeed for directories.
128 In Python, there should be no file objects referring to
129 directories, so we need a check. */
130
131static int
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000132dircheck(PyFileIOObject* self, const char *name)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000133{
134#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
135 struct stat buf;
136 if (self->fd < 0)
137 return 0;
138 if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000139 char *msg = strerror(EISDIR);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000140 PyObject *exc;
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +0000141 if (internal_close(self))
142 return -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000143
Benjamin Peterson1efc23c2008-12-29 18:02:28 +0000144 exc = PyObject_CallFunction(PyExc_IOError, "(iss)",
145 EISDIR, msg, name);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000146 PyErr_SetObject(PyExc_IOError, exc);
147 Py_XDECREF(exc);
148 return -1;
149 }
150#endif
151 return 0;
152}
153
Benjamin Peterson806d4022009-01-19 15:11:51 +0000154static int
155check_fd(int fd)
156{
157#if defined(HAVE_FSTAT)
158 struct stat buf;
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000159 if (!_PyVerify_fd(fd) || (fstat(fd, &buf) < 0 && errno == EBADF)) {
Benjamin Peterson806d4022009-01-19 15:11:51 +0000160 PyObject *exc;
161 char *msg = strerror(EBADF);
162 exc = PyObject_CallFunction(PyExc_OSError, "(is)",
163 EBADF, msg);
164 PyErr_SetObject(PyExc_OSError, exc);
165 Py_XDECREF(exc);
166 return -1;
167 }
168#endif
169 return 0;
170}
171
Guido van Rossuma9e20242007-03-08 00:43:48 +0000172
173static int
174fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
175{
176 PyFileIOObject *self = (PyFileIOObject *) oself;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000177 static char *kwlist[] = {"file", "mode", "closefd", NULL};
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000178 const char *name = NULL;
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000179 PyObject *nameobj, *stringobj = NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000180 char *mode = "r";
Guido van Rossum53807da2007-04-10 19:01:47 +0000181 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000182#ifdef MS_WINDOWS
183 Py_UNICODE *widename = NULL;
184#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000185 int ret = 0;
186 int rwa = 0, plus = 0, append = 0;
187 int flags = 0;
Guido van Rossumb0428152007-04-08 17:44:42 +0000188 int fd = -1;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000189 int closefd = 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000190
191 assert(PyFileIO_Check(oself));
Neal Norwitz88b44da2007-08-12 17:23:54 +0000192 if (self->fd >= 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000193 /* Have to close the existing file first. */
Neal Norwitz88b44da2007-08-12 17:23:54 +0000194 if (internal_close(self) < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000195 return -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000196 }
197
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000198 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:fileio",
199 kwlist, &nameobj, &mode, &closefd))
200 return -1;
201
202 if (PyFloat_Check(nameobj)) {
203 PyErr_SetString(PyExc_TypeError,
204 "integer argument expected, got float");
205 return -1;
206 }
207
208 fd = PyLong_AsLong(nameobj);
209 if (fd < 0) {
210 if (!PyErr_Occurred()) {
Guido van Rossumb0428152007-04-08 17:44:42 +0000211 PyErr_SetString(PyExc_ValueError,
212 "Negative filedescriptor");
213 return -1;
214 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000215 PyErr_Clear();
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000216 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000217
Guido van Rossuma9e20242007-03-08 00:43:48 +0000218#ifdef Py_WIN_WIDE_FILENAMES
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000219 if (GetVersion() < 0x80000000) {
Guido van Rossumb0428152007-04-08 17:44:42 +0000220 /* On NT, so wide API available */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000221 if (PyUnicode_Check(nameobj))
222 widename = PyUnicode_AS_UNICODE(nameobj);
223 }
224 if (widename == NULL)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000225#endif
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000226 if (fd < 0)
227 {
228 if (PyBytes_Check(nameobj) || PyByteArray_Check(nameobj)) {
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000229 Py_ssize_t namelen;
230 if (PyObject_AsCharBuffer(nameobj, &name, &namelen) < 0)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000231 return -1;
232 }
233 else {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000234 PyObject *u = PyUnicode_FromObject(nameobj);
235
236 if (u == NULL)
237 return -1;
238
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000239 stringobj = PyUnicode_AsEncodedString(
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000240 u, Py_FileSystemDefaultEncoding, NULL);
241 Py_DECREF(u);
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000242 if (stringobj == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000243 return -1;
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000244 if (!PyBytes_Check(stringobj)) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000245 PyErr_SetString(PyExc_TypeError,
246 "encoder failed to return bytes");
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000247 goto error;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000248 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000249 name = PyBytes_AS_STRING(stringobj);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000250 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000251 }
252
Guido van Rossum53807da2007-04-10 19:01:47 +0000253 s = mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000254 while (*s) {
255 switch (*s++) {
256 case 'r':
257 if (rwa) {
258 bad_mode:
259 PyErr_SetString(PyExc_ValueError,
260 "Must have exactly one of read/write/append mode");
261 goto error;
262 }
263 rwa = 1;
264 self->readable = 1;
265 break;
266 case 'w':
267 if (rwa)
268 goto bad_mode;
269 rwa = 1;
270 self->writable = 1;
271 flags |= O_CREAT | O_TRUNC;
272 break;
273 case 'a':
274 if (rwa)
275 goto bad_mode;
276 rwa = 1;
277 self->writable = 1;
278 flags |= O_CREAT;
279 append = 1;
280 break;
Benjamin Peterson44309e62008-11-22 00:41:45 +0000281 case 'b':
282 break;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000283 case '+':
284 if (plus)
285 goto bad_mode;
286 self->readable = self->writable = 1;
287 plus = 1;
288 break;
289 default:
290 PyErr_Format(PyExc_ValueError,
291 "invalid mode: %.200s", mode);
292 goto error;
293 }
294 }
295
296 if (!rwa)
297 goto bad_mode;
298
299 if (self->readable && self->writable)
300 flags |= O_RDWR;
301 else if (self->readable)
302 flags |= O_RDONLY;
303 else
304 flags |= O_WRONLY;
305
306#ifdef O_BINARY
307 flags |= O_BINARY;
308#endif
309
Walter Dörwald0e411482007-06-06 16:55:38 +0000310#ifdef O_APPEND
311 if (append)
312 flags |= O_APPEND;
313#endif
314
Guido van Rossumb0428152007-04-08 17:44:42 +0000315 if (fd >= 0) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000316 if (check_fd(fd))
317 goto error;
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000318 self->fd = fd;
319 self->closefd = closefd;
Guido van Rossumb0428152007-04-08 17:44:42 +0000320 }
321 else {
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000322 self->closefd = 1;
323 if (!closefd) {
324 PyErr_SetString(PyExc_ValueError,
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000325 "Cannot use closefd=False with file name");
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000326 goto error;
327 }
328
Guido van Rossumb0428152007-04-08 17:44:42 +0000329 Py_BEGIN_ALLOW_THREADS
330 errno = 0;
Thomas Helleraf2be262007-07-12 11:03:13 +0000331#ifdef MS_WINDOWS
332 if (widename != NULL)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000333 self->fd = _wopen(widename, flags, 0666);
Thomas Helleraf2be262007-07-12 11:03:13 +0000334 else
335#endif
Neal Norwitz88b44da2007-08-12 17:23:54 +0000336 self->fd = open(name, flags, 0666);
Guido van Rossumb0428152007-04-08 17:44:42 +0000337 Py_END_ALLOW_THREADS
Benjamin Peterson3e4f0552008-09-02 00:31:15 +0000338 if (self->fd < 0) {
Christian Heimes0b489542007-10-31 19:20:48 +0000339#ifdef MS_WINDOWS
Hirokazu Yamamoto0f22d692009-01-01 16:03:45 +0000340 if (widename != NULL)
341 PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename);
342 else
Christian Heimes0b489542007-10-31 19:20:48 +0000343#endif
Hirokazu Yamamoto0f22d692009-01-01 16:03:45 +0000344 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
Guido van Rossumb0428152007-04-08 17:44:42 +0000345 goto error;
346 }
Benjamin Peterson1efc23c2008-12-29 18:02:28 +0000347 if(dircheck(self, name) < 0)
Benjamin Peterson3e4f0552008-09-02 00:31:15 +0000348 goto error;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000349 }
350
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000351 if (PyObject_SetAttrString((PyObject *)self, "name", nameobj) < 0)
352 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000353
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000354 if (append) {
355 /* For consistent behaviour, we explicitly seek to the
356 end of file (otherwise, it might be done only on the
357 first write()). */
358 PyObject *pos = portable_lseek(self->fd, NULL, 2);
359 if (pos == NULL)
360 goto error;
361 Py_DECREF(pos);
362 }
363
Guido van Rossuma9e20242007-03-08 00:43:48 +0000364 goto done;
365
366 error:
367 ret = -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000368
Guido van Rossuma9e20242007-03-08 00:43:48 +0000369 done:
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000370 Py_CLEAR(stringobj);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000371 return ret;
372}
373
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000374static int
375fileio_traverse(PyFileIOObject *self, visitproc visit, void *arg)
376{
377 Py_VISIT(self->dict);
378 return 0;
379}
380
381static int
382fileio_clear(PyFileIOObject *self)
383{
384 Py_CLEAR(self->dict);
385 return 0;
386}
387
Guido van Rossuma9e20242007-03-08 00:43:48 +0000388static void
389fileio_dealloc(PyFileIOObject *self)
390{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000391 if (_PyIOBase_finalize((PyObject *) self) < 0)
392 return;
393 _PyObject_GC_UNTRACK(self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000394 if (self->weakreflist != NULL)
395 PyObject_ClearWeakRefs((PyObject *) self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000396 Py_CLEAR(self->dict);
Christian Heimes90aa7642007-12-19 02:45:37 +0000397 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000398}
399
400static PyObject *
401err_closed(void)
402{
403 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
404 return NULL;
405}
406
407static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000408err_mode(char *action)
409{
410 PyErr_Format(PyExc_ValueError, "File not open for %s", action);
411 return NULL;
412}
413
414static PyObject *
Guido van Rossuma9e20242007-03-08 00:43:48 +0000415fileio_fileno(PyFileIOObject *self)
416{
417 if (self->fd < 0)
418 return err_closed();
Christian Heimes217cfd12007-12-02 14:31:20 +0000419 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000420}
421
422static PyObject *
423fileio_readable(PyFileIOObject *self)
424{
425 if (self->fd < 0)
426 return err_closed();
Neal Norwitz88b44da2007-08-12 17:23:54 +0000427 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000428}
429
430static PyObject *
431fileio_writable(PyFileIOObject *self)
432{
433 if (self->fd < 0)
434 return err_closed();
Neal Norwitz88b44da2007-08-12 17:23:54 +0000435 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000436}
437
438static PyObject *
439fileio_seekable(PyFileIOObject *self)
440{
441 if (self->fd < 0)
442 return err_closed();
443 if (self->seekable < 0) {
444 int ret;
445 Py_BEGIN_ALLOW_THREADS
446 ret = lseek(self->fd, 0, SEEK_CUR);
447 Py_END_ALLOW_THREADS
448 if (ret < 0)
449 self->seekable = 0;
450 else
451 self->seekable = 1;
452 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000453 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000454}
455
456static PyObject *
457fileio_readinto(PyFileIOObject *self, PyObject *args)
458{
Martin v. Löwis423be952008-08-13 15:53:07 +0000459 Py_buffer pbuf;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000460 Py_ssize_t n;
Guido van Rossum53807da2007-04-10 19:01:47 +0000461
Guido van Rossuma9e20242007-03-08 00:43:48 +0000462 if (self->fd < 0)
463 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000464 if (!self->readable)
465 return err_mode("reading");
466
Martin v. Löwis423be952008-08-13 15:53:07 +0000467 if (!PyArg_ParseTuple(args, "w*", &pbuf))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000468 return NULL;
469
470 Py_BEGIN_ALLOW_THREADS
471 errno = 0;
Martin v. Löwis423be952008-08-13 15:53:07 +0000472 n = read(self->fd, pbuf.buf, pbuf.len);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000473 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +0000474 PyBuffer_Release(&pbuf);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000475 if (n < 0) {
476 if (errno == EAGAIN)
477 Py_RETURN_NONE;
478 PyErr_SetFromErrno(PyExc_IOError);
479 return NULL;
480 }
481
Christian Heimes217cfd12007-12-02 14:31:20 +0000482 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000483}
484
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000485static size_t
486new_buffersize(PyFileIOObject *self, size_t currentsize)
487{
488#ifdef HAVE_FSTAT
489 off_t pos, end;
490 struct stat st;
491 if (fstat(self->fd, &st) == 0) {
492 end = st.st_size;
493 pos = lseek(self->fd, 0L, SEEK_CUR);
494 if (end >= pos && pos >= 0)
495 return currentsize + end - pos + 1;
496 /* Add 1 so if the file were to grow we'd notice. */
497 }
498#endif
499 if (currentsize > SMALLCHUNK) {
500 /* Keep doubling until we reach BIGCHUNK;
501 then keep adding BIGCHUNK. */
502 if (currentsize <= BIGCHUNK)
503 return currentsize + currentsize;
504 else
505 return currentsize + BIGCHUNK;
506 }
507 return currentsize + SMALLCHUNK;
508}
509
Guido van Rossum7165cb12007-07-10 06:54:34 +0000510static PyObject *
511fileio_readall(PyFileIOObject *self)
512{
513 PyObject *result;
514 Py_ssize_t total = 0;
515 int n;
516
Christian Heimesa872de52008-12-05 08:26:55 +0000517 result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK);
Guido van Rossum7165cb12007-07-10 06:54:34 +0000518 if (result == NULL)
519 return NULL;
520
521 while (1) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000522 Py_ssize_t newsize = new_buffersize(self, total);
Christian Heimesa872de52008-12-05 08:26:55 +0000523
Christian Heimes72b710a2008-05-26 13:28:38 +0000524 if (PyBytes_GET_SIZE(result) < newsize) {
525 if (_PyBytes_Resize(&result, newsize) < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000526 if (total == 0) {
527 Py_DECREF(result);
528 return NULL;
529 }
530 PyErr_Clear();
531 break;
532 }
533 }
534 Py_BEGIN_ALLOW_THREADS
535 errno = 0;
536 n = read(self->fd,
Christian Heimes72b710a2008-05-26 13:28:38 +0000537 PyBytes_AS_STRING(result) + total,
Guido van Rossum7165cb12007-07-10 06:54:34 +0000538 newsize - total);
539 Py_END_ALLOW_THREADS
540 if (n == 0)
541 break;
542 if (n < 0) {
543 if (total > 0)
544 break;
545 if (errno == EAGAIN) {
546 Py_DECREF(result);
547 Py_RETURN_NONE;
548 }
549 Py_DECREF(result);
550 PyErr_SetFromErrno(PyExc_IOError);
551 return NULL;
552 }
553 total += n;
554 }
555
Christian Heimes72b710a2008-05-26 13:28:38 +0000556 if (PyBytes_GET_SIZE(result) > total) {
557 if (_PyBytes_Resize(&result, total) < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000558 /* This should never happen, but just in case */
559 Py_DECREF(result);
560 return NULL;
561 }
562 }
563 return result;
564}
565
Guido van Rossuma9e20242007-03-08 00:43:48 +0000566static PyObject *
567fileio_read(PyFileIOObject *self, PyObject *args)
568{
569 char *ptr;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000570 Py_ssize_t n;
571 Py_ssize_t size = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000572 PyObject *bytes;
573
574 if (self->fd < 0)
575 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000576 if (!self->readable)
577 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000578
Neal Norwitz3c8ba932007-08-08 04:36:17 +0000579 if (!PyArg_ParseTuple(args, "|n", &size))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000580 return NULL;
581
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000582 if (size < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000583 return fileio_readall(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000584 }
585
Christian Heimes72b710a2008-05-26 13:28:38 +0000586 bytes = PyBytes_FromStringAndSize(NULL, size);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000587 if (bytes == NULL)
588 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +0000589 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000590
591 Py_BEGIN_ALLOW_THREADS
592 errno = 0;
593 n = read(self->fd, ptr, size);
594 Py_END_ALLOW_THREADS
595
596 if (n < 0) {
597 if (errno == EAGAIN)
598 Py_RETURN_NONE;
599 PyErr_SetFromErrno(PyExc_IOError);
600 return NULL;
601 }
602
603 if (n != size) {
Christian Heimes72b710a2008-05-26 13:28:38 +0000604 if (_PyBytes_Resize(&bytes, n) < 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000605 Py_DECREF(bytes);
Guido van Rossum53807da2007-04-10 19:01:47 +0000606 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000607 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000608 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000609
610 return (PyObject *) bytes;
611}
612
613static PyObject *
614fileio_write(PyFileIOObject *self, PyObject *args)
615{
Martin v. Löwis423be952008-08-13 15:53:07 +0000616 Py_buffer pbuf;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000617 Py_ssize_t n;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000618
619 if (self->fd < 0)
620 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000621 if (!self->writable)
622 return err_mode("writing");
623
Martin v. Löwis423be952008-08-13 15:53:07 +0000624 if (!PyArg_ParseTuple(args, "s*", &pbuf))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000625 return NULL;
626
627 Py_BEGIN_ALLOW_THREADS
628 errno = 0;
Martin v. Löwis423be952008-08-13 15:53:07 +0000629 n = write(self->fd, pbuf.buf, pbuf.len);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000630 Py_END_ALLOW_THREADS
631
Martin v. Löwis423be952008-08-13 15:53:07 +0000632 PyBuffer_Release(&pbuf);
633
Guido van Rossuma9e20242007-03-08 00:43:48 +0000634 if (n < 0) {
635 if (errno == EAGAIN)
636 Py_RETURN_NONE;
637 PyErr_SetFromErrno(PyExc_IOError);
638 return NULL;
639 }
640
Christian Heimes217cfd12007-12-02 14:31:20 +0000641 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000642}
643
Guido van Rossum53807da2007-04-10 19:01:47 +0000644/* XXX Windows support below is likely incomplete */
645
Guido van Rossum53807da2007-04-10 19:01:47 +0000646/* Cribbed from posix_lseek() */
647static PyObject *
648portable_lseek(int fd, PyObject *posobj, int whence)
649{
650 Py_off_t pos, res;
651
652#ifdef SEEK_SET
653 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
654 switch (whence) {
655#if SEEK_SET != 0
656 case 0: whence = SEEK_SET; break;
657#endif
658#if SEEK_CUR != 1
659 case 1: whence = SEEK_CUR; break;
660#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000661#if SEEK_END != 2
Guido van Rossum53807da2007-04-10 19:01:47 +0000662 case 2: whence = SEEK_END; break;
663#endif
664 }
665#endif /* SEEK_SET */
666
667 if (posobj == NULL)
668 pos = 0;
669 else {
Christian Heimes8e42a0a2007-11-08 18:04:45 +0000670 if(PyFloat_Check(posobj)) {
671 PyErr_SetString(PyExc_TypeError, "an integer is required");
672 return NULL;
673 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000674#if defined(HAVE_LARGEFILE_SUPPORT)
675 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000676#else
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000677 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000678#endif
679 if (PyErr_Occurred())
680 return NULL;
681 }
682
683 Py_BEGIN_ALLOW_THREADS
684#if defined(MS_WIN64) || defined(MS_WINDOWS)
685 res = _lseeki64(fd, pos, whence);
686#else
687 res = lseek(fd, pos, whence);
688#endif
689 Py_END_ALLOW_THREADS
690 if (res < 0)
691 return PyErr_SetFromErrno(PyExc_IOError);
692
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000693#if defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum53807da2007-04-10 19:01:47 +0000694 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000695#else
696 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000697#endif
698}
699
Guido van Rossuma9e20242007-03-08 00:43:48 +0000700static PyObject *
701fileio_seek(PyFileIOObject *self, PyObject *args)
702{
Guido van Rossum53807da2007-04-10 19:01:47 +0000703 PyObject *posobj;
704 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000705
706 if (self->fd < 0)
707 return err_closed();
708
Guido van Rossum53807da2007-04-10 19:01:47 +0000709 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000710 return NULL;
711
Guido van Rossum53807da2007-04-10 19:01:47 +0000712 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000713}
714
715static PyObject *
716fileio_tell(PyFileIOObject *self, PyObject *args)
717{
Guido van Rossuma9e20242007-03-08 00:43:48 +0000718 if (self->fd < 0)
719 return err_closed();
720
Guido van Rossum53807da2007-04-10 19:01:47 +0000721 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000722}
723
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000724#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000725static PyObject *
726fileio_truncate(PyFileIOObject *self, PyObject *args)
727{
Guido van Rossum53807da2007-04-10 19:01:47 +0000728 PyObject *posobj = NULL;
729 Py_off_t pos;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000730 int ret;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000731 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000732
Guido van Rossum53807da2007-04-10 19:01:47 +0000733 fd = self->fd;
734 if (fd < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000735 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000736 if (!self->writable)
737 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000738
Guido van Rossum53807da2007-04-10 19:01:47 +0000739 if (!PyArg_ParseTuple(args, "|O", &posobj))
740 return NULL;
741
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000742 if (posobj == Py_None || posobj == NULL) {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000743 /* Get the current position. */
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000744 posobj = portable_lseek(fd, NULL, 1);
745 if (posobj == NULL)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000746 return NULL;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000747 }
748 else {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000749 /* Move to the position to be truncated. */
750 posobj = portable_lseek(fd, posobj, 0);
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000751 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000752
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000753#if defined(HAVE_LARGEFILE_SUPPORT)
754 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000755#else
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000756 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000757#endif
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000758 if (PyErr_Occurred())
Guido van Rossum53807da2007-04-10 19:01:47 +0000759 return NULL;
760
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000761#ifdef MS_WINDOWS
762 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
763 so don't even try using it. */
764 {
765 HANDLE hFile;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000766
767 /* Truncate. Note that this may grow the file! */
768 Py_BEGIN_ALLOW_THREADS
769 errno = 0;
770 hFile = (HANDLE)_get_osfhandle(fd);
771 ret = hFile == (HANDLE)-1;
772 if (ret == 0) {
773 ret = SetEndOfFile(hFile) == 0;
774 if (ret)
775 errno = EACCES;
776 }
777 Py_END_ALLOW_THREADS
778 }
779#else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000780 Py_BEGIN_ALLOW_THREADS
781 errno = 0;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000782 ret = ftruncate(fd, pos);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000783 Py_END_ALLOW_THREADS
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000784#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000785
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000786 if (ret != 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000787 PyErr_SetFromErrno(PyExc_IOError);
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000788 return NULL;
Guido van Rossum87429772007-04-10 21:06:59 +0000789 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000790
Guido van Rossum87429772007-04-10 21:06:59 +0000791 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000792}
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000793#endif
Guido van Rossum53807da2007-04-10 19:01:47 +0000794
795static char *
796mode_string(PyFileIOObject *self)
797{
798 if (self->readable) {
799 if (self->writable)
Benjamin Peterson44309e62008-11-22 00:41:45 +0000800 return "rb+";
Guido van Rossum53807da2007-04-10 19:01:47 +0000801 else
Benjamin Peterson44309e62008-11-22 00:41:45 +0000802 return "rb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000803 }
804 else
Benjamin Peterson44309e62008-11-22 00:41:45 +0000805 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000806}
Guido van Rossuma9e20242007-03-08 00:43:48 +0000807
808static PyObject *
809fileio_repr(PyFileIOObject *self)
810{
Guido van Rossum53807da2007-04-10 19:01:47 +0000811 if (self->fd < 0)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000812 return PyUnicode_FromFormat("io.FileIO(-1)");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000813
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000814 return PyUnicode_FromFormat("io.FileIO(%d, '%s')",
Guido van Rossum53807da2007-04-10 19:01:47 +0000815 self->fd, mode_string(self));
Guido van Rossuma9e20242007-03-08 00:43:48 +0000816}
817
818static PyObject *
819fileio_isatty(PyFileIOObject *self)
820{
821 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000822
Guido van Rossuma9e20242007-03-08 00:43:48 +0000823 if (self->fd < 0)
824 return err_closed();
825 Py_BEGIN_ALLOW_THREADS
826 res = isatty(self->fd);
827 Py_END_ALLOW_THREADS
828 return PyBool_FromLong(res);
829}
830
Guido van Rossuma9e20242007-03-08 00:43:48 +0000831
832PyDoc_STRVAR(fileio_doc,
833"file(name: str[, mode: str]) -> file IO object\n"
834"\n"
835"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
836"writing or appending. The file will be created if it doesn't exist\n"
837"when opened for writing or appending; it will be truncated when\n"
838"opened for writing. Add a '+' to the mode to allow simultaneous\n"
839"reading and writing.");
840
841PyDoc_STRVAR(read_doc,
842"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
843"\n"
844"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +0000845"In non-blocking mode, returns None if no data is available.\n"
846"On end-of-file, returns ''.");
847
848PyDoc_STRVAR(readall_doc,
849"readall() -> bytes. read all data from the file, returned as bytes.\n"
850"\n"
851"In non-blocking mode, returns as much as is immediately available,\n"
852"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000853
854PyDoc_STRVAR(write_doc,
855"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
856"\n"
857"Only makes one system call, so not all of the data may be written.\n"
858"The number of bytes actually written is returned.");
859
860PyDoc_STRVAR(fileno_doc,
861"fileno() -> int. \"file descriptor\".\n"
862"\n"
863"This is needed for lower-level file interfaces, such the fcntl module.");
864
865PyDoc_STRVAR(seek_doc,
866"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
867"\n"
868"Argument offset is a byte count. Optional argument whence defaults to\n"
869"0 (offset from start of file, offset should be >= 0); other values are 1\n"
870"(move relative to current position, positive or negative), and 2 (move\n"
871"relative to end of file, usually negative, although many platforms allow\n"
872"seeking beyond the end of a file)."
873"\n"
874"Note that not all file objects are seekable.");
875
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000876#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000877PyDoc_STRVAR(truncate_doc,
878"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
879"\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000880"Size defaults to the current file position, as returned by tell()."
881"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000882#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000883
884PyDoc_STRVAR(tell_doc,
885"tell() -> int. Current file position");
886
887PyDoc_STRVAR(readinto_doc,
Benjamin Peterson9a8082f2009-03-05 00:55:56 +0000888"readinto() -> Same as RawIOBase.readinto().");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000889
890PyDoc_STRVAR(close_doc,
891"close() -> None. Close the file.\n"
892"\n"
893"A closed file cannot be used for further I/O operations. close() may be\n"
894"called more than once without error. Changes the fileno to -1.");
895
896PyDoc_STRVAR(isatty_doc,
897"isatty() -> bool. True if the file is connected to a tty device.");
898
Guido van Rossuma9e20242007-03-08 00:43:48 +0000899PyDoc_STRVAR(seekable_doc,
900"seekable() -> bool. True if file supports random-access.");
901
902PyDoc_STRVAR(readable_doc,
903"readable() -> bool. True if file was opened in a read mode.");
904
905PyDoc_STRVAR(writable_doc,
906"writable() -> bool. True if file was opened in a write mode.");
907
908static PyMethodDef fileio_methods[] = {
909 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
Guido van Rossum7165cb12007-07-10 06:54:34 +0000910 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000911 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
912 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
913 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
914 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000915#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000916 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000917#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000918 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
919 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
920 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
921 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
922 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
923 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000924 {NULL, NULL} /* sentinel */
925};
926
Guido van Rossum53807da2007-04-10 19:01:47 +0000927/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
928
Guido van Rossumb0428152007-04-08 17:44:42 +0000929static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000930get_closed(PyFileIOObject *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +0000931{
Guido van Rossum53807da2007-04-10 19:01:47 +0000932 return PyBool_FromLong((long)(self->fd < 0));
933}
934
935static PyObject *
Christian Heimesecc42a22008-11-05 19:30:32 +0000936get_closefd(PyFileIOObject *self, void *closure)
937{
938 return PyBool_FromLong((long)(self->closefd));
939}
940
941static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000942get_mode(PyFileIOObject *self, void *closure)
943{
Guido van Rossumc43e79f2007-06-18 18:26:36 +0000944 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +0000945}
946
947static PyGetSetDef fileio_getsetlist[] = {
948 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Christian Heimesecc42a22008-11-05 19:30:32 +0000949 {"closefd", (getter)get_closefd, NULL,
950 "True if the file descriptor will be closed"},
Guido van Rossum53807da2007-04-10 19:01:47 +0000951 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
Guido van Rossumb0428152007-04-08 17:44:42 +0000952 {0},
953};
954
Guido van Rossuma9e20242007-03-08 00:43:48 +0000955PyTypeObject PyFileIO_Type = {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000956 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000957 "_io.FileIO",
Guido van Rossuma9e20242007-03-08 00:43:48 +0000958 sizeof(PyFileIOObject),
959 0,
960 (destructor)fileio_dealloc, /* tp_dealloc */
961 0, /* tp_print */
962 0, /* tp_getattr */
963 0, /* tp_setattr */
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000964 0, /* tp_reserved */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000965 (reprfunc)fileio_repr, /* tp_repr */
966 0, /* tp_as_number */
967 0, /* tp_as_sequence */
968 0, /* tp_as_mapping */
969 0, /* tp_hash */
970 0, /* tp_call */
971 0, /* tp_str */
972 PyObject_GenericGetAttr, /* tp_getattro */
973 0, /* tp_setattro */
974 0, /* tp_as_buffer */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000975 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
976 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000977 fileio_doc, /* tp_doc */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000978 (traverseproc)fileio_traverse, /* tp_traverse */
979 (inquiry)fileio_clear, /* tp_clear */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000980 0, /* tp_richcompare */
981 offsetof(PyFileIOObject, weakreflist), /* tp_weaklistoffset */
982 0, /* tp_iter */
983 0, /* tp_iternext */
984 fileio_methods, /* tp_methods */
985 0, /* tp_members */
Guido van Rossumb0428152007-04-08 17:44:42 +0000986 fileio_getsetlist, /* tp_getset */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000987 0, /* tp_base */
988 0, /* tp_dict */
989 0, /* tp_descr_get */
990 0, /* tp_descr_set */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000991 offsetof(PyFileIOObject, dict), /* tp_dictoffset */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000992 fileio_init, /* tp_init */
993 PyType_GenericAlloc, /* tp_alloc */
994 fileio_new, /* tp_new */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000995 PyObject_GC_Del, /* tp_free */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000996};