blob: 03632f7680a002b1470bc286d00a102e117234fc [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
Benjamin Peterson806d4022009-01-19 15:11:51 +0000141static int
142check_fd(int fd)
143{
144#if defined(HAVE_FSTAT)
145 struct stat buf;
146 if (fstat(fd, &buf) < 0 && errno == EBADF) {
147 PyObject *exc;
148 char *msg = strerror(EBADF);
149 exc = PyObject_CallFunction(PyExc_OSError, "(is)",
150 EBADF, msg);
151 PyErr_SetObject(PyExc_OSError, exc);
152 Py_XDECREF(exc);
153 return -1;
154 }
155#endif
156 return 0;
157}
158
Guido van Rossuma9e20242007-03-08 00:43:48 +0000159
160static int
161fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
162{
163 PyFileIOObject *self = (PyFileIOObject *) oself;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000164 static char *kwlist[] = {"file", "mode", "closefd", NULL};
Guido van Rossuma9e20242007-03-08 00:43:48 +0000165 char *name = NULL;
166 char *mode = "r";
Guido van Rossum53807da2007-04-10 19:01:47 +0000167 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000168#ifdef MS_WINDOWS
169 Py_UNICODE *widename = NULL;
170#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000171 int ret = 0;
172 int rwa = 0, plus = 0, append = 0;
173 int flags = 0;
Guido van Rossumb0428152007-04-08 17:44:42 +0000174 int fd = -1;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000175 int closefd = 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000176
177 assert(PyFileIO_Check(oself));
Neal Norwitz88b44da2007-08-12 17:23:54 +0000178 if (self->fd >= 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000179 /* Have to close the existing file first. */
Neal Norwitz88b44da2007-08-12 17:23:54 +0000180 if (internal_close(self) < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000181 return -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000182 }
183
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000184 if (PyArg_ParseTupleAndKeywords(args, kwds, "i|si:fileio",
185 kwlist, &fd, &mode, &closefd)) {
Guido van Rossumb0428152007-04-08 17:44:42 +0000186 if (fd < 0) {
187 PyErr_SetString(PyExc_ValueError,
188 "Negative filedescriptor");
189 return -1;
190 }
Benjamin Peterson806d4022009-01-19 15:11:51 +0000191 if (check_fd(fd))
192 return -1;
Guido van Rossumb0428152007-04-08 17:44:42 +0000193 }
194 else {
195 PyErr_Clear();
196
Guido van Rossuma9e20242007-03-08 00:43:48 +0000197#ifdef Py_WIN_WIDE_FILENAMES
Guido van Rossumb0428152007-04-08 17:44:42 +0000198 if (GetVersion() < 0x80000000) {
199 /* On NT, so wide API available */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000200 PyObject *po;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000201 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:fileio",
202 kwlist, &po, &mode, &closefd)
203 ) {
Thomas Helleraf2be262007-07-12 11:03:13 +0000204 widename = PyUnicode_AS_UNICODE(po);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000205 } else {
206 /* Drop the argument parsing error as narrow
207 strings are also valid. */
208 PyErr_Clear();
209 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000210 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000211 if (widename == NULL)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000212#endif
Thomas Helleraf2be262007-07-12 11:03:13 +0000213 {
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000214 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:fileio",
Guido van Rossuma9e20242007-03-08 00:43:48 +0000215 kwlist,
216 Py_FileSystemDefaultEncoding,
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000217 &name, &mode, &closefd))
Neal Norwitz6e0e0e62008-08-24 22:07:28 +0000218 return -1;
Guido van Rossumb0428152007-04-08 17:44:42 +0000219 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000220 }
221
Guido van Rossum53807da2007-04-10 19:01:47 +0000222 s = mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000223 while (*s) {
224 switch (*s++) {
225 case 'r':
226 if (rwa) {
227 bad_mode:
228 PyErr_SetString(PyExc_ValueError,
229 "Must have exactly one of read/write/append mode");
230 goto error;
231 }
232 rwa = 1;
233 self->readable = 1;
234 break;
235 case 'w':
236 if (rwa)
237 goto bad_mode;
238 rwa = 1;
239 self->writable = 1;
240 flags |= O_CREAT | O_TRUNC;
241 break;
242 case 'a':
243 if (rwa)
244 goto bad_mode;
245 rwa = 1;
246 self->writable = 1;
247 flags |= O_CREAT;
248 append = 1;
249 break;
Benjamin Peterson44309e62008-11-22 00:41:45 +0000250 case 'b':
251 break;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000252 case '+':
253 if (plus)
254 goto bad_mode;
255 self->readable = self->writable = 1;
256 plus = 1;
257 break;
258 default:
259 PyErr_Format(PyExc_ValueError,
260 "invalid mode: %.200s", mode);
261 goto error;
262 }
263 }
264
265 if (!rwa)
266 goto bad_mode;
267
268 if (self->readable && self->writable)
269 flags |= O_RDWR;
270 else if (self->readable)
271 flags |= O_RDONLY;
272 else
273 flags |= O_WRONLY;
274
275#ifdef O_BINARY
276 flags |= O_BINARY;
277#endif
278
Walter Dörwald0e411482007-06-06 16:55:38 +0000279#ifdef O_APPEND
280 if (append)
281 flags |= O_APPEND;
282#endif
283
Guido van Rossumb0428152007-04-08 17:44:42 +0000284 if (fd >= 0) {
285 self->fd = fd;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000286 self->closefd = closefd;
Guido van Rossumb0428152007-04-08 17:44:42 +0000287 }
288 else {
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000289 self->closefd = 1;
290 if (!closefd) {
291 PyErr_SetString(PyExc_ValueError,
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000292 "Cannot use closefd=False with file name");
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000293 goto error;
294 }
295
Guido van Rossumb0428152007-04-08 17:44:42 +0000296 Py_BEGIN_ALLOW_THREADS
297 errno = 0;
Thomas Helleraf2be262007-07-12 11:03:13 +0000298#ifdef MS_WINDOWS
299 if (widename != NULL)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000300 self->fd = _wopen(widename, flags, 0666);
Thomas Helleraf2be262007-07-12 11:03:13 +0000301 else
302#endif
Neal Norwitz88b44da2007-08-12 17:23:54 +0000303 self->fd = open(name, flags, 0666);
Guido van Rossumb0428152007-04-08 17:44:42 +0000304 Py_END_ALLOW_THREADS
Benjamin Peterson3e4f0552008-09-02 00:31:15 +0000305 if (self->fd < 0) {
Christian Heimes0b489542007-10-31 19:20:48 +0000306#ifdef MS_WINDOWS
Hirokazu Yamamoto0f22d692009-01-01 16:03:45 +0000307 if (widename != NULL)
308 PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename);
309 else
Christian Heimes0b489542007-10-31 19:20:48 +0000310#endif
Hirokazu Yamamoto0f22d692009-01-01 16:03:45 +0000311 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
Guido van Rossumb0428152007-04-08 17:44:42 +0000312 goto error;
313 }
Benjamin Peterson1efc23c2008-12-29 18:02:28 +0000314 if(dircheck(self, name) < 0)
Benjamin Peterson3e4f0552008-09-02 00:31:15 +0000315 goto error;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000316 }
317
318 goto done;
319
320 error:
321 ret = -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000322
Guido van Rossuma9e20242007-03-08 00:43:48 +0000323 done:
Neal Norwitz2f99b242008-08-24 05:48:10 +0000324 PyMem_Free(name);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000325 return ret;
326}
327
328static void
329fileio_dealloc(PyFileIOObject *self)
330{
331 if (self->weakreflist != NULL)
332 PyObject_ClearWeakRefs((PyObject *) self);
333
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000334 if (self->fd >= 0 && self->closefd) {
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +0000335 if(internal_close(self))
336 PyErr_WriteUnraisable((PyObject*)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000337 }
338
Christian Heimes90aa7642007-12-19 02:45:37 +0000339 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000340}
341
342static PyObject *
343err_closed(void)
344{
345 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
346 return NULL;
347}
348
349static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000350err_mode(char *action)
351{
352 PyErr_Format(PyExc_ValueError, "File not open for %s", action);
353 return NULL;
354}
355
356static PyObject *
Guido van Rossuma9e20242007-03-08 00:43:48 +0000357fileio_fileno(PyFileIOObject *self)
358{
359 if (self->fd < 0)
360 return err_closed();
Christian Heimes217cfd12007-12-02 14:31:20 +0000361 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000362}
363
364static PyObject *
365fileio_readable(PyFileIOObject *self)
366{
367 if (self->fd < 0)
368 return err_closed();
Neal Norwitz88b44da2007-08-12 17:23:54 +0000369 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000370}
371
372static PyObject *
373fileio_writable(PyFileIOObject *self)
374{
375 if (self->fd < 0)
376 return err_closed();
Neal Norwitz88b44da2007-08-12 17:23:54 +0000377 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000378}
379
380static PyObject *
381fileio_seekable(PyFileIOObject *self)
382{
383 if (self->fd < 0)
384 return err_closed();
385 if (self->seekable < 0) {
386 int ret;
387 Py_BEGIN_ALLOW_THREADS
388 ret = lseek(self->fd, 0, SEEK_CUR);
389 Py_END_ALLOW_THREADS
390 if (ret < 0)
391 self->seekable = 0;
392 else
393 self->seekable = 1;
394 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000395 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000396}
397
398static PyObject *
399fileio_readinto(PyFileIOObject *self, PyObject *args)
400{
Martin v. Löwis423be952008-08-13 15:53:07 +0000401 Py_buffer pbuf;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000402 Py_ssize_t n;
Guido van Rossum53807da2007-04-10 19:01:47 +0000403
Guido van Rossuma9e20242007-03-08 00:43:48 +0000404 if (self->fd < 0)
405 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000406 if (!self->readable)
407 return err_mode("reading");
408
Martin v. Löwis423be952008-08-13 15:53:07 +0000409 if (!PyArg_ParseTuple(args, "w*", &pbuf))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000410 return NULL;
411
412 Py_BEGIN_ALLOW_THREADS
413 errno = 0;
Martin v. Löwis423be952008-08-13 15:53:07 +0000414 n = read(self->fd, pbuf.buf, pbuf.len);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000415 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +0000416 PyBuffer_Release(&pbuf);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000417 if (n < 0) {
418 if (errno == EAGAIN)
419 Py_RETURN_NONE;
420 PyErr_SetFromErrno(PyExc_IOError);
421 return NULL;
422 }
423
Christian Heimes217cfd12007-12-02 14:31:20 +0000424 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000425}
426
Guido van Rossum7165cb12007-07-10 06:54:34 +0000427static PyObject *
428fileio_readall(PyFileIOObject *self)
429{
430 PyObject *result;
431 Py_ssize_t total = 0;
432 int n;
433
Christian Heimesa872de52008-12-05 08:26:55 +0000434 result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK);
Guido van Rossum7165cb12007-07-10 06:54:34 +0000435 if (result == NULL)
436 return NULL;
437
438 while (1) {
Christian Heimesa872de52008-12-05 08:26:55 +0000439 Py_ssize_t newsize = (total < SMALLCHUNK) ? SMALLCHUNK : total;
440
441 /* Keep doubling until we reach BIGCHUNK;
442 then keep adding BIGCHUNK. */
443 if (newsize <= BIGCHUNK) {
444 newsize += newsize;
445 }
446 else {
447 /* NOTE: overflow impossible due to limits on BUFSIZ */
448 newsize += BIGCHUNK;
449 }
450
Christian Heimes72b710a2008-05-26 13:28:38 +0000451 if (PyBytes_GET_SIZE(result) < newsize) {
452 if (_PyBytes_Resize(&result, newsize) < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000453 if (total == 0) {
454 Py_DECREF(result);
455 return NULL;
456 }
457 PyErr_Clear();
458 break;
459 }
460 }
461 Py_BEGIN_ALLOW_THREADS
462 errno = 0;
463 n = read(self->fd,
Christian Heimes72b710a2008-05-26 13:28:38 +0000464 PyBytes_AS_STRING(result) + total,
Guido van Rossum7165cb12007-07-10 06:54:34 +0000465 newsize - total);
466 Py_END_ALLOW_THREADS
467 if (n == 0)
468 break;
469 if (n < 0) {
470 if (total > 0)
471 break;
472 if (errno == EAGAIN) {
473 Py_DECREF(result);
474 Py_RETURN_NONE;
475 }
476 Py_DECREF(result);
477 PyErr_SetFromErrno(PyExc_IOError);
478 return NULL;
479 }
480 total += n;
481 }
482
Christian Heimes72b710a2008-05-26 13:28:38 +0000483 if (PyBytes_GET_SIZE(result) > total) {
484 if (_PyBytes_Resize(&result, total) < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000485 /* This should never happen, but just in case */
486 Py_DECREF(result);
487 return NULL;
488 }
489 }
490 return result;
491}
492
Guido van Rossuma9e20242007-03-08 00:43:48 +0000493static PyObject *
494fileio_read(PyFileIOObject *self, PyObject *args)
495{
496 char *ptr;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000497 Py_ssize_t n;
498 Py_ssize_t size = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000499 PyObject *bytes;
500
501 if (self->fd < 0)
502 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000503 if (!self->readable)
504 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000505
Neal Norwitz3c8ba932007-08-08 04:36:17 +0000506 if (!PyArg_ParseTuple(args, "|n", &size))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000507 return NULL;
508
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000509 if (size < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000510 return fileio_readall(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000511 }
512
Christian Heimes72b710a2008-05-26 13:28:38 +0000513 bytes = PyBytes_FromStringAndSize(NULL, size);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000514 if (bytes == NULL)
515 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +0000516 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000517
518 Py_BEGIN_ALLOW_THREADS
519 errno = 0;
520 n = read(self->fd, ptr, size);
521 Py_END_ALLOW_THREADS
522
523 if (n < 0) {
524 if (errno == EAGAIN)
525 Py_RETURN_NONE;
526 PyErr_SetFromErrno(PyExc_IOError);
527 return NULL;
528 }
529
530 if (n != size) {
Christian Heimes72b710a2008-05-26 13:28:38 +0000531 if (_PyBytes_Resize(&bytes, n) < 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000532 Py_DECREF(bytes);
Guido van Rossum53807da2007-04-10 19:01:47 +0000533 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000534 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000535 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000536
537 return (PyObject *) bytes;
538}
539
540static PyObject *
541fileio_write(PyFileIOObject *self, PyObject *args)
542{
Martin v. Löwis423be952008-08-13 15:53:07 +0000543 Py_buffer pbuf;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000544 Py_ssize_t n;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000545
546 if (self->fd < 0)
547 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000548 if (!self->writable)
549 return err_mode("writing");
550
Martin v. Löwis423be952008-08-13 15:53:07 +0000551 if (!PyArg_ParseTuple(args, "s*", &pbuf))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000552 return NULL;
553
554 Py_BEGIN_ALLOW_THREADS
555 errno = 0;
Martin v. Löwis423be952008-08-13 15:53:07 +0000556 n = write(self->fd, pbuf.buf, pbuf.len);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000557 Py_END_ALLOW_THREADS
558
Martin v. Löwis423be952008-08-13 15:53:07 +0000559 PyBuffer_Release(&pbuf);
560
Guido van Rossuma9e20242007-03-08 00:43:48 +0000561 if (n < 0) {
562 if (errno == EAGAIN)
563 Py_RETURN_NONE;
564 PyErr_SetFromErrno(PyExc_IOError);
565 return NULL;
566 }
567
Christian Heimes217cfd12007-12-02 14:31:20 +0000568 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000569}
570
Guido van Rossum53807da2007-04-10 19:01:47 +0000571/* XXX Windows support below is likely incomplete */
572
573#if defined(MS_WIN64) || defined(MS_WINDOWS)
574typedef PY_LONG_LONG Py_off_t;
575#else
576typedef off_t Py_off_t;
577#endif
578
579/* Cribbed from posix_lseek() */
580static PyObject *
581portable_lseek(int fd, PyObject *posobj, int whence)
582{
583 Py_off_t pos, res;
584
585#ifdef SEEK_SET
586 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
587 switch (whence) {
588#if SEEK_SET != 0
589 case 0: whence = SEEK_SET; break;
590#endif
591#if SEEK_CUR != 1
592 case 1: whence = SEEK_CUR; break;
593#endif
594#if SEEL_END != 2
595 case 2: whence = SEEK_END; break;
596#endif
597 }
598#endif /* SEEK_SET */
599
600 if (posobj == NULL)
601 pos = 0;
602 else {
Christian Heimes8e42a0a2007-11-08 18:04:45 +0000603 if(PyFloat_Check(posobj)) {
604 PyErr_SetString(PyExc_TypeError, "an integer is required");
605 return NULL;
606 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000607#if defined(HAVE_LARGEFILE_SUPPORT)
608 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000609#else
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000610 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000611#endif
612 if (PyErr_Occurred())
613 return NULL;
614 }
615
616 Py_BEGIN_ALLOW_THREADS
617#if defined(MS_WIN64) || defined(MS_WINDOWS)
618 res = _lseeki64(fd, pos, whence);
619#else
620 res = lseek(fd, pos, whence);
621#endif
622 Py_END_ALLOW_THREADS
623 if (res < 0)
624 return PyErr_SetFromErrno(PyExc_IOError);
625
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000626#if defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum53807da2007-04-10 19:01:47 +0000627 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000628#else
629 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000630#endif
631}
632
Guido van Rossuma9e20242007-03-08 00:43:48 +0000633static PyObject *
634fileio_seek(PyFileIOObject *self, PyObject *args)
635{
Guido van Rossum53807da2007-04-10 19:01:47 +0000636 PyObject *posobj;
637 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000638
639 if (self->fd < 0)
640 return err_closed();
641
Guido van Rossum53807da2007-04-10 19:01:47 +0000642 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000643 return NULL;
644
Guido van Rossum53807da2007-04-10 19:01:47 +0000645 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000646}
647
648static PyObject *
649fileio_tell(PyFileIOObject *self, PyObject *args)
650{
Guido van Rossuma9e20242007-03-08 00:43:48 +0000651 if (self->fd < 0)
652 return err_closed();
653
Guido van Rossum53807da2007-04-10 19:01:47 +0000654 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000655}
656
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000657#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000658static PyObject *
659fileio_truncate(PyFileIOObject *self, PyObject *args)
660{
Guido van Rossum53807da2007-04-10 19:01:47 +0000661 PyObject *posobj = NULL;
662 Py_off_t pos;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000663 int ret;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000664 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000665
Guido van Rossum53807da2007-04-10 19:01:47 +0000666 fd = self->fd;
667 if (fd < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000668 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000669 if (!self->writable)
670 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000671
Guido van Rossum53807da2007-04-10 19:01:47 +0000672 if (!PyArg_ParseTuple(args, "|O", &posobj))
673 return NULL;
674
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000675 if (posobj == Py_None || posobj == NULL) {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000676 /* Get the current position. */
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000677 posobj = portable_lseek(fd, NULL, 1);
678 if (posobj == NULL)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000679 return NULL;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000680 }
681 else {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000682 /* Move to the position to be truncated. */
683 posobj = portable_lseek(fd, posobj, 0);
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000684 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000685
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000686#if defined(HAVE_LARGEFILE_SUPPORT)
687 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000688#else
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000689 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000690#endif
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000691 if (PyErr_Occurred())
Guido van Rossum53807da2007-04-10 19:01:47 +0000692 return NULL;
693
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000694#ifdef MS_WINDOWS
695 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
696 so don't even try using it. */
697 {
698 HANDLE hFile;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000699
700 /* Truncate. Note that this may grow the file! */
701 Py_BEGIN_ALLOW_THREADS
702 errno = 0;
703 hFile = (HANDLE)_get_osfhandle(fd);
704 ret = hFile == (HANDLE)-1;
705 if (ret == 0) {
706 ret = SetEndOfFile(hFile) == 0;
707 if (ret)
708 errno = EACCES;
709 }
710 Py_END_ALLOW_THREADS
711 }
712#else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000713 Py_BEGIN_ALLOW_THREADS
714 errno = 0;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000715 ret = ftruncate(fd, pos);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000716 Py_END_ALLOW_THREADS
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000717#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000718
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000719 if (ret != 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000720 PyErr_SetFromErrno(PyExc_IOError);
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000721 return NULL;
Guido van Rossum87429772007-04-10 21:06:59 +0000722 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000723
Guido van Rossum87429772007-04-10 21:06:59 +0000724 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000725}
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000726#endif
Guido van Rossum53807da2007-04-10 19:01:47 +0000727
728static char *
729mode_string(PyFileIOObject *self)
730{
731 if (self->readable) {
732 if (self->writable)
Benjamin Peterson44309e62008-11-22 00:41:45 +0000733 return "rb+";
Guido van Rossum53807da2007-04-10 19:01:47 +0000734 else
Benjamin Peterson44309e62008-11-22 00:41:45 +0000735 return "rb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000736 }
737 else
Benjamin Peterson44309e62008-11-22 00:41:45 +0000738 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000739}
Guido van Rossuma9e20242007-03-08 00:43:48 +0000740
741static PyObject *
742fileio_repr(PyFileIOObject *self)
743{
Guido van Rossum53807da2007-04-10 19:01:47 +0000744 if (self->fd < 0)
Walter Dörwald1ab83302007-05-18 17:15:44 +0000745 return PyUnicode_FromFormat("_fileio._FileIO(-1)");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000746
Walter Dörwald1ab83302007-05-18 17:15:44 +0000747 return PyUnicode_FromFormat("_fileio._FileIO(%d, '%s')",
Guido van Rossum53807da2007-04-10 19:01:47 +0000748 self->fd, mode_string(self));
Guido van Rossuma9e20242007-03-08 00:43:48 +0000749}
750
751static PyObject *
752fileio_isatty(PyFileIOObject *self)
753{
754 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000755
Guido van Rossuma9e20242007-03-08 00:43:48 +0000756 if (self->fd < 0)
757 return err_closed();
758 Py_BEGIN_ALLOW_THREADS
759 res = isatty(self->fd);
760 Py_END_ALLOW_THREADS
761 return PyBool_FromLong(res);
762}
763
Guido van Rossuma9e20242007-03-08 00:43:48 +0000764
765PyDoc_STRVAR(fileio_doc,
766"file(name: str[, mode: str]) -> file IO object\n"
767"\n"
768"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
769"writing or appending. The file will be created if it doesn't exist\n"
770"when opened for writing or appending; it will be truncated when\n"
771"opened for writing. Add a '+' to the mode to allow simultaneous\n"
772"reading and writing.");
773
774PyDoc_STRVAR(read_doc,
775"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
776"\n"
777"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +0000778"In non-blocking mode, returns None if no data is available.\n"
779"On end-of-file, returns ''.");
780
781PyDoc_STRVAR(readall_doc,
782"readall() -> bytes. read all data from the file, returned as bytes.\n"
783"\n"
784"In non-blocking mode, returns as much as is immediately available,\n"
785"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000786
787PyDoc_STRVAR(write_doc,
788"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
789"\n"
790"Only makes one system call, so not all of the data may be written.\n"
791"The number of bytes actually written is returned.");
792
793PyDoc_STRVAR(fileno_doc,
794"fileno() -> int. \"file descriptor\".\n"
795"\n"
796"This is needed for lower-level file interfaces, such the fcntl module.");
797
798PyDoc_STRVAR(seek_doc,
799"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
800"\n"
801"Argument offset is a byte count. Optional argument whence defaults to\n"
802"0 (offset from start of file, offset should be >= 0); other values are 1\n"
803"(move relative to current position, positive or negative), and 2 (move\n"
804"relative to end of file, usually negative, although many platforms allow\n"
805"seeking beyond the end of a file)."
806"\n"
807"Note that not all file objects are seekable.");
808
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000809#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000810PyDoc_STRVAR(truncate_doc,
811"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
812"\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000813"Size defaults to the current file position, as returned by tell()."
814"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000815#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000816
817PyDoc_STRVAR(tell_doc,
818"tell() -> int. Current file position");
819
820PyDoc_STRVAR(readinto_doc,
821"readinto() -> Undocumented. Don't use this; it may go away.");
822
823PyDoc_STRVAR(close_doc,
824"close() -> None. Close the file.\n"
825"\n"
826"A closed file cannot be used for further I/O operations. close() may be\n"
827"called more than once without error. Changes the fileno to -1.");
828
829PyDoc_STRVAR(isatty_doc,
830"isatty() -> bool. True if the file is connected to a tty device.");
831
Guido van Rossuma9e20242007-03-08 00:43:48 +0000832PyDoc_STRVAR(seekable_doc,
833"seekable() -> bool. True if file supports random-access.");
834
835PyDoc_STRVAR(readable_doc,
836"readable() -> bool. True if file was opened in a read mode.");
837
838PyDoc_STRVAR(writable_doc,
839"writable() -> bool. True if file was opened in a write mode.");
840
841static PyMethodDef fileio_methods[] = {
842 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
Guido van Rossum7165cb12007-07-10 06:54:34 +0000843 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000844 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
845 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
846 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
847 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000848#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000849 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000850#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000851 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
852 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
853 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
854 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
855 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
856 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000857 {NULL, NULL} /* sentinel */
858};
859
Guido van Rossum53807da2007-04-10 19:01:47 +0000860/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
861
Guido van Rossumb0428152007-04-08 17:44:42 +0000862static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000863get_closed(PyFileIOObject *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +0000864{
Guido van Rossum53807da2007-04-10 19:01:47 +0000865 return PyBool_FromLong((long)(self->fd < 0));
866}
867
868static PyObject *
Christian Heimesecc42a22008-11-05 19:30:32 +0000869get_closefd(PyFileIOObject *self, void *closure)
870{
871 return PyBool_FromLong((long)(self->closefd));
872}
873
874static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000875get_mode(PyFileIOObject *self, void *closure)
876{
Guido van Rossumc43e79f2007-06-18 18:26:36 +0000877 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +0000878}
879
880static PyGetSetDef fileio_getsetlist[] = {
881 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Christian Heimesecc42a22008-11-05 19:30:32 +0000882 {"closefd", (getter)get_closefd, NULL,
883 "True if the file descriptor will be closed"},
Guido van Rossum53807da2007-04-10 19:01:47 +0000884 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
Guido van Rossumb0428152007-04-08 17:44:42 +0000885 {0},
886};
887
Guido van Rossuma9e20242007-03-08 00:43:48 +0000888PyTypeObject PyFileIO_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000889 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +0000890 "_FileIO",
Guido van Rossuma9e20242007-03-08 00:43:48 +0000891 sizeof(PyFileIOObject),
892 0,
893 (destructor)fileio_dealloc, /* tp_dealloc */
894 0, /* tp_print */
895 0, /* tp_getattr */
896 0, /* tp_setattr */
897 0, /* tp_compare */
898 (reprfunc)fileio_repr, /* tp_repr */
899 0, /* tp_as_number */
900 0, /* tp_as_sequence */
901 0, /* tp_as_mapping */
902 0, /* tp_hash */
903 0, /* tp_call */
904 0, /* tp_str */
905 PyObject_GenericGetAttr, /* tp_getattro */
906 0, /* tp_setattro */
907 0, /* tp_as_buffer */
908 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
909 fileio_doc, /* tp_doc */
910 0, /* tp_traverse */
911 0, /* tp_clear */
912 0, /* tp_richcompare */
913 offsetof(PyFileIOObject, weakreflist), /* tp_weaklistoffset */
914 0, /* tp_iter */
915 0, /* tp_iternext */
916 fileio_methods, /* tp_methods */
917 0, /* tp_members */
Guido van Rossumb0428152007-04-08 17:44:42 +0000918 fileio_getsetlist, /* tp_getset */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000919 0, /* tp_base */
920 0, /* tp_dict */
921 0, /* tp_descr_get */
922 0, /* tp_descr_set */
923 0, /* tp_dictoffset */
924 fileio_init, /* tp_init */
925 PyType_GenericAlloc, /* tp_alloc */
926 fileio_new, /* tp_new */
927 PyObject_Del, /* tp_free */
928};
929
930static PyMethodDef module_methods[] = {
931 {NULL, NULL}
932};
933
Martin v. Löwis1a214512008-06-11 05:26:20 +0000934static struct PyModuleDef fileiomodule = {
935 PyModuleDef_HEAD_INIT,
936 "_fileio",
937 "Fast implementation of io.FileIO.",
938 -1,
939 module_methods,
940 NULL,
941 NULL,
942 NULL,
943 NULL
944};
945
Guido van Rossuma9e20242007-03-08 00:43:48 +0000946PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000947PyInit__fileio(void)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000948{
949 PyObject *m; /* a module object */
950
Martin v. Löwis1a214512008-06-11 05:26:20 +0000951 m = PyModule_Create(&fileiomodule);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000952 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000953 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000954 if (PyType_Ready(&PyFileIO_Type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000955 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000956 Py_INCREF(&PyFileIO_Type);
957 PyModule_AddObject(m, "_FileIO", (PyObject *) &PyFileIO_Type);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000958 return m;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000959}