blob: 1ed6417a8520006861cb9456b71c635256b8e4a6 [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 */
Thomas Hellerfdeee3a2007-07-12 11:21:36 +000025#define HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +000026#define WIN32_LEAN_AND_MEAN
27#include <windows.h>
28#endif
29
Christian Heimesa872de52008-12-05 08:26:55 +000030#if BUFSIZ < (8*1024)
31#define SMALLCHUNK (8*1024)
32#elif (BUFSIZ >= (2 << 25))
33#error "unreasonable BUFSIZ > 64MB defined"
34#else
35#define SMALLCHUNK BUFSIZ
36#endif
37
38#if SIZEOF_INT < 4
39#define BIGCHUNK (512 * 32)
40#else
41#define BIGCHUNK (512 * 1024)
42#endif
43
Guido van Rossuma9e20242007-03-08 00:43:48 +000044typedef struct {
45 PyObject_HEAD
46 int fd;
Neal Norwitz88b44da2007-08-12 17:23:54 +000047 unsigned readable : 1;
48 unsigned writable : 1;
49 int seekable : 2; /* -1 means unknown */
Guido van Rossum2dced8b2007-10-30 17:27:30 +000050 int closefd : 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +000051 PyObject *weakreflist;
52} PyFileIOObject;
53
Collin Winteraf334382007-03-08 21:46:15 +000054PyTypeObject PyFileIO_Type;
55
Guido van Rossuma9e20242007-03-08 00:43:48 +000056#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
57
Antoine Pitrou08838b62009-01-21 00:55:13 +000058static PyObject *
59portable_lseek(int fd, PyObject *posobj, int whence);
60
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000061/* Returns 0 on success, -1 with exception set on failure. */
Neal Norwitz88b44da2007-08-12 17:23:54 +000062static int
63internal_close(PyFileIOObject *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +000064{
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000065 int err = 0;
Benjamin Peterson7aedf112009-01-19 15:19:46 +000066 int save_errno = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +000067 if (self->fd >= 0) {
Guido van Rossumb0428152007-04-08 17:44:42 +000068 int fd = self->fd;
69 self->fd = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +000070 Py_BEGIN_ALLOW_THREADS
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000071 err = close(fd);
72 if (err < 0)
Neal Norwitz88b44da2007-08-12 17:23:54 +000073 save_errno = errno;
Guido van Rossuma9e20242007-03-08 00:43:48 +000074 Py_END_ALLOW_THREADS
Neal Norwitz88b44da2007-08-12 17:23:54 +000075 }
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000076 if (err < 0) {
77 errno = save_errno;
78 PyErr_SetFromErrno(PyExc_IOError);
79 return -1;
80 }
81 return 0;
Neal Norwitz88b44da2007-08-12 17:23:54 +000082}
83
84static PyObject *
85fileio_close(PyFileIOObject *self)
86{
Guido van Rossum2dced8b2007-10-30 17:27:30 +000087 if (!self->closefd) {
Christian Heimesecc42a22008-11-05 19:30:32 +000088 self->fd = -1;
Guido van Rossum2dced8b2007-10-30 17:27:30 +000089 Py_RETURN_NONE;
90 }
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000091 if (internal_close(self))
Neal Norwitz88b44da2007-08-12 17:23:54 +000092 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +000093
94 Py_RETURN_NONE;
95}
96
97static PyObject *
98fileio_new(PyTypeObject *type, PyObject *args, PyObject *kews)
99{
100 PyFileIOObject *self;
101
102 assert(type != NULL && type->tp_alloc != NULL);
103
104 self = (PyFileIOObject *) type->tp_alloc(type, 0);
105 if (self != NULL) {
106 self->fd = -1;
Christian Heimesdf32b392008-10-30 21:23:35 +0000107 self->readable = 0;
108 self->writable = 0;
109 self->seekable = -1;
110 self->closefd = 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000111 self->weakreflist = NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000112 }
113
114 return (PyObject *) self;
115}
116
117/* On Unix, open will succeed for directories.
118 In Python, there should be no file objects referring to
119 directories, so we need a check. */
120
121static int
Benjamin Peterson1efc23c2008-12-29 18:02:28 +0000122dircheck(PyFileIOObject* self, char *name)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000123{
124#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
125 struct stat buf;
126 if (self->fd < 0)
127 return 0;
128 if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000129 char *msg = strerror(EISDIR);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000130 PyObject *exc;
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +0000131 if (internal_close(self))
132 return -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000133
Benjamin Peterson1efc23c2008-12-29 18:02:28 +0000134 exc = PyObject_CallFunction(PyExc_IOError, "(iss)",
135 EISDIR, msg, name);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000136 PyErr_SetObject(PyExc_IOError, exc);
137 Py_XDECREF(exc);
138 return -1;
139 }
140#endif
141 return 0;
142}
143
Benjamin Peterson806d4022009-01-19 15:11:51 +0000144static int
145check_fd(int fd)
146{
147#if defined(HAVE_FSTAT)
148 struct stat buf;
Benjamin Petersonf91df042009-02-13 02:50:59 +0000149 if (!_PyVerify_fd(fd) || (fstat(fd, &buf) < 0 && errno == EBADF)) {
Benjamin Peterson806d4022009-01-19 15:11:51 +0000150 PyObject *exc;
151 char *msg = strerror(EBADF);
152 exc = PyObject_CallFunction(PyExc_OSError, "(is)",
153 EBADF, msg);
154 PyErr_SetObject(PyExc_OSError, exc);
155 Py_XDECREF(exc);
156 return -1;
157 }
158#endif
159 return 0;
160}
161
Guido van Rossuma9e20242007-03-08 00:43:48 +0000162
163static int
164fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
165{
166 PyFileIOObject *self = (PyFileIOObject *) oself;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000167 static char *kwlist[] = {"file", "mode", "closefd", NULL};
Guido van Rossuma9e20242007-03-08 00:43:48 +0000168 char *name = NULL;
169 char *mode = "r";
Guido van Rossum53807da2007-04-10 19:01:47 +0000170 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000171#ifdef MS_WINDOWS
172 Py_UNICODE *widename = NULL;
173#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000174 int ret = 0;
175 int rwa = 0, plus = 0, append = 0;
176 int flags = 0;
Guido van Rossumb0428152007-04-08 17:44:42 +0000177 int fd = -1;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000178 int closefd = 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000179
180 assert(PyFileIO_Check(oself));
Neal Norwitz88b44da2007-08-12 17:23:54 +0000181 if (self->fd >= 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000182 /* Have to close the existing file first. */
Neal Norwitz88b44da2007-08-12 17:23:54 +0000183 if (internal_close(self) < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000184 return -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000185 }
186
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000187 if (PyArg_ParseTupleAndKeywords(args, kwds, "i|si:fileio",
188 kwlist, &fd, &mode, &closefd)) {
Guido van Rossumb0428152007-04-08 17:44:42 +0000189 if (fd < 0) {
190 PyErr_SetString(PyExc_ValueError,
191 "Negative filedescriptor");
192 return -1;
193 }
Benjamin Peterson806d4022009-01-19 15:11:51 +0000194 if (check_fd(fd))
195 return -1;
Guido van Rossumb0428152007-04-08 17:44:42 +0000196 }
197 else {
198 PyErr_Clear();
199
Guido van Rossuma9e20242007-03-08 00:43:48 +0000200#ifdef Py_WIN_WIDE_FILENAMES
Guido van Rossumb0428152007-04-08 17:44:42 +0000201 if (GetVersion() < 0x80000000) {
202 /* On NT, so wide API available */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000203 PyObject *po;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000204 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:fileio",
205 kwlist, &po, &mode, &closefd)
206 ) {
Thomas Helleraf2be262007-07-12 11:03:13 +0000207 widename = PyUnicode_AS_UNICODE(po);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000208 } else {
209 /* Drop the argument parsing error as narrow
210 strings are also valid. */
211 PyErr_Clear();
212 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000213 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000214 if (widename == NULL)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000215#endif
Thomas Helleraf2be262007-07-12 11:03:13 +0000216 {
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000217 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:fileio",
Guido van Rossuma9e20242007-03-08 00:43:48 +0000218 kwlist,
219 Py_FileSystemDefaultEncoding,
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000220 &name, &mode, &closefd))
Neal Norwitz6e0e0e62008-08-24 22:07:28 +0000221 return -1;
Guido van Rossumb0428152007-04-08 17:44:42 +0000222 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000223 }
224
Guido van Rossum53807da2007-04-10 19:01:47 +0000225 s = mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000226 while (*s) {
227 switch (*s++) {
228 case 'r':
229 if (rwa) {
230 bad_mode:
231 PyErr_SetString(PyExc_ValueError,
232 "Must have exactly one of read/write/append mode");
233 goto error;
234 }
235 rwa = 1;
236 self->readable = 1;
237 break;
238 case 'w':
239 if (rwa)
240 goto bad_mode;
241 rwa = 1;
242 self->writable = 1;
243 flags |= O_CREAT | O_TRUNC;
244 break;
245 case 'a':
246 if (rwa)
247 goto bad_mode;
248 rwa = 1;
249 self->writable = 1;
250 flags |= O_CREAT;
251 append = 1;
252 break;
Benjamin Peterson44309e62008-11-22 00:41:45 +0000253 case 'b':
254 break;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000255 case '+':
256 if (plus)
257 goto bad_mode;
258 self->readable = self->writable = 1;
259 plus = 1;
260 break;
261 default:
262 PyErr_Format(PyExc_ValueError,
263 "invalid mode: %.200s", mode);
264 goto error;
265 }
266 }
267
268 if (!rwa)
269 goto bad_mode;
270
271 if (self->readable && self->writable)
272 flags |= O_RDWR;
273 else if (self->readable)
274 flags |= O_RDONLY;
275 else
276 flags |= O_WRONLY;
277
278#ifdef O_BINARY
279 flags |= O_BINARY;
280#endif
281
Walter Dörwald0e411482007-06-06 16:55:38 +0000282#ifdef O_APPEND
283 if (append)
284 flags |= O_APPEND;
285#endif
286
Guido van Rossumb0428152007-04-08 17:44:42 +0000287 if (fd >= 0) {
288 self->fd = fd;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000289 self->closefd = closefd;
Guido van Rossumb0428152007-04-08 17:44:42 +0000290 }
291 else {
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000292 self->closefd = 1;
293 if (!closefd) {
294 PyErr_SetString(PyExc_ValueError,
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000295 "Cannot use closefd=False with file name");
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000296 goto error;
297 }
298
Guido van Rossumb0428152007-04-08 17:44:42 +0000299 Py_BEGIN_ALLOW_THREADS
300 errno = 0;
Thomas Helleraf2be262007-07-12 11:03:13 +0000301#ifdef MS_WINDOWS
302 if (widename != NULL)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000303 self->fd = _wopen(widename, flags, 0666);
Thomas Helleraf2be262007-07-12 11:03:13 +0000304 else
305#endif
Neal Norwitz88b44da2007-08-12 17:23:54 +0000306 self->fd = open(name, flags, 0666);
Guido van Rossumb0428152007-04-08 17:44:42 +0000307 Py_END_ALLOW_THREADS
Benjamin Peterson3e4f0552008-09-02 00:31:15 +0000308 if (self->fd < 0) {
Christian Heimes0b489542007-10-31 19:20:48 +0000309#ifdef MS_WINDOWS
Hirokazu Yamamoto0f22d692009-01-01 16:03:45 +0000310 if (widename != NULL)
311 PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename);
312 else
Christian Heimes0b489542007-10-31 19:20:48 +0000313#endif
Hirokazu Yamamoto0f22d692009-01-01 16:03:45 +0000314 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
Guido van Rossumb0428152007-04-08 17:44:42 +0000315 goto error;
316 }
Benjamin Peterson1efc23c2008-12-29 18:02:28 +0000317 if(dircheck(self, name) < 0)
Benjamin Peterson3e4f0552008-09-02 00:31:15 +0000318 goto error;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000319 }
320
Antoine Pitrou08838b62009-01-21 00:55:13 +0000321 if (append) {
322 /* For consistent behaviour, we explicitly seek to the
323 end of file (otherwise, it might be done only on the
324 first write()). */
325 PyObject *pos = portable_lseek(self->fd, NULL, 2);
326 if (pos == NULL)
327 goto error;
328 Py_DECREF(pos);
329 }
330
Guido van Rossuma9e20242007-03-08 00:43:48 +0000331 goto done;
332
333 error:
334 ret = -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000335
Guido van Rossuma9e20242007-03-08 00:43:48 +0000336 done:
Neal Norwitz2f99b242008-08-24 05:48:10 +0000337 PyMem_Free(name);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000338 return ret;
339}
340
341static void
342fileio_dealloc(PyFileIOObject *self)
343{
344 if (self->weakreflist != NULL)
345 PyObject_ClearWeakRefs((PyObject *) self);
346
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000347 if (self->fd >= 0 && self->closefd) {
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +0000348 if(internal_close(self))
349 PyErr_WriteUnraisable((PyObject*)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000350 }
351
Christian Heimes90aa7642007-12-19 02:45:37 +0000352 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000353}
354
355static PyObject *
356err_closed(void)
357{
358 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
359 return NULL;
360}
361
362static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000363err_mode(char *action)
364{
365 PyErr_Format(PyExc_ValueError, "File not open for %s", action);
366 return NULL;
367}
368
369static PyObject *
Guido van Rossuma9e20242007-03-08 00:43:48 +0000370fileio_fileno(PyFileIOObject *self)
371{
372 if (self->fd < 0)
373 return err_closed();
Christian Heimes217cfd12007-12-02 14:31:20 +0000374 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000375}
376
377static PyObject *
378fileio_readable(PyFileIOObject *self)
379{
380 if (self->fd < 0)
381 return err_closed();
Neal Norwitz88b44da2007-08-12 17:23:54 +0000382 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000383}
384
385static PyObject *
386fileio_writable(PyFileIOObject *self)
387{
388 if (self->fd < 0)
389 return err_closed();
Neal Norwitz88b44da2007-08-12 17:23:54 +0000390 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000391}
392
393static PyObject *
394fileio_seekable(PyFileIOObject *self)
395{
396 if (self->fd < 0)
397 return err_closed();
398 if (self->seekable < 0) {
399 int ret;
400 Py_BEGIN_ALLOW_THREADS
401 ret = lseek(self->fd, 0, SEEK_CUR);
402 Py_END_ALLOW_THREADS
403 if (ret < 0)
404 self->seekable = 0;
405 else
406 self->seekable = 1;
407 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000408 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000409}
410
411static PyObject *
412fileio_readinto(PyFileIOObject *self, PyObject *args)
413{
Martin v. Löwis423be952008-08-13 15:53:07 +0000414 Py_buffer pbuf;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000415 Py_ssize_t n;
Guido van Rossum53807da2007-04-10 19:01:47 +0000416
Guido van Rossuma9e20242007-03-08 00:43:48 +0000417 if (self->fd < 0)
418 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000419 if (!self->readable)
420 return err_mode("reading");
421
Martin v. Löwis423be952008-08-13 15:53:07 +0000422 if (!PyArg_ParseTuple(args, "w*", &pbuf))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000423 return NULL;
424
425 Py_BEGIN_ALLOW_THREADS
426 errno = 0;
Martin v. Löwis423be952008-08-13 15:53:07 +0000427 n = read(self->fd, pbuf.buf, pbuf.len);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000428 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +0000429 PyBuffer_Release(&pbuf);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000430 if (n < 0) {
431 if (errno == EAGAIN)
432 Py_RETURN_NONE;
433 PyErr_SetFromErrno(PyExc_IOError);
434 return NULL;
435 }
436
Christian Heimes217cfd12007-12-02 14:31:20 +0000437 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000438}
439
Guido van Rossum7165cb12007-07-10 06:54:34 +0000440static PyObject *
441fileio_readall(PyFileIOObject *self)
442{
443 PyObject *result;
444 Py_ssize_t total = 0;
445 int n;
446
Christian Heimesa872de52008-12-05 08:26:55 +0000447 result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK);
Guido van Rossum7165cb12007-07-10 06:54:34 +0000448 if (result == NULL)
449 return NULL;
450
451 while (1) {
Christian Heimesa872de52008-12-05 08:26:55 +0000452 Py_ssize_t newsize = (total < SMALLCHUNK) ? SMALLCHUNK : total;
453
454 /* Keep doubling until we reach BIGCHUNK;
455 then keep adding BIGCHUNK. */
456 if (newsize <= BIGCHUNK) {
457 newsize += newsize;
458 }
459 else {
460 /* NOTE: overflow impossible due to limits on BUFSIZ */
461 newsize += BIGCHUNK;
462 }
463
Christian Heimes72b710a2008-05-26 13:28:38 +0000464 if (PyBytes_GET_SIZE(result) < newsize) {
465 if (_PyBytes_Resize(&result, newsize) < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000466 if (total == 0) {
467 Py_DECREF(result);
468 return NULL;
469 }
470 PyErr_Clear();
471 break;
472 }
473 }
474 Py_BEGIN_ALLOW_THREADS
475 errno = 0;
476 n = read(self->fd,
Christian Heimes72b710a2008-05-26 13:28:38 +0000477 PyBytes_AS_STRING(result) + total,
Guido van Rossum7165cb12007-07-10 06:54:34 +0000478 newsize - total);
479 Py_END_ALLOW_THREADS
480 if (n == 0)
481 break;
482 if (n < 0) {
483 if (total > 0)
484 break;
485 if (errno == EAGAIN) {
486 Py_DECREF(result);
487 Py_RETURN_NONE;
488 }
489 Py_DECREF(result);
490 PyErr_SetFromErrno(PyExc_IOError);
491 return NULL;
492 }
493 total += n;
494 }
495
Christian Heimes72b710a2008-05-26 13:28:38 +0000496 if (PyBytes_GET_SIZE(result) > total) {
497 if (_PyBytes_Resize(&result, total) < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000498 /* This should never happen, but just in case */
499 Py_DECREF(result);
500 return NULL;
501 }
502 }
503 return result;
504}
505
Guido van Rossuma9e20242007-03-08 00:43:48 +0000506static PyObject *
507fileio_read(PyFileIOObject *self, PyObject *args)
508{
509 char *ptr;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000510 Py_ssize_t n;
511 Py_ssize_t size = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000512 PyObject *bytes;
513
514 if (self->fd < 0)
515 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000516 if (!self->readable)
517 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000518
Neal Norwitz3c8ba932007-08-08 04:36:17 +0000519 if (!PyArg_ParseTuple(args, "|n", &size))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000520 return NULL;
521
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000522 if (size < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000523 return fileio_readall(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000524 }
525
Christian Heimes72b710a2008-05-26 13:28:38 +0000526 bytes = PyBytes_FromStringAndSize(NULL, size);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000527 if (bytes == NULL)
528 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +0000529 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000530
531 Py_BEGIN_ALLOW_THREADS
532 errno = 0;
533 n = read(self->fd, ptr, size);
534 Py_END_ALLOW_THREADS
535
536 if (n < 0) {
537 if (errno == EAGAIN)
538 Py_RETURN_NONE;
539 PyErr_SetFromErrno(PyExc_IOError);
540 return NULL;
541 }
542
543 if (n != size) {
Christian Heimes72b710a2008-05-26 13:28:38 +0000544 if (_PyBytes_Resize(&bytes, n) < 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000545 Py_DECREF(bytes);
Guido van Rossum53807da2007-04-10 19:01:47 +0000546 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000547 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000548 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000549
550 return (PyObject *) bytes;
551}
552
553static PyObject *
554fileio_write(PyFileIOObject *self, PyObject *args)
555{
Martin v. Löwis423be952008-08-13 15:53:07 +0000556 Py_buffer pbuf;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000557 Py_ssize_t n;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000558
559 if (self->fd < 0)
560 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000561 if (!self->writable)
562 return err_mode("writing");
563
Martin v. Löwis423be952008-08-13 15:53:07 +0000564 if (!PyArg_ParseTuple(args, "s*", &pbuf))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000565 return NULL;
566
567 Py_BEGIN_ALLOW_THREADS
568 errno = 0;
Martin v. Löwis423be952008-08-13 15:53:07 +0000569 n = write(self->fd, pbuf.buf, pbuf.len);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000570 Py_END_ALLOW_THREADS
571
Martin v. Löwis423be952008-08-13 15:53:07 +0000572 PyBuffer_Release(&pbuf);
573
Guido van Rossuma9e20242007-03-08 00:43:48 +0000574 if (n < 0) {
575 if (errno == EAGAIN)
576 Py_RETURN_NONE;
577 PyErr_SetFromErrno(PyExc_IOError);
578 return NULL;
579 }
580
Christian Heimes217cfd12007-12-02 14:31:20 +0000581 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000582}
583
Guido van Rossum53807da2007-04-10 19:01:47 +0000584/* XXX Windows support below is likely incomplete */
585
586#if defined(MS_WIN64) || defined(MS_WINDOWS)
587typedef PY_LONG_LONG Py_off_t;
588#else
589typedef off_t Py_off_t;
590#endif
591
592/* Cribbed from posix_lseek() */
593static PyObject *
594portable_lseek(int fd, PyObject *posobj, int whence)
595{
596 Py_off_t pos, res;
597
598#ifdef SEEK_SET
599 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
600 switch (whence) {
601#if SEEK_SET != 0
602 case 0: whence = SEEK_SET; break;
603#endif
604#if SEEK_CUR != 1
605 case 1: whence = SEEK_CUR; break;
606#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000607#if SEEK_END != 2
Guido van Rossum53807da2007-04-10 19:01:47 +0000608 case 2: whence = SEEK_END; break;
609#endif
610 }
611#endif /* SEEK_SET */
612
613 if (posobj == NULL)
614 pos = 0;
615 else {
Christian Heimes8e42a0a2007-11-08 18:04:45 +0000616 if(PyFloat_Check(posobj)) {
617 PyErr_SetString(PyExc_TypeError, "an integer is required");
618 return NULL;
619 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000620#if defined(HAVE_LARGEFILE_SUPPORT)
621 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000622#else
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000623 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000624#endif
625 if (PyErr_Occurred())
626 return NULL;
627 }
628
629 Py_BEGIN_ALLOW_THREADS
630#if defined(MS_WIN64) || defined(MS_WINDOWS)
631 res = _lseeki64(fd, pos, whence);
632#else
633 res = lseek(fd, pos, whence);
634#endif
635 Py_END_ALLOW_THREADS
636 if (res < 0)
637 return PyErr_SetFromErrno(PyExc_IOError);
638
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000639#if defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum53807da2007-04-10 19:01:47 +0000640 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000641#else
642 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000643#endif
644}
645
Guido van Rossuma9e20242007-03-08 00:43:48 +0000646static PyObject *
647fileio_seek(PyFileIOObject *self, PyObject *args)
648{
Guido van Rossum53807da2007-04-10 19:01:47 +0000649 PyObject *posobj;
650 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000651
652 if (self->fd < 0)
653 return err_closed();
654
Guido van Rossum53807da2007-04-10 19:01:47 +0000655 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000656 return NULL;
657
Guido van Rossum53807da2007-04-10 19:01:47 +0000658 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000659}
660
661static PyObject *
662fileio_tell(PyFileIOObject *self, PyObject *args)
663{
Guido van Rossuma9e20242007-03-08 00:43:48 +0000664 if (self->fd < 0)
665 return err_closed();
666
Guido van Rossum53807da2007-04-10 19:01:47 +0000667 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000668}
669
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000670#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000671static PyObject *
672fileio_truncate(PyFileIOObject *self, PyObject *args)
673{
Guido van Rossum53807da2007-04-10 19:01:47 +0000674 PyObject *posobj = NULL;
675 Py_off_t pos;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000676 int ret;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000677 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000678
Guido van Rossum53807da2007-04-10 19:01:47 +0000679 fd = self->fd;
680 if (fd < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000681 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000682 if (!self->writable)
683 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000684
Guido van Rossum53807da2007-04-10 19:01:47 +0000685 if (!PyArg_ParseTuple(args, "|O", &posobj))
686 return NULL;
687
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000688 if (posobj == Py_None || posobj == NULL) {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000689 /* Get the current position. */
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000690 posobj = portable_lseek(fd, NULL, 1);
691 if (posobj == NULL)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000692 return NULL;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000693 }
694 else {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000695 /* Move to the position to be truncated. */
696 posobj = portable_lseek(fd, posobj, 0);
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000697 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000698
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000699#if defined(HAVE_LARGEFILE_SUPPORT)
700 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000701#else
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000702 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000703#endif
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000704 if (PyErr_Occurred())
Guido van Rossum53807da2007-04-10 19:01:47 +0000705 return NULL;
706
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000707#ifdef MS_WINDOWS
708 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
709 so don't even try using it. */
710 {
711 HANDLE hFile;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000712
713 /* Truncate. Note that this may grow the file! */
714 Py_BEGIN_ALLOW_THREADS
715 errno = 0;
716 hFile = (HANDLE)_get_osfhandle(fd);
717 ret = hFile == (HANDLE)-1;
718 if (ret == 0) {
719 ret = SetEndOfFile(hFile) == 0;
720 if (ret)
721 errno = EACCES;
722 }
723 Py_END_ALLOW_THREADS
724 }
725#else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000726 Py_BEGIN_ALLOW_THREADS
727 errno = 0;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000728 ret = ftruncate(fd, pos);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000729 Py_END_ALLOW_THREADS
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000730#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000731
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000732 if (ret != 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000733 PyErr_SetFromErrno(PyExc_IOError);
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000734 return NULL;
Guido van Rossum87429772007-04-10 21:06:59 +0000735 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000736
Guido van Rossum87429772007-04-10 21:06:59 +0000737 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000738}
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000739#endif
Guido van Rossum53807da2007-04-10 19:01:47 +0000740
741static char *
742mode_string(PyFileIOObject *self)
743{
744 if (self->readable) {
745 if (self->writable)
Benjamin Peterson44309e62008-11-22 00:41:45 +0000746 return "rb+";
Guido van Rossum53807da2007-04-10 19:01:47 +0000747 else
Benjamin Peterson44309e62008-11-22 00:41:45 +0000748 return "rb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000749 }
750 else
Benjamin Peterson44309e62008-11-22 00:41:45 +0000751 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000752}
Guido van Rossuma9e20242007-03-08 00:43:48 +0000753
754static PyObject *
755fileio_repr(PyFileIOObject *self)
756{
Guido van Rossum53807da2007-04-10 19:01:47 +0000757 if (self->fd < 0)
Walter Dörwald1ab83302007-05-18 17:15:44 +0000758 return PyUnicode_FromFormat("_fileio._FileIO(-1)");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000759
Walter Dörwald1ab83302007-05-18 17:15:44 +0000760 return PyUnicode_FromFormat("_fileio._FileIO(%d, '%s')",
Guido van Rossum53807da2007-04-10 19:01:47 +0000761 self->fd, mode_string(self));
Guido van Rossuma9e20242007-03-08 00:43:48 +0000762}
763
764static PyObject *
765fileio_isatty(PyFileIOObject *self)
766{
767 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000768
Guido van Rossuma9e20242007-03-08 00:43:48 +0000769 if (self->fd < 0)
770 return err_closed();
771 Py_BEGIN_ALLOW_THREADS
772 res = isatty(self->fd);
773 Py_END_ALLOW_THREADS
774 return PyBool_FromLong(res);
775}
776
Guido van Rossuma9e20242007-03-08 00:43:48 +0000777
778PyDoc_STRVAR(fileio_doc,
779"file(name: str[, mode: str]) -> file IO object\n"
780"\n"
781"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
782"writing or appending. The file will be created if it doesn't exist\n"
783"when opened for writing or appending; it will be truncated when\n"
784"opened for writing. Add a '+' to the mode to allow simultaneous\n"
785"reading and writing.");
786
787PyDoc_STRVAR(read_doc,
788"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
789"\n"
790"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +0000791"In non-blocking mode, returns None if no data is available.\n"
792"On end-of-file, returns ''.");
793
794PyDoc_STRVAR(readall_doc,
795"readall() -> bytes. read all data from the file, returned as bytes.\n"
796"\n"
797"In non-blocking mode, returns as much as is immediately available,\n"
798"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000799
800PyDoc_STRVAR(write_doc,
801"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
802"\n"
803"Only makes one system call, so not all of the data may be written.\n"
804"The number of bytes actually written is returned.");
805
806PyDoc_STRVAR(fileno_doc,
807"fileno() -> int. \"file descriptor\".\n"
808"\n"
809"This is needed for lower-level file interfaces, such the fcntl module.");
810
811PyDoc_STRVAR(seek_doc,
812"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
813"\n"
814"Argument offset is a byte count. Optional argument whence defaults to\n"
815"0 (offset from start of file, offset should be >= 0); other values are 1\n"
816"(move relative to current position, positive or negative), and 2 (move\n"
817"relative to end of file, usually negative, although many platforms allow\n"
818"seeking beyond the end of a file)."
819"\n"
820"Note that not all file objects are seekable.");
821
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000822#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000823PyDoc_STRVAR(truncate_doc,
824"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
825"\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000826"Size defaults to the current file position, as returned by tell()."
827"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000828#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000829
830PyDoc_STRVAR(tell_doc,
831"tell() -> int. Current file position");
832
833PyDoc_STRVAR(readinto_doc,
834"readinto() -> Undocumented. Don't use this; it may go away.");
835
836PyDoc_STRVAR(close_doc,
837"close() -> None. Close the file.\n"
838"\n"
839"A closed file cannot be used for further I/O operations. close() may be\n"
840"called more than once without error. Changes the fileno to -1.");
841
842PyDoc_STRVAR(isatty_doc,
843"isatty() -> bool. True if the file is connected to a tty device.");
844
Guido van Rossuma9e20242007-03-08 00:43:48 +0000845PyDoc_STRVAR(seekable_doc,
846"seekable() -> bool. True if file supports random-access.");
847
848PyDoc_STRVAR(readable_doc,
849"readable() -> bool. True if file was opened in a read mode.");
850
851PyDoc_STRVAR(writable_doc,
852"writable() -> bool. True if file was opened in a write mode.");
853
854static PyMethodDef fileio_methods[] = {
855 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
Guido van Rossum7165cb12007-07-10 06:54:34 +0000856 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000857 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
858 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
859 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
860 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000861#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000862 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000863#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000864 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
865 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
866 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
867 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
868 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
869 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000870 {NULL, NULL} /* sentinel */
871};
872
Guido van Rossum53807da2007-04-10 19:01:47 +0000873/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
874
Guido van Rossumb0428152007-04-08 17:44:42 +0000875static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000876get_closed(PyFileIOObject *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +0000877{
Guido van Rossum53807da2007-04-10 19:01:47 +0000878 return PyBool_FromLong((long)(self->fd < 0));
879}
880
881static PyObject *
Christian Heimesecc42a22008-11-05 19:30:32 +0000882get_closefd(PyFileIOObject *self, void *closure)
883{
884 return PyBool_FromLong((long)(self->closefd));
885}
886
887static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000888get_mode(PyFileIOObject *self, void *closure)
889{
Guido van Rossumc43e79f2007-06-18 18:26:36 +0000890 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +0000891}
892
893static PyGetSetDef fileio_getsetlist[] = {
894 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Christian Heimesecc42a22008-11-05 19:30:32 +0000895 {"closefd", (getter)get_closefd, NULL,
896 "True if the file descriptor will be closed"},
Guido van Rossum53807da2007-04-10 19:01:47 +0000897 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
Guido van Rossumb0428152007-04-08 17:44:42 +0000898 {0},
899};
900
Guido van Rossuma9e20242007-03-08 00:43:48 +0000901PyTypeObject PyFileIO_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000902 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +0000903 "_FileIO",
Guido van Rossuma9e20242007-03-08 00:43:48 +0000904 sizeof(PyFileIOObject),
905 0,
906 (destructor)fileio_dealloc, /* tp_dealloc */
907 0, /* tp_print */
908 0, /* tp_getattr */
909 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000910 0, /* tp_reserved */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000911 (reprfunc)fileio_repr, /* tp_repr */
912 0, /* tp_as_number */
913 0, /* tp_as_sequence */
914 0, /* tp_as_mapping */
915 0, /* tp_hash */
916 0, /* tp_call */
917 0, /* tp_str */
918 PyObject_GenericGetAttr, /* tp_getattro */
919 0, /* tp_setattro */
920 0, /* tp_as_buffer */
921 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
922 fileio_doc, /* tp_doc */
923 0, /* tp_traverse */
924 0, /* tp_clear */
925 0, /* tp_richcompare */
926 offsetof(PyFileIOObject, weakreflist), /* tp_weaklistoffset */
927 0, /* tp_iter */
928 0, /* tp_iternext */
929 fileio_methods, /* tp_methods */
930 0, /* tp_members */
Guido van Rossumb0428152007-04-08 17:44:42 +0000931 fileio_getsetlist, /* tp_getset */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000932 0, /* tp_base */
933 0, /* tp_dict */
934 0, /* tp_descr_get */
935 0, /* tp_descr_set */
936 0, /* tp_dictoffset */
937 fileio_init, /* tp_init */
938 PyType_GenericAlloc, /* tp_alloc */
939 fileio_new, /* tp_new */
940 PyObject_Del, /* tp_free */
941};
942
943static PyMethodDef module_methods[] = {
944 {NULL, NULL}
945};
946
Martin v. Löwis1a214512008-06-11 05:26:20 +0000947static struct PyModuleDef fileiomodule = {
948 PyModuleDef_HEAD_INIT,
949 "_fileio",
950 "Fast implementation of io.FileIO.",
951 -1,
952 module_methods,
953 NULL,
954 NULL,
955 NULL,
956 NULL
957};
958
Guido van Rossuma9e20242007-03-08 00:43:48 +0000959PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000960PyInit__fileio(void)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000961{
962 PyObject *m; /* a module object */
963
Martin v. Löwis1a214512008-06-11 05:26:20 +0000964 m = PyModule_Create(&fileiomodule);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000965 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000966 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000967 if (PyType_Ready(&PyFileIO_Type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000968 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000969 Py_INCREF(&PyFileIO_Type);
970 PyModule_AddObject(m, "_FileIO", (PyObject *) &PyFileIO_Type);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000971 return m;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000972}