blob: a2a5f737f789861d1061495a2ec04ad07917136e [file] [log] [blame]
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001/* Author: Daniel Stutzbach */
2
3#define PY_SSIZE_T_CLEAN
4#include "Python.h"
Andrew M. Kuchling4b81bc72010-02-22 23:12:00 +00005#ifdef HAVE_SYS_TYPES_H
Christian Heimes7f39c9f2008-01-25 12:18:43 +00006#include <sys/types.h>
Andrew M. Kuchling4b81bc72010-02-22 23:12:00 +00007#endif
8#ifdef HAVE_SYS_STAT_H
Christian Heimes7f39c9f2008-01-25 12:18:43 +00009#include <sys/stat.h>
Andrew M. Kuchling4b81bc72010-02-22 23:12:00 +000010#endif
11#ifdef HAVE_FCNTL_H
Christian Heimes7f39c9f2008-01-25 12:18:43 +000012#include <fcntl.h>
Andrew M. Kuchling4b81bc72010-02-22 23:12:00 +000013#endif
Christian Heimes7f39c9f2008-01-25 12:18:43 +000014#include <stddef.h> /* For offsetof */
Antoine Pitrou19690592009-06-12 20:14:08 +000015#include "_iomodule.h"
Christian Heimes7f39c9f2008-01-25 12:18:43 +000016
17/*
18 * Known likely problems:
19 *
20 * - Files larger then 2**32-1
21 * - Files with unicode filenames
22 * - Passing numbers greater than 2**32-1 when an integer is expected
23 * - Making it work on Windows and other oddball platforms
24 *
25 * To Do:
26 *
27 * - autoconfify header file inclusion
28 */
29
30#ifdef MS_WINDOWS
31/* can simulate truncate with Win32 API functions; see file_truncate */
32#define HAVE_FTRUNCATE
33#define WIN32_LEAN_AND_MEAN
34#include <windows.h>
35#endif
36
Antoine Pitrou19690592009-06-12 20:14:08 +000037#if BUFSIZ < (8*1024)
38#define SMALLCHUNK (8*1024)
39#elif (BUFSIZ >= (2 << 25))
40#error "unreasonable BUFSIZ > 64MB defined"
41#else
42#define SMALLCHUNK BUFSIZ
43#endif
44
45#if SIZEOF_INT < 4
46#define BIGCHUNK (512 * 32)
47#else
48#define BIGCHUNK (512 * 1024)
49#endif
50
Christian Heimes7f39c9f2008-01-25 12:18:43 +000051typedef struct {
Antoine Pitroub26dc462010-05-05 16:27:30 +000052 PyObject_HEAD
53 int fd;
54 unsigned int readable : 1;
55 unsigned int writable : 1;
56 signed int seekable : 2; /* -1 means unknown */
57 unsigned int closefd : 1;
58 PyObject *weakreflist;
59 PyObject *dict;
Antoine Pitrou19690592009-06-12 20:14:08 +000060} fileio;
Christian Heimes7f39c9f2008-01-25 12:18:43 +000061
62PyTypeObject PyFileIO_Type;
63
64#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
65
Antoine Pitrou19690592009-06-12 20:14:08 +000066int
67_PyFileIO_closed(PyObject *self)
68{
Antoine Pitroub26dc462010-05-05 16:27:30 +000069 return ((fileio *)self)->fd < 0;
Antoine Pitrou19690592009-06-12 20:14:08 +000070}
71
Antoine Pitroue741cc62009-01-21 00:45:36 +000072static PyObject *
73portable_lseek(int fd, PyObject *posobj, int whence);
74
Antoine Pitrou19690592009-06-12 20:14:08 +000075static PyObject *portable_lseek(int fd, PyObject *posobj, int whence);
76
77/* Returns 0 on success, -1 with exception set on failure. */
Christian Heimes7f39c9f2008-01-25 12:18:43 +000078static int
Antoine Pitrou19690592009-06-12 20:14:08 +000079internal_close(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +000080{
Antoine Pitroub26dc462010-05-05 16:27:30 +000081 int err = 0;
82 int save_errno = 0;
83 if (self->fd >= 0) {
84 int fd = self->fd;
85 self->fd = -1;
86 /* fd is accessible and someone else may have closed it */
87 if (_PyVerify_fd(fd)) {
88 Py_BEGIN_ALLOW_THREADS
89 err = close(fd);
90 if (err < 0)
91 save_errno = errno;
92 Py_END_ALLOW_THREADS
93 } else {
94 save_errno = errno;
95 err = -1;
96 }
97 }
98 if (err < 0) {
99 errno = save_errno;
100 PyErr_SetFromErrno(PyExc_IOError);
101 return -1;
102 }
103 return 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000104}
105
106static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000107fileio_close(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000108{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000109 if (!self->closefd) {
110 self->fd = -1;
111 Py_RETURN_NONE;
112 }
113 errno = internal_close(self);
114 if (errno < 0)
115 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000116
Antoine Pitroub26dc462010-05-05 16:27:30 +0000117 return PyObject_CallMethod((PyObject*)&PyRawIOBase_Type,
118 "close", "O", self);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000119}
120
121static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000122fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000123{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000124 fileio *self;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000125
Antoine Pitroub26dc462010-05-05 16:27:30 +0000126 assert(type != NULL && type->tp_alloc != NULL);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000127
Antoine Pitroub26dc462010-05-05 16:27:30 +0000128 self = (fileio *) type->tp_alloc(type, 0);
129 if (self != NULL) {
130 self->fd = -1;
131 self->readable = 0;
132 self->writable = 0;
133 self->seekable = -1;
134 self->closefd = 1;
135 self->weakreflist = NULL;
136 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000137
Antoine Pitroub26dc462010-05-05 16:27:30 +0000138 return (PyObject *) self;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000139}
140
141/* On Unix, open will succeed for directories.
142 In Python, there should be no file objects referring to
143 directories, so we need a check. */
144
145static int
Antoine Pitrou19690592009-06-12 20:14:08 +0000146dircheck(fileio* self, const char *name)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000147{
148#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
Antoine Pitroub26dc462010-05-05 16:27:30 +0000149 struct stat buf;
150 if (self->fd < 0)
151 return 0;
152 if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
153 char *msg = strerror(EISDIR);
154 PyObject *exc;
155 if (internal_close(self))
156 return -1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000157
Antoine Pitroub26dc462010-05-05 16:27:30 +0000158 exc = PyObject_CallFunction(PyExc_IOError, "(iss)",
159 EISDIR, msg, name);
160 PyErr_SetObject(PyExc_IOError, exc);
161 Py_XDECREF(exc);
162 return -1;
163 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000164#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000165 return 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000166}
167
Benjamin Peterson5848d1f2009-01-19 00:08:08 +0000168static int
169check_fd(int fd)
170{
171#if defined(HAVE_FSTAT)
Antoine Pitroub26dc462010-05-05 16:27:30 +0000172 struct stat buf;
173 if (!_PyVerify_fd(fd) || (fstat(fd, &buf) < 0 && errno == EBADF)) {
174 PyObject *exc;
175 char *msg = strerror(EBADF);
176 exc = PyObject_CallFunction(PyExc_OSError, "(is)",
177 EBADF, msg);
178 PyErr_SetObject(PyExc_OSError, exc);
179 Py_XDECREF(exc);
180 return -1;
181 }
Benjamin Peterson5848d1f2009-01-19 00:08:08 +0000182#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000183 return 0;
Benjamin Peterson5848d1f2009-01-19 00:08:08 +0000184}
185
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000186
187static int
188fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
189{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000190 fileio *self = (fileio *) oself;
191 static char *kwlist[] = {"file", "mode", "closefd", NULL};
192 const char *name = NULL;
193 PyObject *nameobj, *stringobj = NULL;
194 char *mode = "r";
195 char *s;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000196#ifdef MS_WINDOWS
Antoine Pitroub26dc462010-05-05 16:27:30 +0000197 Py_UNICODE *widename = NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000198#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000199 int ret = 0;
200 int rwa = 0, plus = 0, append = 0;
201 int flags = 0;
202 int fd = -1;
203 int closefd = 1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000204
Antoine Pitroub26dc462010-05-05 16:27:30 +0000205 assert(PyFileIO_Check(oself));
206 if (self->fd >= 0) {
207 /* Have to close the existing file first. */
208 if (internal_close(self) < 0)
209 return -1;
210 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000211
Antoine Pitroub26dc462010-05-05 16:27:30 +0000212 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:fileio",
213 kwlist, &nameobj, &mode, &closefd))
214 return -1;
Antoine Pitrou19690592009-06-12 20:14:08 +0000215
Antoine Pitroub26dc462010-05-05 16:27:30 +0000216 if (PyFloat_Check(nameobj)) {
217 PyErr_SetString(PyExc_TypeError,
218 "integer argument expected, got float");
219 return -1;
220 }
Antoine Pitrou19690592009-06-12 20:14:08 +0000221
Antoine Pitroub26dc462010-05-05 16:27:30 +0000222 fd = PyLong_AsLong(nameobj);
223 if (fd < 0) {
224 if (!PyErr_Occurred()) {
225 PyErr_SetString(PyExc_ValueError,
226 "Negative filedescriptor");
227 return -1;
228 }
229 PyErr_Clear();
230 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000231
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000232#ifdef MS_WINDOWS
Antoine Pitroub26dc462010-05-05 16:27:30 +0000233 if (PyUnicode_Check(nameobj))
234 widename = PyUnicode_AS_UNICODE(nameobj);
235 if (widename == NULL)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000236#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000237 if (fd < 0)
238 {
239 if (PyBytes_Check(nameobj) || PyByteArray_Check(nameobj)) {
240 Py_ssize_t namelen;
241 if (PyObject_AsCharBuffer(nameobj, &name, &namelen) < 0)
242 return -1;
243 }
244 else {
245 PyObject *u = PyUnicode_FromObject(nameobj);
Antoine Pitrou19690592009-06-12 20:14:08 +0000246
Antoine Pitroub26dc462010-05-05 16:27:30 +0000247 if (u == NULL)
248 return -1;
Antoine Pitrou19690592009-06-12 20:14:08 +0000249
Antoine Pitroub26dc462010-05-05 16:27:30 +0000250 stringobj = PyUnicode_AsEncodedString(
251 u, Py_FileSystemDefaultEncoding, NULL);
252 Py_DECREF(u);
253 if (stringobj == NULL)
254 return -1;
255 if (!PyBytes_Check(stringobj)) {
256 PyErr_SetString(PyExc_TypeError,
257 "encoder failed to return bytes");
258 goto error;
259 }
260 name = PyBytes_AS_STRING(stringobj);
261 }
262 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000263
Antoine Pitroub26dc462010-05-05 16:27:30 +0000264 s = mode;
265 while (*s) {
266 switch (*s++) {
267 case 'r':
268 if (rwa) {
269 bad_mode:
270 PyErr_SetString(PyExc_ValueError,
271 "Must have exactly one of read/write/append mode");
272 goto error;
273 }
274 rwa = 1;
275 self->readable = 1;
276 break;
277 case 'w':
278 if (rwa)
279 goto bad_mode;
280 rwa = 1;
281 self->writable = 1;
282 flags |= O_CREAT | O_TRUNC;
283 break;
284 case 'a':
285 if (rwa)
286 goto bad_mode;
287 rwa = 1;
288 self->writable = 1;
289 flags |= O_CREAT;
290 append = 1;
291 break;
292 case 'b':
293 break;
294 case '+':
295 if (plus)
296 goto bad_mode;
297 self->readable = self->writable = 1;
298 plus = 1;
299 break;
300 default:
301 PyErr_Format(PyExc_ValueError,
302 "invalid mode: %.200s", mode);
303 goto error;
304 }
305 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000306
Antoine Pitroub26dc462010-05-05 16:27:30 +0000307 if (!rwa)
308 goto bad_mode;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000309
Antoine Pitroub26dc462010-05-05 16:27:30 +0000310 if (self->readable && self->writable)
311 flags |= O_RDWR;
312 else if (self->readable)
313 flags |= O_RDONLY;
314 else
315 flags |= O_WRONLY;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000316
317#ifdef O_BINARY
Antoine Pitroub26dc462010-05-05 16:27:30 +0000318 flags |= O_BINARY;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000319#endif
320
321#ifdef O_APPEND
Antoine Pitroub26dc462010-05-05 16:27:30 +0000322 if (append)
323 flags |= O_APPEND;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000324#endif
325
Antoine Pitroub26dc462010-05-05 16:27:30 +0000326 if (fd >= 0) {
327 if (check_fd(fd))
328 goto error;
329 self->fd = fd;
330 self->closefd = closefd;
331 }
332 else {
333 self->closefd = 1;
334 if (!closefd) {
335 PyErr_SetString(PyExc_ValueError,
336 "Cannot use closefd=False with file name");
337 goto error;
338 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000339
Antoine Pitroub26dc462010-05-05 16:27:30 +0000340 Py_BEGIN_ALLOW_THREADS
341 errno = 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000342#ifdef MS_WINDOWS
Antoine Pitroub26dc462010-05-05 16:27:30 +0000343 if (widename != NULL)
344 self->fd = _wopen(widename, flags, 0666);
345 else
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000346#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000347 self->fd = open(name, flags, 0666);
348 Py_END_ALLOW_THREADS
349 if (self->fd < 0) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000350#ifdef MS_WINDOWS
Antoine Pitroub26dc462010-05-05 16:27:30 +0000351 if (widename != NULL)
352 PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename);
353 else
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000354#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000355 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
356 goto error;
357 }
358 if(dircheck(self, name) < 0)
359 goto error;
360 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000361
Antoine Pitroub26dc462010-05-05 16:27:30 +0000362 if (PyObject_SetAttrString((PyObject *)self, "name", nameobj) < 0)
363 goto error;
Antoine Pitrou19690592009-06-12 20:14:08 +0000364
Antoine Pitroub26dc462010-05-05 16:27:30 +0000365 if (append) {
366 /* For consistent behaviour, we explicitly seek to the
367 end of file (otherwise, it might be done only on the
368 first write()). */
369 PyObject *pos = portable_lseek(self->fd, NULL, 2);
370 if (pos == NULL)
371 goto error;
372 Py_DECREF(pos);
373 }
Antoine Pitroue741cc62009-01-21 00:45:36 +0000374
Antoine Pitroub26dc462010-05-05 16:27:30 +0000375 goto done;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000376
377 error:
Antoine Pitroub26dc462010-05-05 16:27:30 +0000378 ret = -1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000379
380 done:
Antoine Pitroub26dc462010-05-05 16:27:30 +0000381 Py_CLEAR(stringobj);
382 return ret;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000383}
384
Antoine Pitrou19690592009-06-12 20:14:08 +0000385static int
386fileio_traverse(fileio *self, visitproc visit, void *arg)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000387{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000388 Py_VISIT(self->dict);
389 return 0;
Antoine Pitrou19690592009-06-12 20:14:08 +0000390}
391
392static int
393fileio_clear(fileio *self)
394{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000395 Py_CLEAR(self->dict);
396 return 0;
Antoine Pitrou19690592009-06-12 20:14:08 +0000397}
398
399static void
400fileio_dealloc(fileio *self)
401{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000402 if (_PyIOBase_finalize((PyObject *) self) < 0)
403 return;
404 _PyObject_GC_UNTRACK(self);
405 if (self->weakreflist != NULL)
406 PyObject_ClearWeakRefs((PyObject *) self);
407 Py_CLEAR(self->dict);
408 Py_TYPE(self)->tp_free((PyObject *)self);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000409}
410
411static PyObject *
412err_closed(void)
413{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000414 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
415 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000416}
417
418static PyObject *
419err_mode(char *action)
420{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000421 PyErr_Format(PyExc_ValueError, "File not open for %s", action);
422 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000423}
424
425static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000426fileio_fileno(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000427{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000428 if (self->fd < 0)
429 return err_closed();
430 return PyInt_FromLong((long) self->fd);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000431}
432
433static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000434fileio_readable(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000435{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000436 if (self->fd < 0)
437 return err_closed();
438 return PyBool_FromLong((long) self->readable);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000439}
440
441static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000442fileio_writable(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000443{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000444 if (self->fd < 0)
445 return err_closed();
446 return PyBool_FromLong((long) self->writable);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000447}
448
449static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000450fileio_seekable(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000451{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000452 if (self->fd < 0)
453 return err_closed();
454 if (self->seekable < 0) {
455 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
456 if (pos == NULL) {
457 PyErr_Clear();
458 self->seekable = 0;
459 } else {
460 Py_DECREF(pos);
461 self->seekable = 1;
462 }
463 }
464 return PyBool_FromLong((long) self->seekable);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000465}
466
467static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000468fileio_readinto(fileio *self, PyObject *args)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000469{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000470 Py_buffer pbuf;
471 Py_ssize_t n;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000472
Antoine Pitroub26dc462010-05-05 16:27:30 +0000473 if (self->fd < 0)
474 return err_closed();
475 if (!self->readable)
476 return err_mode("reading");
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000477
Antoine Pitroub26dc462010-05-05 16:27:30 +0000478 if (!PyArg_ParseTuple(args, "w*", &pbuf))
479 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000480
Antoine Pitroub26dc462010-05-05 16:27:30 +0000481 if (_PyVerify_fd(self->fd)) {
482 Py_BEGIN_ALLOW_THREADS
483 errno = 0;
484 n = read(self->fd, pbuf.buf, pbuf.len);
485 Py_END_ALLOW_THREADS
486 } else
487 n = -1;
488 PyBuffer_Release(&pbuf);
489 if (n < 0) {
490 if (errno == EAGAIN)
491 Py_RETURN_NONE;
492 PyErr_SetFromErrno(PyExc_IOError);
493 return NULL;
494 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000495
Antoine Pitroub26dc462010-05-05 16:27:30 +0000496 return PyLong_FromSsize_t(n);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000497}
498
Antoine Pitrou19690592009-06-12 20:14:08 +0000499static size_t
500new_buffersize(fileio *self, size_t currentsize)
501{
502#ifdef HAVE_FSTAT
Antoine Pitroub26dc462010-05-05 16:27:30 +0000503 off_t pos, end;
504 struct stat st;
505 if (fstat(self->fd, &st) == 0) {
506 end = st.st_size;
507 pos = lseek(self->fd, 0L, SEEK_CUR);
508 /* Files claiming a size smaller than SMALLCHUNK may
509 actually be streaming pseudo-files. In this case, we
510 apply the more aggressive algorithm below.
511 */
512 if (end >= SMALLCHUNK && end >= pos && pos >= 0) {
513 /* Add 1 so if the file were to grow we'd notice. */
514 return currentsize + end - pos + 1;
515 }
516 }
Antoine Pitrou19690592009-06-12 20:14:08 +0000517#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000518 if (currentsize > SMALLCHUNK) {
519 /* Keep doubling until we reach BIGCHUNK;
520 then keep adding BIGCHUNK. */
521 if (currentsize <= BIGCHUNK)
522 return currentsize + currentsize;
523 else
524 return currentsize + BIGCHUNK;
525 }
526 return currentsize + SMALLCHUNK;
Antoine Pitrou19690592009-06-12 20:14:08 +0000527}
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000528
529static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000530fileio_readall(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000531{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000532 PyObject *result;
533 Py_ssize_t total = 0;
534 int n;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000535
Antoine Pitroub26dc462010-05-05 16:27:30 +0000536 if (!_PyVerify_fd(self->fd))
537 return PyErr_SetFromErrno(PyExc_IOError);
Antoine Pitrou19690592009-06-12 20:14:08 +0000538
Antoine Pitroub26dc462010-05-05 16:27:30 +0000539 result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK);
540 if (result == NULL)
541 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000542
Antoine Pitroub26dc462010-05-05 16:27:30 +0000543 while (1) {
544 size_t newsize = new_buffersize(self, total);
545 if (newsize > PY_SSIZE_T_MAX || newsize <= 0) {
546 PyErr_SetString(PyExc_OverflowError,
547 "unbounded read returned more bytes "
548 "than a Python string can hold ");
549 Py_DECREF(result);
550 return NULL;
551 }
Antoine Pitrou19690592009-06-12 20:14:08 +0000552
Antoine Pitroub26dc462010-05-05 16:27:30 +0000553 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)newsize) {
554 if (_PyBytes_Resize(&result, newsize) < 0) {
555 if (total == 0) {
556 Py_DECREF(result);
557 return NULL;
558 }
559 PyErr_Clear();
560 break;
561 }
562 }
563 Py_BEGIN_ALLOW_THREADS
564 errno = 0;
565 n = read(self->fd,
566 PyBytes_AS_STRING(result) + total,
567 newsize - total);
568 Py_END_ALLOW_THREADS
569 if (n == 0)
570 break;
571 if (n < 0) {
572 if (total > 0)
573 break;
574 if (errno == EAGAIN) {
575 Py_DECREF(result);
576 Py_RETURN_NONE;
577 }
578 Py_DECREF(result);
579 PyErr_SetFromErrno(PyExc_IOError);
580 return NULL;
581 }
582 total += n;
583 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000584
Antoine Pitroub26dc462010-05-05 16:27:30 +0000585 if (PyBytes_GET_SIZE(result) > total) {
586 if (_PyBytes_Resize(&result, total) < 0) {
587 /* This should never happen, but just in case */
588 Py_DECREF(result);
589 return NULL;
590 }
591 }
592 return result;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000593}
594
595static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000596fileio_read(fileio *self, PyObject *args)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000597{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000598 char *ptr;
599 Py_ssize_t n;
600 Py_ssize_t size = -1;
601 PyObject *bytes;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000602
Antoine Pitroub26dc462010-05-05 16:27:30 +0000603 if (self->fd < 0)
604 return err_closed();
605 if (!self->readable)
606 return err_mode("reading");
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000607
Antoine Pitroub26dc462010-05-05 16:27:30 +0000608 if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size))
609 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000610
Antoine Pitroub26dc462010-05-05 16:27:30 +0000611 if (size < 0) {
612 return fileio_readall(self);
613 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000614
Antoine Pitroub26dc462010-05-05 16:27:30 +0000615 bytes = PyBytes_FromStringAndSize(NULL, size);
616 if (bytes == NULL)
617 return NULL;
618 ptr = PyBytes_AS_STRING(bytes);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000619
Antoine Pitroub26dc462010-05-05 16:27:30 +0000620 if (_PyVerify_fd(self->fd)) {
621 Py_BEGIN_ALLOW_THREADS
622 errno = 0;
623 n = read(self->fd, ptr, size);
624 Py_END_ALLOW_THREADS
625 } else
626 n = -1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000627
Antoine Pitroub26dc462010-05-05 16:27:30 +0000628 if (n < 0) {
629 Py_DECREF(bytes);
630 if (errno == EAGAIN)
631 Py_RETURN_NONE;
632 PyErr_SetFromErrno(PyExc_IOError);
633 return NULL;
634 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000635
Antoine Pitroub26dc462010-05-05 16:27:30 +0000636 if (n != size) {
637 if (_PyBytes_Resize(&bytes, n) < 0) {
638 Py_DECREF(bytes);
639 return NULL;
640 }
641 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000642
Antoine Pitroub26dc462010-05-05 16:27:30 +0000643 return (PyObject *) bytes;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000644}
645
646static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000647fileio_write(fileio *self, PyObject *args)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000648{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000649 Py_buffer pbuf;
650 Py_ssize_t n;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000651
Antoine Pitroub26dc462010-05-05 16:27:30 +0000652 if (self->fd < 0)
653 return err_closed();
654 if (!self->writable)
655 return err_mode("writing");
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000656
Antoine Pitroub26dc462010-05-05 16:27:30 +0000657 if (!PyArg_ParseTuple(args, "s*", &pbuf))
658 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000659
Antoine Pitroub26dc462010-05-05 16:27:30 +0000660 if (_PyVerify_fd(self->fd)) {
661 Py_BEGIN_ALLOW_THREADS
662 errno = 0;
663 n = write(self->fd, pbuf.buf, pbuf.len);
664 Py_END_ALLOW_THREADS
665 } else
666 n = -1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000667
Antoine Pitroub26dc462010-05-05 16:27:30 +0000668 PyBuffer_Release(&pbuf);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000669
Antoine Pitroub26dc462010-05-05 16:27:30 +0000670 if (n < 0) {
671 if (errno == EAGAIN)
672 Py_RETURN_NONE;
673 PyErr_SetFromErrno(PyExc_IOError);
674 return NULL;
675 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000676
Antoine Pitroub26dc462010-05-05 16:27:30 +0000677 return PyLong_FromSsize_t(n);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000678}
679
680/* XXX Windows support below is likely incomplete */
681
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000682/* Cribbed from posix_lseek() */
683static PyObject *
684portable_lseek(int fd, PyObject *posobj, int whence)
685{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000686 Py_off_t pos, res;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000687
688#ifdef SEEK_SET
Antoine Pitroub26dc462010-05-05 16:27:30 +0000689 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
690 switch (whence) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000691#if SEEK_SET != 0
Antoine Pitroub26dc462010-05-05 16:27:30 +0000692 case 0: whence = SEEK_SET; break;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000693#endif
694#if SEEK_CUR != 1
Antoine Pitroub26dc462010-05-05 16:27:30 +0000695 case 1: whence = SEEK_CUR; break;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000696#endif
Benjamin Peterson8024cec2009-01-20 14:31:08 +0000697#if SEEK_END != 2
Antoine Pitroub26dc462010-05-05 16:27:30 +0000698 case 2: whence = SEEK_END; break;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000699#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000700 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000701#endif /* SEEK_SET */
702
Antoine Pitroub26dc462010-05-05 16:27:30 +0000703 if (posobj == NULL)
704 pos = 0;
705 else {
706 if(PyFloat_Check(posobj)) {
707 PyErr_SetString(PyExc_TypeError, "an integer is required");
708 return NULL;
709 }
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000710#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitroub26dc462010-05-05 16:27:30 +0000711 pos = PyLong_AsLongLong(posobj);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000712#else
Antoine Pitroub26dc462010-05-05 16:27:30 +0000713 pos = PyLong_AsLong(posobj);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000714#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000715 if (PyErr_Occurred())
716 return NULL;
717 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000718
Antoine Pitroub26dc462010-05-05 16:27:30 +0000719 if (_PyVerify_fd(fd)) {
720 Py_BEGIN_ALLOW_THREADS
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000721#if defined(MS_WIN64) || defined(MS_WINDOWS)
Antoine Pitroub26dc462010-05-05 16:27:30 +0000722 res = _lseeki64(fd, pos, whence);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000723#else
Antoine Pitroub26dc462010-05-05 16:27:30 +0000724 res = lseek(fd, pos, whence);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000725#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000726 Py_END_ALLOW_THREADS
727 } else
728 res = -1;
729 if (res < 0)
730 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000731
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000732#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitroub26dc462010-05-05 16:27:30 +0000733 return PyLong_FromLongLong(res);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000734#else
Antoine Pitroub26dc462010-05-05 16:27:30 +0000735 return PyLong_FromLong(res);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000736#endif
737}
738
739static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000740fileio_seek(fileio *self, PyObject *args)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000741{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000742 PyObject *posobj;
743 int whence = 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000744
Antoine Pitroub26dc462010-05-05 16:27:30 +0000745 if (self->fd < 0)
746 return err_closed();
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000747
Antoine Pitroub26dc462010-05-05 16:27:30 +0000748 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
749 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000750
Antoine Pitroub26dc462010-05-05 16:27:30 +0000751 return portable_lseek(self->fd, posobj, whence);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000752}
753
754static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000755fileio_tell(fileio *self, PyObject *args)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000756{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000757 if (self->fd < 0)
758 return err_closed();
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000759
Antoine Pitroub26dc462010-05-05 16:27:30 +0000760 return portable_lseek(self->fd, NULL, 1);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000761}
762
763#ifdef HAVE_FTRUNCATE
764static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000765fileio_truncate(fileio *self, PyObject *args)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000766{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000767 PyObject *posobj = NULL; /* the new size wanted by the user */
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000768#ifndef MS_WINDOWS
Antoine Pitroub26dc462010-05-05 16:27:30 +0000769 Py_off_t pos;
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000770#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000771 int ret;
772 int fd;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000773
Antoine Pitroub26dc462010-05-05 16:27:30 +0000774 fd = self->fd;
775 if (fd < 0)
776 return err_closed();
777 if (!self->writable)
778 return err_mode("writing");
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000779
Antoine Pitroub26dc462010-05-05 16:27:30 +0000780 if (!PyArg_ParseTuple(args, "|O", &posobj))
781 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000782
Antoine Pitroub26dc462010-05-05 16:27:30 +0000783 if (posobj == Py_None || posobj == NULL) {
784 /* Get the current position. */
785 posobj = portable_lseek(fd, NULL, 1);
786 if (posobj == NULL)
787 return NULL;
788 }
789 else {
790 Py_INCREF(posobj);
791 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000792
793#ifdef MS_WINDOWS
Antoine Pitroub26dc462010-05-05 16:27:30 +0000794 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
795 so don't even try using it. */
796 {
797 PyObject *oldposobj, *tempposobj;
798 HANDLE hFile;
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000799
Antoine Pitroub26dc462010-05-05 16:27:30 +0000800 /* we save the file pointer position */
801 oldposobj = portable_lseek(fd, NULL, 1);
802 if (oldposobj == NULL) {
803 Py_DECREF(posobj);
804 return NULL;
805 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000806
Antoine Pitroub26dc462010-05-05 16:27:30 +0000807 /* we then move to the truncation position */
808 tempposobj = portable_lseek(fd, posobj, 0);
809 if (tempposobj == NULL) {
810 Py_DECREF(oldposobj);
811 Py_DECREF(posobj);
812 return NULL;
813 }
814 Py_DECREF(tempposobj);
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000815
Antoine Pitroub26dc462010-05-05 16:27:30 +0000816 /* Truncate. Note that this may grow the file! */
817 Py_BEGIN_ALLOW_THREADS
818 errno = 0;
819 hFile = (HANDLE)_get_osfhandle(fd);
820 ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
821 if (ret == 0) {
822 ret = SetEndOfFile(hFile) == 0;
823 if (ret)
824 errno = EACCES;
825 }
826 Py_END_ALLOW_THREADS
827
828 /* we restore the file pointer position in any case */
829 tempposobj = portable_lseek(fd, oldposobj, 0);
830 Py_DECREF(oldposobj);
831 if (tempposobj == NULL) {
832 Py_DECREF(posobj);
833 return NULL;
834 }
835 Py_DECREF(tempposobj);
836 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000837#else
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000838
839#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitroub26dc462010-05-05 16:27:30 +0000840 pos = PyLong_AsLongLong(posobj);
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000841#else
Antoine Pitroub26dc462010-05-05 16:27:30 +0000842 pos = PyLong_AsLong(posobj);
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000843#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000844 if (PyErr_Occurred()){
845 Py_DECREF(posobj);
846 return NULL;
847 }
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000848
Antoine Pitroub26dc462010-05-05 16:27:30 +0000849 Py_BEGIN_ALLOW_THREADS
850 errno = 0;
851 ret = ftruncate(fd, pos);
852 Py_END_ALLOW_THREADS
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000853
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000854#endif /* !MS_WINDOWS */
855
Antoine Pitroub26dc462010-05-05 16:27:30 +0000856 if (ret != 0) {
857 Py_DECREF(posobj);
858 PyErr_SetFromErrno(PyExc_IOError);
859 return NULL;
860 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000861
Antoine Pitroub26dc462010-05-05 16:27:30 +0000862 return posobj;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000863}
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000864#endif /* HAVE_FTRUNCATE */
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000865
866static char *
Antoine Pitrou19690592009-06-12 20:14:08 +0000867mode_string(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000868{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000869 if (self->readable) {
870 if (self->writable)
871 return "rb+";
872 else
873 return "rb";
874 }
875 else
876 return "wb";
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000877}
878
879static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000880fileio_repr(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000881{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000882 PyObject *nameobj, *res;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000883
Antoine Pitroub26dc462010-05-05 16:27:30 +0000884 if (self->fd < 0)
885 return PyString_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou19690592009-06-12 20:14:08 +0000886
Antoine Pitroub26dc462010-05-05 16:27:30 +0000887 nameobj = PyObject_GetAttrString((PyObject *) self, "name");
888 if (nameobj == NULL) {
889 if (PyErr_ExceptionMatches(PyExc_AttributeError))
890 PyErr_Clear();
891 else
892 return NULL;
893 res = PyString_FromFormat("<_io.FileIO fd=%d mode='%s'>",
894 self->fd, mode_string(self));
895 }
896 else {
897 PyObject *repr = PyObject_Repr(nameobj);
898 Py_DECREF(nameobj);
899 if (repr == NULL)
900 return NULL;
901 res = PyString_FromFormat("<_io.FileIO name=%s mode='%s'>",
902 PyString_AS_STRING(repr),
903 mode_string(self));
904 Py_DECREF(repr);
905 }
906 return res;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000907}
908
909static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000910fileio_isatty(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000911{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000912 long res;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000913
Antoine Pitroub26dc462010-05-05 16:27:30 +0000914 if (self->fd < 0)
915 return err_closed();
916 Py_BEGIN_ALLOW_THREADS
917 res = isatty(self->fd);
918 Py_END_ALLOW_THREADS
919 return PyBool_FromLong(res);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000920}
921
922
923PyDoc_STRVAR(fileio_doc,
924"file(name: str[, mode: str]) -> file IO object\n"
925"\n"
926"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
Antoine Pitroub26dc462010-05-05 16:27:30 +0000927"writing or appending. The file will be created if it doesn't exist\n"
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000928"when opened for writing or appending; it will be truncated when\n"
929"opened for writing. Add a '+' to the mode to allow simultaneous\n"
930"reading and writing.");
931
932PyDoc_STRVAR(read_doc,
933"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
934"\n"
935"Only makes one system call, so less data may be returned than requested\n"
936"In non-blocking mode, returns None if no data is available.\n"
937"On end-of-file, returns ''.");
938
939PyDoc_STRVAR(readall_doc,
940"readall() -> bytes. read all data from the file, returned as bytes.\n"
941"\n"
942"In non-blocking mode, returns as much as is immediately available,\n"
943"or None if no data is available. On end-of-file, returns ''.");
944
945PyDoc_STRVAR(write_doc,
946"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
947"\n"
948"Only makes one system call, so not all of the data may be written.\n"
949"The number of bytes actually written is returned.");
950
951PyDoc_STRVAR(fileno_doc,
952"fileno() -> int. \"file descriptor\".\n"
953"\n"
954"This is needed for lower-level file interfaces, such the fcntl module.");
955
956PyDoc_STRVAR(seek_doc,
957"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
958"\n"
959"Argument offset is a byte count. Optional argument whence defaults to\n"
960"0 (offset from start of file, offset should be >= 0); other values are 1\n"
961"(move relative to current position, positive or negative), and 2 (move\n"
962"relative to end of file, usually negative, although many platforms allow\n"
963"seeking beyond the end of a file)."
964"\n"
965"Note that not all file objects are seekable.");
966
967#ifdef HAVE_FTRUNCATE
968PyDoc_STRVAR(truncate_doc,
Antoine Pitroub26dc462010-05-05 16:27:30 +0000969"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000970"\n"
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000971"Size defaults to the current file position, as returned by tell()."
972"The current file position is changed to the value of size.");
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000973#endif
974
975PyDoc_STRVAR(tell_doc,
Antoine Pitroub26dc462010-05-05 16:27:30 +0000976"tell() -> int. Current file position");
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000977
978PyDoc_STRVAR(readinto_doc,
Antoine Pitrou19690592009-06-12 20:14:08 +0000979"readinto() -> Same as RawIOBase.readinto().");
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000980
981PyDoc_STRVAR(close_doc,
982"close() -> None. Close the file.\n"
983"\n"
984"A closed file cannot be used for further I/O operations. close() may be\n"
985"called more than once without error. Changes the fileno to -1.");
986
987PyDoc_STRVAR(isatty_doc,
988"isatty() -> bool. True if the file is connected to a tty device.");
989
990PyDoc_STRVAR(seekable_doc,
991"seekable() -> bool. True if file supports random-access.");
992
993PyDoc_STRVAR(readable_doc,
994"readable() -> bool. True if file was opened in a read mode.");
995
996PyDoc_STRVAR(writable_doc,
997"writable() -> bool. True if file was opened in a write mode.");
998
999static PyMethodDef fileio_methods[] = {
Antoine Pitroub26dc462010-05-05 16:27:30 +00001000 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
1001 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
1002 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
1003 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
1004 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
1005 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001006#ifdef HAVE_FTRUNCATE
Antoine Pitroub26dc462010-05-05 16:27:30 +00001007 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001008#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +00001009 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
1010 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
1011 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
1012 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
1013 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
1014 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
1015 {NULL, NULL} /* sentinel */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001016};
1017
1018/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1019
1020static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +00001021get_closed(fileio *self, void *closure)
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001022{
Antoine Pitroub26dc462010-05-05 16:27:30 +00001023 return PyBool_FromLong((long)(self->fd < 0));
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001024}
1025
1026static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +00001027get_closefd(fileio *self, void *closure)
Amaury Forgeot d'Arc32265652008-11-20 23:34:31 +00001028{
Antoine Pitroub26dc462010-05-05 16:27:30 +00001029 return PyBool_FromLong((long)(self->closefd));
Amaury Forgeot d'Arc32265652008-11-20 23:34:31 +00001030}
1031
1032static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +00001033get_mode(fileio *self, void *closure)
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001034{
Antoine Pitroub26dc462010-05-05 16:27:30 +00001035 return PyUnicode_FromString(mode_string(self));
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001036}
1037
1038static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitroub26dc462010-05-05 16:27:30 +00001039 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1040 {"closefd", (getter)get_closefd, NULL,
1041 "True if the file descriptor will be closed"},
1042 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1043 {NULL},
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001044};
1045
1046PyTypeObject PyFileIO_Type = {
Antoine Pitroub26dc462010-05-05 16:27:30 +00001047 PyVarObject_HEAD_INIT(NULL, 0)
1048 "_io.FileIO",
1049 sizeof(fileio),
1050 0,
1051 (destructor)fileio_dealloc, /* tp_dealloc */
1052 0, /* tp_print */
1053 0, /* tp_getattr */
1054 0, /* tp_setattr */
1055 0, /* tp_reserved */
1056 (reprfunc)fileio_repr, /* tp_repr */
1057 0, /* tp_as_number */
1058 0, /* tp_as_sequence */
1059 0, /* tp_as_mapping */
1060 0, /* tp_hash */
1061 0, /* tp_call */
1062 0, /* tp_str */
1063 PyObject_GenericGetAttr, /* tp_getattro */
1064 0, /* tp_setattro */
1065 0, /* tp_as_buffer */
1066 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
1067 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1068 fileio_doc, /* tp_doc */
1069 (traverseproc)fileio_traverse, /* tp_traverse */
1070 (inquiry)fileio_clear, /* tp_clear */
1071 0, /* tp_richcompare */
1072 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1073 0, /* tp_iter */
1074 0, /* tp_iternext */
1075 fileio_methods, /* tp_methods */
1076 0, /* tp_members */
1077 fileio_getsetlist, /* tp_getset */
1078 0, /* tp_base */
1079 0, /* tp_dict */
1080 0, /* tp_descr_get */
1081 0, /* tp_descr_set */
1082 offsetof(fileio, dict), /* tp_dictoffset */
1083 fileio_init, /* tp_init */
1084 PyType_GenericAlloc, /* tp_alloc */
1085 fileio_new, /* tp_new */
1086 PyObject_GC_Del, /* tp_free */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001087};