blob: 4b382fb72988205e38b73827ba8f231c1f9c7cd5 [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);
100 if (errno < 0) {
101 PyErr_SetFromErrno(PyExc_IOError);
Neal Norwitz88b44da2007-08-12 17:23:54 +0000102 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000103 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000104
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000105 return PyObject_CallMethod((PyObject*)&PyRawIOBase_Type,
106 "close", "O", self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000107}
108
109static PyObject *
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000110fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000111{
112 PyFileIOObject *self;
113
114 assert(type != NULL && type->tp_alloc != NULL);
115
116 self = (PyFileIOObject *) type->tp_alloc(type, 0);
117 if (self != NULL) {
118 self->fd = -1;
Christian Heimesdf32b392008-10-30 21:23:35 +0000119 self->readable = 0;
120 self->writable = 0;
121 self->seekable = -1;
122 self->closefd = 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000123 self->weakreflist = NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000124 }
125
126 return (PyObject *) self;
127}
128
129/* On Unix, open will succeed for directories.
130 In Python, there should be no file objects referring to
131 directories, so we need a check. */
132
133static int
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000134dircheck(PyFileIOObject* self, const char *name)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000135{
136#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
137 struct stat buf;
138 if (self->fd < 0)
139 return 0;
140 if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000141 char *msg = strerror(EISDIR);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000142 PyObject *exc;
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +0000143 if (internal_close(self))
144 return -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000145
Benjamin Peterson1efc23c2008-12-29 18:02:28 +0000146 exc = PyObject_CallFunction(PyExc_IOError, "(iss)",
147 EISDIR, msg, name);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000148 PyErr_SetObject(PyExc_IOError, exc);
149 Py_XDECREF(exc);
150 return -1;
151 }
152#endif
153 return 0;
154}
155
Benjamin Peterson806d4022009-01-19 15:11:51 +0000156static int
157check_fd(int fd)
158{
159#if defined(HAVE_FSTAT)
160 struct stat buf;
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000161 if (!_PyVerify_fd(fd) || (fstat(fd, &buf) < 0 && errno == EBADF)) {
Benjamin Peterson806d4022009-01-19 15:11:51 +0000162 PyObject *exc;
163 char *msg = strerror(EBADF);
164 exc = PyObject_CallFunction(PyExc_OSError, "(is)",
165 EBADF, msg);
166 PyErr_SetObject(PyExc_OSError, exc);
167 Py_XDECREF(exc);
168 return -1;
169 }
170#endif
171 return 0;
172}
173
Guido van Rossuma9e20242007-03-08 00:43:48 +0000174
175static int
176fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
177{
178 PyFileIOObject *self = (PyFileIOObject *) oself;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000179 static char *kwlist[] = {"file", "mode", "closefd", NULL};
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000180 const char *name = NULL;
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000181 PyObject *nameobj, *stringobj = NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000182 char *mode = "r";
Guido van Rossum53807da2007-04-10 19:01:47 +0000183 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000184#ifdef MS_WINDOWS
185 Py_UNICODE *widename = NULL;
186#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000187 int ret = 0;
188 int rwa = 0, plus = 0, append = 0;
189 int flags = 0;
Guido van Rossumb0428152007-04-08 17:44:42 +0000190 int fd = -1;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000191 int closefd = 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000192
193 assert(PyFileIO_Check(oself));
Neal Norwitz88b44da2007-08-12 17:23:54 +0000194 if (self->fd >= 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000195 /* Have to close the existing file first. */
Neal Norwitz88b44da2007-08-12 17:23:54 +0000196 if (internal_close(self) < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000197 return -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000198 }
199
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000200 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:fileio",
201 kwlist, &nameobj, &mode, &closefd))
202 return -1;
203
204 if (PyFloat_Check(nameobj)) {
205 PyErr_SetString(PyExc_TypeError,
206 "integer argument expected, got float");
207 return -1;
208 }
209
210 fd = PyLong_AsLong(nameobj);
211 if (fd < 0) {
212 if (!PyErr_Occurred()) {
Guido van Rossumb0428152007-04-08 17:44:42 +0000213 PyErr_SetString(PyExc_ValueError,
214 "Negative filedescriptor");
215 return -1;
216 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000217 PyErr_Clear();
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000218 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000219
Guido van Rossuma9e20242007-03-08 00:43:48 +0000220#ifdef Py_WIN_WIDE_FILENAMES
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000221 if (GetVersion() < 0x80000000) {
Guido van Rossumb0428152007-04-08 17:44:42 +0000222 /* On NT, so wide API available */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000223 if (PyUnicode_Check(nameobj))
224 widename = PyUnicode_AS_UNICODE(nameobj);
225 }
226 if (widename == NULL)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000227#endif
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000228 if (fd < 0)
229 {
230 if (PyBytes_Check(nameobj) || PyByteArray_Check(nameobj)) {
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000231 Py_ssize_t namelen;
232 if (PyObject_AsCharBuffer(nameobj, &name, &namelen) < 0)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000233 return -1;
234 }
235 else {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000236 PyObject *u = PyUnicode_FromObject(nameobj);
237
238 if (u == NULL)
239 return -1;
240
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000241 stringobj = PyUnicode_AsEncodedString(
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000242 u, Py_FileSystemDefaultEncoding, NULL);
243 Py_DECREF(u);
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000244 if (stringobj == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000245 return -1;
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000246 if (!PyBytes_Check(stringobj)) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000247 PyErr_SetString(PyExc_TypeError,
248 "encoder failed to return bytes");
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000249 goto error;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000250 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000251 name = PyBytes_AS_STRING(stringobj);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000252 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000253 }
254
Guido van Rossum53807da2007-04-10 19:01:47 +0000255 s = mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000256 while (*s) {
257 switch (*s++) {
258 case 'r':
259 if (rwa) {
260 bad_mode:
261 PyErr_SetString(PyExc_ValueError,
262 "Must have exactly one of read/write/append mode");
263 goto error;
264 }
265 rwa = 1;
266 self->readable = 1;
267 break;
268 case 'w':
269 if (rwa)
270 goto bad_mode;
271 rwa = 1;
272 self->writable = 1;
273 flags |= O_CREAT | O_TRUNC;
274 break;
275 case 'a':
276 if (rwa)
277 goto bad_mode;
278 rwa = 1;
279 self->writable = 1;
280 flags |= O_CREAT;
281 append = 1;
282 break;
Benjamin Peterson44309e62008-11-22 00:41:45 +0000283 case 'b':
284 break;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000285 case '+':
286 if (plus)
287 goto bad_mode;
288 self->readable = self->writable = 1;
289 plus = 1;
290 break;
291 default:
292 PyErr_Format(PyExc_ValueError,
293 "invalid mode: %.200s", mode);
294 goto error;
295 }
296 }
297
298 if (!rwa)
299 goto bad_mode;
300
301 if (self->readable && self->writable)
302 flags |= O_RDWR;
303 else if (self->readable)
304 flags |= O_RDONLY;
305 else
306 flags |= O_WRONLY;
307
308#ifdef O_BINARY
309 flags |= O_BINARY;
310#endif
311
Walter Dörwald0e411482007-06-06 16:55:38 +0000312#ifdef O_APPEND
313 if (append)
314 flags |= O_APPEND;
315#endif
316
Guido van Rossumb0428152007-04-08 17:44:42 +0000317 if (fd >= 0) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000318 if (check_fd(fd))
319 goto error;
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000320 self->fd = fd;
321 self->closefd = closefd;
Guido van Rossumb0428152007-04-08 17:44:42 +0000322 }
323 else {
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000324 self->closefd = 1;
325 if (!closefd) {
326 PyErr_SetString(PyExc_ValueError,
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000327 "Cannot use closefd=False with file name");
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000328 goto error;
329 }
330
Guido van Rossumb0428152007-04-08 17:44:42 +0000331 Py_BEGIN_ALLOW_THREADS
332 errno = 0;
Thomas Helleraf2be262007-07-12 11:03:13 +0000333#ifdef MS_WINDOWS
334 if (widename != NULL)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000335 self->fd = _wopen(widename, flags, 0666);
Thomas Helleraf2be262007-07-12 11:03:13 +0000336 else
337#endif
Neal Norwitz88b44da2007-08-12 17:23:54 +0000338 self->fd = open(name, flags, 0666);
Guido van Rossumb0428152007-04-08 17:44:42 +0000339 Py_END_ALLOW_THREADS
Benjamin Peterson3e4f0552008-09-02 00:31:15 +0000340 if (self->fd < 0) {
Christian Heimes0b489542007-10-31 19:20:48 +0000341#ifdef MS_WINDOWS
Hirokazu Yamamoto0f22d692009-01-01 16:03:45 +0000342 if (widename != NULL)
343 PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename);
344 else
Christian Heimes0b489542007-10-31 19:20:48 +0000345#endif
Hirokazu Yamamoto0f22d692009-01-01 16:03:45 +0000346 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
Guido van Rossumb0428152007-04-08 17:44:42 +0000347 goto error;
348 }
Benjamin Peterson1efc23c2008-12-29 18:02:28 +0000349 if(dircheck(self, name) < 0)
Benjamin Peterson3e4f0552008-09-02 00:31:15 +0000350 goto error;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000351 }
352
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000353 if (PyObject_SetAttrString((PyObject *)self, "name", nameobj) < 0)
354 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000355
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000356 if (append) {
357 /* For consistent behaviour, we explicitly seek to the
358 end of file (otherwise, it might be done only on the
359 first write()). */
360 PyObject *pos = portable_lseek(self->fd, NULL, 2);
361 if (pos == NULL)
362 goto error;
363 Py_DECREF(pos);
364 }
365
Guido van Rossuma9e20242007-03-08 00:43:48 +0000366 goto done;
367
368 error:
369 ret = -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000370
Guido van Rossuma9e20242007-03-08 00:43:48 +0000371 done:
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000372 Py_CLEAR(stringobj);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000373 return ret;
374}
375
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000376static int
377fileio_traverse(PyFileIOObject *self, visitproc visit, void *arg)
378{
379 Py_VISIT(self->dict);
380 return 0;
381}
382
383static int
384fileio_clear(PyFileIOObject *self)
385{
386 Py_CLEAR(self->dict);
387 return 0;
388}
389
Guido van Rossuma9e20242007-03-08 00:43:48 +0000390static void
391fileio_dealloc(PyFileIOObject *self)
392{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000393 if (_PyIOBase_finalize((PyObject *) self) < 0)
394 return;
395 _PyObject_GC_UNTRACK(self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000396 if (self->weakreflist != NULL)
397 PyObject_ClearWeakRefs((PyObject *) self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000398 Py_CLEAR(self->dict);
Christian Heimes90aa7642007-12-19 02:45:37 +0000399 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000400}
401
402static PyObject *
403err_closed(void)
404{
405 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
406 return NULL;
407}
408
409static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000410err_mode(char *action)
411{
412 PyErr_Format(PyExc_ValueError, "File not open for %s", action);
413 return NULL;
414}
415
416static PyObject *
Guido van Rossuma9e20242007-03-08 00:43:48 +0000417fileio_fileno(PyFileIOObject *self)
418{
419 if (self->fd < 0)
420 return err_closed();
Christian Heimes217cfd12007-12-02 14:31:20 +0000421 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000422}
423
424static PyObject *
425fileio_readable(PyFileIOObject *self)
426{
427 if (self->fd < 0)
428 return err_closed();
Neal Norwitz88b44da2007-08-12 17:23:54 +0000429 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000430}
431
432static PyObject *
433fileio_writable(PyFileIOObject *self)
434{
435 if (self->fd < 0)
436 return err_closed();
Neal Norwitz88b44da2007-08-12 17:23:54 +0000437 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000438}
439
440static PyObject *
441fileio_seekable(PyFileIOObject *self)
442{
443 if (self->fd < 0)
444 return err_closed();
445 if (self->seekable < 0) {
446 int ret;
447 Py_BEGIN_ALLOW_THREADS
448 ret = lseek(self->fd, 0, SEEK_CUR);
449 Py_END_ALLOW_THREADS
450 if (ret < 0)
451 self->seekable = 0;
452 else
453 self->seekable = 1;
454 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000455 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000456}
457
458static PyObject *
459fileio_readinto(PyFileIOObject *self, PyObject *args)
460{
Martin v. Löwis423be952008-08-13 15:53:07 +0000461 Py_buffer pbuf;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000462 Py_ssize_t n;
Guido van Rossum53807da2007-04-10 19:01:47 +0000463
Guido van Rossuma9e20242007-03-08 00:43:48 +0000464 if (self->fd < 0)
465 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000466 if (!self->readable)
467 return err_mode("reading");
468
Martin v. Löwis423be952008-08-13 15:53:07 +0000469 if (!PyArg_ParseTuple(args, "w*", &pbuf))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000470 return NULL;
471
472 Py_BEGIN_ALLOW_THREADS
473 errno = 0;
Martin v. Löwis423be952008-08-13 15:53:07 +0000474 n = read(self->fd, pbuf.buf, pbuf.len);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000475 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +0000476 PyBuffer_Release(&pbuf);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000477 if (n < 0) {
478 if (errno == EAGAIN)
479 Py_RETURN_NONE;
480 PyErr_SetFromErrno(PyExc_IOError);
481 return NULL;
482 }
483
Christian Heimes217cfd12007-12-02 14:31:20 +0000484 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000485}
486
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000487static size_t
488new_buffersize(PyFileIOObject *self, size_t currentsize)
489{
490#ifdef HAVE_FSTAT
491 off_t pos, end;
492 struct stat st;
493 if (fstat(self->fd, &st) == 0) {
494 end = st.st_size;
495 pos = lseek(self->fd, 0L, SEEK_CUR);
496 if (end >= pos && pos >= 0)
497 return currentsize + end - pos + 1;
498 /* Add 1 so if the file were to grow we'd notice. */
499 }
500#endif
501 if (currentsize > SMALLCHUNK) {
502 /* Keep doubling until we reach BIGCHUNK;
503 then keep adding BIGCHUNK. */
504 if (currentsize <= BIGCHUNK)
505 return currentsize + currentsize;
506 else
507 return currentsize + BIGCHUNK;
508 }
509 return currentsize + SMALLCHUNK;
510}
511
Guido van Rossum7165cb12007-07-10 06:54:34 +0000512static PyObject *
513fileio_readall(PyFileIOObject *self)
514{
515 PyObject *result;
516 Py_ssize_t total = 0;
517 int n;
518
Christian Heimesa872de52008-12-05 08:26:55 +0000519 result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK);
Guido van Rossum7165cb12007-07-10 06:54:34 +0000520 if (result == NULL)
521 return NULL;
522
523 while (1) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000524 Py_ssize_t newsize = new_buffersize(self, total);
Christian Heimesa872de52008-12-05 08:26:55 +0000525
Christian Heimes72b710a2008-05-26 13:28:38 +0000526 if (PyBytes_GET_SIZE(result) < newsize) {
527 if (_PyBytes_Resize(&result, newsize) < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000528 if (total == 0) {
529 Py_DECREF(result);
530 return NULL;
531 }
532 PyErr_Clear();
533 break;
534 }
535 }
536 Py_BEGIN_ALLOW_THREADS
537 errno = 0;
538 n = read(self->fd,
Christian Heimes72b710a2008-05-26 13:28:38 +0000539 PyBytes_AS_STRING(result) + total,
Guido van Rossum7165cb12007-07-10 06:54:34 +0000540 newsize - total);
541 Py_END_ALLOW_THREADS
542 if (n == 0)
543 break;
544 if (n < 0) {
545 if (total > 0)
546 break;
547 if (errno == EAGAIN) {
548 Py_DECREF(result);
549 Py_RETURN_NONE;
550 }
551 Py_DECREF(result);
552 PyErr_SetFromErrno(PyExc_IOError);
553 return NULL;
554 }
555 total += n;
556 }
557
Christian Heimes72b710a2008-05-26 13:28:38 +0000558 if (PyBytes_GET_SIZE(result) > total) {
559 if (_PyBytes_Resize(&result, total) < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000560 /* This should never happen, but just in case */
561 Py_DECREF(result);
562 return NULL;
563 }
564 }
565 return result;
566}
567
Guido van Rossuma9e20242007-03-08 00:43:48 +0000568static PyObject *
569fileio_read(PyFileIOObject *self, PyObject *args)
570{
571 char *ptr;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000572 Py_ssize_t n;
573 Py_ssize_t size = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000574 PyObject *bytes;
575
576 if (self->fd < 0)
577 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000578 if (!self->readable)
579 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000580
Neal Norwitz3c8ba932007-08-08 04:36:17 +0000581 if (!PyArg_ParseTuple(args, "|n", &size))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000582 return NULL;
583
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000584 if (size < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000585 return fileio_readall(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000586 }
587
Christian Heimes72b710a2008-05-26 13:28:38 +0000588 bytes = PyBytes_FromStringAndSize(NULL, size);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000589 if (bytes == NULL)
590 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +0000591 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000592
593 Py_BEGIN_ALLOW_THREADS
594 errno = 0;
595 n = read(self->fd, ptr, size);
596 Py_END_ALLOW_THREADS
597
598 if (n < 0) {
599 if (errno == EAGAIN)
600 Py_RETURN_NONE;
601 PyErr_SetFromErrno(PyExc_IOError);
602 return NULL;
603 }
604
605 if (n != size) {
Christian Heimes72b710a2008-05-26 13:28:38 +0000606 if (_PyBytes_Resize(&bytes, n) < 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000607 Py_DECREF(bytes);
Guido van Rossum53807da2007-04-10 19:01:47 +0000608 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000609 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000610 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000611
612 return (PyObject *) bytes;
613}
614
615static PyObject *
616fileio_write(PyFileIOObject *self, PyObject *args)
617{
Martin v. Löwis423be952008-08-13 15:53:07 +0000618 Py_buffer pbuf;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000619 Py_ssize_t n;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000620
621 if (self->fd < 0)
622 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000623 if (!self->writable)
624 return err_mode("writing");
625
Martin v. Löwis423be952008-08-13 15:53:07 +0000626 if (!PyArg_ParseTuple(args, "s*", &pbuf))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000627 return NULL;
628
629 Py_BEGIN_ALLOW_THREADS
630 errno = 0;
Martin v. Löwis423be952008-08-13 15:53:07 +0000631 n = write(self->fd, pbuf.buf, pbuf.len);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000632 Py_END_ALLOW_THREADS
633
Martin v. Löwis423be952008-08-13 15:53:07 +0000634 PyBuffer_Release(&pbuf);
635
Guido van Rossuma9e20242007-03-08 00:43:48 +0000636 if (n < 0) {
637 if (errno == EAGAIN)
638 Py_RETURN_NONE;
639 PyErr_SetFromErrno(PyExc_IOError);
640 return NULL;
641 }
642
Christian Heimes217cfd12007-12-02 14:31:20 +0000643 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000644}
645
Guido van Rossum53807da2007-04-10 19:01:47 +0000646/* XXX Windows support below is likely incomplete */
647
Guido van Rossum53807da2007-04-10 19:01:47 +0000648/* Cribbed from posix_lseek() */
649static PyObject *
650portable_lseek(int fd, PyObject *posobj, int whence)
651{
652 Py_off_t pos, res;
653
654#ifdef SEEK_SET
655 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
656 switch (whence) {
657#if SEEK_SET != 0
658 case 0: whence = SEEK_SET; break;
659#endif
660#if SEEK_CUR != 1
661 case 1: whence = SEEK_CUR; break;
662#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000663#if SEEK_END != 2
Guido van Rossum53807da2007-04-10 19:01:47 +0000664 case 2: whence = SEEK_END; break;
665#endif
666 }
667#endif /* SEEK_SET */
668
669 if (posobj == NULL)
670 pos = 0;
671 else {
Christian Heimes8e42a0a2007-11-08 18:04:45 +0000672 if(PyFloat_Check(posobj)) {
673 PyErr_SetString(PyExc_TypeError, "an integer is required");
674 return NULL;
675 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000676#if defined(HAVE_LARGEFILE_SUPPORT)
677 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000678#else
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000679 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000680#endif
681 if (PyErr_Occurred())
682 return NULL;
683 }
684
685 Py_BEGIN_ALLOW_THREADS
686#if defined(MS_WIN64) || defined(MS_WINDOWS)
687 res = _lseeki64(fd, pos, whence);
688#else
689 res = lseek(fd, pos, whence);
690#endif
691 Py_END_ALLOW_THREADS
692 if (res < 0)
693 return PyErr_SetFromErrno(PyExc_IOError);
694
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000695#if defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum53807da2007-04-10 19:01:47 +0000696 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000697#else
698 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000699#endif
700}
701
Guido van Rossuma9e20242007-03-08 00:43:48 +0000702static PyObject *
703fileio_seek(PyFileIOObject *self, PyObject *args)
704{
Guido van Rossum53807da2007-04-10 19:01:47 +0000705 PyObject *posobj;
706 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000707
708 if (self->fd < 0)
709 return err_closed();
710
Guido van Rossum53807da2007-04-10 19:01:47 +0000711 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000712 return NULL;
713
Guido van Rossum53807da2007-04-10 19:01:47 +0000714 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000715}
716
717static PyObject *
718fileio_tell(PyFileIOObject *self, PyObject *args)
719{
Guido van Rossuma9e20242007-03-08 00:43:48 +0000720 if (self->fd < 0)
721 return err_closed();
722
Guido van Rossum53807da2007-04-10 19:01:47 +0000723 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000724}
725
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000726#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000727static PyObject *
728fileio_truncate(PyFileIOObject *self, PyObject *args)
729{
Guido van Rossum53807da2007-04-10 19:01:47 +0000730 PyObject *posobj = NULL;
731 Py_off_t pos;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000732 int ret;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000733 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000734
Guido van Rossum53807da2007-04-10 19:01:47 +0000735 fd = self->fd;
736 if (fd < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000737 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000738 if (!self->writable)
739 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000740
Guido van Rossum53807da2007-04-10 19:01:47 +0000741 if (!PyArg_ParseTuple(args, "|O", &posobj))
742 return NULL;
743
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000744 if (posobj == Py_None || posobj == NULL) {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000745 /* Get the current position. */
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000746 posobj = portable_lseek(fd, NULL, 1);
747 if (posobj == NULL)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000748 return NULL;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000749 }
750 else {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000751 /* Move to the position to be truncated. */
752 posobj = portable_lseek(fd, posobj, 0);
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000753 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000754
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000755#if defined(HAVE_LARGEFILE_SUPPORT)
756 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000757#else
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000758 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000759#endif
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000760 if (PyErr_Occurred())
Guido van Rossum53807da2007-04-10 19:01:47 +0000761 return NULL;
762
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000763#ifdef MS_WINDOWS
764 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
765 so don't even try using it. */
766 {
767 HANDLE hFile;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000768
769 /* Truncate. Note that this may grow the file! */
770 Py_BEGIN_ALLOW_THREADS
771 errno = 0;
772 hFile = (HANDLE)_get_osfhandle(fd);
773 ret = hFile == (HANDLE)-1;
774 if (ret == 0) {
775 ret = SetEndOfFile(hFile) == 0;
776 if (ret)
777 errno = EACCES;
778 }
779 Py_END_ALLOW_THREADS
780 }
781#else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000782 Py_BEGIN_ALLOW_THREADS
783 errno = 0;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000784 ret = ftruncate(fd, pos);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000785 Py_END_ALLOW_THREADS
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000786#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000787
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000788 if (ret != 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000789 PyErr_SetFromErrno(PyExc_IOError);
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000790 return NULL;
Guido van Rossum87429772007-04-10 21:06:59 +0000791 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000792
Guido van Rossum87429772007-04-10 21:06:59 +0000793 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000794}
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000795#endif
Guido van Rossum53807da2007-04-10 19:01:47 +0000796
797static char *
798mode_string(PyFileIOObject *self)
799{
800 if (self->readable) {
801 if (self->writable)
Benjamin Peterson44309e62008-11-22 00:41:45 +0000802 return "rb+";
Guido van Rossum53807da2007-04-10 19:01:47 +0000803 else
Benjamin Peterson44309e62008-11-22 00:41:45 +0000804 return "rb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000805 }
806 else
Benjamin Peterson44309e62008-11-22 00:41:45 +0000807 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000808}
Guido van Rossuma9e20242007-03-08 00:43:48 +0000809
810static PyObject *
811fileio_repr(PyFileIOObject *self)
812{
Guido van Rossum53807da2007-04-10 19:01:47 +0000813 if (self->fd < 0)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000814 return PyUnicode_FromFormat("io.FileIO(-1)");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000815
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000816 return PyUnicode_FromFormat("io.FileIO(%d, '%s')",
Guido van Rossum53807da2007-04-10 19:01:47 +0000817 self->fd, mode_string(self));
Guido van Rossuma9e20242007-03-08 00:43:48 +0000818}
819
820static PyObject *
821fileio_isatty(PyFileIOObject *self)
822{
823 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000824
Guido van Rossuma9e20242007-03-08 00:43:48 +0000825 if (self->fd < 0)
826 return err_closed();
827 Py_BEGIN_ALLOW_THREADS
828 res = isatty(self->fd);
829 Py_END_ALLOW_THREADS
830 return PyBool_FromLong(res);
831}
832
Guido van Rossuma9e20242007-03-08 00:43:48 +0000833
834PyDoc_STRVAR(fileio_doc,
835"file(name: str[, mode: str]) -> file IO object\n"
836"\n"
837"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
838"writing or appending. The file will be created if it doesn't exist\n"
839"when opened for writing or appending; it will be truncated when\n"
840"opened for writing. Add a '+' to the mode to allow simultaneous\n"
841"reading and writing.");
842
843PyDoc_STRVAR(read_doc,
844"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
845"\n"
846"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +0000847"In non-blocking mode, returns None if no data is available.\n"
848"On end-of-file, returns ''.");
849
850PyDoc_STRVAR(readall_doc,
851"readall() -> bytes. read all data from the file, returned as bytes.\n"
852"\n"
853"In non-blocking mode, returns as much as is immediately available,\n"
854"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000855
856PyDoc_STRVAR(write_doc,
857"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
858"\n"
859"Only makes one system call, so not all of the data may be written.\n"
860"The number of bytes actually written is returned.");
861
862PyDoc_STRVAR(fileno_doc,
863"fileno() -> int. \"file descriptor\".\n"
864"\n"
865"This is needed for lower-level file interfaces, such the fcntl module.");
866
867PyDoc_STRVAR(seek_doc,
868"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
869"\n"
870"Argument offset is a byte count. Optional argument whence defaults to\n"
871"0 (offset from start of file, offset should be >= 0); other values are 1\n"
872"(move relative to current position, positive or negative), and 2 (move\n"
873"relative to end of file, usually negative, although many platforms allow\n"
874"seeking beyond the end of a file)."
875"\n"
876"Note that not all file objects are seekable.");
877
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000878#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000879PyDoc_STRVAR(truncate_doc,
880"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
881"\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000882"Size defaults to the current file position, as returned by tell()."
883"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000884#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000885
886PyDoc_STRVAR(tell_doc,
887"tell() -> int. Current file position");
888
889PyDoc_STRVAR(readinto_doc,
890"readinto() -> Undocumented. Don't use this; it may go away.");
891
892PyDoc_STRVAR(close_doc,
893"close() -> None. Close the file.\n"
894"\n"
895"A closed file cannot be used for further I/O operations. close() may be\n"
896"called more than once without error. Changes the fileno to -1.");
897
898PyDoc_STRVAR(isatty_doc,
899"isatty() -> bool. True if the file is connected to a tty device.");
900
Guido van Rossuma9e20242007-03-08 00:43:48 +0000901PyDoc_STRVAR(seekable_doc,
902"seekable() -> bool. True if file supports random-access.");
903
904PyDoc_STRVAR(readable_doc,
905"readable() -> bool. True if file was opened in a read mode.");
906
907PyDoc_STRVAR(writable_doc,
908"writable() -> bool. True if file was opened in a write mode.");
909
910static PyMethodDef fileio_methods[] = {
911 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
Guido van Rossum7165cb12007-07-10 06:54:34 +0000912 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000913 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
914 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
915 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
916 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000917#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000918 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000919#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000920 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
921 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
922 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
923 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
924 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
925 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000926 {NULL, NULL} /* sentinel */
927};
928
Guido van Rossum53807da2007-04-10 19:01:47 +0000929/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
930
Guido van Rossumb0428152007-04-08 17:44:42 +0000931static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000932get_closed(PyFileIOObject *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +0000933{
Guido van Rossum53807da2007-04-10 19:01:47 +0000934 return PyBool_FromLong((long)(self->fd < 0));
935}
936
937static PyObject *
Christian Heimesecc42a22008-11-05 19:30:32 +0000938get_closefd(PyFileIOObject *self, void *closure)
939{
940 return PyBool_FromLong((long)(self->closefd));
941}
942
943static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000944get_mode(PyFileIOObject *self, void *closure)
945{
Guido van Rossumc43e79f2007-06-18 18:26:36 +0000946 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +0000947}
948
949static PyGetSetDef fileio_getsetlist[] = {
950 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Christian Heimesecc42a22008-11-05 19:30:32 +0000951 {"closefd", (getter)get_closefd, NULL,
952 "True if the file descriptor will be closed"},
Guido van Rossum53807da2007-04-10 19:01:47 +0000953 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
Guido van Rossumb0428152007-04-08 17:44:42 +0000954 {0},
955};
956
Guido van Rossuma9e20242007-03-08 00:43:48 +0000957PyTypeObject PyFileIO_Type = {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000958 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000959 "_io.FileIO",
Guido van Rossuma9e20242007-03-08 00:43:48 +0000960 sizeof(PyFileIOObject),
961 0,
962 (destructor)fileio_dealloc, /* tp_dealloc */
963 0, /* tp_print */
964 0, /* tp_getattr */
965 0, /* tp_setattr */
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000966 0, /* tp_reserved */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000967 (reprfunc)fileio_repr, /* tp_repr */
968 0, /* tp_as_number */
969 0, /* tp_as_sequence */
970 0, /* tp_as_mapping */
971 0, /* tp_hash */
972 0, /* tp_call */
973 0, /* tp_str */
974 PyObject_GenericGetAttr, /* tp_getattro */
975 0, /* tp_setattro */
976 0, /* tp_as_buffer */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000977 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
978 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000979 fileio_doc, /* tp_doc */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000980 (traverseproc)fileio_traverse, /* tp_traverse */
981 (inquiry)fileio_clear, /* tp_clear */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000982 0, /* tp_richcompare */
983 offsetof(PyFileIOObject, weakreflist), /* tp_weaklistoffset */
984 0, /* tp_iter */
985 0, /* tp_iternext */
986 fileio_methods, /* tp_methods */
987 0, /* tp_members */
Guido van Rossumb0428152007-04-08 17:44:42 +0000988 fileio_getsetlist, /* tp_getset */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000989 0, /* tp_base */
990 0, /* tp_dict */
991 0, /* tp_descr_get */
992 0, /* tp_descr_set */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000993 offsetof(PyFileIOObject, dict), /* tp_dictoffset */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000994 fileio_init, /* tp_init */
995 PyType_GenericAlloc, /* tp_alloc */
996 fileio_new, /* tp_new */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000997 PyObject_GC_Del, /* tp_free */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000998};