blob: 555dc12c69dd5a354683786c2bd68bed35336c4c [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(
Martin v. Löwis43c57782009-05-10 08:15:24 +0000248 u, Py_FileSystemDefaultEncoding, "surrogateescape");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000249 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 ");
Antoine Pitrou7d037a72009-03-29 18:55:12 +0000546 Py_DECREF(result);
Antoine Pitrou66994e12009-03-29 00:45:26 +0000547 return NULL;
548 }
Christian Heimesa872de52008-12-05 08:26:55 +0000549
Christian Heimes72b710a2008-05-26 13:28:38 +0000550 if (PyBytes_GET_SIZE(result) < newsize) {
551 if (_PyBytes_Resize(&result, newsize) < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000552 if (total == 0) {
553 Py_DECREF(result);
554 return NULL;
555 }
556 PyErr_Clear();
557 break;
558 }
559 }
560 Py_BEGIN_ALLOW_THREADS
561 errno = 0;
562 n = read(self->fd,
Christian Heimes72b710a2008-05-26 13:28:38 +0000563 PyBytes_AS_STRING(result) + total,
Guido van Rossum7165cb12007-07-10 06:54:34 +0000564 newsize - total);
565 Py_END_ALLOW_THREADS
566 if (n == 0)
567 break;
568 if (n < 0) {
569 if (total > 0)
570 break;
571 if (errno == EAGAIN) {
572 Py_DECREF(result);
573 Py_RETURN_NONE;
574 }
575 Py_DECREF(result);
576 PyErr_SetFromErrno(PyExc_IOError);
577 return NULL;
578 }
579 total += n;
580 }
581
Christian Heimes72b710a2008-05-26 13:28:38 +0000582 if (PyBytes_GET_SIZE(result) > total) {
583 if (_PyBytes_Resize(&result, total) < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000584 /* This should never happen, but just in case */
585 Py_DECREF(result);
586 return NULL;
587 }
588 }
589 return result;
590}
591
Guido van Rossuma9e20242007-03-08 00:43:48 +0000592static PyObject *
593fileio_read(PyFileIOObject *self, PyObject *args)
594{
595 char *ptr;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000596 Py_ssize_t n;
597 Py_ssize_t size = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000598 PyObject *bytes;
599
600 if (self->fd < 0)
601 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000602 if (!self->readable)
603 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000604
Neal Norwitz3c8ba932007-08-08 04:36:17 +0000605 if (!PyArg_ParseTuple(args, "|n", &size))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000606 return NULL;
607
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000608 if (size < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000609 return fileio_readall(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000610 }
611
Christian Heimes72b710a2008-05-26 13:28:38 +0000612 bytes = PyBytes_FromStringAndSize(NULL, size);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000613 if (bytes == NULL)
614 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +0000615 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000616
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000617 if (_PyVerify_fd(self->fd)) {
618 Py_BEGIN_ALLOW_THREADS
619 errno = 0;
620 n = read(self->fd, ptr, size);
621 Py_END_ALLOW_THREADS
622 } else
623 n = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000624
625 if (n < 0) {
Antoine Pitrou8e21fb22009-03-29 18:40:13 +0000626 Py_DECREF(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000627 if (errno == EAGAIN)
628 Py_RETURN_NONE;
629 PyErr_SetFromErrno(PyExc_IOError);
630 return NULL;
631 }
632
633 if (n != size) {
Christian Heimes72b710a2008-05-26 13:28:38 +0000634 if (_PyBytes_Resize(&bytes, n) < 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000635 Py_DECREF(bytes);
Guido van Rossum53807da2007-04-10 19:01:47 +0000636 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000637 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000638 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000639
640 return (PyObject *) bytes;
641}
642
643static PyObject *
644fileio_write(PyFileIOObject *self, PyObject *args)
645{
Martin v. Löwis423be952008-08-13 15:53:07 +0000646 Py_buffer pbuf;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000647 Py_ssize_t n;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000648
649 if (self->fd < 0)
650 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000651 if (!self->writable)
652 return err_mode("writing");
653
Martin v. Löwis423be952008-08-13 15:53:07 +0000654 if (!PyArg_ParseTuple(args, "s*", &pbuf))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000655 return NULL;
656
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000657 if (_PyVerify_fd(self->fd)) {
658 Py_BEGIN_ALLOW_THREADS
659 errno = 0;
660 n = write(self->fd, pbuf.buf, pbuf.len);
661 Py_END_ALLOW_THREADS
662 } else
663 n = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000664
Martin v. Löwis423be952008-08-13 15:53:07 +0000665 PyBuffer_Release(&pbuf);
666
Guido van Rossuma9e20242007-03-08 00:43:48 +0000667 if (n < 0) {
668 if (errno == EAGAIN)
669 Py_RETURN_NONE;
670 PyErr_SetFromErrno(PyExc_IOError);
671 return NULL;
672 }
673
Christian Heimes217cfd12007-12-02 14:31:20 +0000674 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000675}
676
Guido van Rossum53807da2007-04-10 19:01:47 +0000677/* XXX Windows support below is likely incomplete */
678
Guido van Rossum53807da2007-04-10 19:01:47 +0000679/* Cribbed from posix_lseek() */
680static PyObject *
681portable_lseek(int fd, PyObject *posobj, int whence)
682{
683 Py_off_t pos, res;
684
685#ifdef SEEK_SET
686 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
687 switch (whence) {
688#if SEEK_SET != 0
689 case 0: whence = SEEK_SET; break;
690#endif
691#if SEEK_CUR != 1
692 case 1: whence = SEEK_CUR; break;
693#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000694#if SEEK_END != 2
Guido van Rossum53807da2007-04-10 19:01:47 +0000695 case 2: whence = SEEK_END; break;
696#endif
697 }
698#endif /* SEEK_SET */
699
700 if (posobj == NULL)
701 pos = 0;
702 else {
Christian Heimes8e42a0a2007-11-08 18:04:45 +0000703 if(PyFloat_Check(posobj)) {
704 PyErr_SetString(PyExc_TypeError, "an integer is required");
705 return NULL;
706 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000707#if defined(HAVE_LARGEFILE_SUPPORT)
708 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000709#else
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000710 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000711#endif
712 if (PyErr_Occurred())
713 return NULL;
714 }
715
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000716 if (_PyVerify_fd(fd)) {
717 Py_BEGIN_ALLOW_THREADS
Guido van Rossum53807da2007-04-10 19:01:47 +0000718#if defined(MS_WIN64) || defined(MS_WINDOWS)
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000719 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000720#else
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000721 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000722#endif
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000723 Py_END_ALLOW_THREADS
724 } else
725 res = -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000726 if (res < 0)
727 return PyErr_SetFromErrno(PyExc_IOError);
728
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000729#if defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum53807da2007-04-10 19:01:47 +0000730 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000731#else
732 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000733#endif
734}
735
Guido van Rossuma9e20242007-03-08 00:43:48 +0000736static PyObject *
737fileio_seek(PyFileIOObject *self, PyObject *args)
738{
Guido van Rossum53807da2007-04-10 19:01:47 +0000739 PyObject *posobj;
740 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000741
742 if (self->fd < 0)
743 return err_closed();
744
Guido van Rossum53807da2007-04-10 19:01:47 +0000745 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000746 return NULL;
747
Guido van Rossum53807da2007-04-10 19:01:47 +0000748 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000749}
750
751static PyObject *
752fileio_tell(PyFileIOObject *self, PyObject *args)
753{
Guido van Rossuma9e20242007-03-08 00:43:48 +0000754 if (self->fd < 0)
755 return err_closed();
756
Guido van Rossum53807da2007-04-10 19:01:47 +0000757 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000758}
759
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000760#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000761static PyObject *
762fileio_truncate(PyFileIOObject *self, PyObject *args)
763{
Guido van Rossum53807da2007-04-10 19:01:47 +0000764 PyObject *posobj = NULL;
765 Py_off_t pos;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000766 int ret;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000767 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000768
Guido van Rossum53807da2007-04-10 19:01:47 +0000769 fd = self->fd;
770 if (fd < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000771 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000772 if (!self->writable)
773 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000774
Guido van Rossum53807da2007-04-10 19:01:47 +0000775 if (!PyArg_ParseTuple(args, "|O", &posobj))
776 return NULL;
777
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000778 if (posobj == Py_None || posobj == NULL) {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000779 /* Get the current position. */
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000780 posobj = portable_lseek(fd, NULL, 1);
781 if (posobj == NULL)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000782 return NULL;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000783 }
784 else {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000785 /* Move to the position to be truncated. */
786 posobj = portable_lseek(fd, posobj, 0);
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000787 }
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000788 if (posobj == NULL)
789 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000790
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000791#if defined(HAVE_LARGEFILE_SUPPORT)
792 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000793#else
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000794 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000795#endif
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000796 if (pos == -1 && PyErr_Occurred())
Guido van Rossum53807da2007-04-10 19:01:47 +0000797 return NULL;
798
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000799#ifdef MS_WINDOWS
800 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
801 so don't even try using it. */
802 {
803 HANDLE hFile;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000804
805 /* Truncate. Note that this may grow the file! */
806 Py_BEGIN_ALLOW_THREADS
807 errno = 0;
808 hFile = (HANDLE)_get_osfhandle(fd);
809 ret = hFile == (HANDLE)-1;
810 if (ret == 0) {
811 ret = SetEndOfFile(hFile) == 0;
812 if (ret)
813 errno = EACCES;
814 }
815 Py_END_ALLOW_THREADS
816 }
817#else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000818 Py_BEGIN_ALLOW_THREADS
819 errno = 0;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000820 ret = ftruncate(fd, pos);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000821 Py_END_ALLOW_THREADS
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000822#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000823
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000824 if (ret != 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000825 PyErr_SetFromErrno(PyExc_IOError);
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000826 return NULL;
Guido van Rossum87429772007-04-10 21:06:59 +0000827 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000828
Guido van Rossum87429772007-04-10 21:06:59 +0000829 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000830}
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000831#endif
Guido van Rossum53807da2007-04-10 19:01:47 +0000832
833static char *
834mode_string(PyFileIOObject *self)
835{
836 if (self->readable) {
837 if (self->writable)
Benjamin Peterson44309e62008-11-22 00:41:45 +0000838 return "rb+";
Guido van Rossum53807da2007-04-10 19:01:47 +0000839 else
Benjamin Peterson44309e62008-11-22 00:41:45 +0000840 return "rb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000841 }
842 else
Benjamin Peterson44309e62008-11-22 00:41:45 +0000843 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000844}
Guido van Rossuma9e20242007-03-08 00:43:48 +0000845
846static PyObject *
847fileio_repr(PyFileIOObject *self)
848{
Guido van Rossum53807da2007-04-10 19:01:47 +0000849 if (self->fd < 0)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000850 return PyUnicode_FromFormat("io.FileIO(-1)");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000851
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000852 return PyUnicode_FromFormat("io.FileIO(%d, '%s')",
Guido van Rossum53807da2007-04-10 19:01:47 +0000853 self->fd, mode_string(self));
Guido van Rossuma9e20242007-03-08 00:43:48 +0000854}
855
856static PyObject *
857fileio_isatty(PyFileIOObject *self)
858{
859 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000860
Guido van Rossuma9e20242007-03-08 00:43:48 +0000861 if (self->fd < 0)
862 return err_closed();
863 Py_BEGIN_ALLOW_THREADS
864 res = isatty(self->fd);
865 Py_END_ALLOW_THREADS
866 return PyBool_FromLong(res);
867}
868
Guido van Rossuma9e20242007-03-08 00:43:48 +0000869
870PyDoc_STRVAR(fileio_doc,
871"file(name: str[, mode: str]) -> file IO object\n"
872"\n"
873"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
874"writing or appending. The file will be created if it doesn't exist\n"
875"when opened for writing or appending; it will be truncated when\n"
876"opened for writing. Add a '+' to the mode to allow simultaneous\n"
877"reading and writing.");
878
879PyDoc_STRVAR(read_doc,
880"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
881"\n"
882"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +0000883"In non-blocking mode, returns None if no data is available.\n"
884"On end-of-file, returns ''.");
885
886PyDoc_STRVAR(readall_doc,
887"readall() -> bytes. read all data from the file, returned as bytes.\n"
888"\n"
889"In non-blocking mode, returns as much as is immediately available,\n"
890"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000891
892PyDoc_STRVAR(write_doc,
893"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
894"\n"
895"Only makes one system call, so not all of the data may be written.\n"
896"The number of bytes actually written is returned.");
897
898PyDoc_STRVAR(fileno_doc,
899"fileno() -> int. \"file descriptor\".\n"
900"\n"
901"This is needed for lower-level file interfaces, such the fcntl module.");
902
903PyDoc_STRVAR(seek_doc,
904"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
905"\n"
906"Argument offset is a byte count. Optional argument whence defaults to\n"
907"0 (offset from start of file, offset should be >= 0); other values are 1\n"
908"(move relative to current position, positive or negative), and 2 (move\n"
909"relative to end of file, usually negative, although many platforms allow\n"
910"seeking beyond the end of a file)."
911"\n"
912"Note that not all file objects are seekable.");
913
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000914#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000915PyDoc_STRVAR(truncate_doc,
916"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
917"\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000918"Size defaults to the current file position, as returned by tell()."
919"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000920#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000921
922PyDoc_STRVAR(tell_doc,
923"tell() -> int. Current file position");
924
925PyDoc_STRVAR(readinto_doc,
Benjamin Peterson9a8082f2009-03-05 00:55:56 +0000926"readinto() -> Same as RawIOBase.readinto().");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000927
928PyDoc_STRVAR(close_doc,
929"close() -> None. Close the file.\n"
930"\n"
931"A closed file cannot be used for further I/O operations. close() may be\n"
932"called more than once without error. Changes the fileno to -1.");
933
934PyDoc_STRVAR(isatty_doc,
935"isatty() -> bool. True if the file is connected to a tty device.");
936
Guido van Rossuma9e20242007-03-08 00:43:48 +0000937PyDoc_STRVAR(seekable_doc,
938"seekable() -> bool. True if file supports random-access.");
939
940PyDoc_STRVAR(readable_doc,
941"readable() -> bool. True if file was opened in a read mode.");
942
943PyDoc_STRVAR(writable_doc,
944"writable() -> bool. True if file was opened in a write mode.");
945
946static PyMethodDef fileio_methods[] = {
947 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
Guido van Rossum7165cb12007-07-10 06:54:34 +0000948 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000949 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
950 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
951 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
952 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000953#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000954 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000955#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000956 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
957 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
958 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
959 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
960 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
961 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000962 {NULL, NULL} /* sentinel */
963};
964
Guido van Rossum53807da2007-04-10 19:01:47 +0000965/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
966
Guido van Rossumb0428152007-04-08 17:44:42 +0000967static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000968get_closed(PyFileIOObject *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +0000969{
Guido van Rossum53807da2007-04-10 19:01:47 +0000970 return PyBool_FromLong((long)(self->fd < 0));
971}
972
973static PyObject *
Christian Heimesecc42a22008-11-05 19:30:32 +0000974get_closefd(PyFileIOObject *self, void *closure)
975{
976 return PyBool_FromLong((long)(self->closefd));
977}
978
979static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000980get_mode(PyFileIOObject *self, void *closure)
981{
Guido van Rossumc43e79f2007-06-18 18:26:36 +0000982 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +0000983}
984
985static PyGetSetDef fileio_getsetlist[] = {
986 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Christian Heimesecc42a22008-11-05 19:30:32 +0000987 {"closefd", (getter)get_closefd, NULL,
988 "True if the file descriptor will be closed"},
Guido van Rossum53807da2007-04-10 19:01:47 +0000989 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
Benjamin Peterson1fea3212009-04-19 03:15:20 +0000990 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +0000991};
992
Guido van Rossuma9e20242007-03-08 00:43:48 +0000993PyTypeObject PyFileIO_Type = {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000994 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000995 "_io.FileIO",
Guido van Rossuma9e20242007-03-08 00:43:48 +0000996 sizeof(PyFileIOObject),
997 0,
998 (destructor)fileio_dealloc, /* tp_dealloc */
999 0, /* tp_print */
1000 0, /* tp_getattr */
1001 0, /* tp_setattr */
Antoine Pitrou7fb111b2009-03-04 11:14:01 +00001002 0, /* tp_reserved */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001003 (reprfunc)fileio_repr, /* tp_repr */
1004 0, /* tp_as_number */
1005 0, /* tp_as_sequence */
1006 0, /* tp_as_mapping */
1007 0, /* tp_hash */
1008 0, /* tp_call */
1009 0, /* tp_str */
1010 PyObject_GenericGetAttr, /* tp_getattro */
1011 0, /* tp_setattro */
1012 0, /* tp_as_buffer */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001013 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
1014 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001015 fileio_doc, /* tp_doc */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001016 (traverseproc)fileio_traverse, /* tp_traverse */
1017 (inquiry)fileio_clear, /* tp_clear */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001018 0, /* tp_richcompare */
1019 offsetof(PyFileIOObject, weakreflist), /* tp_weaklistoffset */
1020 0, /* tp_iter */
1021 0, /* tp_iternext */
1022 fileio_methods, /* tp_methods */
1023 0, /* tp_members */
Guido van Rossumb0428152007-04-08 17:44:42 +00001024 fileio_getsetlist, /* tp_getset */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001025 0, /* tp_base */
1026 0, /* tp_dict */
1027 0, /* tp_descr_get */
1028 0, /* tp_descr_set */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001029 offsetof(PyFileIOObject, dict), /* tp_dictoffset */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001030 fileio_init, /* tp_init */
1031 PyType_GenericAlloc, /* tp_alloc */
1032 fileio_new, /* tp_new */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001033 PyObject_GC_Del, /* tp_free */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001034};