blob: c23d5a3c1636c81a79608bb668ed27519003371b [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
Neal Norwitz88b44da2007-08-12 17:23:54 +000058/* Returns 0 on success, errno (which is < 0) on failure. */
59static int
60internal_close(PyFileIOObject *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +000061{
Neal Norwitz88b44da2007-08-12 17:23:54 +000062 int save_errno = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +000063 if (self->fd >= 0) {
Guido van Rossumb0428152007-04-08 17:44:42 +000064 int fd = self->fd;
65 self->fd = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +000066 Py_BEGIN_ALLOW_THREADS
Neal Norwitz88b44da2007-08-12 17:23:54 +000067 if (close(fd) < 0)
68 save_errno = errno;
Guido van Rossuma9e20242007-03-08 00:43:48 +000069 Py_END_ALLOW_THREADS
Neal Norwitz88b44da2007-08-12 17:23:54 +000070 }
71 return save_errno;
72}
73
74static PyObject *
75fileio_close(PyFileIOObject *self)
76{
Guido van Rossum2dced8b2007-10-30 17:27:30 +000077 if (!self->closefd) {
Christian Heimesecc42a22008-11-05 19:30:32 +000078 self->fd = -1;
Guido van Rossum2dced8b2007-10-30 17:27:30 +000079 Py_RETURN_NONE;
80 }
Neal Norwitz88b44da2007-08-12 17:23:54 +000081 errno = internal_close(self);
82 if (errno < 0) {
83 PyErr_SetFromErrno(PyExc_IOError);
84 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +000085 }
86
87 Py_RETURN_NONE;
88}
89
90static PyObject *
91fileio_new(PyTypeObject *type, PyObject *args, PyObject *kews)
92{
93 PyFileIOObject *self;
94
95 assert(type != NULL && type->tp_alloc != NULL);
96
97 self = (PyFileIOObject *) type->tp_alloc(type, 0);
98 if (self != NULL) {
99 self->fd = -1;
Christian Heimesdf32b392008-10-30 21:23:35 +0000100 self->readable = 0;
101 self->writable = 0;
102 self->seekable = -1;
103 self->closefd = 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000104 self->weakreflist = NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000105 }
106
107 return (PyObject *) self;
108}
109
110/* On Unix, open will succeed for directories.
111 In Python, there should be no file objects referring to
112 directories, so we need a check. */
113
114static int
115dircheck(PyFileIOObject* self)
116{
117#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
118 struct stat buf;
119 if (self->fd < 0)
120 return 0;
121 if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000122 char *msg = strerror(EISDIR);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000123 PyObject *exc;
Neal Norwitz88b44da2007-08-12 17:23:54 +0000124 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000125
Guido van Rossuma9e20242007-03-08 00:43:48 +0000126 exc = PyObject_CallFunction(PyExc_IOError, "(is)",
127 EISDIR, msg);
128 PyErr_SetObject(PyExc_IOError, exc);
129 Py_XDECREF(exc);
130 return -1;
131 }
132#endif
133 return 0;
134}
135
136
137static int
138fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
139{
140 PyFileIOObject *self = (PyFileIOObject *) oself;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000141 static char *kwlist[] = {"file", "mode", "closefd", NULL};
Guido van Rossuma9e20242007-03-08 00:43:48 +0000142 char *name = NULL;
143 char *mode = "r";
Guido van Rossum53807da2007-04-10 19:01:47 +0000144 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000145#ifdef MS_WINDOWS
146 Py_UNICODE *widename = NULL;
147#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000148 int ret = 0;
149 int rwa = 0, plus = 0, append = 0;
150 int flags = 0;
Guido van Rossumb0428152007-04-08 17:44:42 +0000151 int fd = -1;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000152 int closefd = 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000153
154 assert(PyFileIO_Check(oself));
Neal Norwitz88b44da2007-08-12 17:23:54 +0000155 if (self->fd >= 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000156 /* Have to close the existing file first. */
Neal Norwitz88b44da2007-08-12 17:23:54 +0000157 if (internal_close(self) < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000158 return -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000159 }
160
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000161 if (PyArg_ParseTupleAndKeywords(args, kwds, "i|si:fileio",
162 kwlist, &fd, &mode, &closefd)) {
Guido van Rossumb0428152007-04-08 17:44:42 +0000163 if (fd < 0) {
164 PyErr_SetString(PyExc_ValueError,
165 "Negative filedescriptor");
166 return -1;
167 }
168 }
169 else {
170 PyErr_Clear();
171
Guido van Rossuma9e20242007-03-08 00:43:48 +0000172#ifdef Py_WIN_WIDE_FILENAMES
Guido van Rossumb0428152007-04-08 17:44:42 +0000173 if (GetVersion() < 0x80000000) {
174 /* On NT, so wide API available */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000175 PyObject *po;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000176 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:fileio",
177 kwlist, &po, &mode, &closefd)
178 ) {
Thomas Helleraf2be262007-07-12 11:03:13 +0000179 widename = PyUnicode_AS_UNICODE(po);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000180 } else {
181 /* Drop the argument parsing error as narrow
182 strings are also valid. */
183 PyErr_Clear();
184 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000185 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000186 if (widename == NULL)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000187#endif
Thomas Helleraf2be262007-07-12 11:03:13 +0000188 {
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000189 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:fileio",
Guido van Rossuma9e20242007-03-08 00:43:48 +0000190 kwlist,
191 Py_FileSystemDefaultEncoding,
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000192 &name, &mode, &closefd))
Neal Norwitz6e0e0e62008-08-24 22:07:28 +0000193 return -1;
Guido van Rossumb0428152007-04-08 17:44:42 +0000194 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000195 }
196
Guido van Rossum53807da2007-04-10 19:01:47 +0000197 s = mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000198 while (*s) {
199 switch (*s++) {
200 case 'r':
201 if (rwa) {
202 bad_mode:
203 PyErr_SetString(PyExc_ValueError,
204 "Must have exactly one of read/write/append mode");
205 goto error;
206 }
207 rwa = 1;
208 self->readable = 1;
209 break;
210 case 'w':
211 if (rwa)
212 goto bad_mode;
213 rwa = 1;
214 self->writable = 1;
215 flags |= O_CREAT | O_TRUNC;
216 break;
217 case 'a':
218 if (rwa)
219 goto bad_mode;
220 rwa = 1;
221 self->writable = 1;
222 flags |= O_CREAT;
223 append = 1;
224 break;
Benjamin Peterson44309e62008-11-22 00:41:45 +0000225 case 'b':
226 break;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000227 case '+':
228 if (plus)
229 goto bad_mode;
230 self->readable = self->writable = 1;
231 plus = 1;
232 break;
233 default:
234 PyErr_Format(PyExc_ValueError,
235 "invalid mode: %.200s", mode);
236 goto error;
237 }
238 }
239
240 if (!rwa)
241 goto bad_mode;
242
243 if (self->readable && self->writable)
244 flags |= O_RDWR;
245 else if (self->readable)
246 flags |= O_RDONLY;
247 else
248 flags |= O_WRONLY;
249
250#ifdef O_BINARY
251 flags |= O_BINARY;
252#endif
253
Walter Dörwald0e411482007-06-06 16:55:38 +0000254#ifdef O_APPEND
255 if (append)
256 flags |= O_APPEND;
257#endif
258
Guido van Rossumb0428152007-04-08 17:44:42 +0000259 if (fd >= 0) {
260 self->fd = fd;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000261 self->closefd = closefd;
Guido van Rossumb0428152007-04-08 17:44:42 +0000262 }
263 else {
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000264 self->closefd = 1;
265 if (!closefd) {
266 PyErr_SetString(PyExc_ValueError,
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000267 "Cannot use closefd=False with file name");
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000268 goto error;
269 }
270
Guido van Rossumb0428152007-04-08 17:44:42 +0000271 Py_BEGIN_ALLOW_THREADS
272 errno = 0;
Thomas Helleraf2be262007-07-12 11:03:13 +0000273#ifdef MS_WINDOWS
274 if (widename != NULL)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000275 self->fd = _wopen(widename, flags, 0666);
Thomas Helleraf2be262007-07-12 11:03:13 +0000276 else
277#endif
Neal Norwitz88b44da2007-08-12 17:23:54 +0000278 self->fd = open(name, flags, 0666);
Guido van Rossumb0428152007-04-08 17:44:42 +0000279 Py_END_ALLOW_THREADS
Benjamin Peterson3e4f0552008-09-02 00:31:15 +0000280 if (self->fd < 0) {
Christian Heimes0b489542007-10-31 19:20:48 +0000281#ifdef MS_WINDOWS
282 PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename);
283#else
Guido van Rossumb0428152007-04-08 17:44:42 +0000284 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
Christian Heimes0b489542007-10-31 19:20:48 +0000285#endif
Guido van Rossumb0428152007-04-08 17:44:42 +0000286 goto error;
287 }
Benjamin Peterson3e4f0552008-09-02 00:31:15 +0000288 if(dircheck(self) < 0)
289 goto error;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000290 }
291
292 goto done;
293
294 error:
295 ret = -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000296
Guido van Rossuma9e20242007-03-08 00:43:48 +0000297 done:
Neal Norwitz2f99b242008-08-24 05:48:10 +0000298 PyMem_Free(name);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000299 return ret;
300}
301
302static void
303fileio_dealloc(PyFileIOObject *self)
304{
305 if (self->weakreflist != NULL)
306 PyObject_ClearWeakRefs((PyObject *) self);
307
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000308 if (self->fd >= 0 && self->closefd) {
Neal Norwitz88b44da2007-08-12 17:23:54 +0000309 errno = internal_close(self);
310 if (errno < 0) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000311 PySys_WriteStderr("close failed: [Errno %d] %s\n",
312 errno, strerror(errno));
Neal Norwitz88b44da2007-08-12 17:23:54 +0000313 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000314 }
315
Christian Heimes90aa7642007-12-19 02:45:37 +0000316 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000317}
318
319static PyObject *
320err_closed(void)
321{
322 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
323 return NULL;
324}
325
326static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000327err_mode(char *action)
328{
329 PyErr_Format(PyExc_ValueError, "File not open for %s", action);
330 return NULL;
331}
332
333static PyObject *
Guido van Rossuma9e20242007-03-08 00:43:48 +0000334fileio_fileno(PyFileIOObject *self)
335{
336 if (self->fd < 0)
337 return err_closed();
Christian Heimes217cfd12007-12-02 14:31:20 +0000338 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000339}
340
341static PyObject *
342fileio_readable(PyFileIOObject *self)
343{
344 if (self->fd < 0)
345 return err_closed();
Neal Norwitz88b44da2007-08-12 17:23:54 +0000346 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000347}
348
349static PyObject *
350fileio_writable(PyFileIOObject *self)
351{
352 if (self->fd < 0)
353 return err_closed();
Neal Norwitz88b44da2007-08-12 17:23:54 +0000354 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000355}
356
357static PyObject *
358fileio_seekable(PyFileIOObject *self)
359{
360 if (self->fd < 0)
361 return err_closed();
362 if (self->seekable < 0) {
363 int ret;
364 Py_BEGIN_ALLOW_THREADS
365 ret = lseek(self->fd, 0, SEEK_CUR);
366 Py_END_ALLOW_THREADS
367 if (ret < 0)
368 self->seekable = 0;
369 else
370 self->seekable = 1;
371 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000372 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000373}
374
375static PyObject *
376fileio_readinto(PyFileIOObject *self, PyObject *args)
377{
Martin v. Löwis423be952008-08-13 15:53:07 +0000378 Py_buffer pbuf;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000379 Py_ssize_t n;
Guido van Rossum53807da2007-04-10 19:01:47 +0000380
Guido van Rossuma9e20242007-03-08 00:43:48 +0000381 if (self->fd < 0)
382 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000383 if (!self->readable)
384 return err_mode("reading");
385
Martin v. Löwis423be952008-08-13 15:53:07 +0000386 if (!PyArg_ParseTuple(args, "w*", &pbuf))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000387 return NULL;
388
389 Py_BEGIN_ALLOW_THREADS
390 errno = 0;
Martin v. Löwis423be952008-08-13 15:53:07 +0000391 n = read(self->fd, pbuf.buf, pbuf.len);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000392 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +0000393 PyBuffer_Release(&pbuf);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000394 if (n < 0) {
395 if (errno == EAGAIN)
396 Py_RETURN_NONE;
397 PyErr_SetFromErrno(PyExc_IOError);
398 return NULL;
399 }
400
Christian Heimes217cfd12007-12-02 14:31:20 +0000401 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000402}
403
Guido van Rossum7165cb12007-07-10 06:54:34 +0000404static PyObject *
405fileio_readall(PyFileIOObject *self)
406{
407 PyObject *result;
408 Py_ssize_t total = 0;
409 int n;
410
Christian Heimesa872de52008-12-05 08:26:55 +0000411 result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK);
Guido van Rossum7165cb12007-07-10 06:54:34 +0000412 if (result == NULL)
413 return NULL;
414
415 while (1) {
Christian Heimesa872de52008-12-05 08:26:55 +0000416 Py_ssize_t newsize = (total < SMALLCHUNK) ? SMALLCHUNK : total;
417
418 /* Keep doubling until we reach BIGCHUNK;
419 then keep adding BIGCHUNK. */
420 if (newsize <= BIGCHUNK) {
421 newsize += newsize;
422 }
423 else {
424 /* NOTE: overflow impossible due to limits on BUFSIZ */
425 newsize += BIGCHUNK;
426 }
427
Christian Heimes72b710a2008-05-26 13:28:38 +0000428 if (PyBytes_GET_SIZE(result) < newsize) {
429 if (_PyBytes_Resize(&result, newsize) < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000430 if (total == 0) {
431 Py_DECREF(result);
432 return NULL;
433 }
434 PyErr_Clear();
435 break;
436 }
437 }
438 Py_BEGIN_ALLOW_THREADS
439 errno = 0;
440 n = read(self->fd,
Christian Heimes72b710a2008-05-26 13:28:38 +0000441 PyBytes_AS_STRING(result) + total,
Guido van Rossum7165cb12007-07-10 06:54:34 +0000442 newsize - total);
443 Py_END_ALLOW_THREADS
444 if (n == 0)
445 break;
446 if (n < 0) {
447 if (total > 0)
448 break;
449 if (errno == EAGAIN) {
450 Py_DECREF(result);
451 Py_RETURN_NONE;
452 }
453 Py_DECREF(result);
454 PyErr_SetFromErrno(PyExc_IOError);
455 return NULL;
456 }
457 total += n;
458 }
459
Christian Heimes72b710a2008-05-26 13:28:38 +0000460 if (PyBytes_GET_SIZE(result) > total) {
461 if (_PyBytes_Resize(&result, total) < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000462 /* This should never happen, but just in case */
463 Py_DECREF(result);
464 return NULL;
465 }
466 }
467 return result;
468}
469
Guido van Rossuma9e20242007-03-08 00:43:48 +0000470static PyObject *
471fileio_read(PyFileIOObject *self, PyObject *args)
472{
473 char *ptr;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000474 Py_ssize_t n;
475 Py_ssize_t size = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000476 PyObject *bytes;
477
478 if (self->fd < 0)
479 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000480 if (!self->readable)
481 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000482
Neal Norwitz3c8ba932007-08-08 04:36:17 +0000483 if (!PyArg_ParseTuple(args, "|n", &size))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000484 return NULL;
485
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000486 if (size < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000487 return fileio_readall(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000488 }
489
Christian Heimes72b710a2008-05-26 13:28:38 +0000490 bytes = PyBytes_FromStringAndSize(NULL, size);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000491 if (bytes == NULL)
492 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +0000493 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000494
495 Py_BEGIN_ALLOW_THREADS
496 errno = 0;
497 n = read(self->fd, ptr, size);
498 Py_END_ALLOW_THREADS
499
500 if (n < 0) {
501 if (errno == EAGAIN)
502 Py_RETURN_NONE;
503 PyErr_SetFromErrno(PyExc_IOError);
504 return NULL;
505 }
506
507 if (n != size) {
Christian Heimes72b710a2008-05-26 13:28:38 +0000508 if (_PyBytes_Resize(&bytes, n) < 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000509 Py_DECREF(bytes);
Guido van Rossum53807da2007-04-10 19:01:47 +0000510 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000511 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000512 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000513
514 return (PyObject *) bytes;
515}
516
517static PyObject *
518fileio_write(PyFileIOObject *self, PyObject *args)
519{
Martin v. Löwis423be952008-08-13 15:53:07 +0000520 Py_buffer pbuf;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000521 Py_ssize_t n;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000522
523 if (self->fd < 0)
524 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000525 if (!self->writable)
526 return err_mode("writing");
527
Martin v. Löwis423be952008-08-13 15:53:07 +0000528 if (!PyArg_ParseTuple(args, "s*", &pbuf))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000529 return NULL;
530
531 Py_BEGIN_ALLOW_THREADS
532 errno = 0;
Martin v. Löwis423be952008-08-13 15:53:07 +0000533 n = write(self->fd, pbuf.buf, pbuf.len);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000534 Py_END_ALLOW_THREADS
535
Martin v. Löwis423be952008-08-13 15:53:07 +0000536 PyBuffer_Release(&pbuf);
537
Guido van Rossuma9e20242007-03-08 00:43:48 +0000538 if (n < 0) {
539 if (errno == EAGAIN)
540 Py_RETURN_NONE;
541 PyErr_SetFromErrno(PyExc_IOError);
542 return NULL;
543 }
544
Christian Heimes217cfd12007-12-02 14:31:20 +0000545 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000546}
547
Guido van Rossum53807da2007-04-10 19:01:47 +0000548/* XXX Windows support below is likely incomplete */
549
550#if defined(MS_WIN64) || defined(MS_WINDOWS)
551typedef PY_LONG_LONG Py_off_t;
552#else
553typedef off_t Py_off_t;
554#endif
555
556/* Cribbed from posix_lseek() */
557static PyObject *
558portable_lseek(int fd, PyObject *posobj, int whence)
559{
560 Py_off_t pos, res;
561
562#ifdef SEEK_SET
563 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
564 switch (whence) {
565#if SEEK_SET != 0
566 case 0: whence = SEEK_SET; break;
567#endif
568#if SEEK_CUR != 1
569 case 1: whence = SEEK_CUR; break;
570#endif
571#if SEEL_END != 2
572 case 2: whence = SEEK_END; break;
573#endif
574 }
575#endif /* SEEK_SET */
576
577 if (posobj == NULL)
578 pos = 0;
579 else {
Christian Heimes8e42a0a2007-11-08 18:04:45 +0000580 if(PyFloat_Check(posobj)) {
581 PyErr_SetString(PyExc_TypeError, "an integer is required");
582 return NULL;
583 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000584#if defined(HAVE_LARGEFILE_SUPPORT)
585 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000586#else
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000587 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000588#endif
589 if (PyErr_Occurred())
590 return NULL;
591 }
592
593 Py_BEGIN_ALLOW_THREADS
594#if defined(MS_WIN64) || defined(MS_WINDOWS)
595 res = _lseeki64(fd, pos, whence);
596#else
597 res = lseek(fd, pos, whence);
598#endif
599 Py_END_ALLOW_THREADS
600 if (res < 0)
601 return PyErr_SetFromErrno(PyExc_IOError);
602
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000603#if defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum53807da2007-04-10 19:01:47 +0000604 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000605#else
606 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000607#endif
608}
609
Guido van Rossuma9e20242007-03-08 00:43:48 +0000610static PyObject *
611fileio_seek(PyFileIOObject *self, PyObject *args)
612{
Guido van Rossum53807da2007-04-10 19:01:47 +0000613 PyObject *posobj;
614 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000615
616 if (self->fd < 0)
617 return err_closed();
618
Guido van Rossum53807da2007-04-10 19:01:47 +0000619 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000620 return NULL;
621
Guido van Rossum53807da2007-04-10 19:01:47 +0000622 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000623}
624
625static PyObject *
626fileio_tell(PyFileIOObject *self, PyObject *args)
627{
Guido van Rossuma9e20242007-03-08 00:43:48 +0000628 if (self->fd < 0)
629 return err_closed();
630
Guido van Rossum53807da2007-04-10 19:01:47 +0000631 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000632}
633
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000634#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000635static PyObject *
636fileio_truncate(PyFileIOObject *self, PyObject *args)
637{
Guido van Rossum53807da2007-04-10 19:01:47 +0000638 PyObject *posobj = NULL;
639 Py_off_t pos;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000640 int ret;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000641 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000642
Guido van Rossum53807da2007-04-10 19:01:47 +0000643 fd = self->fd;
644 if (fd < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000645 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000646 if (!self->writable)
647 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000648
Guido van Rossum53807da2007-04-10 19:01:47 +0000649 if (!PyArg_ParseTuple(args, "|O", &posobj))
650 return NULL;
651
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000652 if (posobj == Py_None || posobj == NULL) {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000653 /* Get the current position. */
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000654 posobj = portable_lseek(fd, NULL, 1);
655 if (posobj == NULL)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000656 return NULL;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000657 }
658 else {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000659 /* Move to the position to be truncated. */
660 posobj = portable_lseek(fd, posobj, 0);
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000661 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000662
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000663#if defined(HAVE_LARGEFILE_SUPPORT)
664 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000665#else
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000666 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000667#endif
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000668 if (PyErr_Occurred())
Guido van Rossum53807da2007-04-10 19:01:47 +0000669 return NULL;
670
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000671#ifdef MS_WINDOWS
672 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
673 so don't even try using it. */
674 {
675 HANDLE hFile;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000676
677 /* Truncate. Note that this may grow the file! */
678 Py_BEGIN_ALLOW_THREADS
679 errno = 0;
680 hFile = (HANDLE)_get_osfhandle(fd);
681 ret = hFile == (HANDLE)-1;
682 if (ret == 0) {
683 ret = SetEndOfFile(hFile) == 0;
684 if (ret)
685 errno = EACCES;
686 }
687 Py_END_ALLOW_THREADS
688 }
689#else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000690 Py_BEGIN_ALLOW_THREADS
691 errno = 0;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000692 ret = ftruncate(fd, pos);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000693 Py_END_ALLOW_THREADS
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000694#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000695
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000696 if (ret != 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000697 PyErr_SetFromErrno(PyExc_IOError);
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000698 return NULL;
Guido van Rossum87429772007-04-10 21:06:59 +0000699 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000700
Guido van Rossum87429772007-04-10 21:06:59 +0000701 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000702}
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000703#endif
Guido van Rossum53807da2007-04-10 19:01:47 +0000704
705static char *
706mode_string(PyFileIOObject *self)
707{
708 if (self->readable) {
709 if (self->writable)
Benjamin Peterson44309e62008-11-22 00:41:45 +0000710 return "rb+";
Guido van Rossum53807da2007-04-10 19:01:47 +0000711 else
Benjamin Peterson44309e62008-11-22 00:41:45 +0000712 return "rb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000713 }
714 else
Benjamin Peterson44309e62008-11-22 00:41:45 +0000715 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000716}
Guido van Rossuma9e20242007-03-08 00:43:48 +0000717
718static PyObject *
719fileio_repr(PyFileIOObject *self)
720{
Guido van Rossum53807da2007-04-10 19:01:47 +0000721 if (self->fd < 0)
Walter Dörwald1ab83302007-05-18 17:15:44 +0000722 return PyUnicode_FromFormat("_fileio._FileIO(-1)");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000723
Walter Dörwald1ab83302007-05-18 17:15:44 +0000724 return PyUnicode_FromFormat("_fileio._FileIO(%d, '%s')",
Guido van Rossum53807da2007-04-10 19:01:47 +0000725 self->fd, mode_string(self));
Guido van Rossuma9e20242007-03-08 00:43:48 +0000726}
727
728static PyObject *
729fileio_isatty(PyFileIOObject *self)
730{
731 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000732
Guido van Rossuma9e20242007-03-08 00:43:48 +0000733 if (self->fd < 0)
734 return err_closed();
735 Py_BEGIN_ALLOW_THREADS
736 res = isatty(self->fd);
737 Py_END_ALLOW_THREADS
738 return PyBool_FromLong(res);
739}
740
Guido van Rossuma9e20242007-03-08 00:43:48 +0000741
742PyDoc_STRVAR(fileio_doc,
743"file(name: str[, mode: str]) -> file IO object\n"
744"\n"
745"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
746"writing or appending. The file will be created if it doesn't exist\n"
747"when opened for writing or appending; it will be truncated when\n"
748"opened for writing. Add a '+' to the mode to allow simultaneous\n"
749"reading and writing.");
750
751PyDoc_STRVAR(read_doc,
752"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
753"\n"
754"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +0000755"In non-blocking mode, returns None if no data is available.\n"
756"On end-of-file, returns ''.");
757
758PyDoc_STRVAR(readall_doc,
759"readall() -> bytes. read all data from the file, returned as bytes.\n"
760"\n"
761"In non-blocking mode, returns as much as is immediately available,\n"
762"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000763
764PyDoc_STRVAR(write_doc,
765"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
766"\n"
767"Only makes one system call, so not all of the data may be written.\n"
768"The number of bytes actually written is returned.");
769
770PyDoc_STRVAR(fileno_doc,
771"fileno() -> int. \"file descriptor\".\n"
772"\n"
773"This is needed for lower-level file interfaces, such the fcntl module.");
774
775PyDoc_STRVAR(seek_doc,
776"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
777"\n"
778"Argument offset is a byte count. Optional argument whence defaults to\n"
779"0 (offset from start of file, offset should be >= 0); other values are 1\n"
780"(move relative to current position, positive or negative), and 2 (move\n"
781"relative to end of file, usually negative, although many platforms allow\n"
782"seeking beyond the end of a file)."
783"\n"
784"Note that not all file objects are seekable.");
785
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000786#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000787PyDoc_STRVAR(truncate_doc,
788"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
789"\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000790"Size defaults to the current file position, as returned by tell()."
791"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000792#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000793
794PyDoc_STRVAR(tell_doc,
795"tell() -> int. Current file position");
796
797PyDoc_STRVAR(readinto_doc,
798"readinto() -> Undocumented. Don't use this; it may go away.");
799
800PyDoc_STRVAR(close_doc,
801"close() -> None. Close the file.\n"
802"\n"
803"A closed file cannot be used for further I/O operations. close() may be\n"
804"called more than once without error. Changes the fileno to -1.");
805
806PyDoc_STRVAR(isatty_doc,
807"isatty() -> bool. True if the file is connected to a tty device.");
808
Guido van Rossuma9e20242007-03-08 00:43:48 +0000809PyDoc_STRVAR(seekable_doc,
810"seekable() -> bool. True if file supports random-access.");
811
812PyDoc_STRVAR(readable_doc,
813"readable() -> bool. True if file was opened in a read mode.");
814
815PyDoc_STRVAR(writable_doc,
816"writable() -> bool. True if file was opened in a write mode.");
817
818static PyMethodDef fileio_methods[] = {
819 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
Guido van Rossum7165cb12007-07-10 06:54:34 +0000820 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000821 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
822 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
823 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
824 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000825#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000826 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000827#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000828 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
829 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
830 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
831 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
832 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
833 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000834 {NULL, NULL} /* sentinel */
835};
836
Guido van Rossum53807da2007-04-10 19:01:47 +0000837/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
838
Guido van Rossumb0428152007-04-08 17:44:42 +0000839static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000840get_closed(PyFileIOObject *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +0000841{
Guido van Rossum53807da2007-04-10 19:01:47 +0000842 return PyBool_FromLong((long)(self->fd < 0));
843}
844
845static PyObject *
Christian Heimesecc42a22008-11-05 19:30:32 +0000846get_closefd(PyFileIOObject *self, void *closure)
847{
848 return PyBool_FromLong((long)(self->closefd));
849}
850
851static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000852get_mode(PyFileIOObject *self, void *closure)
853{
Guido van Rossumc43e79f2007-06-18 18:26:36 +0000854 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +0000855}
856
857static PyGetSetDef fileio_getsetlist[] = {
858 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Christian Heimesecc42a22008-11-05 19:30:32 +0000859 {"closefd", (getter)get_closefd, NULL,
860 "True if the file descriptor will be closed"},
Guido van Rossum53807da2007-04-10 19:01:47 +0000861 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
Guido van Rossumb0428152007-04-08 17:44:42 +0000862 {0},
863};
864
Guido van Rossuma9e20242007-03-08 00:43:48 +0000865PyTypeObject PyFileIO_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000866 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +0000867 "_FileIO",
Guido van Rossuma9e20242007-03-08 00:43:48 +0000868 sizeof(PyFileIOObject),
869 0,
870 (destructor)fileio_dealloc, /* tp_dealloc */
871 0, /* tp_print */
872 0, /* tp_getattr */
873 0, /* tp_setattr */
874 0, /* tp_compare */
875 (reprfunc)fileio_repr, /* tp_repr */
876 0, /* tp_as_number */
877 0, /* tp_as_sequence */
878 0, /* tp_as_mapping */
879 0, /* tp_hash */
880 0, /* tp_call */
881 0, /* tp_str */
882 PyObject_GenericGetAttr, /* tp_getattro */
883 0, /* tp_setattro */
884 0, /* tp_as_buffer */
885 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
886 fileio_doc, /* tp_doc */
887 0, /* tp_traverse */
888 0, /* tp_clear */
889 0, /* tp_richcompare */
890 offsetof(PyFileIOObject, weakreflist), /* tp_weaklistoffset */
891 0, /* tp_iter */
892 0, /* tp_iternext */
893 fileio_methods, /* tp_methods */
894 0, /* tp_members */
Guido van Rossumb0428152007-04-08 17:44:42 +0000895 fileio_getsetlist, /* tp_getset */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000896 0, /* tp_base */
897 0, /* tp_dict */
898 0, /* tp_descr_get */
899 0, /* tp_descr_set */
900 0, /* tp_dictoffset */
901 fileio_init, /* tp_init */
902 PyType_GenericAlloc, /* tp_alloc */
903 fileio_new, /* tp_new */
904 PyObject_Del, /* tp_free */
905};
906
907static PyMethodDef module_methods[] = {
908 {NULL, NULL}
909};
910
Martin v. Löwis1a214512008-06-11 05:26:20 +0000911static struct PyModuleDef fileiomodule = {
912 PyModuleDef_HEAD_INIT,
913 "_fileio",
914 "Fast implementation of io.FileIO.",
915 -1,
916 module_methods,
917 NULL,
918 NULL,
919 NULL,
920 NULL
921};
922
Guido van Rossuma9e20242007-03-08 00:43:48 +0000923PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000924PyInit__fileio(void)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000925{
926 PyObject *m; /* a module object */
927
Martin v. Löwis1a214512008-06-11 05:26:20 +0000928 m = PyModule_Create(&fileiomodule);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000929 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000930 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000931 if (PyType_Ready(&PyFileIO_Type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000932 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000933 Py_INCREF(&PyFileIO_Type);
934 PyModule_AddObject(m, "_FileIO", (PyObject *) &PyFileIO_Type);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000935 return m;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000936}