Added more cross-reference targets and tidied up list of useful handlers.
[python.git] / Modules / _fileio.c
blob0f09ecd8a3b0df09138c8f7084fdca9ece0cb86e
1 /* Author: Daniel Stutzbach */
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 */
11 * Known likely problems:
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
18 * To Do:
20 * - autoconfify header file inclusion
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
30 typedef struct {
31 PyObject_HEAD
32 int fd;
33 unsigned readable : 1;
34 unsigned writable : 1;
35 int seekable : 2; /* -1 means unknown */
36 int closefd : 1;
37 PyObject *weakreflist;
38 } PyFileIOObject;
40 PyTypeObject PyFileIO_Type;
42 #define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
44 /* Returns 0 on success, errno (which is < 0) on failure. */
45 static int
46 internal_close(PyFileIOObject *self)
48 int save_errno = 0;
49 if (self->fd >= 0) {
50 int fd = self->fd;
51 self->fd = -1;
52 Py_BEGIN_ALLOW_THREADS
53 if (close(fd) < 0)
54 save_errno = errno;
55 Py_END_ALLOW_THREADS
57 return save_errno;
60 static PyObject *
61 fileio_close(PyFileIOObject *self)
63 if (!self->closefd) {
64 self->fd = -1;
65 Py_RETURN_NONE;
67 errno = internal_close(self);
68 if (errno < 0) {
69 PyErr_SetFromErrno(PyExc_IOError);
70 return NULL;
73 Py_RETURN_NONE;
76 static PyObject *
77 fileio_new(PyTypeObject *type, PyObject *args, PyObject *kews)
79 PyFileIOObject *self;
81 assert(type != NULL && type->tp_alloc != NULL);
83 self = (PyFileIOObject *) type->tp_alloc(type, 0);
84 if (self != NULL) {
85 self->fd = -1;
86 self->readable = 0;
87 self->writable = 0;
88 self->seekable = -1;
89 self->closefd = 1;
90 self->weakreflist = NULL;
93 return (PyObject *) self;
96 /* On Unix, open will succeed for directories.
97 In Python, there should be no file objects referring to
98 directories, so we need a check. */
100 static int
101 dircheck(PyFileIOObject* self, char *name)
103 #if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
104 struct stat buf;
105 if (self->fd < 0)
106 return 0;
107 if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
108 char *msg = strerror(EISDIR);
109 PyObject *exc;
110 internal_close(self);
112 exc = PyObject_CallFunction(PyExc_IOError, "(iss)",
113 EISDIR, msg, name);
114 PyErr_SetObject(PyExc_IOError, exc);
115 Py_XDECREF(exc);
116 return -1;
118 #endif
119 return 0;
122 static int
123 check_fd(int fd)
125 #if defined(HAVE_FSTAT)
126 struct stat buf;
127 if (fstat(fd, &buf) < 0 && errno == EBADF) {
128 PyObject *exc;
129 char *msg = strerror(EBADF);
130 exc = PyObject_CallFunction(PyExc_OSError, "(is)",
131 EBADF, msg);
132 PyErr_SetObject(PyExc_OSError, exc);
133 Py_XDECREF(exc);
134 return -1;
136 #endif
137 return 0;
141 static int
142 fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
144 PyFileIOObject *self = (PyFileIOObject *) oself;
145 static char *kwlist[] = {"file", "mode", "closefd", NULL};
146 char *name = NULL;
147 char *mode = "r";
148 char *s;
149 #ifdef MS_WINDOWS
150 Py_UNICODE *widename = NULL;
151 #endif
152 int ret = 0;
153 int rwa = 0, plus = 0, append = 0;
154 int flags = 0;
155 int fd = -1;
156 int closefd = 1;
158 assert(PyFileIO_Check(oself));
159 if (self->fd >= 0) {
160 /* Have to close the existing file first. */
161 if (internal_close(self) < 0)
162 return -1;
165 if (PyArg_ParseTupleAndKeywords(args, kwds, "i|si:fileio",
166 kwlist, &fd, &mode, &closefd)) {
167 if (fd < 0) {
168 PyErr_SetString(PyExc_ValueError,
169 "Negative filedescriptor");
170 return -1;
172 if (check_fd(fd))
173 return -1;
175 else {
176 PyErr_Clear();
178 #ifdef Py_WIN_WIDE_FILENAMES
179 if (GetVersion() < 0x80000000) {
180 /* On NT, so wide API available */
181 PyObject *po;
182 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:fileio",
183 kwlist, &po, &mode, &closefd)
185 widename = PyUnicode_AS_UNICODE(po);
186 } else {
187 /* Drop the argument parsing error as narrow
188 strings are also valid. */
189 PyErr_Clear();
192 if (widename == NULL)
193 #endif
195 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:fileio",
196 kwlist,
197 Py_FileSystemDefaultEncoding,
198 &name, &mode, &closefd))
199 return -1;
203 s = mode;
204 while (*s) {
205 switch (*s++) {
206 case 'r':
207 if (rwa) {
208 bad_mode:
209 PyErr_SetString(PyExc_ValueError,
210 "Must have exactly one of read/write/append mode");
211 goto error;
213 rwa = 1;
214 self->readable = 1;
215 break;
216 case 'w':
217 if (rwa)
218 goto bad_mode;
219 rwa = 1;
220 self->writable = 1;
221 flags |= O_CREAT | O_TRUNC;
222 break;
223 case 'a':
224 if (rwa)
225 goto bad_mode;
226 rwa = 1;
227 self->writable = 1;
228 flags |= O_CREAT;
229 append = 1;
230 break;
231 case 'b':
232 break;
233 case '+':
234 if (plus)
235 goto bad_mode;
236 self->readable = self->writable = 1;
237 plus = 1;
238 break;
239 default:
240 PyErr_Format(PyExc_ValueError,
241 "invalid mode: %.200s", mode);
242 goto error;
246 if (!rwa)
247 goto bad_mode;
249 if (self->readable && self->writable)
250 flags |= O_RDWR;
251 else if (self->readable)
252 flags |= O_RDONLY;
253 else
254 flags |= O_WRONLY;
256 #ifdef O_BINARY
257 flags |= O_BINARY;
258 #endif
260 #ifdef O_APPEND
261 if (append)
262 flags |= O_APPEND;
263 #endif
265 if (fd >= 0) {
266 self->fd = fd;
267 self->closefd = closefd;
269 else {
270 self->closefd = 1;
271 if (!closefd) {
272 PyErr_SetString(PyExc_ValueError,
273 "Cannot use closefd=False with file name");
274 goto error;
277 Py_BEGIN_ALLOW_THREADS
278 errno = 0;
279 #ifdef MS_WINDOWS
280 if (widename != NULL)
281 self->fd = _wopen(widename, flags, 0666);
282 else
283 #endif
284 self->fd = open(name, flags, 0666);
285 Py_END_ALLOW_THREADS
286 if (self->fd < 0) {
287 #ifdef MS_WINDOWS
288 if (widename != NULL)
289 PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename);
290 else
291 #endif
292 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
293 goto error;
295 if(dircheck(self, name) < 0)
296 goto error;
299 goto done;
301 error:
302 ret = -1;
304 done:
305 PyMem_Free(name);
306 return ret;
309 static void
310 fileio_dealloc(PyFileIOObject *self)
312 if (self->weakreflist != NULL)
313 PyObject_ClearWeakRefs((PyObject *) self);
315 if (self->fd >= 0 && self->closefd) {
316 errno = internal_close(self);
317 if (errno < 0) {
318 PySys_WriteStderr("close failed: [Errno %d] %s\n",
319 errno, strerror(errno));
323 Py_TYPE(self)->tp_free((PyObject *)self);
326 static PyObject *
327 err_closed(void)
329 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
330 return NULL;
333 static PyObject *
334 err_mode(char *action)
336 PyErr_Format(PyExc_ValueError, "File not open for %s", action);
337 return NULL;
340 static PyObject *
341 fileio_fileno(PyFileIOObject *self)
343 if (self->fd < 0)
344 return err_closed();
345 return PyInt_FromLong((long) self->fd);
348 static PyObject *
349 fileio_readable(PyFileIOObject *self)
351 if (self->fd < 0)
352 return err_closed();
353 return PyBool_FromLong((long) self->readable);
356 static PyObject *
357 fileio_writable(PyFileIOObject *self)
359 if (self->fd < 0)
360 return err_closed();
361 return PyBool_FromLong((long) self->writable);
364 static PyObject *
365 fileio_seekable(PyFileIOObject *self)
367 if (self->fd < 0)
368 return err_closed();
369 if (self->seekable < 0) {
370 int ret;
371 Py_BEGIN_ALLOW_THREADS
372 ret = lseek(self->fd, 0, SEEK_CUR);
373 Py_END_ALLOW_THREADS
374 if (ret < 0)
375 self->seekable = 0;
376 else
377 self->seekable = 1;
379 return PyBool_FromLong((long) self->seekable);
382 static PyObject *
383 fileio_readinto(PyFileIOObject *self, PyObject *args)
385 Py_buffer pbuf;
386 Py_ssize_t n;
388 if (self->fd < 0)
389 return err_closed();
390 if (!self->readable)
391 return err_mode("reading");
393 if (!PyArg_ParseTuple(args, "w*", &pbuf))
394 return NULL;
396 Py_BEGIN_ALLOW_THREADS
397 errno = 0;
398 n = read(self->fd, pbuf.buf, pbuf.len);
399 Py_END_ALLOW_THREADS
400 PyBuffer_Release(&pbuf);
401 if (n < 0) {
402 if (errno == EAGAIN)
403 Py_RETURN_NONE;
404 PyErr_SetFromErrno(PyExc_IOError);
405 return NULL;
408 return PyLong_FromSsize_t(n);
411 #define DEFAULT_BUFFER_SIZE (8*1024)
413 static PyObject *
414 fileio_readall(PyFileIOObject *self)
416 PyObject *result;
417 Py_ssize_t total = 0;
418 int n;
420 result = PyString_FromStringAndSize(NULL, DEFAULT_BUFFER_SIZE);
421 if (result == NULL)
422 return NULL;
424 while (1) {
425 Py_ssize_t newsize = total + DEFAULT_BUFFER_SIZE;
426 if (PyString_GET_SIZE(result) < newsize) {
427 if (_PyString_Resize(&result, newsize) < 0) {
428 if (total == 0) {
429 Py_DECREF(result);
430 return NULL;
432 PyErr_Clear();
433 break;
436 Py_BEGIN_ALLOW_THREADS
437 errno = 0;
438 n = read(self->fd,
439 PyString_AS_STRING(result) + total,
440 newsize - total);
441 Py_END_ALLOW_THREADS
442 if (n == 0)
443 break;
444 if (n < 0) {
445 if (total > 0)
446 break;
447 if (errno == EAGAIN) {
448 Py_DECREF(result);
449 Py_RETURN_NONE;
451 Py_DECREF(result);
452 PyErr_SetFromErrno(PyExc_IOError);
453 return NULL;
455 total += n;
458 if (PyString_GET_SIZE(result) > total) {
459 if (_PyString_Resize(&result, total) < 0) {
460 /* This should never happen, but just in case */
461 Py_DECREF(result);
462 return NULL;
465 return result;
468 static PyObject *
469 fileio_read(PyFileIOObject *self, PyObject *args)
471 char *ptr;
472 Py_ssize_t n;
473 Py_ssize_t size = -1;
474 PyObject *bytes;
476 if (self->fd < 0)
477 return err_closed();
478 if (!self->readable)
479 return err_mode("reading");
481 if (!PyArg_ParseTuple(args, "|n", &size))
482 return NULL;
484 if (size < 0) {
485 return fileio_readall(self);
488 bytes = PyString_FromStringAndSize(NULL, size);
489 if (bytes == NULL)
490 return NULL;
491 ptr = PyString_AS_STRING(bytes);
493 Py_BEGIN_ALLOW_THREADS
494 errno = 0;
495 n = read(self->fd, ptr, size);
496 Py_END_ALLOW_THREADS
498 if (n < 0) {
499 if (errno == EAGAIN)
500 Py_RETURN_NONE;
501 PyErr_SetFromErrno(PyExc_IOError);
502 return NULL;
505 if (n != size) {
506 if (_PyString_Resize(&bytes, n) < 0) {
507 Py_DECREF(bytes);
508 return NULL;
512 return (PyObject *) bytes;
515 static PyObject *
516 fileio_write(PyFileIOObject *self, PyObject *args)
518 Py_buffer pbuf;
519 Py_ssize_t n;
521 if (self->fd < 0)
522 return err_closed();
523 if (!self->writable)
524 return err_mode("writing");
526 if (!PyArg_ParseTuple(args, "s*", &pbuf))
527 return NULL;
529 Py_BEGIN_ALLOW_THREADS
530 errno = 0;
531 n = write(self->fd, pbuf.buf, pbuf.len);
532 Py_END_ALLOW_THREADS
534 PyBuffer_Release(&pbuf);
536 if (n < 0) {
537 if (errno == EAGAIN)
538 Py_RETURN_NONE;
539 PyErr_SetFromErrno(PyExc_IOError);
540 return NULL;
543 return PyLong_FromSsize_t(n);
546 /* XXX Windows support below is likely incomplete */
548 #if defined(MS_WIN64) || defined(MS_WINDOWS)
549 typedef PY_LONG_LONG Py_off_t;
550 #else
551 typedef off_t Py_off_t;
552 #endif
554 /* Cribbed from posix_lseek() */
555 static PyObject *
556 portable_lseek(int fd, PyObject *posobj, int whence)
558 Py_off_t pos, res;
560 #ifdef SEEK_SET
561 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
562 switch (whence) {
563 #if SEEK_SET != 0
564 case 0: whence = SEEK_SET; break;
565 #endif
566 #if SEEK_CUR != 1
567 case 1: whence = SEEK_CUR; break;
568 #endif
569 #if SEEL_END != 2
570 case 2: whence = SEEK_END; break;
571 #endif
573 #endif /* SEEK_SET */
575 if (posobj == NULL)
576 pos = 0;
577 else {
578 if(PyFloat_Check(posobj)) {
579 PyErr_SetString(PyExc_TypeError, "an integer is required");
580 return NULL;
582 #if defined(HAVE_LARGEFILE_SUPPORT)
583 pos = PyLong_AsLongLong(posobj);
584 #else
585 pos = PyLong_AsLong(posobj);
586 #endif
587 if (PyErr_Occurred())
588 return NULL;
591 Py_BEGIN_ALLOW_THREADS
592 #if defined(MS_WIN64) || defined(MS_WINDOWS)
593 res = _lseeki64(fd, pos, whence);
594 #else
595 res = lseek(fd, pos, whence);
596 #endif
597 Py_END_ALLOW_THREADS
598 if (res < 0)
599 return PyErr_SetFromErrno(PyExc_IOError);
601 #if defined(HAVE_LARGEFILE_SUPPORT)
602 return PyLong_FromLongLong(res);
603 #else
604 return PyLong_FromLong(res);
605 #endif
608 static PyObject *
609 fileio_seek(PyFileIOObject *self, PyObject *args)
611 PyObject *posobj;
612 int whence = 0;
614 if (self->fd < 0)
615 return err_closed();
617 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
618 return NULL;
620 return portable_lseek(self->fd, posobj, whence);
623 static PyObject *
624 fileio_tell(PyFileIOObject *self, PyObject *args)
626 if (self->fd < 0)
627 return err_closed();
629 return portable_lseek(self->fd, NULL, 1);
632 #ifdef HAVE_FTRUNCATE
633 static PyObject *
634 fileio_truncate(PyFileIOObject *self, PyObject *args)
636 PyObject *posobj = NULL;
637 Py_off_t pos;
638 int ret;
639 int fd;
641 fd = self->fd;
642 if (fd < 0)
643 return err_closed();
644 if (!self->writable)
645 return err_mode("writing");
647 if (!PyArg_ParseTuple(args, "|O", &posobj))
648 return NULL;
650 if (posobj == Py_None || posobj == NULL) {
651 /* Get the current position. */
652 posobj = portable_lseek(fd, NULL, 1);
653 if (posobj == NULL)
654 return NULL;
656 else {
657 /* Move to the position to be truncated. */
658 posobj = portable_lseek(fd, posobj, 0);
661 #if defined(HAVE_LARGEFILE_SUPPORT)
662 pos = PyLong_AsLongLong(posobj);
663 #else
664 pos = PyLong_AsLong(posobj);
665 #endif
666 if (PyErr_Occurred())
667 return NULL;
669 #ifdef MS_WINDOWS
670 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
671 so don't even try using it. */
673 HANDLE hFile;
675 /* Truncate. Note that this may grow the file! */
676 Py_BEGIN_ALLOW_THREADS
677 errno = 0;
678 hFile = (HANDLE)_get_osfhandle(fd);
679 ret = hFile == (HANDLE)-1;
680 if (ret == 0) {
681 ret = SetEndOfFile(hFile) == 0;
682 if (ret)
683 errno = EACCES;
685 Py_END_ALLOW_THREADS
687 #else
688 Py_BEGIN_ALLOW_THREADS
689 errno = 0;
690 ret = ftruncate(fd, pos);
691 Py_END_ALLOW_THREADS
692 #endif /* !MS_WINDOWS */
694 if (ret != 0) {
695 PyErr_SetFromErrno(PyExc_IOError);
696 return NULL;
699 return posobj;
701 #endif
703 static char *
704 mode_string(PyFileIOObject *self)
706 if (self->readable) {
707 if (self->writable)
708 return "rb+";
709 else
710 return "rb";
712 else
713 return "wb";
716 static PyObject *
717 fileio_repr(PyFileIOObject *self)
719 if (self->fd < 0)
720 return PyString_FromFormat("_fileio._FileIO(-1)");
722 return PyString_FromFormat("_fileio._FileIO(%d, '%s')",
723 self->fd, mode_string(self));
726 static PyObject *
727 fileio_isatty(PyFileIOObject *self)
729 long res;
731 if (self->fd < 0)
732 return err_closed();
733 Py_BEGIN_ALLOW_THREADS
734 res = isatty(self->fd);
735 Py_END_ALLOW_THREADS
736 return PyBool_FromLong(res);
740 PyDoc_STRVAR(fileio_doc,
741 "file(name: str[, mode: str]) -> file IO object\n"
742 "\n"
743 "Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
744 "writing or appending. The file will be created if it doesn't exist\n"
745 "when opened for writing or appending; it will be truncated when\n"
746 "opened for writing. Add a '+' to the mode to allow simultaneous\n"
747 "reading and writing.");
749 PyDoc_STRVAR(read_doc,
750 "read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
751 "\n"
752 "Only makes one system call, so less data may be returned than requested\n"
753 "In non-blocking mode, returns None if no data is available.\n"
754 "On end-of-file, returns ''.");
756 PyDoc_STRVAR(readall_doc,
757 "readall() -> bytes. read all data from the file, returned as bytes.\n"
758 "\n"
759 "In non-blocking mode, returns as much as is immediately available,\n"
760 "or None if no data is available. On end-of-file, returns ''.");
762 PyDoc_STRVAR(write_doc,
763 "write(b: bytes) -> int. Write bytes b to file, return number written.\n"
764 "\n"
765 "Only makes one system call, so not all of the data may be written.\n"
766 "The number of bytes actually written is returned.");
768 PyDoc_STRVAR(fileno_doc,
769 "fileno() -> int. \"file descriptor\".\n"
770 "\n"
771 "This is needed for lower-level file interfaces, such the fcntl module.");
773 PyDoc_STRVAR(seek_doc,
774 "seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
775 "\n"
776 "Argument offset is a byte count. Optional argument whence defaults to\n"
777 "0 (offset from start of file, offset should be >= 0); other values are 1\n"
778 "(move relative to current position, positive or negative), and 2 (move\n"
779 "relative to end of file, usually negative, although many platforms allow\n"
780 "seeking beyond the end of a file)."
781 "\n"
782 "Note that not all file objects are seekable.");
784 #ifdef HAVE_FTRUNCATE
785 PyDoc_STRVAR(truncate_doc,
786 "truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
787 "\n"
788 "Size defaults to the current file position, as returned by tell()."
789 "The current file position is changed to the value of size.");
790 #endif
792 PyDoc_STRVAR(tell_doc,
793 "tell() -> int. Current file position");
795 PyDoc_STRVAR(readinto_doc,
796 "readinto() -> Undocumented. Don't use this; it may go away.");
798 PyDoc_STRVAR(close_doc,
799 "close() -> None. Close the file.\n"
800 "\n"
801 "A closed file cannot be used for further I/O operations. close() may be\n"
802 "called more than once without error. Changes the fileno to -1.");
804 PyDoc_STRVAR(isatty_doc,
805 "isatty() -> bool. True if the file is connected to a tty device.");
807 PyDoc_STRVAR(seekable_doc,
808 "seekable() -> bool. True if file supports random-access.");
810 PyDoc_STRVAR(readable_doc,
811 "readable() -> bool. True if file was opened in a read mode.");
813 PyDoc_STRVAR(writable_doc,
814 "writable() -> bool. True if file was opened in a write mode.");
816 static PyMethodDef fileio_methods[] = {
817 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
818 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
819 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
820 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
821 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
822 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
823 #ifdef HAVE_FTRUNCATE
824 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
825 #endif
826 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
827 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
828 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
829 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
830 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
831 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
832 {NULL, NULL} /* sentinel */
835 /* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
837 static PyObject *
838 get_closed(PyFileIOObject *self, void *closure)
840 return PyBool_FromLong((long)(self->fd < 0));
843 static PyObject *
844 get_closefd(PyFileIOObject *self, void *closure)
846 return PyBool_FromLong((long)(self->closefd));
849 static PyObject *
850 get_mode(PyFileIOObject *self, void *closure)
852 return PyString_FromString(mode_string(self));
855 static PyGetSetDef fileio_getsetlist[] = {
856 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
857 {"closefd", (getter)get_closefd, NULL,
858 "True if the file descriptor will be closed"},
859 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
860 {0},
863 PyTypeObject PyFileIO_Type = {
864 PyVarObject_HEAD_INIT(NULL, 0)
865 "_FileIO",
866 sizeof(PyFileIOObject),
868 (destructor)fileio_dealloc, /* tp_dealloc */
869 0, /* tp_print */
870 0, /* tp_getattr */
871 0, /* tp_setattr */
872 0, /* tp_compare */
873 (reprfunc)fileio_repr, /* tp_repr */
874 0, /* tp_as_number */
875 0, /* tp_as_sequence */
876 0, /* tp_as_mapping */
877 0, /* tp_hash */
878 0, /* tp_call */
879 0, /* tp_str */
880 PyObject_GenericGetAttr, /* tp_getattro */
881 0, /* tp_setattro */
882 0, /* tp_as_buffer */
883 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
884 fileio_doc, /* tp_doc */
885 0, /* tp_traverse */
886 0, /* tp_clear */
887 0, /* tp_richcompare */
888 offsetof(PyFileIOObject, weakreflist), /* tp_weaklistoffset */
889 0, /* tp_iter */
890 0, /* tp_iternext */
891 fileio_methods, /* tp_methods */
892 0, /* tp_members */
893 fileio_getsetlist, /* tp_getset */
894 0, /* tp_base */
895 0, /* tp_dict */
896 0, /* tp_descr_get */
897 0, /* tp_descr_set */
898 0, /* tp_dictoffset */
899 fileio_init, /* tp_init */
900 PyType_GenericAlloc, /* tp_alloc */
901 fileio_new, /* tp_new */
902 PyObject_Del, /* tp_free */
905 static PyMethodDef module_methods[] = {
906 {NULL, NULL}
909 PyMODINIT_FUNC
910 init_fileio(void)
912 PyObject *m; /* a module object */
914 m = Py_InitModule3("_fileio", module_methods,
915 "Fast implementation of io.FileIO.");
916 if (m == NULL)
917 return;
918 if (PyType_Ready(&PyFileIO_Type) < 0)
919 return;
920 Py_INCREF(&PyFileIO_Type);
921 PyModule_AddObject(m, "_FileIO", (PyObject *) &PyFileIO_Type);