blob: 88ce2f1ae203872069b29556a73213cc4c8a276b [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
21 * - Make the ABC RawIO type and inherit from it.
22 *
23 * Unanswered questions:
24 *
Guido van Rossumb0428152007-04-08 17:44:42 +000025 * - Add mode and name properties a la Python 2 file objects?
26 * - Check for readable/writable before attempting to read/write?
Guido van Rossuma9e20242007-03-08 00:43:48 +000027 */
28
29#ifdef MS_WINDOWS
30/* can simulate truncate with Win32 API functions; see file_truncate */
31#define HAVE_FTRUNCATE
32#define WIN32_LEAN_AND_MEAN
33#include <windows.h>
34#endif
35
36typedef struct {
37 PyObject_HEAD
38 int fd;
39 int own_fd; /* 1 means we should close fd */
40 int readable;
41 int writable;
42 int seekable; /* -1 means unknown */
43 PyObject *weakreflist;
44} PyFileIOObject;
45
Collin Winteraf334382007-03-08 21:46:15 +000046PyTypeObject PyFileIO_Type;
47
Guido van Rossuma9e20242007-03-08 00:43:48 +000048#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
49
50/* Note: if this function is changed so that it can return a true value,
51 * then we need a separate function for __exit__
52 */
53static PyObject *
54fileio_close(PyFileIOObject *self)
55{
56 if (self->fd >= 0) {
Guido van Rossumb0428152007-04-08 17:44:42 +000057 int fd = self->fd;
58 self->fd = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +000059 Py_BEGIN_ALLOW_THREADS
60 errno = 0;
Guido van Rossumb0428152007-04-08 17:44:42 +000061 close(fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +000062 Py_END_ALLOW_THREADS
63 if (errno < 0) {
64 PyErr_SetFromErrno(PyExc_IOError);
65 return NULL;
66 }
Guido van Rossuma9e20242007-03-08 00:43:48 +000067 }
68
69 Py_RETURN_NONE;
70}
71
72static PyObject *
73fileio_new(PyTypeObject *type, PyObject *args, PyObject *kews)
74{
75 PyFileIOObject *self;
76
77 assert(type != NULL && type->tp_alloc != NULL);
78
79 self = (PyFileIOObject *) type->tp_alloc(type, 0);
80 if (self != NULL) {
81 self->fd = -1;
82 self->weakreflist = NULL;
83 self->own_fd = 1;
84 self->seekable = -1;
85 }
86
87 return (PyObject *) self;
88}
89
90/* On Unix, open will succeed for directories.
91 In Python, there should be no file objects referring to
92 directories, so we need a check. */
93
94static int
95dircheck(PyFileIOObject* self)
96{
97#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
98 struct stat buf;
99 if (self->fd < 0)
100 return 0;
101 if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
102#ifdef HAVE_STRERROR
103 char *msg = strerror(EISDIR);
104#else
105 char *msg = "Is a directory";
106#endif
107 PyObject *exc;
108 PyObject *closeresult = fileio_close(self);
109 Py_DECREF(closeresult);
110
111 exc = PyObject_CallFunction(PyExc_IOError, "(is)",
112 EISDIR, msg);
113 PyErr_SetObject(PyExc_IOError, exc);
114 Py_XDECREF(exc);
115 return -1;
116 }
117#endif
118 return 0;
119}
120
121
122static int
123fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
124{
125 PyFileIOObject *self = (PyFileIOObject *) oself;
Guido van Rossumb0428152007-04-08 17:44:42 +0000126 static char *kwlist[] = {"file", "mode", NULL};
Guido van Rossuma9e20242007-03-08 00:43:48 +0000127 char *name = NULL;
128 char *mode = "r";
129 char *s;
130 int wideargument = 0;
131 int ret = 0;
132 int rwa = 0, plus = 0, append = 0;
133 int flags = 0;
Guido van Rossumb0428152007-04-08 17:44:42 +0000134 int fd = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000135
136 assert(PyFileIO_Check(oself));
137 if (self->fd >= 0)
138 {
139 /* Have to close the existing file first. */
140 PyObject *closeresult = fileio_close(self);
141 if (closeresult == NULL)
142 return -1;
143 Py_DECREF(closeresult);
144 }
145
Guido van Rossumb0428152007-04-08 17:44:42 +0000146 if (PyArg_ParseTupleAndKeywords(args, kwds, "i|s:fileio",
147 kwlist, &fd, &mode)) {
148 if (fd < 0) {
149 PyErr_SetString(PyExc_ValueError,
150 "Negative filedescriptor");
151 return -1;
152 }
153 }
154 else {
155 PyErr_Clear();
156
Guido van Rossuma9e20242007-03-08 00:43:48 +0000157#ifdef Py_WIN_WIDE_FILENAMES
Guido van Rossumb0428152007-04-08 17:44:42 +0000158 if (GetVersion() < 0x80000000) {
159 /* On NT, so wide API available */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000160 PyObject *po;
161 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|s:fileio",
162 kwlist, &po, &mode)) {
163 wideargument = 1;
164 } else {
165 /* Drop the argument parsing error as narrow
166 strings are also valid. */
167 PyErr_Clear();
168 }
169
170 PyErr_SetString(PyExc_NotImplementedError,
Guido van Rossumb0428152007-04-08 17:44:42 +0000171 "Windows wide filenames are not yet supported");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000172 goto error;
Guido van Rossumb0428152007-04-08 17:44:42 +0000173 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000174#endif
175
Guido van Rossumb0428152007-04-08 17:44:42 +0000176 if (!wideargument) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000177 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|s:fileio",
178 kwlist,
179 Py_FileSystemDefaultEncoding,
180 &name, &mode))
181 goto error;
Guido van Rossumb0428152007-04-08 17:44:42 +0000182 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000183 }
184
185 self->readable = self->writable = 0;
186 s = mode;
187 while (*s) {
188 switch (*s++) {
189 case 'r':
190 if (rwa) {
191 bad_mode:
192 PyErr_SetString(PyExc_ValueError,
193 "Must have exactly one of read/write/append mode");
194 goto error;
195 }
196 rwa = 1;
197 self->readable = 1;
198 break;
199 case 'w':
200 if (rwa)
201 goto bad_mode;
202 rwa = 1;
203 self->writable = 1;
204 flags |= O_CREAT | O_TRUNC;
205 break;
206 case 'a':
207 if (rwa)
208 goto bad_mode;
209 rwa = 1;
210 self->writable = 1;
211 flags |= O_CREAT;
212 append = 1;
213 break;
214 case '+':
215 if (plus)
216 goto bad_mode;
217 self->readable = self->writable = 1;
218 plus = 1;
219 break;
220 default:
221 PyErr_Format(PyExc_ValueError,
222 "invalid mode: %.200s", mode);
223 goto error;
224 }
225 }
226
227 if (!rwa)
228 goto bad_mode;
229
230 if (self->readable && self->writable)
231 flags |= O_RDWR;
232 else if (self->readable)
233 flags |= O_RDONLY;
234 else
235 flags |= O_WRONLY;
236
237#ifdef O_BINARY
238 flags |= O_BINARY;
239#endif
240
Guido van Rossumb0428152007-04-08 17:44:42 +0000241 if (fd >= 0) {
242 self->fd = fd;
243 /* XXX Should we set self->own_fd = 0 ??? */
244 }
245 else {
246 Py_BEGIN_ALLOW_THREADS
247 errno = 0;
248 self->fd = open(name, flags, 0666);
249 Py_END_ALLOW_THREADS
250 if (self->fd < 0 || dircheck(self) < 0) {
251 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
252 goto error;
253 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000254 }
255
256 goto done;
257
258 error:
259 ret = -1;
260
261 done:
262 PyMem_Free(name);
263 return ret;
264}
265
266static void
267fileio_dealloc(PyFileIOObject *self)
268{
269 if (self->weakreflist != NULL)
270 PyObject_ClearWeakRefs((PyObject *) self);
271
272 if (self->fd >= 0 && self->own_fd) {
273 PyObject *closeresult = fileio_close(self);
274 if (closeresult == NULL) {
275#ifdef HAVE_STRERROR
276 PySys_WriteStderr("close failed: [Errno %d] %s\n", errno, strerror(errno));
277#else
278 PySys_WriteStderr("close failed: [Errno %d]\n", errno);
279#endif
280 } else
281 Py_DECREF(closeresult);
282 }
283
284 self->ob_type->tp_free((PyObject *)self);
285}
286
287static PyObject *
288err_closed(void)
289{
290 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
291 return NULL;
292}
293
294static PyObject *
295fileio_fileno(PyFileIOObject *self)
296{
297 if (self->fd < 0)
298 return err_closed();
299 return PyInt_FromLong((long) self->fd);
300}
301
302static PyObject *
303fileio_readable(PyFileIOObject *self)
304{
305 if (self->fd < 0)
306 return err_closed();
307 return PyInt_FromLong((long) self->readable);
308}
309
310static PyObject *
311fileio_writable(PyFileIOObject *self)
312{
313 if (self->fd < 0)
314 return err_closed();
315 return PyInt_FromLong((long) self->writable);
316}
317
318static PyObject *
319fileio_seekable(PyFileIOObject *self)
320{
321 if (self->fd < 0)
322 return err_closed();
323 if (self->seekable < 0) {
324 int ret;
325 Py_BEGIN_ALLOW_THREADS
326 ret = lseek(self->fd, 0, SEEK_CUR);
327 Py_END_ALLOW_THREADS
328 if (ret < 0)
329 self->seekable = 0;
330 else
331 self->seekable = 1;
332 }
333 return PyInt_FromLong((long) self->seekable);
334}
335
336static PyObject *
337fileio_readinto(PyFileIOObject *self, PyObject *args)
338{
339 char *ptr;
340 Py_ssize_t n;
341
342 if (self->fd < 0)
343 return err_closed();
344 if (!PyArg_ParseTuple(args, "w#", &ptr, &n))
345 return NULL;
346
347 Py_BEGIN_ALLOW_THREADS
348 errno = 0;
349 n = read(self->fd, ptr, n);
350 Py_END_ALLOW_THREADS
351 if (n < 0) {
352 if (errno == EAGAIN)
353 Py_RETURN_NONE;
354 PyErr_SetFromErrno(PyExc_IOError);
355 return NULL;
356 }
357
358 return PyInt_FromLong(n);
359}
360
361static PyObject *
362fileio_read(PyFileIOObject *self, PyObject *args)
363{
364 char *ptr;
365 Py_ssize_t n, size;
366 PyObject *bytes;
367
368 if (self->fd < 0)
369 return err_closed();
370
371 if (!PyArg_ParseTuple(args, "i", &size))
372 return NULL;
373
374 bytes = PyBytes_FromStringAndSize(NULL, size);
375 if (bytes == NULL)
376 return NULL;
377 ptr = PyBytes_AsString(bytes);
378
379 Py_BEGIN_ALLOW_THREADS
380 errno = 0;
381 n = read(self->fd, ptr, size);
382 Py_END_ALLOW_THREADS
383
384 if (n < 0) {
385 if (errno == EAGAIN)
386 Py_RETURN_NONE;
387 PyErr_SetFromErrno(PyExc_IOError);
388 return NULL;
389 }
390
391 if (n != size) {
392 if (PyBytes_Resize(bytes, n) < 0) {
393 Py_DECREF(bytes);
394 return NULL;
395 }
396 }
397
398 return (PyObject *) bytes;
399}
400
401static PyObject *
402fileio_write(PyFileIOObject *self, PyObject *args)
403{
404 Py_ssize_t n;
405 char *ptr;
406
407 if (self->fd < 0)
408 return err_closed();
409 if (!PyArg_ParseTuple(args, "s#", &ptr, &n))
410 return NULL;
411
412 Py_BEGIN_ALLOW_THREADS
413 errno = 0;
414 n = write(self->fd, ptr, n);
415 Py_END_ALLOW_THREADS
416
417 if (n < 0) {
418 if (errno == EAGAIN)
419 Py_RETURN_NONE;
420 PyErr_SetFromErrno(PyExc_IOError);
421 return NULL;
422 }
423
424 return PyInt_FromLong(n);
425}
426
427static PyObject *
428fileio_seek(PyFileIOObject *self, PyObject *args)
429{
430 Py_ssize_t offset;
431 Py_ssize_t whence = 0;
432
433 if (self->fd < 0)
434 return err_closed();
435
436 if (!PyArg_ParseTuple(args, "i|i", &offset, &whence))
437 return NULL;
438
439 Py_BEGIN_ALLOW_THREADS
440 errno = 0;
441 offset = lseek(self->fd, offset, whence);
442 Py_END_ALLOW_THREADS
443
444 if (offset < 0) {
445 PyErr_SetFromErrno(PyExc_IOError);
446 return NULL;
447 }
448
449 Py_RETURN_NONE;
450}
451
452static PyObject *
453fileio_tell(PyFileIOObject *self, PyObject *args)
454{
455 Py_ssize_t offset;
456
457 if (self->fd < 0)
458 return err_closed();
459
460 Py_BEGIN_ALLOW_THREADS
461 errno = 0;
462 offset = lseek(self->fd, 0, SEEK_CUR);
463 Py_END_ALLOW_THREADS
464
465 if (offset < 0) {
466 PyErr_SetFromErrno(PyExc_IOError);
467 return NULL;
468 }
469
470 return PyInt_FromLong(offset);
471}
472
473#ifdef HAVE_FTRUNCATE
474static PyObject *
475fileio_truncate(PyFileIOObject *self, PyObject *args)
476{
477 Py_ssize_t length;
478 int ret;
479
480 if (self->fd < 0)
481 return err_closed();
482
483 /* Setup default value */
484 Py_BEGIN_ALLOW_THREADS
485 errno = 0;
486 length = lseek(self->fd, 0, SEEK_CUR);
487 Py_END_ALLOW_THREADS
488
489 if (length < 0) {
490 PyErr_SetFromErrno(PyExc_IOError);
491 return NULL;
492 }
493
494 if (!PyArg_ParseTuple(args, "|i", &length))
495 return NULL;
496
497#ifdef MS_WINDOWS
498 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
499 so don't even try using it. */
500 {
501 HANDLE hFile;
502 Py_ssize_t initialpos;
503
504 /* Have to move current pos to desired endpoint on Windows. */
505 Py_BEGIN_ALLOW_THREADS
506 errno = 0;
507 ret = _portable_fseek(f->f_fp, newsize, SEEK_SET) != 0;
508 Py_END_ALLOW_THREADS
509 if (ret)
510 goto onioerror;
511
512 /* Truncate. Note that this may grow the file! */
513 Py_BEGIN_ALLOW_THREADS
514 errno = 0;
515 hFile = (HANDLE)_get_osfhandle(fileno(f->f_fp));
516 ret = hFile == (HANDLE)-1;
517 if (ret == 0) {
518 ret = SetEndOfFile(hFile) == 0;
519 if (ret)
520 errno = EACCES;
521 }
522 Py_END_ALLOW_THREADS
523 if (ret)
524 goto onioerror;
525 }
526#else
527 Py_BEGIN_ALLOW_THREADS
528 errno = 0;
529 ret = ftruncate(self->fd, length);
530 Py_END_ALLOW_THREADS
531#endif /* !MS_WINDOWS */
532
533 if (ret < 0) {
534 onioerror:
535 PyErr_SetFromErrno(PyExc_IOError);
536 return NULL;
537 }
538
539 /* Return to initial position */
540 Py_BEGIN_ALLOW_THREADS
541 errno = 0;
542 ret = lseek(self->fd, length, SEEK_SET);
543 Py_END_ALLOW_THREADS
544 if (ret < 0)
545 goto onioerror;
546
547 Py_RETURN_NONE;
548}
549#endif
550
551static PyObject *
552fileio_repr(PyFileIOObject *self)
553{
554 PyObject *ret = NULL;
555
556 ret = PyString_FromFormat("<%s file at %p>",
557 self->fd < 0 ? "closed" : "open",
558 self);
559 return ret;
560}
561
562static PyObject *
563fileio_isatty(PyFileIOObject *self)
564{
565 long res;
566
567 if (self->fd < 0)
568 return err_closed();
569 Py_BEGIN_ALLOW_THREADS
570 res = isatty(self->fd);
571 Py_END_ALLOW_THREADS
572 return PyBool_FromLong(res);
573}
574
575static PyObject *
576fileio_self(PyFileIOObject *self)
577{
578 if (self->fd < 0)
579 return err_closed();
580 Py_INCREF(self);
581 return (PyObject *)self;
582}
583
584PyDoc_STRVAR(fileio_doc,
585"file(name: str[, mode: str]) -> file IO object\n"
586"\n"
587"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
588"writing or appending. The file will be created if it doesn't exist\n"
589"when opened for writing or appending; it will be truncated when\n"
590"opened for writing. Add a '+' to the mode to allow simultaneous\n"
591"reading and writing.");
592
593PyDoc_STRVAR(read_doc,
594"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
595"\n"
596"Only makes one system call, so less data may be returned than requested\n"
597"In non-blocking mode, returns None if no data is available. On\n"
598"end-of-file, returns 0.");
599
600PyDoc_STRVAR(write_doc,
601"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
602"\n"
603"Only makes one system call, so not all of the data may be written.\n"
604"The number of bytes actually written is returned.");
605
606PyDoc_STRVAR(fileno_doc,
607"fileno() -> int. \"file descriptor\".\n"
608"\n"
609"This is needed for lower-level file interfaces, such the fcntl module.");
610
611PyDoc_STRVAR(seek_doc,
612"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
613"\n"
614"Argument offset is a byte count. Optional argument whence defaults to\n"
615"0 (offset from start of file, offset should be >= 0); other values are 1\n"
616"(move relative to current position, positive or negative), and 2 (move\n"
617"relative to end of file, usually negative, although many platforms allow\n"
618"seeking beyond the end of a file)."
619"\n"
620"Note that not all file objects are seekable.");
621
622PyDoc_STRVAR(truncate_doc,
623"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
624"\n"
625"Size defaults to the current file position, as returned by tell().");
626
627PyDoc_STRVAR(tell_doc,
628"tell() -> int. Current file position");
629
630PyDoc_STRVAR(readinto_doc,
631"readinto() -> Undocumented. Don't use this; it may go away.");
632
633PyDoc_STRVAR(close_doc,
634"close() -> None. Close the file.\n"
635"\n"
636"A closed file cannot be used for further I/O operations. close() may be\n"
637"called more than once without error. Changes the fileno to -1.");
638
639PyDoc_STRVAR(isatty_doc,
640"isatty() -> bool. True if the file is connected to a tty device.");
641
642PyDoc_STRVAR(enter_doc,
643"__enter__() -> self.");
644
645PyDoc_STRVAR(exit_doc,
646"__exit__(*excinfo) -> None. Closes the file.");
647
648PyDoc_STRVAR(seekable_doc,
649"seekable() -> bool. True if file supports random-access.");
650
651PyDoc_STRVAR(readable_doc,
652"readable() -> bool. True if file was opened in a read mode.");
653
654PyDoc_STRVAR(writable_doc,
655"writable() -> bool. True if file was opened in a write mode.");
656
657static PyMethodDef fileio_methods[] = {
658 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
659 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
660 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
661 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
662 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
663 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
664 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
665 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
666 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
667 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
668 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
669 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
670 {"__enter__",(PyCFunction)fileio_self, METH_NOARGS, enter_doc},
671 {"__exit__", (PyCFunction)fileio_close, METH_VARARGS, exit_doc},
672 {NULL, NULL} /* sentinel */
673};
674
Guido van Rossumb0428152007-04-08 17:44:42 +0000675/* 'closed' is an attribute for backwards compatibility reasons. */
676static PyObject *
677get_closed(PyFileIOObject *f, void *closure)
678{
679 return PyBool_FromLong((long)(f->fd < 0));
680}
681
682static PyGetSetDef fileio_getsetlist[] = {
683 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
684 {0},
685};
686
Guido van Rossuma9e20242007-03-08 00:43:48 +0000687PyTypeObject PyFileIO_Type = {
688 PyObject_HEAD_INIT(&PyType_Type)
689 0,
690 "FileIO",
691 sizeof(PyFileIOObject),
692 0,
693 (destructor)fileio_dealloc, /* tp_dealloc */
694 0, /* tp_print */
695 0, /* tp_getattr */
696 0, /* tp_setattr */
697 0, /* tp_compare */
698 (reprfunc)fileio_repr, /* tp_repr */
699 0, /* tp_as_number */
700 0, /* tp_as_sequence */
701 0, /* tp_as_mapping */
702 0, /* tp_hash */
703 0, /* tp_call */
704 0, /* tp_str */
705 PyObject_GenericGetAttr, /* tp_getattro */
706 0, /* tp_setattro */
707 0, /* tp_as_buffer */
708 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
709 fileio_doc, /* tp_doc */
710 0, /* tp_traverse */
711 0, /* tp_clear */
712 0, /* tp_richcompare */
713 offsetof(PyFileIOObject, weakreflist), /* tp_weaklistoffset */
714 0, /* tp_iter */
715 0, /* tp_iternext */
716 fileio_methods, /* tp_methods */
717 0, /* tp_members */
Guido van Rossumb0428152007-04-08 17:44:42 +0000718 fileio_getsetlist, /* tp_getset */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000719 0, /* tp_base */
720 0, /* tp_dict */
721 0, /* tp_descr_get */
722 0, /* tp_descr_set */
723 0, /* tp_dictoffset */
724 fileio_init, /* tp_init */
725 PyType_GenericAlloc, /* tp_alloc */
726 fileio_new, /* tp_new */
727 PyObject_Del, /* tp_free */
728};
729
730static PyMethodDef module_methods[] = {
731 {NULL, NULL}
732};
733
734PyMODINIT_FUNC
735init_fileio(void)
736{
737 PyObject *m; /* a module object */
738
739 m = Py_InitModule3("_fileio", module_methods,
740 "Fast implementation of io.FileIO.");
741 if (m == NULL)
742 return;
743 if (PyType_Ready(&PyFileIO_Type) < 0)
744 return;
745 Py_INCREF(&PyFileIO_Type);
746 PyModule_AddObject(m, "_FileIO", (PyObject *) &PyFileIO_Type);
747}