blob: f02c5ef16a2f9308c488eed0f3887b9e3327b51a [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
30typedef struct {
31 PyObject_HEAD
32 int fd;
Neal Norwitz88b44da2007-08-12 17:23:54 +000033 unsigned readable : 1;
34 unsigned writable : 1;
35 int seekable : 2; /* -1 means unknown */
Guido van Rossum2dced8b2007-10-30 17:27:30 +000036 int closefd : 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +000037 PyObject *weakreflist;
38} PyFileIOObject;
39
Collin Winteraf334382007-03-08 21:46:15 +000040PyTypeObject PyFileIO_Type;
41
Guido van Rossuma9e20242007-03-08 00:43:48 +000042#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
43
Neal Norwitz88b44da2007-08-12 17:23:54 +000044/* Returns 0 on success, errno (which is < 0) on failure. */
45static int
46internal_close(PyFileIOObject *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +000047{
Neal Norwitz88b44da2007-08-12 17:23:54 +000048 int save_errno = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +000049 if (self->fd >= 0) {
Guido van Rossumb0428152007-04-08 17:44:42 +000050 int fd = self->fd;
51 self->fd = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +000052 Py_BEGIN_ALLOW_THREADS
Neal Norwitz88b44da2007-08-12 17:23:54 +000053 if (close(fd) < 0)
54 save_errno = errno;
Guido van Rossuma9e20242007-03-08 00:43:48 +000055 Py_END_ALLOW_THREADS
Neal Norwitz88b44da2007-08-12 17:23:54 +000056 }
57 return save_errno;
58}
59
60static PyObject *
61fileio_close(PyFileIOObject *self)
62{
Guido van Rossum2dced8b2007-10-30 17:27:30 +000063 if (!self->closefd) {
64 if (PyErr_WarnEx(PyExc_RuntimeWarning,
65 "Trying to close unclosable fd!", 3) < 0) {
66 return NULL;
67 }
68 Py_RETURN_NONE;
69 }
Neal Norwitz88b44da2007-08-12 17:23:54 +000070 errno = internal_close(self);
71 if (errno < 0) {
72 PyErr_SetFromErrno(PyExc_IOError);
73 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +000074 }
75
76 Py_RETURN_NONE;
77}
78
79static PyObject *
80fileio_new(PyTypeObject *type, PyObject *args, PyObject *kews)
81{
82 PyFileIOObject *self;
83
84 assert(type != NULL && type->tp_alloc != NULL);
85
86 self = (PyFileIOObject *) type->tp_alloc(type, 0);
87 if (self != NULL) {
88 self->fd = -1;
89 self->weakreflist = NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +000090 }
91
92 return (PyObject *) self;
93}
94
95/* On Unix, open will succeed for directories.
96 In Python, there should be no file objects referring to
97 directories, so we need a check. */
98
99static int
100dircheck(PyFileIOObject* self)
101{
102#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
103 struct stat buf;
104 if (self->fd < 0)
105 return 0;
106 if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
107#ifdef HAVE_STRERROR
108 char *msg = strerror(EISDIR);
109#else
110 char *msg = "Is a directory";
111#endif
112 PyObject *exc;
Neal Norwitz88b44da2007-08-12 17:23:54 +0000113 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000114
Guido van Rossuma9e20242007-03-08 00:43:48 +0000115 exc = PyObject_CallFunction(PyExc_IOError, "(is)",
116 EISDIR, msg);
117 PyErr_SetObject(PyExc_IOError, exc);
118 Py_XDECREF(exc);
119 return -1;
120 }
121#endif
122 return 0;
123}
124
125
126static int
127fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
128{
129 PyFileIOObject *self = (PyFileIOObject *) oself;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000130 static char *kwlist[] = {"file", "mode", "closefd", NULL};
Guido van Rossuma9e20242007-03-08 00:43:48 +0000131 char *name = NULL;
132 char *mode = "r";
Guido van Rossum53807da2007-04-10 19:01:47 +0000133 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000134#ifdef MS_WINDOWS
135 Py_UNICODE *widename = NULL;
136#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000137 int ret = 0;
138 int rwa = 0, plus = 0, append = 0;
139 int flags = 0;
Guido van Rossumb0428152007-04-08 17:44:42 +0000140 int fd = -1;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000141 int closefd = 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000142
143 assert(PyFileIO_Check(oself));
Neal Norwitz88b44da2007-08-12 17:23:54 +0000144 if (self->fd >= 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000145 /* Have to close the existing file first. */
Neal Norwitz88b44da2007-08-12 17:23:54 +0000146 if (internal_close(self) < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000147 return -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000148 }
149
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000150 if (PyArg_ParseTupleAndKeywords(args, kwds, "i|si:fileio",
151 kwlist, &fd, &mode, &closefd)) {
Guido van Rossumb0428152007-04-08 17:44:42 +0000152 if (fd < 0) {
153 PyErr_SetString(PyExc_ValueError,
154 "Negative filedescriptor");
155 return -1;
156 }
157 }
158 else {
159 PyErr_Clear();
160
Guido van Rossuma9e20242007-03-08 00:43:48 +0000161#ifdef Py_WIN_WIDE_FILENAMES
Guido van Rossumb0428152007-04-08 17:44:42 +0000162 if (GetVersion() < 0x80000000) {
163 /* On NT, so wide API available */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000164 PyObject *po;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000165 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:fileio",
166 kwlist, &po, &mode, &closefd)
167 ) {
Thomas Helleraf2be262007-07-12 11:03:13 +0000168 widename = PyUnicode_AS_UNICODE(po);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000169 } else {
170 /* Drop the argument parsing error as narrow
171 strings are also valid. */
172 PyErr_Clear();
173 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000174 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000175 if (widename == NULL)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000176#endif
Thomas Helleraf2be262007-07-12 11:03:13 +0000177 {
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000178 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:fileio",
Guido van Rossuma9e20242007-03-08 00:43:48 +0000179 kwlist,
180 Py_FileSystemDefaultEncoding,
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000181 &name, &mode, &closefd))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000182 goto error;
Guido van Rossumb0428152007-04-08 17:44:42 +0000183 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000184 }
185
186 self->readable = self->writable = 0;
Thomas Helleraf2be262007-07-12 11:03:13 +0000187 self->seekable = -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000188 s = mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000189 while (*s) {
190 switch (*s++) {
191 case 'r':
192 if (rwa) {
193 bad_mode:
194 PyErr_SetString(PyExc_ValueError,
195 "Must have exactly one of read/write/append mode");
196 goto error;
197 }
198 rwa = 1;
199 self->readable = 1;
200 break;
201 case 'w':
202 if (rwa)
203 goto bad_mode;
204 rwa = 1;
205 self->writable = 1;
206 flags |= O_CREAT | O_TRUNC;
207 break;
208 case 'a':
209 if (rwa)
210 goto bad_mode;
211 rwa = 1;
212 self->writable = 1;
213 flags |= O_CREAT;
214 append = 1;
215 break;
216 case '+':
217 if (plus)
218 goto bad_mode;
219 self->readable = self->writable = 1;
220 plus = 1;
221 break;
222 default:
223 PyErr_Format(PyExc_ValueError,
224 "invalid mode: %.200s", mode);
225 goto error;
226 }
227 }
228
229 if (!rwa)
230 goto bad_mode;
231
232 if (self->readable && self->writable)
233 flags |= O_RDWR;
234 else if (self->readable)
235 flags |= O_RDONLY;
236 else
237 flags |= O_WRONLY;
238
239#ifdef O_BINARY
240 flags |= O_BINARY;
241#endif
242
Walter Dörwald0e411482007-06-06 16:55:38 +0000243#ifdef O_APPEND
244 if (append)
245 flags |= O_APPEND;
246#endif
247
Guido van Rossumb0428152007-04-08 17:44:42 +0000248 if (fd >= 0) {
249 self->fd = fd;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000250 self->closefd = closefd;
Guido van Rossumb0428152007-04-08 17:44:42 +0000251 }
252 else {
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000253 self->closefd = 1;
254 if (!closefd) {
255 PyErr_SetString(PyExc_ValueError,
256 "Cannot use closefd=True with file name");
257 goto error;
258 }
259
Guido van Rossumb0428152007-04-08 17:44:42 +0000260 Py_BEGIN_ALLOW_THREADS
261 errno = 0;
Thomas Helleraf2be262007-07-12 11:03:13 +0000262#ifdef MS_WINDOWS
263 if (widename != NULL)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000264 self->fd = _wopen(widename, flags, 0666);
Thomas Helleraf2be262007-07-12 11:03:13 +0000265 else
266#endif
Neal Norwitz88b44da2007-08-12 17:23:54 +0000267 self->fd = open(name, flags, 0666);
Guido van Rossumb0428152007-04-08 17:44:42 +0000268 Py_END_ALLOW_THREADS
269 if (self->fd < 0 || dircheck(self) < 0) {
Christian Heimes0b489542007-10-31 19:20:48 +0000270#ifdef MS_WINDOWS
271 PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename);
272#else
Guido van Rossumb0428152007-04-08 17:44:42 +0000273 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
Christian Heimes0b489542007-10-31 19:20:48 +0000274#endif
Guido van Rossumb0428152007-04-08 17:44:42 +0000275 goto error;
276 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000277 }
278
279 goto done;
280
281 error:
282 ret = -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000283
Guido van Rossuma9e20242007-03-08 00:43:48 +0000284 done:
285 PyMem_Free(name);
286 return ret;
287}
288
289static void
290fileio_dealloc(PyFileIOObject *self)
291{
292 if (self->weakreflist != NULL)
293 PyObject_ClearWeakRefs((PyObject *) self);
294
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000295 if (self->fd >= 0 && self->closefd) {
Neal Norwitz88b44da2007-08-12 17:23:54 +0000296 errno = internal_close(self);
297 if (errno < 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000298#ifdef HAVE_STRERROR
Guido van Rossum53807da2007-04-10 19:01:47 +0000299 PySys_WriteStderr("close failed: [Errno %d] %s\n",
300 errno, strerror(errno));
Guido van Rossuma9e20242007-03-08 00:43:48 +0000301#else
302 PySys_WriteStderr("close failed: [Errno %d]\n", errno);
303#endif
Neal Norwitz88b44da2007-08-12 17:23:54 +0000304 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000305 }
306
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000307 Py_Type(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000308}
309
310static PyObject *
311err_closed(void)
312{
313 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
314 return NULL;
315}
316
317static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000318err_mode(char *action)
319{
320 PyErr_Format(PyExc_ValueError, "File not open for %s", action);
321 return NULL;
322}
323
324static PyObject *
Guido van Rossuma9e20242007-03-08 00:43:48 +0000325fileio_fileno(PyFileIOObject *self)
326{
327 if (self->fd < 0)
328 return err_closed();
329 return PyInt_FromLong((long) self->fd);
330}
331
332static PyObject *
333fileio_readable(PyFileIOObject *self)
334{
335 if (self->fd < 0)
336 return err_closed();
Neal Norwitz88b44da2007-08-12 17:23:54 +0000337 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000338}
339
340static PyObject *
341fileio_writable(PyFileIOObject *self)
342{
343 if (self->fd < 0)
344 return err_closed();
Neal Norwitz88b44da2007-08-12 17:23:54 +0000345 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000346}
347
348static PyObject *
349fileio_seekable(PyFileIOObject *self)
350{
351 if (self->fd < 0)
352 return err_closed();
353 if (self->seekable < 0) {
354 int ret;
355 Py_BEGIN_ALLOW_THREADS
356 ret = lseek(self->fd, 0, SEEK_CUR);
357 Py_END_ALLOW_THREADS
358 if (ret < 0)
359 self->seekable = 0;
360 else
361 self->seekable = 1;
362 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000363 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000364}
365
366static PyObject *
367fileio_readinto(PyFileIOObject *self, PyObject *args)
368{
369 char *ptr;
370 Py_ssize_t n;
Guido van Rossum53807da2007-04-10 19:01:47 +0000371
Guido van Rossuma9e20242007-03-08 00:43:48 +0000372 if (self->fd < 0)
373 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000374 if (!self->readable)
375 return err_mode("reading");
376
Guido van Rossuma9e20242007-03-08 00:43:48 +0000377 if (!PyArg_ParseTuple(args, "w#", &ptr, &n))
378 return NULL;
379
380 Py_BEGIN_ALLOW_THREADS
381 errno = 0;
382 n = read(self->fd, ptr, n);
383 Py_END_ALLOW_THREADS
384 if (n < 0) {
385 if (errno == EAGAIN)
386 Py_RETURN_NONE;
387 PyErr_SetFromErrno(PyExc_IOError);
388 return NULL;
389 }
390
Neal Norwitz88b44da2007-08-12 17:23:54 +0000391 return PyInt_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000392}
393
Guido van Rossum7165cb12007-07-10 06:54:34 +0000394#define DEFAULT_BUFFER_SIZE (8*1024)
395
396static PyObject *
397fileio_readall(PyFileIOObject *self)
398{
399 PyObject *result;
400 Py_ssize_t total = 0;
401 int n;
402
403 result = PyBytes_FromStringAndSize(NULL, DEFAULT_BUFFER_SIZE);
404 if (result == NULL)
405 return NULL;
406
407 while (1) {
408 Py_ssize_t newsize = total + DEFAULT_BUFFER_SIZE;
409 if (PyBytes_GET_SIZE(result) < newsize) {
410 if (PyBytes_Resize(result, newsize) < 0) {
411 if (total == 0) {
412 Py_DECREF(result);
413 return NULL;
414 }
415 PyErr_Clear();
416 break;
417 }
418 }
419 Py_BEGIN_ALLOW_THREADS
420 errno = 0;
421 n = read(self->fd,
422 PyBytes_AS_STRING(result) + total,
423 newsize - total);
424 Py_END_ALLOW_THREADS
425 if (n == 0)
426 break;
427 if (n < 0) {
428 if (total > 0)
429 break;
430 if (errno == EAGAIN) {
431 Py_DECREF(result);
432 Py_RETURN_NONE;
433 }
434 Py_DECREF(result);
435 PyErr_SetFromErrno(PyExc_IOError);
436 return NULL;
437 }
438 total += n;
439 }
440
441 if (PyBytes_GET_SIZE(result) > total) {
442 if (PyBytes_Resize(result, total) < 0) {
443 /* This should never happen, but just in case */
444 Py_DECREF(result);
445 return NULL;
446 }
447 }
448 return result;
449}
450
Guido van Rossuma9e20242007-03-08 00:43:48 +0000451static PyObject *
452fileio_read(PyFileIOObject *self, PyObject *args)
453{
454 char *ptr;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000455 Py_ssize_t n;
456 Py_ssize_t size = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000457 PyObject *bytes;
458
459 if (self->fd < 0)
460 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000461 if (!self->readable)
462 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000463
Neal Norwitz3c8ba932007-08-08 04:36:17 +0000464 if (!PyArg_ParseTuple(args, "|n", &size))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000465 return NULL;
466
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000467 if (size < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000468 return fileio_readall(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000469 }
470
Guido van Rossuma9e20242007-03-08 00:43:48 +0000471 bytes = PyBytes_FromStringAndSize(NULL, size);
472 if (bytes == NULL)
473 return NULL;
474 ptr = PyBytes_AsString(bytes);
475
476 Py_BEGIN_ALLOW_THREADS
477 errno = 0;
478 n = read(self->fd, ptr, size);
479 Py_END_ALLOW_THREADS
480
481 if (n < 0) {
482 if (errno == EAGAIN)
483 Py_RETURN_NONE;
484 PyErr_SetFromErrno(PyExc_IOError);
485 return NULL;
486 }
487
488 if (n != size) {
489 if (PyBytes_Resize(bytes, n) < 0) {
490 Py_DECREF(bytes);
Guido van Rossum53807da2007-04-10 19:01:47 +0000491 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000492 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000493 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000494
495 return (PyObject *) bytes;
496}
497
498static PyObject *
499fileio_write(PyFileIOObject *self, PyObject *args)
500{
501 Py_ssize_t n;
502 char *ptr;
503
504 if (self->fd < 0)
505 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000506 if (!self->writable)
507 return err_mode("writing");
508
Guido van Rossuma9e20242007-03-08 00:43:48 +0000509 if (!PyArg_ParseTuple(args, "s#", &ptr, &n))
510 return NULL;
511
512 Py_BEGIN_ALLOW_THREADS
513 errno = 0;
514 n = write(self->fd, ptr, n);
515 Py_END_ALLOW_THREADS
516
517 if (n < 0) {
518 if (errno == EAGAIN)
519 Py_RETURN_NONE;
520 PyErr_SetFromErrno(PyExc_IOError);
521 return NULL;
522 }
523
Neal Norwitz88b44da2007-08-12 17:23:54 +0000524 return PyInt_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000525}
526
Guido van Rossum53807da2007-04-10 19:01:47 +0000527/* XXX Windows support below is likely incomplete */
528
529#if defined(MS_WIN64) || defined(MS_WINDOWS)
530typedef PY_LONG_LONG Py_off_t;
531#else
532typedef off_t Py_off_t;
533#endif
534
535/* Cribbed from posix_lseek() */
536static PyObject *
537portable_lseek(int fd, PyObject *posobj, int whence)
538{
539 Py_off_t pos, res;
540
541#ifdef SEEK_SET
542 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
543 switch (whence) {
544#if SEEK_SET != 0
545 case 0: whence = SEEK_SET; break;
546#endif
547#if SEEK_CUR != 1
548 case 1: whence = SEEK_CUR; break;
549#endif
550#if SEEL_END != 2
551 case 2: whence = SEEK_END; break;
552#endif
553 }
554#endif /* SEEK_SET */
555
556 if (posobj == NULL)
557 pos = 0;
558 else {
559#if !defined(HAVE_LARGEFILE_SUPPORT)
560 pos = PyInt_AsLong(posobj);
561#else
562 pos = PyLong_Check(posobj) ?
563 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
564#endif
565 if (PyErr_Occurred())
566 return NULL;
567 }
568
569 Py_BEGIN_ALLOW_THREADS
570#if defined(MS_WIN64) || defined(MS_WINDOWS)
571 res = _lseeki64(fd, pos, whence);
572#else
573 res = lseek(fd, pos, whence);
574#endif
575 Py_END_ALLOW_THREADS
576 if (res < 0)
577 return PyErr_SetFromErrno(PyExc_IOError);
578
579#if !defined(HAVE_LARGEFILE_SUPPORT)
580 return PyInt_FromLong(res);
581#else
582 return PyLong_FromLongLong(res);
583#endif
584}
585
Guido van Rossuma9e20242007-03-08 00:43:48 +0000586static PyObject *
587fileio_seek(PyFileIOObject *self, PyObject *args)
588{
Guido van Rossum53807da2007-04-10 19:01:47 +0000589 PyObject *posobj;
590 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000591
592 if (self->fd < 0)
593 return err_closed();
594
Guido van Rossum53807da2007-04-10 19:01:47 +0000595 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000596 return NULL;
597
Guido van Rossum53807da2007-04-10 19:01:47 +0000598 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000599}
600
601static PyObject *
602fileio_tell(PyFileIOObject *self, PyObject *args)
603{
Guido van Rossuma9e20242007-03-08 00:43:48 +0000604 if (self->fd < 0)
605 return err_closed();
606
Guido van Rossum53807da2007-04-10 19:01:47 +0000607 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000608}
609
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000610#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000611static PyObject *
612fileio_truncate(PyFileIOObject *self, PyObject *args)
613{
Guido van Rossum53807da2007-04-10 19:01:47 +0000614 PyObject *posobj = NULL;
615 Py_off_t pos;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000616 int ret;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000617 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000618
Guido van Rossum53807da2007-04-10 19:01:47 +0000619 fd = self->fd;
620 if (fd < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000621 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000622 if (!self->writable)
623 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000624
Guido van Rossum53807da2007-04-10 19:01:47 +0000625 if (!PyArg_ParseTuple(args, "|O", &posobj))
626 return NULL;
627
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000628 if (posobj == Py_None || posobj == NULL) {
629 posobj = portable_lseek(fd, NULL, 1);
630 if (posobj == NULL)
631 return NULL;
632 }
633 else {
634 Py_INCREF(posobj);
635 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000636
637#if !defined(HAVE_LARGEFILE_SUPPORT)
638 pos = PyInt_AsLong(posobj);
639#else
640 pos = PyLong_Check(posobj) ?
641 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
642#endif
Guido van Rossum87429772007-04-10 21:06:59 +0000643 if (PyErr_Occurred()) {
644 Py_DECREF(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000645 return NULL;
Guido van Rossum87429772007-04-10 21:06:59 +0000646 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000647
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000648#ifdef MS_WINDOWS
649 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
650 so don't even try using it. */
651 {
652 HANDLE hFile;
Guido van Rossum57233cb2007-10-26 17:19:33 +0000653 PyObject *pos2, *oldposobj;
654
655 /* store the current position */
656 oldposobj = portable_lseek(self->fd, NULL, 1);
657 if (oldposobj == NULL) {
658 Py_DECREF(posobj);
659 return NULL;
660 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000661
662 /* Have to move current pos to desired endpoint on Windows. */
663 errno = 0;
664 pos2 = portable_lseek(fd, posobj, SEEK_SET);
Guido van Rossum57233cb2007-10-26 17:19:33 +0000665 if (pos2 == NULL) {
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000666 Py_DECREF(posobj);
Guido van Rossum57233cb2007-10-26 17:19:33 +0000667 Py_DECREF(oldposobj);
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000668 return NULL;
669 }
670 Py_DECREF(pos2);
671
672 /* Truncate. Note that this may grow the file! */
673 Py_BEGIN_ALLOW_THREADS
674 errno = 0;
675 hFile = (HANDLE)_get_osfhandle(fd);
676 ret = hFile == (HANDLE)-1;
677 if (ret == 0) {
678 ret = SetEndOfFile(hFile) == 0;
679 if (ret)
680 errno = EACCES;
681 }
682 Py_END_ALLOW_THREADS
Guido van Rossum57233cb2007-10-26 17:19:33 +0000683
684 if (ret == 0) {
685 /* Move to the previous position in the file */
686 pos2 = portable_lseek(fd, oldposobj, SEEK_SET);
687 if (pos2 == NULL) {
688 Py_DECREF(posobj);
689 Py_DECREF(oldposobj);
690 return NULL;
691 }
692 }
693 Py_DECREF(pos2);
694 Py_DECREF(oldposobj);
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000695 }
696#else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000697 Py_BEGIN_ALLOW_THREADS
698 errno = 0;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000699 ret = ftruncate(fd, pos);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000700 Py_END_ALLOW_THREADS
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000701#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000702
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000703 if (ret != 0) {
Guido van Rossum87429772007-04-10 21:06:59 +0000704 Py_DECREF(posobj);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000705 PyErr_SetFromErrno(PyExc_IOError);
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000706 return NULL;
Guido van Rossum87429772007-04-10 21:06:59 +0000707 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000708
Guido van Rossum87429772007-04-10 21:06:59 +0000709 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000710}
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000711#endif
Guido van Rossum53807da2007-04-10 19:01:47 +0000712
713static char *
714mode_string(PyFileIOObject *self)
715{
716 if (self->readable) {
717 if (self->writable)
718 return "r+";
719 else
720 return "r";
721 }
722 else
723 return "w";
724}
Guido van Rossuma9e20242007-03-08 00:43:48 +0000725
726static PyObject *
727fileio_repr(PyFileIOObject *self)
728{
Guido van Rossum53807da2007-04-10 19:01:47 +0000729 if (self->fd < 0)
Walter Dörwald1ab83302007-05-18 17:15:44 +0000730 return PyUnicode_FromFormat("_fileio._FileIO(-1)");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000731
Walter Dörwald1ab83302007-05-18 17:15:44 +0000732 return PyUnicode_FromFormat("_fileio._FileIO(%d, '%s')",
Guido van Rossum53807da2007-04-10 19:01:47 +0000733 self->fd, mode_string(self));
Guido van Rossuma9e20242007-03-08 00:43:48 +0000734}
735
736static PyObject *
737fileio_isatty(PyFileIOObject *self)
738{
739 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000740
Guido van Rossuma9e20242007-03-08 00:43:48 +0000741 if (self->fd < 0)
742 return err_closed();
743 Py_BEGIN_ALLOW_THREADS
744 res = isatty(self->fd);
745 Py_END_ALLOW_THREADS
746 return PyBool_FromLong(res);
747}
748
Guido van Rossuma9e20242007-03-08 00:43:48 +0000749
750PyDoc_STRVAR(fileio_doc,
751"file(name: str[, mode: str]) -> file IO object\n"
752"\n"
753"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
754"writing or appending. The file will be created if it doesn't exist\n"
755"when opened for writing or appending; it will be truncated when\n"
756"opened for writing. Add a '+' to the mode to allow simultaneous\n"
757"reading and writing.");
758
759PyDoc_STRVAR(read_doc,
760"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
761"\n"
762"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +0000763"In non-blocking mode, returns None if no data is available.\n"
764"On end-of-file, returns ''.");
765
766PyDoc_STRVAR(readall_doc,
767"readall() -> bytes. read all data from the file, returned as bytes.\n"
768"\n"
769"In non-blocking mode, returns as much as is immediately available,\n"
770"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000771
772PyDoc_STRVAR(write_doc,
773"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
774"\n"
775"Only makes one system call, so not all of the data may be written.\n"
776"The number of bytes actually written is returned.");
777
778PyDoc_STRVAR(fileno_doc,
779"fileno() -> int. \"file descriptor\".\n"
780"\n"
781"This is needed for lower-level file interfaces, such the fcntl module.");
782
783PyDoc_STRVAR(seek_doc,
784"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
785"\n"
786"Argument offset is a byte count. Optional argument whence defaults to\n"
787"0 (offset from start of file, offset should be >= 0); other values are 1\n"
788"(move relative to current position, positive or negative), and 2 (move\n"
789"relative to end of file, usually negative, although many platforms allow\n"
790"seeking beyond the end of a file)."
791"\n"
792"Note that not all file objects are seekable.");
793
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000794#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000795PyDoc_STRVAR(truncate_doc,
796"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
797"\n"
798"Size defaults to the current file position, as returned by tell().");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000799#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000800
801PyDoc_STRVAR(tell_doc,
802"tell() -> int. Current file position");
803
804PyDoc_STRVAR(readinto_doc,
805"readinto() -> Undocumented. Don't use this; it may go away.");
806
807PyDoc_STRVAR(close_doc,
808"close() -> None. Close the file.\n"
809"\n"
810"A closed file cannot be used for further I/O operations. close() may be\n"
811"called more than once without error. Changes the fileno to -1.");
812
813PyDoc_STRVAR(isatty_doc,
814"isatty() -> bool. True if the file is connected to a tty device.");
815
Guido van Rossuma9e20242007-03-08 00:43:48 +0000816PyDoc_STRVAR(seekable_doc,
817"seekable() -> bool. True if file supports random-access.");
818
819PyDoc_STRVAR(readable_doc,
820"readable() -> bool. True if file was opened in a read mode.");
821
822PyDoc_STRVAR(writable_doc,
823"writable() -> bool. True if file was opened in a write mode.");
824
825static PyMethodDef fileio_methods[] = {
826 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
Guido van Rossum7165cb12007-07-10 06:54:34 +0000827 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000828 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
829 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
830 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
831 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000832#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000833 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000834#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000835 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
836 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
837 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
838 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
839 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
840 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000841 {NULL, NULL} /* sentinel */
842};
843
Guido van Rossum53807da2007-04-10 19:01:47 +0000844/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
845
Guido van Rossumb0428152007-04-08 17:44:42 +0000846static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000847get_closed(PyFileIOObject *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +0000848{
Guido van Rossum53807da2007-04-10 19:01:47 +0000849 return PyBool_FromLong((long)(self->fd < 0));
850}
851
852static PyObject *
853get_mode(PyFileIOObject *self, void *closure)
854{
Guido van Rossumc43e79f2007-06-18 18:26:36 +0000855 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +0000856}
857
858static PyGetSetDef fileio_getsetlist[] = {
859 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Guido van Rossum53807da2007-04-10 19:01:47 +0000860 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
Guido van Rossumb0428152007-04-08 17:44:42 +0000861 {0},
862};
863
Guido van Rossuma9e20242007-03-08 00:43:48 +0000864PyTypeObject PyFileIO_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000865 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000866 "FileIO",
867 sizeof(PyFileIOObject),
868 0,
869 (destructor)fileio_dealloc, /* tp_dealloc */
870 0, /* tp_print */
871 0, /* tp_getattr */
872 0, /* tp_setattr */
873 0, /* tp_compare */
874 (reprfunc)fileio_repr, /* tp_repr */
875 0, /* tp_as_number */
876 0, /* tp_as_sequence */
877 0, /* tp_as_mapping */
878 0, /* tp_hash */
879 0, /* tp_call */
880 0, /* tp_str */
881 PyObject_GenericGetAttr, /* tp_getattro */
882 0, /* tp_setattro */
883 0, /* tp_as_buffer */
884 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
885 fileio_doc, /* tp_doc */
886 0, /* tp_traverse */
887 0, /* tp_clear */
888 0, /* tp_richcompare */
889 offsetof(PyFileIOObject, weakreflist), /* tp_weaklistoffset */
890 0, /* tp_iter */
891 0, /* tp_iternext */
892 fileio_methods, /* tp_methods */
893 0, /* tp_members */
Guido van Rossumb0428152007-04-08 17:44:42 +0000894 fileio_getsetlist, /* tp_getset */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000895 0, /* tp_base */
896 0, /* tp_dict */
897 0, /* tp_descr_get */
898 0, /* tp_descr_set */
899 0, /* tp_dictoffset */
900 fileio_init, /* tp_init */
901 PyType_GenericAlloc, /* tp_alloc */
902 fileio_new, /* tp_new */
903 PyObject_Del, /* tp_free */
904};
905
906static PyMethodDef module_methods[] = {
907 {NULL, NULL}
908};
909
910PyMODINIT_FUNC
911init_fileio(void)
912{
913 PyObject *m; /* a module object */
914
915 m = Py_InitModule3("_fileio", module_methods,
916 "Fast implementation of io.FileIO.");
917 if (m == NULL)
918 return;
919 if (PyType_Ready(&PyFileIO_Type) < 0)
920 return;
921 Py_INCREF(&PyFileIO_Type);
922 PyModule_AddObject(m, "_FileIO", (PyObject *) &PyFileIO_Type);
923}