blob: 8579e4e60bdb0568e0e36519bffe974a89e7ea5d [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
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000058/* Returns 0 on success, -1 with exception set on failure. */
Neal Norwitz88b44da2007-08-12 17:23:54 +000059static int
60internal_close(PyFileIOObject *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +000061{
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000062 int err = 0;
63 int save_errno;
Guido van Rossuma9e20242007-03-08 00:43:48 +000064 if (self->fd >= 0) {
Guido van Rossumb0428152007-04-08 17:44:42 +000065 int fd = self->fd;
66 self->fd = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +000067 Py_BEGIN_ALLOW_THREADS
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000068 err = close(fd);
69 if (err < 0)
Neal Norwitz88b44da2007-08-12 17:23:54 +000070 save_errno = errno;
Guido van Rossuma9e20242007-03-08 00:43:48 +000071 Py_END_ALLOW_THREADS
Neal Norwitz88b44da2007-08-12 17:23:54 +000072 }
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000073 if (err < 0) {
74 errno = save_errno;
75 PyErr_SetFromErrno(PyExc_IOError);
76 return -1;
77 }
78 return 0;
Neal Norwitz88b44da2007-08-12 17:23:54 +000079}
80
81static PyObject *
82fileio_close(PyFileIOObject *self)
83{
Guido van Rossum2dced8b2007-10-30 17:27:30 +000084 if (!self->closefd) {
Christian Heimesecc42a22008-11-05 19:30:32 +000085 self->fd = -1;
Guido van Rossum2dced8b2007-10-30 17:27:30 +000086 Py_RETURN_NONE;
87 }
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000088 if (internal_close(self))
Neal Norwitz88b44da2007-08-12 17:23:54 +000089 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +000090
91 Py_RETURN_NONE;
92}
93
94static PyObject *
95fileio_new(PyTypeObject *type, PyObject *args, PyObject *kews)
96{
97 PyFileIOObject *self;
98
99 assert(type != NULL && type->tp_alloc != NULL);
100
101 self = (PyFileIOObject *) type->tp_alloc(type, 0);
102 if (self != NULL) {
103 self->fd = -1;
Christian Heimesdf32b392008-10-30 21:23:35 +0000104 self->readable = 0;
105 self->writable = 0;
106 self->seekable = -1;
107 self->closefd = 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000108 self->weakreflist = NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000109 }
110
111 return (PyObject *) self;
112}
113
114/* On Unix, open will succeed for directories.
115 In Python, there should be no file objects referring to
116 directories, so we need a check. */
117
118static int
Benjamin Peterson1efc23c2008-12-29 18:02:28 +0000119dircheck(PyFileIOObject* self, char *name)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000120{
121#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
122 struct stat buf;
123 if (self->fd < 0)
124 return 0;
125 if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000126 char *msg = strerror(EISDIR);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000127 PyObject *exc;
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +0000128 if (internal_close(self))
129 return -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000130
Benjamin Peterson1efc23c2008-12-29 18:02:28 +0000131 exc = PyObject_CallFunction(PyExc_IOError, "(iss)",
132 EISDIR, msg, name);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000133 PyErr_SetObject(PyExc_IOError, exc);
134 Py_XDECREF(exc);
135 return -1;
136 }
137#endif
138 return 0;
139}
140
141
142static int
143fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
144{
145 PyFileIOObject *self = (PyFileIOObject *) oself;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000146 static char *kwlist[] = {"file", "mode", "closefd", NULL};
Guido van Rossuma9e20242007-03-08 00:43:48 +0000147 char *name = NULL;
148 char *mode = "r";
Guido van Rossum53807da2007-04-10 19:01:47 +0000149 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000150#ifdef MS_WINDOWS
151 Py_UNICODE *widename = NULL;
152#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000153 int ret = 0;
154 int rwa = 0, plus = 0, append = 0;
155 int flags = 0;
Guido van Rossumb0428152007-04-08 17:44:42 +0000156 int fd = -1;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000157 int closefd = 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000158
159 assert(PyFileIO_Check(oself));
Neal Norwitz88b44da2007-08-12 17:23:54 +0000160 if (self->fd >= 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000161 /* Have to close the existing file first. */
Neal Norwitz88b44da2007-08-12 17:23:54 +0000162 if (internal_close(self) < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000163 return -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000164 }
165
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000166 if (PyArg_ParseTupleAndKeywords(args, kwds, "i|si:fileio",
167 kwlist, &fd, &mode, &closefd)) {
Guido van Rossumb0428152007-04-08 17:44:42 +0000168 if (fd < 0) {
169 PyErr_SetString(PyExc_ValueError,
170 "Negative filedescriptor");
171 return -1;
172 }
173 }
174 else {
175 PyErr_Clear();
176
Guido van Rossuma9e20242007-03-08 00:43:48 +0000177#ifdef Py_WIN_WIDE_FILENAMES
Guido van Rossumb0428152007-04-08 17:44:42 +0000178 if (GetVersion() < 0x80000000) {
179 /* On NT, so wide API available */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000180 PyObject *po;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000181 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:fileio",
182 kwlist, &po, &mode, &closefd)
183 ) {
Thomas Helleraf2be262007-07-12 11:03:13 +0000184 widename = PyUnicode_AS_UNICODE(po);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000185 } else {
186 /* Drop the argument parsing error as narrow
187 strings are also valid. */
188 PyErr_Clear();
189 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000190 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000191 if (widename == NULL)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000192#endif
Thomas Helleraf2be262007-07-12 11:03:13 +0000193 {
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000194 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:fileio",
Guido van Rossuma9e20242007-03-08 00:43:48 +0000195 kwlist,
196 Py_FileSystemDefaultEncoding,
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000197 &name, &mode, &closefd))
Neal Norwitz6e0e0e62008-08-24 22:07:28 +0000198 return -1;
Guido van Rossumb0428152007-04-08 17:44:42 +0000199 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000200 }
201
Guido van Rossum53807da2007-04-10 19:01:47 +0000202 s = mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000203 while (*s) {
204 switch (*s++) {
205 case 'r':
206 if (rwa) {
207 bad_mode:
208 PyErr_SetString(PyExc_ValueError,
209 "Must have exactly one of read/write/append mode");
210 goto error;
211 }
212 rwa = 1;
213 self->readable = 1;
214 break;
215 case 'w':
216 if (rwa)
217 goto bad_mode;
218 rwa = 1;
219 self->writable = 1;
220 flags |= O_CREAT | O_TRUNC;
221 break;
222 case 'a':
223 if (rwa)
224 goto bad_mode;
225 rwa = 1;
226 self->writable = 1;
227 flags |= O_CREAT;
228 append = 1;
229 break;
Benjamin Peterson44309e62008-11-22 00:41:45 +0000230 case 'b':
231 break;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000232 case '+':
233 if (plus)
234 goto bad_mode;
235 self->readable = self->writable = 1;
236 plus = 1;
237 break;
238 default:
239 PyErr_Format(PyExc_ValueError,
240 "invalid mode: %.200s", mode);
241 goto error;
242 }
243 }
244
245 if (!rwa)
246 goto bad_mode;
247
248 if (self->readable && self->writable)
249 flags |= O_RDWR;
250 else if (self->readable)
251 flags |= O_RDONLY;
252 else
253 flags |= O_WRONLY;
254
255#ifdef O_BINARY
256 flags |= O_BINARY;
257#endif
258
Walter Dörwald0e411482007-06-06 16:55:38 +0000259#ifdef O_APPEND
260 if (append)
261 flags |= O_APPEND;
262#endif
263
Guido van Rossumb0428152007-04-08 17:44:42 +0000264 if (fd >= 0) {
265 self->fd = fd;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000266 self->closefd = closefd;
Guido van Rossumb0428152007-04-08 17:44:42 +0000267 }
268 else {
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000269 self->closefd = 1;
270 if (!closefd) {
271 PyErr_SetString(PyExc_ValueError,
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000272 "Cannot use closefd=False with file name");
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000273 goto error;
274 }
275
Guido van Rossumb0428152007-04-08 17:44:42 +0000276 Py_BEGIN_ALLOW_THREADS
277 errno = 0;
Thomas Helleraf2be262007-07-12 11:03:13 +0000278#ifdef MS_WINDOWS
279 if (widename != NULL)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000280 self->fd = _wopen(widename, flags, 0666);
Thomas Helleraf2be262007-07-12 11:03:13 +0000281 else
282#endif
Neal Norwitz88b44da2007-08-12 17:23:54 +0000283 self->fd = open(name, flags, 0666);
Guido van Rossumb0428152007-04-08 17:44:42 +0000284 Py_END_ALLOW_THREADS
Benjamin Peterson3e4f0552008-09-02 00:31:15 +0000285 if (self->fd < 0) {
Christian Heimes0b489542007-10-31 19:20:48 +0000286#ifdef MS_WINDOWS
Hirokazu Yamamoto0f22d692009-01-01 16:03:45 +0000287 if (widename != NULL)
288 PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename);
289 else
Christian Heimes0b489542007-10-31 19:20:48 +0000290#endif
Hirokazu Yamamoto0f22d692009-01-01 16:03:45 +0000291 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
Guido van Rossumb0428152007-04-08 17:44:42 +0000292 goto error;
293 }
Benjamin Peterson1efc23c2008-12-29 18:02:28 +0000294 if(dircheck(self, name) < 0)
Benjamin Peterson3e4f0552008-09-02 00:31:15 +0000295 goto error;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000296 }
297
298 goto done;
299
300 error:
301 ret = -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000302
Guido van Rossuma9e20242007-03-08 00:43:48 +0000303 done:
Neal Norwitz2f99b242008-08-24 05:48:10 +0000304 PyMem_Free(name);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000305 return ret;
306}
307
308static void
309fileio_dealloc(PyFileIOObject *self)
310{
311 if (self->weakreflist != NULL)
312 PyObject_ClearWeakRefs((PyObject *) self);
313
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000314 if (self->fd >= 0 && self->closefd) {
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +0000315 if(internal_close(self))
316 PyErr_WriteUnraisable((PyObject*)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000317 }
318
Christian Heimes90aa7642007-12-19 02:45:37 +0000319 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000320}
321
322static PyObject *
323err_closed(void)
324{
325 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
326 return NULL;
327}
328
329static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000330err_mode(char *action)
331{
332 PyErr_Format(PyExc_ValueError, "File not open for %s", action);
333 return NULL;
334}
335
336static PyObject *
Guido van Rossuma9e20242007-03-08 00:43:48 +0000337fileio_fileno(PyFileIOObject *self)
338{
339 if (self->fd < 0)
340 return err_closed();
Christian Heimes217cfd12007-12-02 14:31:20 +0000341 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000342}
343
344static PyObject *
345fileio_readable(PyFileIOObject *self)
346{
347 if (self->fd < 0)
348 return err_closed();
Neal Norwitz88b44da2007-08-12 17:23:54 +0000349 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000350}
351
352static PyObject *
353fileio_writable(PyFileIOObject *self)
354{
355 if (self->fd < 0)
356 return err_closed();
Neal Norwitz88b44da2007-08-12 17:23:54 +0000357 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000358}
359
360static PyObject *
361fileio_seekable(PyFileIOObject *self)
362{
363 if (self->fd < 0)
364 return err_closed();
365 if (self->seekable < 0) {
366 int ret;
367 Py_BEGIN_ALLOW_THREADS
368 ret = lseek(self->fd, 0, SEEK_CUR);
369 Py_END_ALLOW_THREADS
370 if (ret < 0)
371 self->seekable = 0;
372 else
373 self->seekable = 1;
374 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000375 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000376}
377
378static PyObject *
379fileio_readinto(PyFileIOObject *self, PyObject *args)
380{
Martin v. Löwis423be952008-08-13 15:53:07 +0000381 Py_buffer pbuf;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000382 Py_ssize_t n;
Guido van Rossum53807da2007-04-10 19:01:47 +0000383
Guido van Rossuma9e20242007-03-08 00:43:48 +0000384 if (self->fd < 0)
385 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000386 if (!self->readable)
387 return err_mode("reading");
388
Martin v. Löwis423be952008-08-13 15:53:07 +0000389 if (!PyArg_ParseTuple(args, "w*", &pbuf))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000390 return NULL;
391
392 Py_BEGIN_ALLOW_THREADS
393 errno = 0;
Martin v. Löwis423be952008-08-13 15:53:07 +0000394 n = read(self->fd, pbuf.buf, pbuf.len);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000395 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +0000396 PyBuffer_Release(&pbuf);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000397 if (n < 0) {
398 if (errno == EAGAIN)
399 Py_RETURN_NONE;
400 PyErr_SetFromErrno(PyExc_IOError);
401 return NULL;
402 }
403
Christian Heimes217cfd12007-12-02 14:31:20 +0000404 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000405}
406
Guido van Rossum7165cb12007-07-10 06:54:34 +0000407static PyObject *
408fileio_readall(PyFileIOObject *self)
409{
410 PyObject *result;
411 Py_ssize_t total = 0;
412 int n;
413
Christian Heimesa872de52008-12-05 08:26:55 +0000414 result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK);
Guido van Rossum7165cb12007-07-10 06:54:34 +0000415 if (result == NULL)
416 return NULL;
417
418 while (1) {
Christian Heimesa872de52008-12-05 08:26:55 +0000419 Py_ssize_t newsize = (total < SMALLCHUNK) ? SMALLCHUNK : total;
420
421 /* Keep doubling until we reach BIGCHUNK;
422 then keep adding BIGCHUNK. */
423 if (newsize <= BIGCHUNK) {
424 newsize += newsize;
425 }
426 else {
427 /* NOTE: overflow impossible due to limits on BUFSIZ */
428 newsize += BIGCHUNK;
429 }
430
Christian Heimes72b710a2008-05-26 13:28:38 +0000431 if (PyBytes_GET_SIZE(result) < newsize) {
432 if (_PyBytes_Resize(&result, newsize) < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000433 if (total == 0) {
434 Py_DECREF(result);
435 return NULL;
436 }
437 PyErr_Clear();
438 break;
439 }
440 }
441 Py_BEGIN_ALLOW_THREADS
442 errno = 0;
443 n = read(self->fd,
Christian Heimes72b710a2008-05-26 13:28:38 +0000444 PyBytes_AS_STRING(result) + total,
Guido van Rossum7165cb12007-07-10 06:54:34 +0000445 newsize - total);
446 Py_END_ALLOW_THREADS
447 if (n == 0)
448 break;
449 if (n < 0) {
450 if (total > 0)
451 break;
452 if (errno == EAGAIN) {
453 Py_DECREF(result);
454 Py_RETURN_NONE;
455 }
456 Py_DECREF(result);
457 PyErr_SetFromErrno(PyExc_IOError);
458 return NULL;
459 }
460 total += n;
461 }
462
Christian Heimes72b710a2008-05-26 13:28:38 +0000463 if (PyBytes_GET_SIZE(result) > total) {
464 if (_PyBytes_Resize(&result, total) < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000465 /* This should never happen, but just in case */
466 Py_DECREF(result);
467 return NULL;
468 }
469 }
470 return result;
471}
472
Guido van Rossuma9e20242007-03-08 00:43:48 +0000473static PyObject *
474fileio_read(PyFileIOObject *self, PyObject *args)
475{
476 char *ptr;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000477 Py_ssize_t n;
478 Py_ssize_t size = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000479 PyObject *bytes;
480
481 if (self->fd < 0)
482 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000483 if (!self->readable)
484 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000485
Neal Norwitz3c8ba932007-08-08 04:36:17 +0000486 if (!PyArg_ParseTuple(args, "|n", &size))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000487 return NULL;
488
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000489 if (size < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000490 return fileio_readall(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000491 }
492
Christian Heimes72b710a2008-05-26 13:28:38 +0000493 bytes = PyBytes_FromStringAndSize(NULL, size);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000494 if (bytes == NULL)
495 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +0000496 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000497
498 Py_BEGIN_ALLOW_THREADS
499 errno = 0;
500 n = read(self->fd, ptr, size);
501 Py_END_ALLOW_THREADS
502
503 if (n < 0) {
504 if (errno == EAGAIN)
505 Py_RETURN_NONE;
506 PyErr_SetFromErrno(PyExc_IOError);
507 return NULL;
508 }
509
510 if (n != size) {
Christian Heimes72b710a2008-05-26 13:28:38 +0000511 if (_PyBytes_Resize(&bytes, n) < 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000512 Py_DECREF(bytes);
Guido van Rossum53807da2007-04-10 19:01:47 +0000513 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000514 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000515 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000516
517 return (PyObject *) bytes;
518}
519
520static PyObject *
521fileio_write(PyFileIOObject *self, PyObject *args)
522{
Martin v. Löwis423be952008-08-13 15:53:07 +0000523 Py_buffer pbuf;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000524 Py_ssize_t n;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000525
526 if (self->fd < 0)
527 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000528 if (!self->writable)
529 return err_mode("writing");
530
Martin v. Löwis423be952008-08-13 15:53:07 +0000531 if (!PyArg_ParseTuple(args, "s*", &pbuf))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000532 return NULL;
533
534 Py_BEGIN_ALLOW_THREADS
535 errno = 0;
Martin v. Löwis423be952008-08-13 15:53:07 +0000536 n = write(self->fd, pbuf.buf, pbuf.len);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000537 Py_END_ALLOW_THREADS
538
Martin v. Löwis423be952008-08-13 15:53:07 +0000539 PyBuffer_Release(&pbuf);
540
Guido van Rossuma9e20242007-03-08 00:43:48 +0000541 if (n < 0) {
542 if (errno == EAGAIN)
543 Py_RETURN_NONE;
544 PyErr_SetFromErrno(PyExc_IOError);
545 return NULL;
546 }
547
Christian Heimes217cfd12007-12-02 14:31:20 +0000548 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000549}
550
Guido van Rossum53807da2007-04-10 19:01:47 +0000551/* XXX Windows support below is likely incomplete */
552
553#if defined(MS_WIN64) || defined(MS_WINDOWS)
554typedef PY_LONG_LONG Py_off_t;
555#else
556typedef off_t Py_off_t;
557#endif
558
559/* Cribbed from posix_lseek() */
560static PyObject *
561portable_lseek(int fd, PyObject *posobj, int whence)
562{
563 Py_off_t pos, res;
564
565#ifdef SEEK_SET
566 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
567 switch (whence) {
568#if SEEK_SET != 0
569 case 0: whence = SEEK_SET; break;
570#endif
571#if SEEK_CUR != 1
572 case 1: whence = SEEK_CUR; break;
573#endif
574#if SEEL_END != 2
575 case 2: whence = SEEK_END; break;
576#endif
577 }
578#endif /* SEEK_SET */
579
580 if (posobj == NULL)
581 pos = 0;
582 else {
Christian Heimes8e42a0a2007-11-08 18:04:45 +0000583 if(PyFloat_Check(posobj)) {
584 PyErr_SetString(PyExc_TypeError, "an integer is required");
585 return NULL;
586 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000587#if defined(HAVE_LARGEFILE_SUPPORT)
588 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000589#else
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000590 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000591#endif
592 if (PyErr_Occurred())
593 return NULL;
594 }
595
596 Py_BEGIN_ALLOW_THREADS
597#if defined(MS_WIN64) || defined(MS_WINDOWS)
598 res = _lseeki64(fd, pos, whence);
599#else
600 res = lseek(fd, pos, whence);
601#endif
602 Py_END_ALLOW_THREADS
603 if (res < 0)
604 return PyErr_SetFromErrno(PyExc_IOError);
605
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000606#if defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum53807da2007-04-10 19:01:47 +0000607 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000608#else
609 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000610#endif
611}
612
Guido van Rossuma9e20242007-03-08 00:43:48 +0000613static PyObject *
614fileio_seek(PyFileIOObject *self, PyObject *args)
615{
Guido van Rossum53807da2007-04-10 19:01:47 +0000616 PyObject *posobj;
617 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000618
619 if (self->fd < 0)
620 return err_closed();
621
Guido van Rossum53807da2007-04-10 19:01:47 +0000622 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000623 return NULL;
624
Guido van Rossum53807da2007-04-10 19:01:47 +0000625 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000626}
627
628static PyObject *
629fileio_tell(PyFileIOObject *self, PyObject *args)
630{
Guido van Rossuma9e20242007-03-08 00:43:48 +0000631 if (self->fd < 0)
632 return err_closed();
633
Guido van Rossum53807da2007-04-10 19:01:47 +0000634 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000635}
636
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000637#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000638static PyObject *
639fileio_truncate(PyFileIOObject *self, PyObject *args)
640{
Guido van Rossum53807da2007-04-10 19:01:47 +0000641 PyObject *posobj = NULL;
642 Py_off_t pos;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000643 int ret;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000644 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000645
Guido van Rossum53807da2007-04-10 19:01:47 +0000646 fd = self->fd;
647 if (fd < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000648 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000649 if (!self->writable)
650 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000651
Guido van Rossum53807da2007-04-10 19:01:47 +0000652 if (!PyArg_ParseTuple(args, "|O", &posobj))
653 return NULL;
654
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000655 if (posobj == Py_None || posobj == NULL) {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000656 /* Get the current position. */
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000657 posobj = portable_lseek(fd, NULL, 1);
658 if (posobj == NULL)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000659 return NULL;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000660 }
661 else {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000662 /* Move to the position to be truncated. */
663 posobj = portable_lseek(fd, posobj, 0);
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000664 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000665
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000666#if defined(HAVE_LARGEFILE_SUPPORT)
667 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000668#else
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000669 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000670#endif
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000671 if (PyErr_Occurred())
Guido van Rossum53807da2007-04-10 19:01:47 +0000672 return NULL;
673
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000674#ifdef MS_WINDOWS
675 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
676 so don't even try using it. */
677 {
678 HANDLE hFile;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000679
680 /* Truncate. Note that this may grow the file! */
681 Py_BEGIN_ALLOW_THREADS
682 errno = 0;
683 hFile = (HANDLE)_get_osfhandle(fd);
684 ret = hFile == (HANDLE)-1;
685 if (ret == 0) {
686 ret = SetEndOfFile(hFile) == 0;
687 if (ret)
688 errno = EACCES;
689 }
690 Py_END_ALLOW_THREADS
691 }
692#else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000693 Py_BEGIN_ALLOW_THREADS
694 errno = 0;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000695 ret = ftruncate(fd, pos);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000696 Py_END_ALLOW_THREADS
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000697#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000698
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000699 if (ret != 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000700 PyErr_SetFromErrno(PyExc_IOError);
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000701 return NULL;
Guido van Rossum87429772007-04-10 21:06:59 +0000702 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000703
Guido van Rossum87429772007-04-10 21:06:59 +0000704 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000705}
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000706#endif
Guido van Rossum53807da2007-04-10 19:01:47 +0000707
708static char *
709mode_string(PyFileIOObject *self)
710{
711 if (self->readable) {
712 if (self->writable)
Benjamin Peterson44309e62008-11-22 00:41:45 +0000713 return "rb+";
Guido van Rossum53807da2007-04-10 19:01:47 +0000714 else
Benjamin Peterson44309e62008-11-22 00:41:45 +0000715 return "rb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000716 }
717 else
Benjamin Peterson44309e62008-11-22 00:41:45 +0000718 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000719}
Guido van Rossuma9e20242007-03-08 00:43:48 +0000720
721static PyObject *
722fileio_repr(PyFileIOObject *self)
723{
Guido van Rossum53807da2007-04-10 19:01:47 +0000724 if (self->fd < 0)
Walter Dörwald1ab83302007-05-18 17:15:44 +0000725 return PyUnicode_FromFormat("_fileio._FileIO(-1)");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000726
Walter Dörwald1ab83302007-05-18 17:15:44 +0000727 return PyUnicode_FromFormat("_fileio._FileIO(%d, '%s')",
Guido van Rossum53807da2007-04-10 19:01:47 +0000728 self->fd, mode_string(self));
Guido van Rossuma9e20242007-03-08 00:43:48 +0000729}
730
731static PyObject *
732fileio_isatty(PyFileIOObject *self)
733{
734 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000735
Guido van Rossuma9e20242007-03-08 00:43:48 +0000736 if (self->fd < 0)
737 return err_closed();
738 Py_BEGIN_ALLOW_THREADS
739 res = isatty(self->fd);
740 Py_END_ALLOW_THREADS
741 return PyBool_FromLong(res);
742}
743
Guido van Rossuma9e20242007-03-08 00:43:48 +0000744
745PyDoc_STRVAR(fileio_doc,
746"file(name: str[, mode: str]) -> file IO object\n"
747"\n"
748"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
749"writing or appending. The file will be created if it doesn't exist\n"
750"when opened for writing or appending; it will be truncated when\n"
751"opened for writing. Add a '+' to the mode to allow simultaneous\n"
752"reading and writing.");
753
754PyDoc_STRVAR(read_doc,
755"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
756"\n"
757"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +0000758"In non-blocking mode, returns None if no data is available.\n"
759"On end-of-file, returns ''.");
760
761PyDoc_STRVAR(readall_doc,
762"readall() -> bytes. read all data from the file, returned as bytes.\n"
763"\n"
764"In non-blocking mode, returns as much as is immediately available,\n"
765"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000766
767PyDoc_STRVAR(write_doc,
768"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
769"\n"
770"Only makes one system call, so not all of the data may be written.\n"
771"The number of bytes actually written is returned.");
772
773PyDoc_STRVAR(fileno_doc,
774"fileno() -> int. \"file descriptor\".\n"
775"\n"
776"This is needed for lower-level file interfaces, such the fcntl module.");
777
778PyDoc_STRVAR(seek_doc,
779"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
780"\n"
781"Argument offset is a byte count. Optional argument whence defaults to\n"
782"0 (offset from start of file, offset should be >= 0); other values are 1\n"
783"(move relative to current position, positive or negative), and 2 (move\n"
784"relative to end of file, usually negative, although many platforms allow\n"
785"seeking beyond the end of a file)."
786"\n"
787"Note that not all file objects are seekable.");
788
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000789#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000790PyDoc_STRVAR(truncate_doc,
791"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
792"\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000793"Size defaults to the current file position, as returned by tell()."
794"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000795#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000796
797PyDoc_STRVAR(tell_doc,
798"tell() -> int. Current file position");
799
800PyDoc_STRVAR(readinto_doc,
801"readinto() -> Undocumented. Don't use this; it may go away.");
802
803PyDoc_STRVAR(close_doc,
804"close() -> None. Close the file.\n"
805"\n"
806"A closed file cannot be used for further I/O operations. close() may be\n"
807"called more than once without error. Changes the fileno to -1.");
808
809PyDoc_STRVAR(isatty_doc,
810"isatty() -> bool. True if the file is connected to a tty device.");
811
Guido van Rossuma9e20242007-03-08 00:43:48 +0000812PyDoc_STRVAR(seekable_doc,
813"seekable() -> bool. True if file supports random-access.");
814
815PyDoc_STRVAR(readable_doc,
816"readable() -> bool. True if file was opened in a read mode.");
817
818PyDoc_STRVAR(writable_doc,
819"writable() -> bool. True if file was opened in a write mode.");
820
821static PyMethodDef fileio_methods[] = {
822 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
Guido van Rossum7165cb12007-07-10 06:54:34 +0000823 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000824 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
825 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
826 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
827 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000828#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000829 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000830#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000831 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
832 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
833 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
834 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
835 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
836 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000837 {NULL, NULL} /* sentinel */
838};
839
Guido van Rossum53807da2007-04-10 19:01:47 +0000840/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
841
Guido van Rossumb0428152007-04-08 17:44:42 +0000842static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000843get_closed(PyFileIOObject *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +0000844{
Guido van Rossum53807da2007-04-10 19:01:47 +0000845 return PyBool_FromLong((long)(self->fd < 0));
846}
847
848static PyObject *
Christian Heimesecc42a22008-11-05 19:30:32 +0000849get_closefd(PyFileIOObject *self, void *closure)
850{
851 return PyBool_FromLong((long)(self->closefd));
852}
853
854static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000855get_mode(PyFileIOObject *self, void *closure)
856{
Guido van Rossumc43e79f2007-06-18 18:26:36 +0000857 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +0000858}
859
860static PyGetSetDef fileio_getsetlist[] = {
861 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Christian Heimesecc42a22008-11-05 19:30:32 +0000862 {"closefd", (getter)get_closefd, NULL,
863 "True if the file descriptor will be closed"},
Guido van Rossum53807da2007-04-10 19:01:47 +0000864 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
Guido van Rossumb0428152007-04-08 17:44:42 +0000865 {0},
866};
867
Guido van Rossuma9e20242007-03-08 00:43:48 +0000868PyTypeObject PyFileIO_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000869 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +0000870 "_FileIO",
Guido van Rossuma9e20242007-03-08 00:43:48 +0000871 sizeof(PyFileIOObject),
872 0,
873 (destructor)fileio_dealloc, /* tp_dealloc */
874 0, /* tp_print */
875 0, /* tp_getattr */
876 0, /* tp_setattr */
877 0, /* tp_compare */
878 (reprfunc)fileio_repr, /* tp_repr */
879 0, /* tp_as_number */
880 0, /* tp_as_sequence */
881 0, /* tp_as_mapping */
882 0, /* tp_hash */
883 0, /* tp_call */
884 0, /* tp_str */
885 PyObject_GenericGetAttr, /* tp_getattro */
886 0, /* tp_setattro */
887 0, /* tp_as_buffer */
888 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
889 fileio_doc, /* tp_doc */
890 0, /* tp_traverse */
891 0, /* tp_clear */
892 0, /* tp_richcompare */
893 offsetof(PyFileIOObject, weakreflist), /* tp_weaklistoffset */
894 0, /* tp_iter */
895 0, /* tp_iternext */
896 fileio_methods, /* tp_methods */
897 0, /* tp_members */
Guido van Rossumb0428152007-04-08 17:44:42 +0000898 fileio_getsetlist, /* tp_getset */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000899 0, /* tp_base */
900 0, /* tp_dict */
901 0, /* tp_descr_get */
902 0, /* tp_descr_set */
903 0, /* tp_dictoffset */
904 fileio_init, /* tp_init */
905 PyType_GenericAlloc, /* tp_alloc */
906 fileio_new, /* tp_new */
907 PyObject_Del, /* tp_free */
908};
909
910static PyMethodDef module_methods[] = {
911 {NULL, NULL}
912};
913
Martin v. Löwis1a214512008-06-11 05:26:20 +0000914static struct PyModuleDef fileiomodule = {
915 PyModuleDef_HEAD_INIT,
916 "_fileio",
917 "Fast implementation of io.FileIO.",
918 -1,
919 module_methods,
920 NULL,
921 NULL,
922 NULL,
923 NULL
924};
925
Guido van Rossuma9e20242007-03-08 00:43:48 +0000926PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000927PyInit__fileio(void)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000928{
929 PyObject *m; /* a module object */
930
Martin v. Löwis1a214512008-06-11 05:26:20 +0000931 m = PyModule_Create(&fileiomodule);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000932 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000933 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000934 if (PyType_Ready(&PyFileIO_Type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000935 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000936 Py_INCREF(&PyFileIO_Type);
937 PyModule_AddObject(m, "_FileIO", (PyObject *) &PyFileIO_Type);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000938 return m;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000939}