blob: 83921eaae780e5d2e69de210a362d962e9a22f6e [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,
Georg Brandl10603802010-11-26 08:10:41 +0000271 "Must have exactly one of read/write/append "
272 "mode and at most one plus");
Antoine Pitroub26dc462010-05-05 16:27:30 +0000273 goto error;
274 }
275 rwa = 1;
276 self->readable = 1;
277 break;
278 case 'w':
279 if (rwa)
280 goto bad_mode;
281 rwa = 1;
282 self->writable = 1;
283 flags |= O_CREAT | O_TRUNC;
284 break;
285 case 'a':
286 if (rwa)
287 goto bad_mode;
288 rwa = 1;
289 self->writable = 1;
290 flags |= O_CREAT;
291 append = 1;
292 break;
293 case 'b':
294 break;
295 case '+':
296 if (plus)
297 goto bad_mode;
298 self->readable = self->writable = 1;
299 plus = 1;
300 break;
301 default:
302 PyErr_Format(PyExc_ValueError,
303 "invalid mode: %.200s", mode);
304 goto error;
305 }
306 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000307
Antoine Pitroub26dc462010-05-05 16:27:30 +0000308 if (!rwa)
309 goto bad_mode;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000310
Antoine Pitroub26dc462010-05-05 16:27:30 +0000311 if (self->readable && self->writable)
312 flags |= O_RDWR;
313 else if (self->readable)
314 flags |= O_RDONLY;
315 else
316 flags |= O_WRONLY;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000317
318#ifdef O_BINARY
Antoine Pitroub26dc462010-05-05 16:27:30 +0000319 flags |= O_BINARY;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000320#endif
321
322#ifdef O_APPEND
Antoine Pitroub26dc462010-05-05 16:27:30 +0000323 if (append)
324 flags |= O_APPEND;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000325#endif
326
Antoine Pitroub26dc462010-05-05 16:27:30 +0000327 if (fd >= 0) {
328 if (check_fd(fd))
329 goto error;
330 self->fd = fd;
331 self->closefd = closefd;
332 }
333 else {
334 self->closefd = 1;
335 if (!closefd) {
336 PyErr_SetString(PyExc_ValueError,
337 "Cannot use closefd=False with file name");
338 goto error;
339 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000340
Antoine Pitroub26dc462010-05-05 16:27:30 +0000341 Py_BEGIN_ALLOW_THREADS
342 errno = 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000343#ifdef MS_WINDOWS
Antoine Pitroub26dc462010-05-05 16:27:30 +0000344 if (widename != NULL)
345 self->fd = _wopen(widename, flags, 0666);
346 else
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000347#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000348 self->fd = open(name, flags, 0666);
349 Py_END_ALLOW_THREADS
350 if (self->fd < 0) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000351#ifdef MS_WINDOWS
Antoine Pitroub26dc462010-05-05 16:27:30 +0000352 if (widename != NULL)
353 PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename);
354 else
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000355#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000356 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
357 goto error;
358 }
359 if(dircheck(self, name) < 0)
360 goto error;
361 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000362
Antoine Pitroub26dc462010-05-05 16:27:30 +0000363 if (PyObject_SetAttrString((PyObject *)self, "name", nameobj) < 0)
364 goto error;
Antoine Pitrou19690592009-06-12 20:14:08 +0000365
Antoine Pitroub26dc462010-05-05 16:27:30 +0000366 if (append) {
367 /* For consistent behaviour, we explicitly seek to the
368 end of file (otherwise, it might be done only on the
369 first write()). */
370 PyObject *pos = portable_lseek(self->fd, NULL, 2);
Antoine Pitrou594a0462010-10-31 13:05:48 +0000371 if (pos == NULL) {
372 if (closefd) {
373 close(self->fd);
374 self->fd = -1;
375 }
Antoine Pitroub26dc462010-05-05 16:27:30 +0000376 goto error;
Antoine Pitrou594a0462010-10-31 13:05:48 +0000377 }
Antoine Pitroub26dc462010-05-05 16:27:30 +0000378 Py_DECREF(pos);
379 }
Antoine Pitroue741cc62009-01-21 00:45:36 +0000380
Antoine Pitroub26dc462010-05-05 16:27:30 +0000381 goto done;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000382
383 error:
Antoine Pitroub26dc462010-05-05 16:27:30 +0000384 ret = -1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000385
386 done:
Antoine Pitroub26dc462010-05-05 16:27:30 +0000387 Py_CLEAR(stringobj);
388 return ret;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000389}
390
Antoine Pitrou19690592009-06-12 20:14:08 +0000391static int
392fileio_traverse(fileio *self, visitproc visit, void *arg)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000393{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000394 Py_VISIT(self->dict);
395 return 0;
Antoine Pitrou19690592009-06-12 20:14:08 +0000396}
397
398static int
399fileio_clear(fileio *self)
400{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000401 Py_CLEAR(self->dict);
402 return 0;
Antoine Pitrou19690592009-06-12 20:14:08 +0000403}
404
405static void
406fileio_dealloc(fileio *self)
407{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000408 if (_PyIOBase_finalize((PyObject *) self) < 0)
409 return;
410 _PyObject_GC_UNTRACK(self);
411 if (self->weakreflist != NULL)
412 PyObject_ClearWeakRefs((PyObject *) self);
413 Py_CLEAR(self->dict);
414 Py_TYPE(self)->tp_free((PyObject *)self);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000415}
416
417static PyObject *
418err_closed(void)
419{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000420 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
421 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000422}
423
424static PyObject *
425err_mode(char *action)
426{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000427 PyErr_Format(PyExc_ValueError, "File not open for %s", action);
428 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000429}
430
431static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000432fileio_fileno(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000433{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000434 if (self->fd < 0)
435 return err_closed();
436 return PyInt_FromLong((long) self->fd);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000437}
438
439static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000440fileio_readable(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000441{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000442 if (self->fd < 0)
443 return err_closed();
444 return PyBool_FromLong((long) self->readable);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000445}
446
447static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000448fileio_writable(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000449{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000450 if (self->fd < 0)
451 return err_closed();
452 return PyBool_FromLong((long) self->writable);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000453}
454
455static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000456fileio_seekable(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000457{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000458 if (self->fd < 0)
459 return err_closed();
460 if (self->seekable < 0) {
461 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
462 if (pos == NULL) {
463 PyErr_Clear();
464 self->seekable = 0;
465 } else {
466 Py_DECREF(pos);
467 self->seekable = 1;
468 }
469 }
470 return PyBool_FromLong((long) self->seekable);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000471}
472
473static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000474fileio_readinto(fileio *self, PyObject *args)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000475{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000476 Py_buffer pbuf;
477 Py_ssize_t n;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000478
Antoine Pitroub26dc462010-05-05 16:27:30 +0000479 if (self->fd < 0)
480 return err_closed();
481 if (!self->readable)
482 return err_mode("reading");
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000483
Antoine Pitroub26dc462010-05-05 16:27:30 +0000484 if (!PyArg_ParseTuple(args, "w*", &pbuf))
485 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000486
Antoine Pitroub26dc462010-05-05 16:27:30 +0000487 if (_PyVerify_fd(self->fd)) {
488 Py_BEGIN_ALLOW_THREADS
489 errno = 0;
490 n = read(self->fd, pbuf.buf, pbuf.len);
491 Py_END_ALLOW_THREADS
492 } else
493 n = -1;
494 PyBuffer_Release(&pbuf);
495 if (n < 0) {
496 if (errno == EAGAIN)
497 Py_RETURN_NONE;
498 PyErr_SetFromErrno(PyExc_IOError);
499 return NULL;
500 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000501
Antoine Pitroub26dc462010-05-05 16:27:30 +0000502 return PyLong_FromSsize_t(n);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000503}
504
Antoine Pitrou19690592009-06-12 20:14:08 +0000505static size_t
506new_buffersize(fileio *self, size_t currentsize)
507{
508#ifdef HAVE_FSTAT
Antoine Pitroub26dc462010-05-05 16:27:30 +0000509 off_t pos, end;
510 struct stat st;
511 if (fstat(self->fd, &st) == 0) {
512 end = st.st_size;
513 pos = lseek(self->fd, 0L, SEEK_CUR);
514 /* Files claiming a size smaller than SMALLCHUNK may
515 actually be streaming pseudo-files. In this case, we
516 apply the more aggressive algorithm below.
517 */
518 if (end >= SMALLCHUNK && end >= pos && pos >= 0) {
519 /* Add 1 so if the file were to grow we'd notice. */
520 return currentsize + end - pos + 1;
521 }
522 }
Antoine Pitrou19690592009-06-12 20:14:08 +0000523#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000524 if (currentsize > SMALLCHUNK) {
525 /* Keep doubling until we reach BIGCHUNK;
526 then keep adding BIGCHUNK. */
527 if (currentsize <= BIGCHUNK)
528 return currentsize + currentsize;
529 else
530 return currentsize + BIGCHUNK;
531 }
532 return currentsize + SMALLCHUNK;
Antoine Pitrou19690592009-06-12 20:14:08 +0000533}
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000534
535static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000536fileio_readall(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000537{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000538 PyObject *result;
539 Py_ssize_t total = 0;
540 int n;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000541
Antoine Pitroub26dc462010-05-05 16:27:30 +0000542 if (!_PyVerify_fd(self->fd))
543 return PyErr_SetFromErrno(PyExc_IOError);
Antoine Pitrou19690592009-06-12 20:14:08 +0000544
Antoine Pitroub26dc462010-05-05 16:27:30 +0000545 result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK);
546 if (result == NULL)
547 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000548
Antoine Pitroub26dc462010-05-05 16:27:30 +0000549 while (1) {
550 size_t newsize = new_buffersize(self, total);
551 if (newsize > PY_SSIZE_T_MAX || newsize <= 0) {
552 PyErr_SetString(PyExc_OverflowError,
553 "unbounded read returned more bytes "
554 "than a Python string can hold ");
555 Py_DECREF(result);
556 return NULL;
557 }
Antoine Pitrou19690592009-06-12 20:14:08 +0000558
Antoine Pitroub26dc462010-05-05 16:27:30 +0000559 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)newsize) {
560 if (_PyBytes_Resize(&result, newsize) < 0) {
561 if (total == 0) {
562 Py_DECREF(result);
563 return NULL;
564 }
565 PyErr_Clear();
566 break;
567 }
568 }
569 Py_BEGIN_ALLOW_THREADS
570 errno = 0;
571 n = read(self->fd,
572 PyBytes_AS_STRING(result) + total,
573 newsize - total);
574 Py_END_ALLOW_THREADS
575 if (n == 0)
576 break;
577 if (n < 0) {
578 if (total > 0)
579 break;
580 if (errno == EAGAIN) {
581 Py_DECREF(result);
582 Py_RETURN_NONE;
583 }
584 Py_DECREF(result);
585 PyErr_SetFromErrno(PyExc_IOError);
586 return NULL;
587 }
588 total += n;
589 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000590
Antoine Pitroub26dc462010-05-05 16:27:30 +0000591 if (PyBytes_GET_SIZE(result) > total) {
592 if (_PyBytes_Resize(&result, total) < 0) {
593 /* This should never happen, but just in case */
594 Py_DECREF(result);
595 return NULL;
596 }
597 }
598 return result;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000599}
600
601static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000602fileio_read(fileio *self, PyObject *args)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000603{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000604 char *ptr;
605 Py_ssize_t n;
606 Py_ssize_t size = -1;
607 PyObject *bytes;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000608
Antoine Pitroub26dc462010-05-05 16:27:30 +0000609 if (self->fd < 0)
610 return err_closed();
611 if (!self->readable)
612 return err_mode("reading");
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000613
Antoine Pitroub26dc462010-05-05 16:27:30 +0000614 if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size))
615 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000616
Antoine Pitroub26dc462010-05-05 16:27:30 +0000617 if (size < 0) {
618 return fileio_readall(self);
619 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000620
Antoine Pitroub26dc462010-05-05 16:27:30 +0000621 bytes = PyBytes_FromStringAndSize(NULL, size);
622 if (bytes == NULL)
623 return NULL;
624 ptr = PyBytes_AS_STRING(bytes);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000625
Antoine Pitroub26dc462010-05-05 16:27:30 +0000626 if (_PyVerify_fd(self->fd)) {
627 Py_BEGIN_ALLOW_THREADS
628 errno = 0;
629 n = read(self->fd, ptr, size);
630 Py_END_ALLOW_THREADS
631 } else
632 n = -1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000633
Antoine Pitroub26dc462010-05-05 16:27:30 +0000634 if (n < 0) {
635 Py_DECREF(bytes);
636 if (errno == EAGAIN)
637 Py_RETURN_NONE;
638 PyErr_SetFromErrno(PyExc_IOError);
639 return NULL;
640 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000641
Antoine Pitroub26dc462010-05-05 16:27:30 +0000642 if (n != size) {
643 if (_PyBytes_Resize(&bytes, n) < 0) {
644 Py_DECREF(bytes);
645 return NULL;
646 }
647 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000648
Antoine Pitroub26dc462010-05-05 16:27:30 +0000649 return (PyObject *) bytes;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000650}
651
652static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000653fileio_write(fileio *self, PyObject *args)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000654{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000655 Py_buffer pbuf;
656 Py_ssize_t n;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000657
Antoine Pitroub26dc462010-05-05 16:27:30 +0000658 if (self->fd < 0)
659 return err_closed();
660 if (!self->writable)
661 return err_mode("writing");
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000662
Antoine Pitroub26dc462010-05-05 16:27:30 +0000663 if (!PyArg_ParseTuple(args, "s*", &pbuf))
664 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000665
Antoine Pitroub26dc462010-05-05 16:27:30 +0000666 if (_PyVerify_fd(self->fd)) {
667 Py_BEGIN_ALLOW_THREADS
668 errno = 0;
669 n = write(self->fd, pbuf.buf, pbuf.len);
670 Py_END_ALLOW_THREADS
671 } else
672 n = -1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000673
Antoine Pitroub26dc462010-05-05 16:27:30 +0000674 PyBuffer_Release(&pbuf);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000675
Antoine Pitroub26dc462010-05-05 16:27:30 +0000676 if (n < 0) {
677 if (errno == EAGAIN)
678 Py_RETURN_NONE;
679 PyErr_SetFromErrno(PyExc_IOError);
680 return NULL;
681 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000682
Antoine Pitroub26dc462010-05-05 16:27:30 +0000683 return PyLong_FromSsize_t(n);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000684}
685
686/* XXX Windows support below is likely incomplete */
687
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000688/* Cribbed from posix_lseek() */
689static PyObject *
690portable_lseek(int fd, PyObject *posobj, int whence)
691{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000692 Py_off_t pos, res;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000693
694#ifdef SEEK_SET
Antoine Pitroub26dc462010-05-05 16:27:30 +0000695 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
696 switch (whence) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000697#if SEEK_SET != 0
Antoine Pitroub26dc462010-05-05 16:27:30 +0000698 case 0: whence = SEEK_SET; break;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000699#endif
700#if SEEK_CUR != 1
Antoine Pitroub26dc462010-05-05 16:27:30 +0000701 case 1: whence = SEEK_CUR; break;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000702#endif
Benjamin Peterson8024cec2009-01-20 14:31:08 +0000703#if SEEK_END != 2
Antoine Pitroub26dc462010-05-05 16:27:30 +0000704 case 2: whence = SEEK_END; break;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000705#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000706 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000707#endif /* SEEK_SET */
708
Antoine Pitroub26dc462010-05-05 16:27:30 +0000709 if (posobj == NULL)
710 pos = 0;
711 else {
712 if(PyFloat_Check(posobj)) {
713 PyErr_SetString(PyExc_TypeError, "an integer is required");
714 return NULL;
715 }
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000716#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitroub26dc462010-05-05 16:27:30 +0000717 pos = PyLong_AsLongLong(posobj);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000718#else
Antoine Pitroub26dc462010-05-05 16:27:30 +0000719 pos = PyLong_AsLong(posobj);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000720#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000721 if (PyErr_Occurred())
722 return NULL;
723 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000724
Antoine Pitroub26dc462010-05-05 16:27:30 +0000725 if (_PyVerify_fd(fd)) {
726 Py_BEGIN_ALLOW_THREADS
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000727#if defined(MS_WIN64) || defined(MS_WINDOWS)
Antoine Pitroub26dc462010-05-05 16:27:30 +0000728 res = _lseeki64(fd, pos, whence);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000729#else
Antoine Pitroub26dc462010-05-05 16:27:30 +0000730 res = lseek(fd, pos, whence);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000731#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000732 Py_END_ALLOW_THREADS
733 } else
734 res = -1;
735 if (res < 0)
736 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000737
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000738#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitroub26dc462010-05-05 16:27:30 +0000739 return PyLong_FromLongLong(res);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000740#else
Antoine Pitroub26dc462010-05-05 16:27:30 +0000741 return PyLong_FromLong(res);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000742#endif
743}
744
745static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000746fileio_seek(fileio *self, PyObject *args)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000747{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000748 PyObject *posobj;
749 int whence = 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000750
Antoine Pitroub26dc462010-05-05 16:27:30 +0000751 if (self->fd < 0)
752 return err_closed();
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000753
Antoine Pitroub26dc462010-05-05 16:27:30 +0000754 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
755 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000756
Antoine Pitroub26dc462010-05-05 16:27:30 +0000757 return portable_lseek(self->fd, posobj, whence);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000758}
759
760static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000761fileio_tell(fileio *self, PyObject *args)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000762{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000763 if (self->fd < 0)
764 return err_closed();
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000765
Antoine Pitroub26dc462010-05-05 16:27:30 +0000766 return portable_lseek(self->fd, NULL, 1);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000767}
768
769#ifdef HAVE_FTRUNCATE
770static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000771fileio_truncate(fileio *self, PyObject *args)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000772{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000773 PyObject *posobj = NULL; /* the new size wanted by the user */
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000774#ifndef MS_WINDOWS
Antoine Pitroub26dc462010-05-05 16:27:30 +0000775 Py_off_t pos;
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000776#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000777 int ret;
778 int fd;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000779
Antoine Pitroub26dc462010-05-05 16:27:30 +0000780 fd = self->fd;
781 if (fd < 0)
782 return err_closed();
783 if (!self->writable)
784 return err_mode("writing");
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000785
Antoine Pitroub26dc462010-05-05 16:27:30 +0000786 if (!PyArg_ParseTuple(args, "|O", &posobj))
787 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000788
Antoine Pitroub26dc462010-05-05 16:27:30 +0000789 if (posobj == Py_None || posobj == NULL) {
790 /* Get the current position. */
791 posobj = portable_lseek(fd, NULL, 1);
792 if (posobj == NULL)
793 return NULL;
794 }
795 else {
796 Py_INCREF(posobj);
797 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000798
799#ifdef MS_WINDOWS
Antoine Pitroub26dc462010-05-05 16:27:30 +0000800 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
801 so don't even try using it. */
802 {
803 PyObject *oldposobj, *tempposobj;
804 HANDLE hFile;
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000805
Antoine Pitroub26dc462010-05-05 16:27:30 +0000806 /* we save the file pointer position */
807 oldposobj = portable_lseek(fd, NULL, 1);
808 if (oldposobj == NULL) {
809 Py_DECREF(posobj);
810 return NULL;
811 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000812
Antoine Pitroub26dc462010-05-05 16:27:30 +0000813 /* we then move to the truncation position */
814 tempposobj = portable_lseek(fd, posobj, 0);
815 if (tempposobj == NULL) {
816 Py_DECREF(oldposobj);
817 Py_DECREF(posobj);
818 return NULL;
819 }
820 Py_DECREF(tempposobj);
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000821
Antoine Pitroub26dc462010-05-05 16:27:30 +0000822 /* Truncate. Note that this may grow the file! */
823 Py_BEGIN_ALLOW_THREADS
824 errno = 0;
825 hFile = (HANDLE)_get_osfhandle(fd);
826 ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
827 if (ret == 0) {
828 ret = SetEndOfFile(hFile) == 0;
829 if (ret)
830 errno = EACCES;
831 }
832 Py_END_ALLOW_THREADS
833
834 /* we restore the file pointer position in any case */
835 tempposobj = portable_lseek(fd, oldposobj, 0);
836 Py_DECREF(oldposobj);
837 if (tempposobj == NULL) {
838 Py_DECREF(posobj);
839 return NULL;
840 }
841 Py_DECREF(tempposobj);
842 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000843#else
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000844
845#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitroub26dc462010-05-05 16:27:30 +0000846 pos = PyLong_AsLongLong(posobj);
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000847#else
Antoine Pitroub26dc462010-05-05 16:27:30 +0000848 pos = PyLong_AsLong(posobj);
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000849#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +0000850 if (PyErr_Occurred()){
851 Py_DECREF(posobj);
852 return NULL;
853 }
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000854
Antoine Pitroub26dc462010-05-05 16:27:30 +0000855 Py_BEGIN_ALLOW_THREADS
856 errno = 0;
857 ret = ftruncate(fd, pos);
858 Py_END_ALLOW_THREADS
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000859
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000860#endif /* !MS_WINDOWS */
861
Antoine Pitroub26dc462010-05-05 16:27:30 +0000862 if (ret != 0) {
863 Py_DECREF(posobj);
864 PyErr_SetFromErrno(PyExc_IOError);
865 return NULL;
866 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000867
Antoine Pitroub26dc462010-05-05 16:27:30 +0000868 return posobj;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000869}
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000870#endif /* HAVE_FTRUNCATE */
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000871
872static char *
Antoine Pitrou19690592009-06-12 20:14:08 +0000873mode_string(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000874{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000875 if (self->readable) {
876 if (self->writable)
877 return "rb+";
878 else
879 return "rb";
880 }
881 else
882 return "wb";
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000883}
884
885static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000886fileio_repr(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000887{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000888 PyObject *nameobj, *res;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000889
Antoine Pitroub26dc462010-05-05 16:27:30 +0000890 if (self->fd < 0)
891 return PyString_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou19690592009-06-12 20:14:08 +0000892
Antoine Pitroub26dc462010-05-05 16:27:30 +0000893 nameobj = PyObject_GetAttrString((PyObject *) self, "name");
894 if (nameobj == NULL) {
895 if (PyErr_ExceptionMatches(PyExc_AttributeError))
896 PyErr_Clear();
897 else
898 return NULL;
899 res = PyString_FromFormat("<_io.FileIO fd=%d mode='%s'>",
900 self->fd, mode_string(self));
901 }
902 else {
903 PyObject *repr = PyObject_Repr(nameobj);
904 Py_DECREF(nameobj);
905 if (repr == NULL)
906 return NULL;
907 res = PyString_FromFormat("<_io.FileIO name=%s mode='%s'>",
908 PyString_AS_STRING(repr),
909 mode_string(self));
910 Py_DECREF(repr);
911 }
912 return res;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000913}
914
915static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000916fileio_isatty(fileio *self)
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000917{
Antoine Pitroub26dc462010-05-05 16:27:30 +0000918 long res;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000919
Antoine Pitroub26dc462010-05-05 16:27:30 +0000920 if (self->fd < 0)
921 return err_closed();
922 Py_BEGIN_ALLOW_THREADS
923 res = isatty(self->fd);
924 Py_END_ALLOW_THREADS
925 return PyBool_FromLong(res);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000926}
927
928
929PyDoc_STRVAR(fileio_doc,
930"file(name: str[, mode: str]) -> file IO object\n"
931"\n"
932"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
Antoine Pitroub26dc462010-05-05 16:27:30 +0000933"writing or appending. The file will be created if it doesn't exist\n"
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000934"when opened for writing or appending; it will be truncated when\n"
935"opened for writing. Add a '+' to the mode to allow simultaneous\n"
936"reading and writing.");
937
938PyDoc_STRVAR(read_doc,
939"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
940"\n"
941"Only makes one system call, so less data may be returned than requested\n"
942"In non-blocking mode, returns None if no data is available.\n"
943"On end-of-file, returns ''.");
944
945PyDoc_STRVAR(readall_doc,
946"readall() -> bytes. read all data from the file, returned as bytes.\n"
947"\n"
948"In non-blocking mode, returns as much as is immediately available,\n"
949"or None if no data is available. On end-of-file, returns ''.");
950
951PyDoc_STRVAR(write_doc,
952"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
953"\n"
954"Only makes one system call, so not all of the data may be written.\n"
955"The number of bytes actually written is returned.");
956
957PyDoc_STRVAR(fileno_doc,
958"fileno() -> int. \"file descriptor\".\n"
959"\n"
960"This is needed for lower-level file interfaces, such the fcntl module.");
961
962PyDoc_STRVAR(seek_doc,
963"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
964"\n"
965"Argument offset is a byte count. Optional argument whence defaults to\n"
966"0 (offset from start of file, offset should be >= 0); other values are 1\n"
967"(move relative to current position, positive or negative), and 2 (move\n"
968"relative to end of file, usually negative, although many platforms allow\n"
969"seeking beyond the end of a file)."
970"\n"
971"Note that not all file objects are seekable.");
972
973#ifdef HAVE_FTRUNCATE
974PyDoc_STRVAR(truncate_doc,
Antoine Pitroub26dc462010-05-05 16:27:30 +0000975"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000976"\n"
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000977"Size defaults to the current file position, as returned by tell()."
978"The current file position is changed to the value of size.");
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000979#endif
980
981PyDoc_STRVAR(tell_doc,
Antoine Pitroub26dc462010-05-05 16:27:30 +0000982"tell() -> int. Current file position");
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000983
984PyDoc_STRVAR(readinto_doc,
Antoine Pitrou19690592009-06-12 20:14:08 +0000985"readinto() -> Same as RawIOBase.readinto().");
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000986
987PyDoc_STRVAR(close_doc,
988"close() -> None. Close the file.\n"
989"\n"
990"A closed file cannot be used for further I/O operations. close() may be\n"
991"called more than once without error. Changes the fileno to -1.");
992
993PyDoc_STRVAR(isatty_doc,
994"isatty() -> bool. True if the file is connected to a tty device.");
995
996PyDoc_STRVAR(seekable_doc,
997"seekable() -> bool. True if file supports random-access.");
998
999PyDoc_STRVAR(readable_doc,
1000"readable() -> bool. True if file was opened in a read mode.");
1001
1002PyDoc_STRVAR(writable_doc,
1003"writable() -> bool. True if file was opened in a write mode.");
1004
1005static PyMethodDef fileio_methods[] = {
Antoine Pitroub26dc462010-05-05 16:27:30 +00001006 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
1007 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
1008 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
1009 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
1010 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
1011 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001012#ifdef HAVE_FTRUNCATE
Antoine Pitroub26dc462010-05-05 16:27:30 +00001013 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001014#endif
Antoine Pitroub26dc462010-05-05 16:27:30 +00001015 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
1016 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
1017 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
1018 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
1019 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
1020 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
1021 {NULL, NULL} /* sentinel */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001022};
1023
1024/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1025
1026static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +00001027get_closed(fileio *self, void *closure)
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001028{
Antoine Pitroub26dc462010-05-05 16:27:30 +00001029 return PyBool_FromLong((long)(self->fd < 0));
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001030}
1031
1032static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +00001033get_closefd(fileio *self, void *closure)
Amaury Forgeot d'Arc32265652008-11-20 23:34:31 +00001034{
Antoine Pitroub26dc462010-05-05 16:27:30 +00001035 return PyBool_FromLong((long)(self->closefd));
Amaury Forgeot d'Arc32265652008-11-20 23:34:31 +00001036}
1037
1038static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +00001039get_mode(fileio *self, void *closure)
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001040{
Antoine Pitroub26dc462010-05-05 16:27:30 +00001041 return PyUnicode_FromString(mode_string(self));
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001042}
1043
1044static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitroub26dc462010-05-05 16:27:30 +00001045 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1046 {"closefd", (getter)get_closefd, NULL,
1047 "True if the file descriptor will be closed"},
1048 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1049 {NULL},
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001050};
1051
1052PyTypeObject PyFileIO_Type = {
Antoine Pitroub26dc462010-05-05 16:27:30 +00001053 PyVarObject_HEAD_INIT(NULL, 0)
1054 "_io.FileIO",
1055 sizeof(fileio),
1056 0,
1057 (destructor)fileio_dealloc, /* tp_dealloc */
1058 0, /* tp_print */
1059 0, /* tp_getattr */
1060 0, /* tp_setattr */
1061 0, /* tp_reserved */
1062 (reprfunc)fileio_repr, /* tp_repr */
1063 0, /* tp_as_number */
1064 0, /* tp_as_sequence */
1065 0, /* tp_as_mapping */
1066 0, /* tp_hash */
1067 0, /* tp_call */
1068 0, /* tp_str */
1069 PyObject_GenericGetAttr, /* tp_getattro */
1070 0, /* tp_setattro */
1071 0, /* tp_as_buffer */
1072 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
1073 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1074 fileio_doc, /* tp_doc */
1075 (traverseproc)fileio_traverse, /* tp_traverse */
1076 (inquiry)fileio_clear, /* tp_clear */
1077 0, /* tp_richcompare */
1078 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1079 0, /* tp_iter */
1080 0, /* tp_iternext */
1081 fileio_methods, /* tp_methods */
1082 0, /* tp_members */
1083 fileio_getsetlist, /* tp_getset */
1084 0, /* tp_base */
1085 0, /* tp_dict */
1086 0, /* tp_descr_get */
1087 0, /* tp_descr_set */
1088 offsetof(fileio, dict), /* tp_dictoffset */
1089 fileio_init, /* tp_init */
1090 PyType_GenericAlloc, /* tp_alloc */
1091 fileio_new, /* tp_new */
1092 PyObject_GC_Del, /* tp_free */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001093};