blob: 364748adee6566e893023604a105045760afb284 [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 */
9
10/*
11 * Known likely problems:
12 *
13 * - Files larger then 2**32-1
14 * - Files with unicode filenames
15 * - Passing numbers greater than 2**32-1 when an integer is expected
16 * - Making it work on Windows and other oddball platforms
17 *
18 * To Do:
19 *
20 * - autoconfify header file inclusion
Guido van Rossuma9e20242007-03-08 00:43:48 +000021 */
22
23#ifdef MS_WINDOWS
24/* can simulate truncate with Win32 API functions; see file_truncate */
25#define HAVE_FTRUNCATE
26#define WIN32_LEAN_AND_MEAN
27#include <windows.h>
28#endif
29
30typedef struct {
31 PyObject_HEAD
32 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +000033 int readable;
34 int writable;
35 int seekable; /* -1 means unknown */
36 PyObject *weakreflist;
37} PyFileIOObject;
38
Collin Winteraf334382007-03-08 21:46:15 +000039PyTypeObject PyFileIO_Type;
40
Guido van Rossuma9e20242007-03-08 00:43:48 +000041#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
42
43/* Note: if this function is changed so that it can return a true value,
44 * then we need a separate function for __exit__
45 */
46static PyObject *
47fileio_close(PyFileIOObject *self)
48{
49 if (self->fd >= 0) {
Guido van Rossumb0428152007-04-08 17:44:42 +000050 int fd = self->fd;
51 self->fd = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +000052 Py_BEGIN_ALLOW_THREADS
53 errno = 0;
Guido van Rossumb0428152007-04-08 17:44:42 +000054 close(fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +000055 Py_END_ALLOW_THREADS
56 if (errno < 0) {
57 PyErr_SetFromErrno(PyExc_IOError);
58 return NULL;
59 }
Guido van Rossuma9e20242007-03-08 00:43:48 +000060 }
61
62 Py_RETURN_NONE;
63}
64
65static PyObject *
66fileio_new(PyTypeObject *type, PyObject *args, PyObject *kews)
67{
68 PyFileIOObject *self;
69
70 assert(type != NULL && type->tp_alloc != NULL);
71
72 self = (PyFileIOObject *) type->tp_alloc(type, 0);
73 if (self != NULL) {
74 self->fd = -1;
75 self->weakreflist = NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +000076 }
77
78 return (PyObject *) self;
79}
80
81/* On Unix, open will succeed for directories.
82 In Python, there should be no file objects referring to
83 directories, so we need a check. */
84
85static int
86dircheck(PyFileIOObject* self)
87{
88#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
89 struct stat buf;
90 if (self->fd < 0)
91 return 0;
92 if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
93#ifdef HAVE_STRERROR
94 char *msg = strerror(EISDIR);
95#else
96 char *msg = "Is a directory";
97#endif
98 PyObject *exc;
99 PyObject *closeresult = fileio_close(self);
100 Py_DECREF(closeresult);
Guido van Rossum53807da2007-04-10 19:01:47 +0000101
Guido van Rossuma9e20242007-03-08 00:43:48 +0000102 exc = PyObject_CallFunction(PyExc_IOError, "(is)",
103 EISDIR, msg);
104 PyErr_SetObject(PyExc_IOError, exc);
105 Py_XDECREF(exc);
106 return -1;
107 }
108#endif
109 return 0;
110}
111
112
113static int
114fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
115{
116 PyFileIOObject *self = (PyFileIOObject *) oself;
Guido van Rossumb0428152007-04-08 17:44:42 +0000117 static char *kwlist[] = {"file", "mode", NULL};
Guido van Rossuma9e20242007-03-08 00:43:48 +0000118 char *name = NULL;
119 char *mode = "r";
Guido van Rossum53807da2007-04-10 19:01:47 +0000120 char *s;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000121 int wideargument = 0;
122 int ret = 0;
123 int rwa = 0, plus = 0, append = 0;
124 int flags = 0;
Guido van Rossumb0428152007-04-08 17:44:42 +0000125 int fd = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000126
127 assert(PyFileIO_Check(oself));
128 if (self->fd >= 0)
129 {
130 /* Have to close the existing file first. */
131 PyObject *closeresult = fileio_close(self);
132 if (closeresult == NULL)
133 return -1;
134 Py_DECREF(closeresult);
135 }
136
Guido van Rossumb0428152007-04-08 17:44:42 +0000137 if (PyArg_ParseTupleAndKeywords(args, kwds, "i|s:fileio",
138 kwlist, &fd, &mode)) {
139 if (fd < 0) {
140 PyErr_SetString(PyExc_ValueError,
141 "Negative filedescriptor");
142 return -1;
143 }
144 }
145 else {
146 PyErr_Clear();
147
Guido van Rossuma9e20242007-03-08 00:43:48 +0000148#ifdef Py_WIN_WIDE_FILENAMES
Guido van Rossumb0428152007-04-08 17:44:42 +0000149 if (GetVersion() < 0x80000000) {
150 /* On NT, so wide API available */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000151 PyObject *po;
152 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|s:fileio",
153 kwlist, &po, &mode)) {
154 wideargument = 1;
155 } else {
156 /* Drop the argument parsing error as narrow
157 strings are also valid. */
158 PyErr_Clear();
159 }
160
161 PyErr_SetString(PyExc_NotImplementedError,
Guido van Rossumb0428152007-04-08 17:44:42 +0000162 "Windows wide filenames are not yet supported");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000163 goto error;
Guido van Rossumb0428152007-04-08 17:44:42 +0000164 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000165#endif
166
Guido van Rossumb0428152007-04-08 17:44:42 +0000167 if (!wideargument) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000168 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|s:fileio",
169 kwlist,
170 Py_FileSystemDefaultEncoding,
171 &name, &mode))
172 goto error;
Guido van Rossumb0428152007-04-08 17:44:42 +0000173 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000174 }
175
176 self->readable = self->writable = 0;
Guido van Rossum53807da2007-04-10 19:01:47 +0000177 self->seekable = -1;
178 s = mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000179 while (*s) {
180 switch (*s++) {
181 case 'r':
182 if (rwa) {
183 bad_mode:
184 PyErr_SetString(PyExc_ValueError,
185 "Must have exactly one of read/write/append mode");
186 goto error;
187 }
188 rwa = 1;
189 self->readable = 1;
190 break;
191 case 'w':
192 if (rwa)
193 goto bad_mode;
194 rwa = 1;
195 self->writable = 1;
196 flags |= O_CREAT | O_TRUNC;
197 break;
198 case 'a':
199 if (rwa)
200 goto bad_mode;
201 rwa = 1;
202 self->writable = 1;
203 flags |= O_CREAT;
204 append = 1;
205 break;
206 case '+':
207 if (plus)
208 goto bad_mode;
209 self->readable = self->writable = 1;
210 plus = 1;
211 break;
212 default:
213 PyErr_Format(PyExc_ValueError,
214 "invalid mode: %.200s", mode);
215 goto error;
216 }
217 }
218
219 if (!rwa)
220 goto bad_mode;
221
222 if (self->readable && self->writable)
223 flags |= O_RDWR;
224 else if (self->readable)
225 flags |= O_RDONLY;
226 else
227 flags |= O_WRONLY;
228
229#ifdef O_BINARY
230 flags |= O_BINARY;
231#endif
232
Guido van Rossumb0428152007-04-08 17:44:42 +0000233 if (fd >= 0) {
234 self->fd = fd;
Guido van Rossumb0428152007-04-08 17:44:42 +0000235 }
236 else {
237 Py_BEGIN_ALLOW_THREADS
238 errno = 0;
239 self->fd = open(name, flags, 0666);
240 Py_END_ALLOW_THREADS
241 if (self->fd < 0 || dircheck(self) < 0) {
242 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
243 goto error;
244 }
Walter Dörwald3a77c7a2007-06-06 16:31:14 +0000245 if (append) {
246 int result;
247 Py_BEGIN_ALLOW_THREADS
248 errno = 0;
249 result = lseek(self->fd, 0, SEEK_END);
250 Py_END_ALLOW_THREADS
251 if (result < 0) {
252 close(self->fd);
253 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
254 goto error;
255 }
256 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000257 }
258
259 goto done;
260
261 error:
262 ret = -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000263
Guido van Rossuma9e20242007-03-08 00:43:48 +0000264 done:
265 PyMem_Free(name);
266 return ret;
267}
268
269static void
270fileio_dealloc(PyFileIOObject *self)
271{
272 if (self->weakreflist != NULL)
273 PyObject_ClearWeakRefs((PyObject *) self);
274
Guido van Rossum53807da2007-04-10 19:01:47 +0000275 if (self->fd >= 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000276 PyObject *closeresult = fileio_close(self);
277 if (closeresult == NULL) {
278#ifdef HAVE_STRERROR
Guido van Rossum53807da2007-04-10 19:01:47 +0000279 PySys_WriteStderr("close failed: [Errno %d] %s\n",
280 errno, strerror(errno));
Guido van Rossuma9e20242007-03-08 00:43:48 +0000281#else
282 PySys_WriteStderr("close failed: [Errno %d]\n", errno);
283#endif
Guido van Rossum53807da2007-04-10 19:01:47 +0000284 } else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000285 Py_DECREF(closeresult);
286 }
287
288 self->ob_type->tp_free((PyObject *)self);
289}
290
291static PyObject *
292err_closed(void)
293{
294 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
295 return NULL;
296}
297
298static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000299err_mode(char *action)
300{
301 PyErr_Format(PyExc_ValueError, "File not open for %s", action);
302 return NULL;
303}
304
305static PyObject *
Guido van Rossuma9e20242007-03-08 00:43:48 +0000306fileio_fileno(PyFileIOObject *self)
307{
308 if (self->fd < 0)
309 return err_closed();
310 return PyInt_FromLong((long) self->fd);
311}
312
313static PyObject *
314fileio_readable(PyFileIOObject *self)
315{
316 if (self->fd < 0)
317 return err_closed();
318 return PyInt_FromLong((long) self->readable);
319}
320
321static PyObject *
322fileio_writable(PyFileIOObject *self)
323{
324 if (self->fd < 0)
325 return err_closed();
326 return PyInt_FromLong((long) self->writable);
327}
328
329static PyObject *
330fileio_seekable(PyFileIOObject *self)
331{
332 if (self->fd < 0)
333 return err_closed();
334 if (self->seekable < 0) {
335 int ret;
336 Py_BEGIN_ALLOW_THREADS
337 ret = lseek(self->fd, 0, SEEK_CUR);
338 Py_END_ALLOW_THREADS
339 if (ret < 0)
340 self->seekable = 0;
341 else
342 self->seekable = 1;
343 }
344 return PyInt_FromLong((long) self->seekable);
345}
346
347static PyObject *
348fileio_readinto(PyFileIOObject *self, PyObject *args)
349{
350 char *ptr;
351 Py_ssize_t n;
Guido van Rossum53807da2007-04-10 19:01:47 +0000352
Guido van Rossuma9e20242007-03-08 00:43:48 +0000353 if (self->fd < 0)
354 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000355 if (!self->readable)
356 return err_mode("reading");
357
Guido van Rossuma9e20242007-03-08 00:43:48 +0000358 if (!PyArg_ParseTuple(args, "w#", &ptr, &n))
359 return NULL;
360
361 Py_BEGIN_ALLOW_THREADS
362 errno = 0;
363 n = read(self->fd, ptr, n);
364 Py_END_ALLOW_THREADS
365 if (n < 0) {
366 if (errno == EAGAIN)
367 Py_RETURN_NONE;
368 PyErr_SetFromErrno(PyExc_IOError);
369 return NULL;
370 }
371
372 return PyInt_FromLong(n);
373}
374
375static PyObject *
376fileio_read(PyFileIOObject *self, PyObject *args)
377{
378 char *ptr;
379 Py_ssize_t n, size;
380 PyObject *bytes;
381
382 if (self->fd < 0)
383 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000384 if (!self->readable)
385 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000386
387 if (!PyArg_ParseTuple(args, "i", &size))
388 return NULL;
389
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000390 if (size < 0) {
391 PyErr_SetString(PyExc_ValueError,
392 "negative read count");
393 return NULL;
394 }
395
Guido van Rossuma9e20242007-03-08 00:43:48 +0000396 bytes = PyBytes_FromStringAndSize(NULL, size);
397 if (bytes == NULL)
398 return NULL;
399 ptr = PyBytes_AsString(bytes);
400
401 Py_BEGIN_ALLOW_THREADS
402 errno = 0;
403 n = read(self->fd, ptr, size);
404 Py_END_ALLOW_THREADS
405
406 if (n < 0) {
407 if (errno == EAGAIN)
408 Py_RETURN_NONE;
409 PyErr_SetFromErrno(PyExc_IOError);
410 return NULL;
411 }
412
413 if (n != size) {
414 if (PyBytes_Resize(bytes, n) < 0) {
415 Py_DECREF(bytes);
Guido van Rossum53807da2007-04-10 19:01:47 +0000416 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000417 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000418 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000419
420 return (PyObject *) bytes;
421}
422
423static PyObject *
424fileio_write(PyFileIOObject *self, PyObject *args)
425{
426 Py_ssize_t n;
427 char *ptr;
428
429 if (self->fd < 0)
430 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000431 if (!self->writable)
432 return err_mode("writing");
433
Guido van Rossuma9e20242007-03-08 00:43:48 +0000434 if (!PyArg_ParseTuple(args, "s#", &ptr, &n))
435 return NULL;
436
437 Py_BEGIN_ALLOW_THREADS
438 errno = 0;
439 n = write(self->fd, ptr, n);
440 Py_END_ALLOW_THREADS
441
442 if (n < 0) {
443 if (errno == EAGAIN)
444 Py_RETURN_NONE;
445 PyErr_SetFromErrno(PyExc_IOError);
446 return NULL;
447 }
448
449 return PyInt_FromLong(n);
450}
451
Guido van Rossum53807da2007-04-10 19:01:47 +0000452/* XXX Windows support below is likely incomplete */
453
454#if defined(MS_WIN64) || defined(MS_WINDOWS)
455typedef PY_LONG_LONG Py_off_t;
456#else
457typedef off_t Py_off_t;
458#endif
459
460/* Cribbed from posix_lseek() */
461static PyObject *
462portable_lseek(int fd, PyObject *posobj, int whence)
463{
464 Py_off_t pos, res;
465
466#ifdef SEEK_SET
467 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
468 switch (whence) {
469#if SEEK_SET != 0
470 case 0: whence = SEEK_SET; break;
471#endif
472#if SEEK_CUR != 1
473 case 1: whence = SEEK_CUR; break;
474#endif
475#if SEEL_END != 2
476 case 2: whence = SEEK_END; break;
477#endif
478 }
479#endif /* SEEK_SET */
480
481 if (posobj == NULL)
482 pos = 0;
483 else {
484#if !defined(HAVE_LARGEFILE_SUPPORT)
485 pos = PyInt_AsLong(posobj);
486#else
487 pos = PyLong_Check(posobj) ?
488 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
489#endif
490 if (PyErr_Occurred())
491 return NULL;
492 }
493
494 Py_BEGIN_ALLOW_THREADS
495#if defined(MS_WIN64) || defined(MS_WINDOWS)
496 res = _lseeki64(fd, pos, whence);
497#else
498 res = lseek(fd, pos, whence);
499#endif
500 Py_END_ALLOW_THREADS
501 if (res < 0)
502 return PyErr_SetFromErrno(PyExc_IOError);
503
504#if !defined(HAVE_LARGEFILE_SUPPORT)
505 return PyInt_FromLong(res);
506#else
507 return PyLong_FromLongLong(res);
508#endif
509}
510
Guido van Rossuma9e20242007-03-08 00:43:48 +0000511static PyObject *
512fileio_seek(PyFileIOObject *self, PyObject *args)
513{
Guido van Rossum53807da2007-04-10 19:01:47 +0000514 PyObject *posobj;
515 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000516
517 if (self->fd < 0)
518 return err_closed();
519
Guido van Rossum53807da2007-04-10 19:01:47 +0000520 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000521 return NULL;
522
Guido van Rossum53807da2007-04-10 19:01:47 +0000523 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000524}
525
526static PyObject *
527fileio_tell(PyFileIOObject *self, PyObject *args)
528{
Guido van Rossuma9e20242007-03-08 00:43:48 +0000529 if (self->fd < 0)
530 return err_closed();
531
Guido van Rossum53807da2007-04-10 19:01:47 +0000532 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000533}
534
Guido van Rossuma9e20242007-03-08 00:43:48 +0000535static PyObject *
536fileio_truncate(PyFileIOObject *self, PyObject *args)
537{
Guido van Rossum53807da2007-04-10 19:01:47 +0000538 PyObject *posobj = NULL;
539 Py_off_t pos;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000540 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000541
Guido van Rossum53807da2007-04-10 19:01:47 +0000542 fd = self->fd;
543 if (fd < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000544 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000545 if (!self->writable)
546 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000547
Guido van Rossum53807da2007-04-10 19:01:47 +0000548 if (!PyArg_ParseTuple(args, "|O", &posobj))
549 return NULL;
550
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000551 if (posobj == Py_None || posobj == NULL) {
552 posobj = portable_lseek(fd, NULL, 1);
553 if (posobj == NULL)
554 return NULL;
555 }
556 else {
557 Py_INCREF(posobj);
558 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000559
560#if !defined(HAVE_LARGEFILE_SUPPORT)
561 pos = PyInt_AsLong(posobj);
562#else
563 pos = PyLong_Check(posobj) ?
564 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
565#endif
Guido van Rossum87429772007-04-10 21:06:59 +0000566 if (PyErr_Occurred()) {
567 Py_DECREF(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000568 return NULL;
Guido van Rossum87429772007-04-10 21:06:59 +0000569 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000570
Guido van Rossuma9e20242007-03-08 00:43:48 +0000571 Py_BEGIN_ALLOW_THREADS
572 errno = 0;
Guido van Rossum53807da2007-04-10 19:01:47 +0000573 pos = ftruncate(fd, pos);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000574 Py_END_ALLOW_THREADS
575
Guido van Rossum87429772007-04-10 21:06:59 +0000576 if (pos < 0) {
577 Py_DECREF(posobj);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000578 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum87429772007-04-10 21:06:59 +0000579 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000580
Guido van Rossum87429772007-04-10 21:06:59 +0000581 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000582}
Guido van Rossum53807da2007-04-10 19:01:47 +0000583
584static char *
585mode_string(PyFileIOObject *self)
586{
587 if (self->readable) {
588 if (self->writable)
589 return "r+";
590 else
591 return "r";
592 }
593 else
594 return "w";
595}
Guido van Rossuma9e20242007-03-08 00:43:48 +0000596
597static PyObject *
598fileio_repr(PyFileIOObject *self)
599{
Guido van Rossum53807da2007-04-10 19:01:47 +0000600 if (self->fd < 0)
Walter Dörwald1ab83302007-05-18 17:15:44 +0000601 return PyUnicode_FromFormat("_fileio._FileIO(-1)");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000602
Walter Dörwald1ab83302007-05-18 17:15:44 +0000603 return PyUnicode_FromFormat("_fileio._FileIO(%d, '%s')",
Guido van Rossum53807da2007-04-10 19:01:47 +0000604 self->fd, mode_string(self));
Guido van Rossuma9e20242007-03-08 00:43:48 +0000605}
606
607static PyObject *
608fileio_isatty(PyFileIOObject *self)
609{
610 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000611
Guido van Rossuma9e20242007-03-08 00:43:48 +0000612 if (self->fd < 0)
613 return err_closed();
614 Py_BEGIN_ALLOW_THREADS
615 res = isatty(self->fd);
616 Py_END_ALLOW_THREADS
617 return PyBool_FromLong(res);
618}
619
Guido van Rossuma9e20242007-03-08 00:43:48 +0000620
621PyDoc_STRVAR(fileio_doc,
622"file(name: str[, mode: str]) -> file IO object\n"
623"\n"
624"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
625"writing or appending. The file will be created if it doesn't exist\n"
626"when opened for writing or appending; it will be truncated when\n"
627"opened for writing. Add a '+' to the mode to allow simultaneous\n"
628"reading and writing.");
629
630PyDoc_STRVAR(read_doc,
631"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
632"\n"
633"Only makes one system call, so less data may be returned than requested\n"
634"In non-blocking mode, returns None if no data is available. On\n"
635"end-of-file, returns 0.");
636
637PyDoc_STRVAR(write_doc,
638"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
639"\n"
640"Only makes one system call, so not all of the data may be written.\n"
641"The number of bytes actually written is returned.");
642
643PyDoc_STRVAR(fileno_doc,
644"fileno() -> int. \"file descriptor\".\n"
645"\n"
646"This is needed for lower-level file interfaces, such the fcntl module.");
647
648PyDoc_STRVAR(seek_doc,
649"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
650"\n"
651"Argument offset is a byte count. Optional argument whence defaults to\n"
652"0 (offset from start of file, offset should be >= 0); other values are 1\n"
653"(move relative to current position, positive or negative), and 2 (move\n"
654"relative to end of file, usually negative, although many platforms allow\n"
655"seeking beyond the end of a file)."
656"\n"
657"Note that not all file objects are seekable.");
658
659PyDoc_STRVAR(truncate_doc,
660"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
661"\n"
662"Size defaults to the current file position, as returned by tell().");
663
664PyDoc_STRVAR(tell_doc,
665"tell() -> int. Current file position");
666
667PyDoc_STRVAR(readinto_doc,
668"readinto() -> Undocumented. Don't use this; it may go away.");
669
670PyDoc_STRVAR(close_doc,
671"close() -> None. Close the file.\n"
672"\n"
673"A closed file cannot be used for further I/O operations. close() may be\n"
674"called more than once without error. Changes the fileno to -1.");
675
676PyDoc_STRVAR(isatty_doc,
677"isatty() -> bool. True if the file is connected to a tty device.");
678
Guido van Rossuma9e20242007-03-08 00:43:48 +0000679PyDoc_STRVAR(seekable_doc,
680"seekable() -> bool. True if file supports random-access.");
681
682PyDoc_STRVAR(readable_doc,
683"readable() -> bool. True if file was opened in a read mode.");
684
685PyDoc_STRVAR(writable_doc,
686"writable() -> bool. True if file was opened in a write mode.");
687
688static PyMethodDef fileio_methods[] = {
689 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
690 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
691 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
692 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
693 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
694 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
695 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
696 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
697 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
698 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
699 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
700 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000701 {NULL, NULL} /* sentinel */
702};
703
Guido van Rossum53807da2007-04-10 19:01:47 +0000704/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
705
Guido van Rossumb0428152007-04-08 17:44:42 +0000706static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000707get_closed(PyFileIOObject *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +0000708{
Guido van Rossum53807da2007-04-10 19:01:47 +0000709 return PyBool_FromLong((long)(self->fd < 0));
710}
711
712static PyObject *
713get_mode(PyFileIOObject *self, void *closure)
714{
715 return PyString_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +0000716}
717
718static PyGetSetDef fileio_getsetlist[] = {
719 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Guido van Rossum53807da2007-04-10 19:01:47 +0000720 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
Guido van Rossumb0428152007-04-08 17:44:42 +0000721 {0},
722};
723
Guido van Rossuma9e20242007-03-08 00:43:48 +0000724PyTypeObject PyFileIO_Type = {
725 PyObject_HEAD_INIT(&PyType_Type)
726 0,
727 "FileIO",
728 sizeof(PyFileIOObject),
729 0,
730 (destructor)fileio_dealloc, /* tp_dealloc */
731 0, /* tp_print */
732 0, /* tp_getattr */
733 0, /* tp_setattr */
734 0, /* tp_compare */
735 (reprfunc)fileio_repr, /* tp_repr */
736 0, /* tp_as_number */
737 0, /* tp_as_sequence */
738 0, /* tp_as_mapping */
739 0, /* tp_hash */
740 0, /* tp_call */
741 0, /* tp_str */
742 PyObject_GenericGetAttr, /* tp_getattro */
743 0, /* tp_setattro */
744 0, /* tp_as_buffer */
745 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
746 fileio_doc, /* tp_doc */
747 0, /* tp_traverse */
748 0, /* tp_clear */
749 0, /* tp_richcompare */
750 offsetof(PyFileIOObject, weakreflist), /* tp_weaklistoffset */
751 0, /* tp_iter */
752 0, /* tp_iternext */
753 fileio_methods, /* tp_methods */
754 0, /* tp_members */
Guido van Rossumb0428152007-04-08 17:44:42 +0000755 fileio_getsetlist, /* tp_getset */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000756 0, /* tp_base */
757 0, /* tp_dict */
758 0, /* tp_descr_get */
759 0, /* tp_descr_set */
760 0, /* tp_dictoffset */
761 fileio_init, /* tp_init */
762 PyType_GenericAlloc, /* tp_alloc */
763 fileio_new, /* tp_new */
764 PyObject_Del, /* tp_free */
765};
766
767static PyMethodDef module_methods[] = {
768 {NULL, NULL}
769};
770
771PyMODINIT_FUNC
772init_fileio(void)
773{
774 PyObject *m; /* a module object */
775
776 m = Py_InitModule3("_fileio", module_methods,
777 "Fast implementation of io.FileIO.");
778 if (m == NULL)
779 return;
780 if (PyType_Ready(&PyFileIO_Type) < 0)
781 return;
782 Py_INCREF(&PyFileIO_Type);
783 PyModule_AddObject(m, "_FileIO", (PyObject *) &PyFileIO_Type);
784}