blob: c6b97fbaf61fbdf71884f9b7738a11ac86f8b9aa [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
Antoine Pitroua28fcfd2009-03-13 23:42:55 +000069static PyObject *portable_lseek(int fd, PyObject *posobj, int whence);
70
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000071/* Returns 0 on success, -1 with exception set on failure. */
Neal Norwitz88b44da2007-08-12 17:23:54 +000072static int
73internal_close(PyFileIOObject *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +000074{
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000075 int err = 0;
Benjamin Peterson7aedf112009-01-19 15:19:46 +000076 int save_errno = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +000077 if (self->fd >= 0) {
Guido van Rossumb0428152007-04-08 17:44:42 +000078 int fd = self->fd;
79 self->fd = -1;
Kristján Valur Jónssondc7c1282009-03-24 13:21:53 +000080 /* fd is accessible and someone else may have closed it */
81 if (_PyVerify_fd(fd)) {
82 Py_BEGIN_ALLOW_THREADS
83 err = close(fd);
84 if (err < 0)
85 save_errno = errno;
86 Py_END_ALLOW_THREADS
Kristján Valur Jónsson8915e0e2009-03-24 13:51:36 +000087 } else {
88 save_errno = errno;
89 err = -1;
90 }
Neal Norwitz88b44da2007-08-12 17:23:54 +000091 }
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000092 if (err < 0) {
93 errno = save_errno;
94 PyErr_SetFromErrno(PyExc_IOError);
95 return -1;
96 }
97 return 0;
Neal Norwitz88b44da2007-08-12 17:23:54 +000098}
99
100static PyObject *
101fileio_close(PyFileIOObject *self)
102{
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000103 if (!self->closefd) {
Christian Heimesecc42a22008-11-05 19:30:32 +0000104 self->fd = -1;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000105 Py_RETURN_NONE;
106 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000107 errno = internal_close(self);
Antoine Pitrou0ae29cf2009-03-13 22:33:17 +0000108 if (errno < 0)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000109 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000110
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000111 return PyObject_CallMethod((PyObject*)&PyRawIOBase_Type,
112 "close", "O", self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000113}
114
115static PyObject *
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000116fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000117{
118 PyFileIOObject *self;
119
120 assert(type != NULL && type->tp_alloc != NULL);
121
122 self = (PyFileIOObject *) type->tp_alloc(type, 0);
123 if (self != NULL) {
124 self->fd = -1;
Christian Heimesdf32b392008-10-30 21:23:35 +0000125 self->readable = 0;
126 self->writable = 0;
127 self->seekable = -1;
128 self->closefd = 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000129 self->weakreflist = NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000130 }
131
132 return (PyObject *) self;
133}
134
135/* On Unix, open will succeed for directories.
136 In Python, there should be no file objects referring to
137 directories, so we need a check. */
138
139static int
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000140dircheck(PyFileIOObject* self, const char *name)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000141{
142#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
143 struct stat buf;
144 if (self->fd < 0)
145 return 0;
146 if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000147 char *msg = strerror(EISDIR);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000148 PyObject *exc;
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +0000149 if (internal_close(self))
150 return -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000151
Benjamin Peterson1efc23c2008-12-29 18:02:28 +0000152 exc = PyObject_CallFunction(PyExc_IOError, "(iss)",
153 EISDIR, msg, name);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000154 PyErr_SetObject(PyExc_IOError, exc);
155 Py_XDECREF(exc);
156 return -1;
157 }
158#endif
159 return 0;
160}
161
Benjamin Peterson806d4022009-01-19 15:11:51 +0000162static int
163check_fd(int fd)
164{
165#if defined(HAVE_FSTAT)
166 struct stat buf;
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000167 if (!_PyVerify_fd(fd) || (fstat(fd, &buf) < 0 && errno == EBADF)) {
Benjamin Peterson806d4022009-01-19 15:11:51 +0000168 PyObject *exc;
169 char *msg = strerror(EBADF);
170 exc = PyObject_CallFunction(PyExc_OSError, "(is)",
171 EBADF, msg);
172 PyErr_SetObject(PyExc_OSError, exc);
173 Py_XDECREF(exc);
174 return -1;
175 }
176#endif
177 return 0;
178}
179
Guido van Rossuma9e20242007-03-08 00:43:48 +0000180
181static int
182fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
183{
184 PyFileIOObject *self = (PyFileIOObject *) oself;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000185 static char *kwlist[] = {"file", "mode", "closefd", NULL};
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000186 const char *name = NULL;
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000187 PyObject *nameobj, *stringobj = NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000188 char *mode = "r";
Guido van Rossum53807da2007-04-10 19:01:47 +0000189 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000190#ifdef MS_WINDOWS
191 Py_UNICODE *widename = NULL;
192#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000193 int ret = 0;
194 int rwa = 0, plus = 0, append = 0;
195 int flags = 0;
Guido van Rossumb0428152007-04-08 17:44:42 +0000196 int fd = -1;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000197 int closefd = 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000198
199 assert(PyFileIO_Check(oself));
Neal Norwitz88b44da2007-08-12 17:23:54 +0000200 if (self->fd >= 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000201 /* Have to close the existing file first. */
Neal Norwitz88b44da2007-08-12 17:23:54 +0000202 if (internal_close(self) < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000203 return -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000204 }
205
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000206 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:fileio",
207 kwlist, &nameobj, &mode, &closefd))
208 return -1;
209
210 if (PyFloat_Check(nameobj)) {
211 PyErr_SetString(PyExc_TypeError,
212 "integer argument expected, got float");
213 return -1;
214 }
215
216 fd = PyLong_AsLong(nameobj);
217 if (fd < 0) {
218 if (!PyErr_Occurred()) {
Guido van Rossumb0428152007-04-08 17:44:42 +0000219 PyErr_SetString(PyExc_ValueError,
220 "Negative filedescriptor");
221 return -1;
222 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000223 PyErr_Clear();
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000224 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000225
Guido van Rossuma9e20242007-03-08 00:43:48 +0000226#ifdef Py_WIN_WIDE_FILENAMES
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000227 if (GetVersion() < 0x80000000) {
Guido van Rossumb0428152007-04-08 17:44:42 +0000228 /* On NT, so wide API available */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000229 if (PyUnicode_Check(nameobj))
230 widename = PyUnicode_AS_UNICODE(nameobj);
231 }
232 if (widename == NULL)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000233#endif
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000234 if (fd < 0)
235 {
236 if (PyBytes_Check(nameobj) || PyByteArray_Check(nameobj)) {
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000237 Py_ssize_t namelen;
238 if (PyObject_AsCharBuffer(nameobj, &name, &namelen) < 0)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000239 return -1;
240 }
241 else {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000242 PyObject *u = PyUnicode_FromObject(nameobj);
243
244 if (u == NULL)
245 return -1;
246
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000247 stringobj = PyUnicode_AsEncodedString(
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000248 u, Py_FileSystemDefaultEncoding, NULL);
249 Py_DECREF(u);
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000250 if (stringobj == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000251 return -1;
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000252 if (!PyBytes_Check(stringobj)) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000253 PyErr_SetString(PyExc_TypeError,
254 "encoder failed to return bytes");
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000255 goto error;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000256 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000257 name = PyBytes_AS_STRING(stringobj);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000258 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000259 }
260
Guido van Rossum53807da2007-04-10 19:01:47 +0000261 s = mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000262 while (*s) {
263 switch (*s++) {
264 case 'r':
265 if (rwa) {
266 bad_mode:
267 PyErr_SetString(PyExc_ValueError,
268 "Must have exactly one of read/write/append mode");
269 goto error;
270 }
271 rwa = 1;
272 self->readable = 1;
273 break;
274 case 'w':
275 if (rwa)
276 goto bad_mode;
277 rwa = 1;
278 self->writable = 1;
279 flags |= O_CREAT | O_TRUNC;
280 break;
281 case 'a':
282 if (rwa)
283 goto bad_mode;
284 rwa = 1;
285 self->writable = 1;
286 flags |= O_CREAT;
287 append = 1;
288 break;
Benjamin Peterson44309e62008-11-22 00:41:45 +0000289 case 'b':
290 break;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000291 case '+':
292 if (plus)
293 goto bad_mode;
294 self->readable = self->writable = 1;
295 plus = 1;
296 break;
297 default:
298 PyErr_Format(PyExc_ValueError,
299 "invalid mode: %.200s", mode);
300 goto error;
301 }
302 }
303
304 if (!rwa)
305 goto bad_mode;
306
307 if (self->readable && self->writable)
308 flags |= O_RDWR;
309 else if (self->readable)
310 flags |= O_RDONLY;
311 else
312 flags |= O_WRONLY;
313
314#ifdef O_BINARY
315 flags |= O_BINARY;
316#endif
317
Walter Dörwald0e411482007-06-06 16:55:38 +0000318#ifdef O_APPEND
319 if (append)
320 flags |= O_APPEND;
321#endif
322
Guido van Rossumb0428152007-04-08 17:44:42 +0000323 if (fd >= 0) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000324 if (check_fd(fd))
325 goto error;
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000326 self->fd = fd;
327 self->closefd = closefd;
Guido van Rossumb0428152007-04-08 17:44:42 +0000328 }
329 else {
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000330 self->closefd = 1;
331 if (!closefd) {
332 PyErr_SetString(PyExc_ValueError,
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000333 "Cannot use closefd=False with file name");
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000334 goto error;
335 }
336
Guido van Rossumb0428152007-04-08 17:44:42 +0000337 Py_BEGIN_ALLOW_THREADS
338 errno = 0;
Thomas Helleraf2be262007-07-12 11:03:13 +0000339#ifdef MS_WINDOWS
340 if (widename != NULL)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000341 self->fd = _wopen(widename, flags, 0666);
Thomas Helleraf2be262007-07-12 11:03:13 +0000342 else
343#endif
Neal Norwitz88b44da2007-08-12 17:23:54 +0000344 self->fd = open(name, flags, 0666);
Guido van Rossumb0428152007-04-08 17:44:42 +0000345 Py_END_ALLOW_THREADS
Benjamin Peterson3e4f0552008-09-02 00:31:15 +0000346 if (self->fd < 0) {
Christian Heimes0b489542007-10-31 19:20:48 +0000347#ifdef MS_WINDOWS
Hirokazu Yamamoto0f22d692009-01-01 16:03:45 +0000348 if (widename != NULL)
349 PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename);
350 else
Christian Heimes0b489542007-10-31 19:20:48 +0000351#endif
Hirokazu Yamamoto0f22d692009-01-01 16:03:45 +0000352 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
Guido van Rossumb0428152007-04-08 17:44:42 +0000353 goto error;
354 }
Benjamin Peterson1efc23c2008-12-29 18:02:28 +0000355 if(dircheck(self, name) < 0)
Benjamin Peterson3e4f0552008-09-02 00:31:15 +0000356 goto error;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000357 }
358
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000359 if (PyObject_SetAttrString((PyObject *)self, "name", nameobj) < 0)
360 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000361
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000362 if (append) {
363 /* For consistent behaviour, we explicitly seek to the
364 end of file (otherwise, it might be done only on the
365 first write()). */
366 PyObject *pos = portable_lseek(self->fd, NULL, 2);
367 if (pos == NULL)
368 goto error;
369 Py_DECREF(pos);
370 }
371
Guido van Rossuma9e20242007-03-08 00:43:48 +0000372 goto done;
373
374 error:
375 ret = -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000376
Guido van Rossuma9e20242007-03-08 00:43:48 +0000377 done:
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000378 Py_CLEAR(stringobj);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000379 return ret;
380}
381
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000382static int
383fileio_traverse(PyFileIOObject *self, visitproc visit, void *arg)
384{
385 Py_VISIT(self->dict);
386 return 0;
387}
388
389static int
390fileio_clear(PyFileIOObject *self)
391{
392 Py_CLEAR(self->dict);
393 return 0;
394}
395
Guido van Rossuma9e20242007-03-08 00:43:48 +0000396static void
397fileio_dealloc(PyFileIOObject *self)
398{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000399 if (_PyIOBase_finalize((PyObject *) self) < 0)
400 return;
401 _PyObject_GC_UNTRACK(self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000402 if (self->weakreflist != NULL)
403 PyObject_ClearWeakRefs((PyObject *) self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000404 Py_CLEAR(self->dict);
Christian Heimes90aa7642007-12-19 02:45:37 +0000405 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000406}
407
408static PyObject *
409err_closed(void)
410{
411 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
412 return NULL;
413}
414
415static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000416err_mode(char *action)
417{
418 PyErr_Format(PyExc_ValueError, "File not open for %s", action);
419 return NULL;
420}
421
422static PyObject *
Guido van Rossuma9e20242007-03-08 00:43:48 +0000423fileio_fileno(PyFileIOObject *self)
424{
425 if (self->fd < 0)
426 return err_closed();
Christian Heimes217cfd12007-12-02 14:31:20 +0000427 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000428}
429
430static PyObject *
431fileio_readable(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->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000436}
437
438static PyObject *
439fileio_writable(PyFileIOObject *self)
440{
441 if (self->fd < 0)
442 return err_closed();
Neal Norwitz88b44da2007-08-12 17:23:54 +0000443 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000444}
445
446static PyObject *
447fileio_seekable(PyFileIOObject *self)
448{
449 if (self->fd < 0)
450 return err_closed();
451 if (self->seekable < 0) {
Antoine Pitroua28fcfd2009-03-13 23:42:55 +0000452 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
453 if (pos == NULL) {
454 PyErr_Clear();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000455 self->seekable = 0;
Antoine Pitroua28fcfd2009-03-13 23:42:55 +0000456 } else {
457 Py_DECREF(pos);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000458 self->seekable = 1;
Antoine Pitroua28fcfd2009-03-13 23:42:55 +0000459 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000460 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000461 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000462}
463
464static PyObject *
465fileio_readinto(PyFileIOObject *self, PyObject *args)
466{
Martin v. Löwis423be952008-08-13 15:53:07 +0000467 Py_buffer pbuf;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000468 Py_ssize_t n;
Guido van Rossum53807da2007-04-10 19:01:47 +0000469
Guido van Rossuma9e20242007-03-08 00:43:48 +0000470 if (self->fd < 0)
471 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000472 if (!self->readable)
473 return err_mode("reading");
474
Martin v. Löwis423be952008-08-13 15:53:07 +0000475 if (!PyArg_ParseTuple(args, "w*", &pbuf))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000476 return NULL;
477
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000478 if (_PyVerify_fd(self->fd)) {
479 Py_BEGIN_ALLOW_THREADS
480 errno = 0;
481 n = read(self->fd, pbuf.buf, pbuf.len);
482 Py_END_ALLOW_THREADS
483 } else
484 n = -1;
Martin v. Löwis423be952008-08-13 15:53:07 +0000485 PyBuffer_Release(&pbuf);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000486 if (n < 0) {
487 if (errno == EAGAIN)
488 Py_RETURN_NONE;
489 PyErr_SetFromErrno(PyExc_IOError);
490 return NULL;
491 }
492
Christian Heimes217cfd12007-12-02 14:31:20 +0000493 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000494}
495
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000496static size_t
497new_buffersize(PyFileIOObject *self, size_t currentsize)
498{
499#ifdef HAVE_FSTAT
500 off_t pos, end;
501 struct stat st;
502 if (fstat(self->fd, &st) == 0) {
503 end = st.st_size;
504 pos = lseek(self->fd, 0L, SEEK_CUR);
Antoine Pitrou66994e12009-03-29 00:45:26 +0000505 /* Files claiming a size smaller than SMALLCHUNK may
506 actually be streaming pseudo-files. In this case, we
507 apply the more aggressive algorithm below.
508 */
Antoine Pitrou9041daa2009-03-29 01:09:51 +0000509 if (end >= SMALLCHUNK && end >= pos && pos >= 0) {
Antoine Pitrou66994e12009-03-29 00:45:26 +0000510 /* Add 1 so if the file were to grow we'd notice. */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000511 return currentsize + end - pos + 1;
Antoine Pitrou66994e12009-03-29 00:45:26 +0000512 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000513 }
514#endif
515 if (currentsize > SMALLCHUNK) {
516 /* Keep doubling until we reach BIGCHUNK;
517 then keep adding BIGCHUNK. */
518 if (currentsize <= BIGCHUNK)
519 return currentsize + currentsize;
520 else
521 return currentsize + BIGCHUNK;
522 }
523 return currentsize + SMALLCHUNK;
524}
525
Guido van Rossum7165cb12007-07-10 06:54:34 +0000526static PyObject *
527fileio_readall(PyFileIOObject *self)
528{
529 PyObject *result;
530 Py_ssize_t total = 0;
531 int n;
532
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000533 if (!_PyVerify_fd(self->fd))
534 return PyErr_SetFromErrno(PyExc_IOError);
535
Christian Heimesa872de52008-12-05 08:26:55 +0000536 result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK);
Guido van Rossum7165cb12007-07-10 06:54:34 +0000537 if (result == NULL)
538 return NULL;
539
540 while (1) {
Antoine Pitrou66994e12009-03-29 00:45:26 +0000541 size_t newsize = new_buffersize(self, total);
542 if (newsize > PY_SSIZE_T_MAX || newsize <= 0) {
543 PyErr_SetString(PyExc_OverflowError,
544 "unbounded read returned more bytes "
545 "than a Python string can hold ");
546 return NULL;
547 }
Christian Heimesa872de52008-12-05 08:26:55 +0000548
Christian Heimes72b710a2008-05-26 13:28:38 +0000549 if (PyBytes_GET_SIZE(result) < newsize) {
550 if (_PyBytes_Resize(&result, newsize) < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000551 if (total == 0) {
552 Py_DECREF(result);
553 return NULL;
554 }
555 PyErr_Clear();
556 break;
557 }
558 }
559 Py_BEGIN_ALLOW_THREADS
560 errno = 0;
561 n = read(self->fd,
Christian Heimes72b710a2008-05-26 13:28:38 +0000562 PyBytes_AS_STRING(result) + total,
Guido van Rossum7165cb12007-07-10 06:54:34 +0000563 newsize - total);
564 Py_END_ALLOW_THREADS
565 if (n == 0)
566 break;
567 if (n < 0) {
568 if (total > 0)
569 break;
570 if (errno == EAGAIN) {
571 Py_DECREF(result);
572 Py_RETURN_NONE;
573 }
574 Py_DECREF(result);
575 PyErr_SetFromErrno(PyExc_IOError);
576 return NULL;
577 }
578 total += n;
579 }
580
Christian Heimes72b710a2008-05-26 13:28:38 +0000581 if (PyBytes_GET_SIZE(result) > total) {
582 if (_PyBytes_Resize(&result, total) < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000583 /* This should never happen, but just in case */
584 Py_DECREF(result);
585 return NULL;
586 }
587 }
588 return result;
589}
590
Guido van Rossuma9e20242007-03-08 00:43:48 +0000591static PyObject *
592fileio_read(PyFileIOObject *self, PyObject *args)
593{
594 char *ptr;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000595 Py_ssize_t n;
596 Py_ssize_t size = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000597 PyObject *bytes;
598
599 if (self->fd < 0)
600 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000601 if (!self->readable)
602 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000603
Neal Norwitz3c8ba932007-08-08 04:36:17 +0000604 if (!PyArg_ParseTuple(args, "|n", &size))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000605 return NULL;
606
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000607 if (size < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000608 return fileio_readall(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000609 }
610
Christian Heimes72b710a2008-05-26 13:28:38 +0000611 bytes = PyBytes_FromStringAndSize(NULL, size);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000612 if (bytes == NULL)
613 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +0000614 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000615
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000616 if (_PyVerify_fd(self->fd)) {
617 Py_BEGIN_ALLOW_THREADS
618 errno = 0;
619 n = read(self->fd, ptr, size);
620 Py_END_ALLOW_THREADS
621 } else
622 n = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000623
624 if (n < 0) {
625 if (errno == EAGAIN)
626 Py_RETURN_NONE;
627 PyErr_SetFromErrno(PyExc_IOError);
628 return NULL;
629 }
630
631 if (n != size) {
Christian Heimes72b710a2008-05-26 13:28:38 +0000632 if (_PyBytes_Resize(&bytes, n) < 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000633 Py_DECREF(bytes);
Guido van Rossum53807da2007-04-10 19:01:47 +0000634 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000635 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000636 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000637
638 return (PyObject *) bytes;
639}
640
641static PyObject *
642fileio_write(PyFileIOObject *self, PyObject *args)
643{
Martin v. Löwis423be952008-08-13 15:53:07 +0000644 Py_buffer pbuf;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000645 Py_ssize_t n;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000646
647 if (self->fd < 0)
648 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000649 if (!self->writable)
650 return err_mode("writing");
651
Martin v. Löwis423be952008-08-13 15:53:07 +0000652 if (!PyArg_ParseTuple(args, "s*", &pbuf))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000653 return NULL;
654
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000655 if (_PyVerify_fd(self->fd)) {
656 Py_BEGIN_ALLOW_THREADS
657 errno = 0;
658 n = write(self->fd, pbuf.buf, pbuf.len);
659 Py_END_ALLOW_THREADS
660 } else
661 n = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000662
Martin v. Löwis423be952008-08-13 15:53:07 +0000663 PyBuffer_Release(&pbuf);
664
Guido van Rossuma9e20242007-03-08 00:43:48 +0000665 if (n < 0) {
666 if (errno == EAGAIN)
667 Py_RETURN_NONE;
668 PyErr_SetFromErrno(PyExc_IOError);
669 return NULL;
670 }
671
Christian Heimes217cfd12007-12-02 14:31:20 +0000672 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000673}
674
Guido van Rossum53807da2007-04-10 19:01:47 +0000675/* XXX Windows support below is likely incomplete */
676
Guido van Rossum53807da2007-04-10 19:01:47 +0000677/* Cribbed from posix_lseek() */
678static PyObject *
679portable_lseek(int fd, PyObject *posobj, int whence)
680{
681 Py_off_t pos, res;
682
683#ifdef SEEK_SET
684 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
685 switch (whence) {
686#if SEEK_SET != 0
687 case 0: whence = SEEK_SET; break;
688#endif
689#if SEEK_CUR != 1
690 case 1: whence = SEEK_CUR; break;
691#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000692#if SEEK_END != 2
Guido van Rossum53807da2007-04-10 19:01:47 +0000693 case 2: whence = SEEK_END; break;
694#endif
695 }
696#endif /* SEEK_SET */
697
698 if (posobj == NULL)
699 pos = 0;
700 else {
Christian Heimes8e42a0a2007-11-08 18:04:45 +0000701 if(PyFloat_Check(posobj)) {
702 PyErr_SetString(PyExc_TypeError, "an integer is required");
703 return NULL;
704 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000705#if defined(HAVE_LARGEFILE_SUPPORT)
706 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000707#else
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000708 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000709#endif
710 if (PyErr_Occurred())
711 return NULL;
712 }
713
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000714 if (_PyVerify_fd(fd)) {
715 Py_BEGIN_ALLOW_THREADS
Guido van Rossum53807da2007-04-10 19:01:47 +0000716#if defined(MS_WIN64) || defined(MS_WINDOWS)
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000717 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000718#else
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000719 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000720#endif
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000721 Py_END_ALLOW_THREADS
722 } else
723 res = -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000724 if (res < 0)
725 return PyErr_SetFromErrno(PyExc_IOError);
726
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000727#if defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum53807da2007-04-10 19:01:47 +0000728 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000729#else
730 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000731#endif
732}
733
Guido van Rossuma9e20242007-03-08 00:43:48 +0000734static PyObject *
735fileio_seek(PyFileIOObject *self, PyObject *args)
736{
Guido van Rossum53807da2007-04-10 19:01:47 +0000737 PyObject *posobj;
738 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000739
740 if (self->fd < 0)
741 return err_closed();
742
Guido van Rossum53807da2007-04-10 19:01:47 +0000743 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000744 return NULL;
745
Guido van Rossum53807da2007-04-10 19:01:47 +0000746 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000747}
748
749static PyObject *
750fileio_tell(PyFileIOObject *self, PyObject *args)
751{
Guido van Rossuma9e20242007-03-08 00:43:48 +0000752 if (self->fd < 0)
753 return err_closed();
754
Guido van Rossum53807da2007-04-10 19:01:47 +0000755 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000756}
757
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000758#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000759static PyObject *
760fileio_truncate(PyFileIOObject *self, PyObject *args)
761{
Guido van Rossum53807da2007-04-10 19:01:47 +0000762 PyObject *posobj = NULL;
763 Py_off_t pos;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000764 int ret;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000765 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000766
Guido van Rossum53807da2007-04-10 19:01:47 +0000767 fd = self->fd;
768 if (fd < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000769 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000770 if (!self->writable)
771 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000772
Guido van Rossum53807da2007-04-10 19:01:47 +0000773 if (!PyArg_ParseTuple(args, "|O", &posobj))
774 return NULL;
775
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000776 if (posobj == Py_None || posobj == NULL) {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000777 /* Get the current position. */
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000778 posobj = portable_lseek(fd, NULL, 1);
779 if (posobj == NULL)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000780 return NULL;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000781 }
782 else {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000783 /* Move to the position to be truncated. */
784 posobj = portable_lseek(fd, posobj, 0);
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000785 }
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000786 if (posobj == NULL)
787 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000788
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000789#if defined(HAVE_LARGEFILE_SUPPORT)
790 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000791#else
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000792 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000793#endif
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000794 if (pos == -1 && PyErr_Occurred())
Guido van Rossum53807da2007-04-10 19:01:47 +0000795 return NULL;
796
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000797#ifdef MS_WINDOWS
798 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
799 so don't even try using it. */
800 {
801 HANDLE hFile;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000802
803 /* Truncate. Note that this may grow the file! */
804 Py_BEGIN_ALLOW_THREADS
805 errno = 0;
806 hFile = (HANDLE)_get_osfhandle(fd);
807 ret = hFile == (HANDLE)-1;
808 if (ret == 0) {
809 ret = SetEndOfFile(hFile) == 0;
810 if (ret)
811 errno = EACCES;
812 }
813 Py_END_ALLOW_THREADS
814 }
815#else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000816 Py_BEGIN_ALLOW_THREADS
817 errno = 0;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000818 ret = ftruncate(fd, pos);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000819 Py_END_ALLOW_THREADS
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000820#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000821
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000822 if (ret != 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000823 PyErr_SetFromErrno(PyExc_IOError);
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000824 return NULL;
Guido van Rossum87429772007-04-10 21:06:59 +0000825 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000826
Guido van Rossum87429772007-04-10 21:06:59 +0000827 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000828}
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000829#endif
Guido van Rossum53807da2007-04-10 19:01:47 +0000830
831static char *
832mode_string(PyFileIOObject *self)
833{
834 if (self->readable) {
835 if (self->writable)
Benjamin Peterson44309e62008-11-22 00:41:45 +0000836 return "rb+";
Guido van Rossum53807da2007-04-10 19:01:47 +0000837 else
Benjamin Peterson44309e62008-11-22 00:41:45 +0000838 return "rb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000839 }
840 else
Benjamin Peterson44309e62008-11-22 00:41:45 +0000841 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000842}
Guido van Rossuma9e20242007-03-08 00:43:48 +0000843
844static PyObject *
845fileio_repr(PyFileIOObject *self)
846{
Guido van Rossum53807da2007-04-10 19:01:47 +0000847 if (self->fd < 0)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000848 return PyUnicode_FromFormat("io.FileIO(-1)");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000849
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000850 return PyUnicode_FromFormat("io.FileIO(%d, '%s')",
Guido van Rossum53807da2007-04-10 19:01:47 +0000851 self->fd, mode_string(self));
Guido van Rossuma9e20242007-03-08 00:43:48 +0000852}
853
854static PyObject *
855fileio_isatty(PyFileIOObject *self)
856{
857 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000858
Guido van Rossuma9e20242007-03-08 00:43:48 +0000859 if (self->fd < 0)
860 return err_closed();
861 Py_BEGIN_ALLOW_THREADS
862 res = isatty(self->fd);
863 Py_END_ALLOW_THREADS
864 return PyBool_FromLong(res);
865}
866
Guido van Rossuma9e20242007-03-08 00:43:48 +0000867
868PyDoc_STRVAR(fileio_doc,
869"file(name: str[, mode: str]) -> file IO object\n"
870"\n"
871"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
872"writing or appending. The file will be created if it doesn't exist\n"
873"when opened for writing or appending; it will be truncated when\n"
874"opened for writing. Add a '+' to the mode to allow simultaneous\n"
875"reading and writing.");
876
877PyDoc_STRVAR(read_doc,
878"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
879"\n"
880"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +0000881"In non-blocking mode, returns None if no data is available.\n"
882"On end-of-file, returns ''.");
883
884PyDoc_STRVAR(readall_doc,
885"readall() -> bytes. read all data from the file, returned as bytes.\n"
886"\n"
887"In non-blocking mode, returns as much as is immediately available,\n"
888"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000889
890PyDoc_STRVAR(write_doc,
891"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
892"\n"
893"Only makes one system call, so not all of the data may be written.\n"
894"The number of bytes actually written is returned.");
895
896PyDoc_STRVAR(fileno_doc,
897"fileno() -> int. \"file descriptor\".\n"
898"\n"
899"This is needed for lower-level file interfaces, such the fcntl module.");
900
901PyDoc_STRVAR(seek_doc,
902"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
903"\n"
904"Argument offset is a byte count. Optional argument whence defaults to\n"
905"0 (offset from start of file, offset should be >= 0); other values are 1\n"
906"(move relative to current position, positive or negative), and 2 (move\n"
907"relative to end of file, usually negative, although many platforms allow\n"
908"seeking beyond the end of a file)."
909"\n"
910"Note that not all file objects are seekable.");
911
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000912#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000913PyDoc_STRVAR(truncate_doc,
914"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
915"\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000916"Size defaults to the current file position, as returned by tell()."
917"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000918#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000919
920PyDoc_STRVAR(tell_doc,
921"tell() -> int. Current file position");
922
923PyDoc_STRVAR(readinto_doc,
Benjamin Peterson9a8082f2009-03-05 00:55:56 +0000924"readinto() -> Same as RawIOBase.readinto().");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000925
926PyDoc_STRVAR(close_doc,
927"close() -> None. Close the file.\n"
928"\n"
929"A closed file cannot be used for further I/O operations. close() may be\n"
930"called more than once without error. Changes the fileno to -1.");
931
932PyDoc_STRVAR(isatty_doc,
933"isatty() -> bool. True if the file is connected to a tty device.");
934
Guido van Rossuma9e20242007-03-08 00:43:48 +0000935PyDoc_STRVAR(seekable_doc,
936"seekable() -> bool. True if file supports random-access.");
937
938PyDoc_STRVAR(readable_doc,
939"readable() -> bool. True if file was opened in a read mode.");
940
941PyDoc_STRVAR(writable_doc,
942"writable() -> bool. True if file was opened in a write mode.");
943
944static PyMethodDef fileio_methods[] = {
945 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
Guido van Rossum7165cb12007-07-10 06:54:34 +0000946 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000947 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
948 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
949 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
950 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000951#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000952 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000953#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000954 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
955 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
956 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
957 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
958 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
959 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000960 {NULL, NULL} /* sentinel */
961};
962
Guido van Rossum53807da2007-04-10 19:01:47 +0000963/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
964
Guido van Rossumb0428152007-04-08 17:44:42 +0000965static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000966get_closed(PyFileIOObject *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +0000967{
Guido van Rossum53807da2007-04-10 19:01:47 +0000968 return PyBool_FromLong((long)(self->fd < 0));
969}
970
971static PyObject *
Christian Heimesecc42a22008-11-05 19:30:32 +0000972get_closefd(PyFileIOObject *self, void *closure)
973{
974 return PyBool_FromLong((long)(self->closefd));
975}
976
977static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000978get_mode(PyFileIOObject *self, void *closure)
979{
Guido van Rossumc43e79f2007-06-18 18:26:36 +0000980 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +0000981}
982
983static PyGetSetDef fileio_getsetlist[] = {
984 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Christian Heimesecc42a22008-11-05 19:30:32 +0000985 {"closefd", (getter)get_closefd, NULL,
986 "True if the file descriptor will be closed"},
Guido van Rossum53807da2007-04-10 19:01:47 +0000987 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
Guido van Rossumb0428152007-04-08 17:44:42 +0000988 {0},
989};
990
Guido van Rossuma9e20242007-03-08 00:43:48 +0000991PyTypeObject PyFileIO_Type = {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000992 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000993 "_io.FileIO",
Guido van Rossuma9e20242007-03-08 00:43:48 +0000994 sizeof(PyFileIOObject),
995 0,
996 (destructor)fileio_dealloc, /* tp_dealloc */
997 0, /* tp_print */
998 0, /* tp_getattr */
999 0, /* tp_setattr */
Antoine Pitrou7fb111b2009-03-04 11:14:01 +00001000 0, /* tp_reserved */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001001 (reprfunc)fileio_repr, /* tp_repr */
1002 0, /* tp_as_number */
1003 0, /* tp_as_sequence */
1004 0, /* tp_as_mapping */
1005 0, /* tp_hash */
1006 0, /* tp_call */
1007 0, /* tp_str */
1008 PyObject_GenericGetAttr, /* tp_getattro */
1009 0, /* tp_setattro */
1010 0, /* tp_as_buffer */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001011 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
1012 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001013 fileio_doc, /* tp_doc */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001014 (traverseproc)fileio_traverse, /* tp_traverse */
1015 (inquiry)fileio_clear, /* tp_clear */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001016 0, /* tp_richcompare */
1017 offsetof(PyFileIOObject, weakreflist), /* tp_weaklistoffset */
1018 0, /* tp_iter */
1019 0, /* tp_iternext */
1020 fileio_methods, /* tp_methods */
1021 0, /* tp_members */
Guido van Rossumb0428152007-04-08 17:44:42 +00001022 fileio_getsetlist, /* tp_getset */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001023 0, /* tp_base */
1024 0, /* tp_dict */
1025 0, /* tp_descr_get */
1026 0, /* tp_descr_set */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001027 offsetof(PyFileIOObject, dict), /* tp_dictoffset */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001028 fileio_init, /* tp_init */
1029 PyType_GenericAlloc, /* tp_alloc */
1030 fileio_new, /* tp_new */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001031 PyObject_GC_Del, /* tp_free */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001032};