blob: 2cec2132235d68b36c8a62082cb5ce9bcd3c2fd9 [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) {
Christian Heimesecc42a22008-11-05 19:30:32 +000064 self->fd = -1;
Guido van Rossum2dced8b2007-10-30 17:27:30 +000065 Py_RETURN_NONE;
66 }
Neal Norwitz88b44da2007-08-12 17:23:54 +000067 errno = internal_close(self);
68 if (errno < 0) {
69 PyErr_SetFromErrno(PyExc_IOError);
70 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +000071 }
72
73 Py_RETURN_NONE;
74}
75
76static PyObject *
77fileio_new(PyTypeObject *type, PyObject *args, PyObject *kews)
78{
79 PyFileIOObject *self;
80
81 assert(type != NULL && type->tp_alloc != NULL);
82
83 self = (PyFileIOObject *) type->tp_alloc(type, 0);
84 if (self != NULL) {
85 self->fd = -1;
Christian Heimesdf32b392008-10-30 21:23:35 +000086 self->readable = 0;
87 self->writable = 0;
88 self->seekable = -1;
89 self->closefd = 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +000090 self->weakreflist = NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +000091 }
92
93 return (PyObject *) self;
94}
95
96/* On Unix, open will succeed for directories.
97 In Python, there should be no file objects referring to
98 directories, so we need a check. */
99
100static int
101dircheck(PyFileIOObject* self)
102{
103#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
104 struct stat buf;
105 if (self->fd < 0)
106 return 0;
107 if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000108 char *msg = strerror(EISDIR);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000109 PyObject *exc;
Neal Norwitz88b44da2007-08-12 17:23:54 +0000110 internal_close(self);
Guido van Rossum53807da2007-04-10 19:01:47 +0000111
Guido van Rossuma9e20242007-03-08 00:43:48 +0000112 exc = PyObject_CallFunction(PyExc_IOError, "(is)",
113 EISDIR, msg);
114 PyErr_SetObject(PyExc_IOError, exc);
115 Py_XDECREF(exc);
116 return -1;
117 }
118#endif
119 return 0;
120}
121
122
123static int
124fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
125{
126 PyFileIOObject *self = (PyFileIOObject *) oself;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000127 static char *kwlist[] = {"file", "mode", "closefd", NULL};
Guido van Rossuma9e20242007-03-08 00:43:48 +0000128 char *name = NULL;
129 char *mode = "r";
Guido van Rossum53807da2007-04-10 19:01:47 +0000130 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000131#ifdef MS_WINDOWS
132 Py_UNICODE *widename = NULL;
133#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000134 int ret = 0;
135 int rwa = 0, plus = 0, append = 0;
136 int flags = 0;
Guido van Rossumb0428152007-04-08 17:44:42 +0000137 int fd = -1;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000138 int closefd = 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000139
140 assert(PyFileIO_Check(oself));
Neal Norwitz88b44da2007-08-12 17:23:54 +0000141 if (self->fd >= 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000142 /* Have to close the existing file first. */
Neal Norwitz88b44da2007-08-12 17:23:54 +0000143 if (internal_close(self) < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000144 return -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000145 }
146
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000147 if (PyArg_ParseTupleAndKeywords(args, kwds, "i|si:fileio",
148 kwlist, &fd, &mode, &closefd)) {
Guido van Rossumb0428152007-04-08 17:44:42 +0000149 if (fd < 0) {
150 PyErr_SetString(PyExc_ValueError,
151 "Negative filedescriptor");
152 return -1;
153 }
154 }
155 else {
156 PyErr_Clear();
157
Guido van Rossuma9e20242007-03-08 00:43:48 +0000158#ifdef Py_WIN_WIDE_FILENAMES
Guido van Rossumb0428152007-04-08 17:44:42 +0000159 if (GetVersion() < 0x80000000) {
160 /* On NT, so wide API available */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000161 PyObject *po;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000162 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:fileio",
163 kwlist, &po, &mode, &closefd)
164 ) {
Thomas Helleraf2be262007-07-12 11:03:13 +0000165 widename = PyUnicode_AS_UNICODE(po);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000166 } else {
167 /* Drop the argument parsing error as narrow
168 strings are also valid. */
169 PyErr_Clear();
170 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000171 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000172 if (widename == NULL)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000173#endif
Thomas Helleraf2be262007-07-12 11:03:13 +0000174 {
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000175 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:fileio",
Guido van Rossuma9e20242007-03-08 00:43:48 +0000176 kwlist,
177 Py_FileSystemDefaultEncoding,
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000178 &name, &mode, &closefd))
Neal Norwitz6e0e0e62008-08-24 22:07:28 +0000179 return -1;
Guido van Rossumb0428152007-04-08 17:44:42 +0000180 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000181 }
182
Guido van Rossum53807da2007-04-10 19:01:47 +0000183 s = mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000184 while (*s) {
185 switch (*s++) {
186 case 'r':
187 if (rwa) {
188 bad_mode:
189 PyErr_SetString(PyExc_ValueError,
190 "Must have exactly one of read/write/append mode");
191 goto error;
192 }
193 rwa = 1;
194 self->readable = 1;
195 break;
196 case 'w':
197 if (rwa)
198 goto bad_mode;
199 rwa = 1;
200 self->writable = 1;
201 flags |= O_CREAT | O_TRUNC;
202 break;
203 case 'a':
204 if (rwa)
205 goto bad_mode;
206 rwa = 1;
207 self->writable = 1;
208 flags |= O_CREAT;
209 append = 1;
210 break;
211 case '+':
212 if (plus)
213 goto bad_mode;
214 self->readable = self->writable = 1;
215 plus = 1;
216 break;
217 default:
218 PyErr_Format(PyExc_ValueError,
219 "invalid mode: %.200s", mode);
220 goto error;
221 }
222 }
223
224 if (!rwa)
225 goto bad_mode;
226
227 if (self->readable && self->writable)
228 flags |= O_RDWR;
229 else if (self->readable)
230 flags |= O_RDONLY;
231 else
232 flags |= O_WRONLY;
233
234#ifdef O_BINARY
235 flags |= O_BINARY;
236#endif
237
Walter Dörwald0e411482007-06-06 16:55:38 +0000238#ifdef O_APPEND
239 if (append)
240 flags |= O_APPEND;
241#endif
242
Guido van Rossumb0428152007-04-08 17:44:42 +0000243 if (fd >= 0) {
244 self->fd = fd;
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000245 self->closefd = closefd;
Guido van Rossumb0428152007-04-08 17:44:42 +0000246 }
247 else {
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000248 self->closefd = 1;
249 if (!closefd) {
250 PyErr_SetString(PyExc_ValueError,
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000251 "Cannot use closefd=False with file name");
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000252 goto error;
253 }
254
Guido van Rossumb0428152007-04-08 17:44:42 +0000255 Py_BEGIN_ALLOW_THREADS
256 errno = 0;
Thomas Helleraf2be262007-07-12 11:03:13 +0000257#ifdef MS_WINDOWS
258 if (widename != NULL)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000259 self->fd = _wopen(widename, flags, 0666);
Thomas Helleraf2be262007-07-12 11:03:13 +0000260 else
261#endif
Neal Norwitz88b44da2007-08-12 17:23:54 +0000262 self->fd = open(name, flags, 0666);
Guido van Rossumb0428152007-04-08 17:44:42 +0000263 Py_END_ALLOW_THREADS
Benjamin Peterson3e4f0552008-09-02 00:31:15 +0000264 if (self->fd < 0) {
Christian Heimes0b489542007-10-31 19:20:48 +0000265#ifdef MS_WINDOWS
266 PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename);
267#else
Guido van Rossumb0428152007-04-08 17:44:42 +0000268 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
Christian Heimes0b489542007-10-31 19:20:48 +0000269#endif
Guido van Rossumb0428152007-04-08 17:44:42 +0000270 goto error;
271 }
Benjamin Peterson3e4f0552008-09-02 00:31:15 +0000272 if(dircheck(self) < 0)
273 goto error;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000274 }
275
276 goto done;
277
278 error:
279 ret = -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000280
Guido van Rossuma9e20242007-03-08 00:43:48 +0000281 done:
Neal Norwitz2f99b242008-08-24 05:48:10 +0000282 PyMem_Free(name);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000283 return ret;
284}
285
286static void
287fileio_dealloc(PyFileIOObject *self)
288{
289 if (self->weakreflist != NULL)
290 PyObject_ClearWeakRefs((PyObject *) self);
291
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000292 if (self->fd >= 0 && self->closefd) {
Neal Norwitz88b44da2007-08-12 17:23:54 +0000293 errno = internal_close(self);
294 if (errno < 0) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000295 PySys_WriteStderr("close failed: [Errno %d] %s\n",
296 errno, strerror(errno));
Neal Norwitz88b44da2007-08-12 17:23:54 +0000297 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000298 }
299
Christian Heimes90aa7642007-12-19 02:45:37 +0000300 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000301}
302
303static PyObject *
304err_closed(void)
305{
306 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
307 return NULL;
308}
309
310static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000311err_mode(char *action)
312{
313 PyErr_Format(PyExc_ValueError, "File not open for %s", action);
314 return NULL;
315}
316
317static PyObject *
Guido van Rossuma9e20242007-03-08 00:43:48 +0000318fileio_fileno(PyFileIOObject *self)
319{
320 if (self->fd < 0)
321 return err_closed();
Christian Heimes217cfd12007-12-02 14:31:20 +0000322 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000323}
324
325static PyObject *
326fileio_readable(PyFileIOObject *self)
327{
328 if (self->fd < 0)
329 return err_closed();
Neal Norwitz88b44da2007-08-12 17:23:54 +0000330 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000331}
332
333static PyObject *
334fileio_writable(PyFileIOObject *self)
335{
336 if (self->fd < 0)
337 return err_closed();
Neal Norwitz88b44da2007-08-12 17:23:54 +0000338 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000339}
340
341static PyObject *
342fileio_seekable(PyFileIOObject *self)
343{
344 if (self->fd < 0)
345 return err_closed();
346 if (self->seekable < 0) {
347 int ret;
348 Py_BEGIN_ALLOW_THREADS
349 ret = lseek(self->fd, 0, SEEK_CUR);
350 Py_END_ALLOW_THREADS
351 if (ret < 0)
352 self->seekable = 0;
353 else
354 self->seekable = 1;
355 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000356 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000357}
358
359static PyObject *
360fileio_readinto(PyFileIOObject *self, PyObject *args)
361{
Martin v. Löwis423be952008-08-13 15:53:07 +0000362 Py_buffer pbuf;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000363 Py_ssize_t n;
Guido van Rossum53807da2007-04-10 19:01:47 +0000364
Guido van Rossuma9e20242007-03-08 00:43:48 +0000365 if (self->fd < 0)
366 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000367 if (!self->readable)
368 return err_mode("reading");
369
Martin v. Löwis423be952008-08-13 15:53:07 +0000370 if (!PyArg_ParseTuple(args, "w*", &pbuf))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000371 return NULL;
372
373 Py_BEGIN_ALLOW_THREADS
374 errno = 0;
Martin v. Löwis423be952008-08-13 15:53:07 +0000375 n = read(self->fd, pbuf.buf, pbuf.len);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000376 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +0000377 PyBuffer_Release(&pbuf);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000378 if (n < 0) {
379 if (errno == EAGAIN)
380 Py_RETURN_NONE;
381 PyErr_SetFromErrno(PyExc_IOError);
382 return NULL;
383 }
384
Christian Heimes217cfd12007-12-02 14:31:20 +0000385 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000386}
387
Guido van Rossum7165cb12007-07-10 06:54:34 +0000388#define DEFAULT_BUFFER_SIZE (8*1024)
389
390static PyObject *
391fileio_readall(PyFileIOObject *self)
392{
393 PyObject *result;
394 Py_ssize_t total = 0;
395 int n;
396
Christian Heimes72b710a2008-05-26 13:28:38 +0000397 result = PyBytes_FromStringAndSize(NULL, DEFAULT_BUFFER_SIZE);
Guido van Rossum7165cb12007-07-10 06:54:34 +0000398 if (result == NULL)
399 return NULL;
400
401 while (1) {
402 Py_ssize_t newsize = total + DEFAULT_BUFFER_SIZE;
Christian Heimes72b710a2008-05-26 13:28:38 +0000403 if (PyBytes_GET_SIZE(result) < newsize) {
404 if (_PyBytes_Resize(&result, newsize) < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000405 if (total == 0) {
406 Py_DECREF(result);
407 return NULL;
408 }
409 PyErr_Clear();
410 break;
411 }
412 }
413 Py_BEGIN_ALLOW_THREADS
414 errno = 0;
415 n = read(self->fd,
Christian Heimes72b710a2008-05-26 13:28:38 +0000416 PyBytes_AS_STRING(result) + total,
Guido van Rossum7165cb12007-07-10 06:54:34 +0000417 newsize - total);
418 Py_END_ALLOW_THREADS
419 if (n == 0)
420 break;
421 if (n < 0) {
422 if (total > 0)
423 break;
424 if (errno == EAGAIN) {
425 Py_DECREF(result);
426 Py_RETURN_NONE;
427 }
428 Py_DECREF(result);
429 PyErr_SetFromErrno(PyExc_IOError);
430 return NULL;
431 }
432 total += n;
433 }
434
Christian Heimes72b710a2008-05-26 13:28:38 +0000435 if (PyBytes_GET_SIZE(result) > total) {
436 if (_PyBytes_Resize(&result, total) < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000437 /* This should never happen, but just in case */
438 Py_DECREF(result);
439 return NULL;
440 }
441 }
442 return result;
443}
444
Guido van Rossuma9e20242007-03-08 00:43:48 +0000445static PyObject *
446fileio_read(PyFileIOObject *self, PyObject *args)
447{
448 char *ptr;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000449 Py_ssize_t n;
450 Py_ssize_t size = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000451 PyObject *bytes;
452
453 if (self->fd < 0)
454 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000455 if (!self->readable)
456 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000457
Neal Norwitz3c8ba932007-08-08 04:36:17 +0000458 if (!PyArg_ParseTuple(args, "|n", &size))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000459 return NULL;
460
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000461 if (size < 0) {
Guido van Rossum7165cb12007-07-10 06:54:34 +0000462 return fileio_readall(self);
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000463 }
464
Christian Heimes72b710a2008-05-26 13:28:38 +0000465 bytes = PyBytes_FromStringAndSize(NULL, size);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000466 if (bytes == NULL)
467 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +0000468 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000469
470 Py_BEGIN_ALLOW_THREADS
471 errno = 0;
472 n = read(self->fd, ptr, size);
473 Py_END_ALLOW_THREADS
474
475 if (n < 0) {
476 if (errno == EAGAIN)
477 Py_RETURN_NONE;
478 PyErr_SetFromErrno(PyExc_IOError);
479 return NULL;
480 }
481
482 if (n != size) {
Christian Heimes72b710a2008-05-26 13:28:38 +0000483 if (_PyBytes_Resize(&bytes, n) < 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000484 Py_DECREF(bytes);
Guido van Rossum53807da2007-04-10 19:01:47 +0000485 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000486 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000487 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000488
489 return (PyObject *) bytes;
490}
491
492static PyObject *
493fileio_write(PyFileIOObject *self, PyObject *args)
494{
Martin v. Löwis423be952008-08-13 15:53:07 +0000495 Py_buffer pbuf;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000496 Py_ssize_t n;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000497
498 if (self->fd < 0)
499 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000500 if (!self->writable)
501 return err_mode("writing");
502
Martin v. Löwis423be952008-08-13 15:53:07 +0000503 if (!PyArg_ParseTuple(args, "s*", &pbuf))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000504 return NULL;
505
506 Py_BEGIN_ALLOW_THREADS
507 errno = 0;
Martin v. Löwis423be952008-08-13 15:53:07 +0000508 n = write(self->fd, pbuf.buf, pbuf.len);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000509 Py_END_ALLOW_THREADS
510
Martin v. Löwis423be952008-08-13 15:53:07 +0000511 PyBuffer_Release(&pbuf);
512
Guido van Rossuma9e20242007-03-08 00:43:48 +0000513 if (n < 0) {
514 if (errno == EAGAIN)
515 Py_RETURN_NONE;
516 PyErr_SetFromErrno(PyExc_IOError);
517 return NULL;
518 }
519
Christian Heimes217cfd12007-12-02 14:31:20 +0000520 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000521}
522
Guido van Rossum53807da2007-04-10 19:01:47 +0000523/* XXX Windows support below is likely incomplete */
524
525#if defined(MS_WIN64) || defined(MS_WINDOWS)
526typedef PY_LONG_LONG Py_off_t;
527#else
528typedef off_t Py_off_t;
529#endif
530
531/* Cribbed from posix_lseek() */
532static PyObject *
533portable_lseek(int fd, PyObject *posobj, int whence)
534{
535 Py_off_t pos, res;
536
537#ifdef SEEK_SET
538 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
539 switch (whence) {
540#if SEEK_SET != 0
541 case 0: whence = SEEK_SET; break;
542#endif
543#if SEEK_CUR != 1
544 case 1: whence = SEEK_CUR; break;
545#endif
546#if SEEL_END != 2
547 case 2: whence = SEEK_END; break;
548#endif
549 }
550#endif /* SEEK_SET */
551
552 if (posobj == NULL)
553 pos = 0;
554 else {
Christian Heimes8e42a0a2007-11-08 18:04:45 +0000555 if(PyFloat_Check(posobj)) {
556 PyErr_SetString(PyExc_TypeError, "an integer is required");
557 return NULL;
558 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000559#if defined(HAVE_LARGEFILE_SUPPORT)
560 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000561#else
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000562 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000563#endif
564 if (PyErr_Occurred())
565 return NULL;
566 }
567
568 Py_BEGIN_ALLOW_THREADS
569#if defined(MS_WIN64) || defined(MS_WINDOWS)
570 res = _lseeki64(fd, pos, whence);
571#else
572 res = lseek(fd, pos, whence);
573#endif
574 Py_END_ALLOW_THREADS
575 if (res < 0)
576 return PyErr_SetFromErrno(PyExc_IOError);
577
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000578#if defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum53807da2007-04-10 19:01:47 +0000579 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000580#else
581 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000582#endif
583}
584
Guido van Rossuma9e20242007-03-08 00:43:48 +0000585static PyObject *
586fileio_seek(PyFileIOObject *self, PyObject *args)
587{
Guido van Rossum53807da2007-04-10 19:01:47 +0000588 PyObject *posobj;
589 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000590
591 if (self->fd < 0)
592 return err_closed();
593
Guido van Rossum53807da2007-04-10 19:01:47 +0000594 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
Guido van Rossuma9e20242007-03-08 00:43:48 +0000595 return NULL;
596
Guido van Rossum53807da2007-04-10 19:01:47 +0000597 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000598}
599
600static PyObject *
601fileio_tell(PyFileIOObject *self, PyObject *args)
602{
Guido van Rossuma9e20242007-03-08 00:43:48 +0000603 if (self->fd < 0)
604 return err_closed();
605
Guido van Rossum53807da2007-04-10 19:01:47 +0000606 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000607}
608
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000609#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000610static PyObject *
611fileio_truncate(PyFileIOObject *self, PyObject *args)
612{
Guido van Rossum53807da2007-04-10 19:01:47 +0000613 PyObject *posobj = NULL;
614 Py_off_t pos;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000615 int ret;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000616 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000617
Guido van Rossum53807da2007-04-10 19:01:47 +0000618 fd = self->fd;
619 if (fd < 0)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000620 return err_closed();
Guido van Rossum53807da2007-04-10 19:01:47 +0000621 if (!self->writable)
622 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000623
Guido van Rossum53807da2007-04-10 19:01:47 +0000624 if (!PyArg_ParseTuple(args, "|O", &posobj))
625 return NULL;
626
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000627 if (posobj == Py_None || posobj == NULL) {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000628 /* Get the current position. */
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000629 posobj = portable_lseek(fd, NULL, 1);
630 if (posobj == NULL)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000631 return NULL;
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000632 }
633 else {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000634 /* Move to the position to be truncated. */
635 posobj = portable_lseek(fd, posobj, 0);
Guido van Rossumdc0b1a12007-04-12 22:55:07 +0000636 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000637
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000638#if defined(HAVE_LARGEFILE_SUPPORT)
639 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000640#else
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000641 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000642#endif
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000643 if (PyErr_Occurred())
Guido van Rossum53807da2007-04-10 19:01:47 +0000644 return NULL;
645
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000646#ifdef MS_WINDOWS
647 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
648 so don't even try using it. */
649 {
650 HANDLE hFile;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000651
652 /* Truncate. Note that this may grow the file! */
653 Py_BEGIN_ALLOW_THREADS
654 errno = 0;
655 hFile = (HANDLE)_get_osfhandle(fd);
656 ret = hFile == (HANDLE)-1;
657 if (ret == 0) {
658 ret = SetEndOfFile(hFile) == 0;
659 if (ret)
660 errno = EACCES;
661 }
662 Py_END_ALLOW_THREADS
663 }
664#else
Guido van Rossuma9e20242007-03-08 00:43:48 +0000665 Py_BEGIN_ALLOW_THREADS
666 errno = 0;
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000667 ret = ftruncate(fd, pos);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000668 Py_END_ALLOW_THREADS
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000669#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000670
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000671 if (ret != 0) {
Guido van Rossuma9e20242007-03-08 00:43:48 +0000672 PyErr_SetFromErrno(PyExc_IOError);
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000673 return NULL;
Guido van Rossum87429772007-04-10 21:06:59 +0000674 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000675
Guido van Rossum87429772007-04-10 21:06:59 +0000676 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000677}
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000678#endif
Guido van Rossum53807da2007-04-10 19:01:47 +0000679
680static char *
681mode_string(PyFileIOObject *self)
682{
683 if (self->readable) {
684 if (self->writable)
685 return "r+";
686 else
687 return "r";
688 }
689 else
690 return "w";
691}
Guido van Rossuma9e20242007-03-08 00:43:48 +0000692
693static PyObject *
694fileio_repr(PyFileIOObject *self)
695{
Guido van Rossum53807da2007-04-10 19:01:47 +0000696 if (self->fd < 0)
Walter Dörwald1ab83302007-05-18 17:15:44 +0000697 return PyUnicode_FromFormat("_fileio._FileIO(-1)");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000698
Walter Dörwald1ab83302007-05-18 17:15:44 +0000699 return PyUnicode_FromFormat("_fileio._FileIO(%d, '%s')",
Guido van Rossum53807da2007-04-10 19:01:47 +0000700 self->fd, mode_string(self));
Guido van Rossuma9e20242007-03-08 00:43:48 +0000701}
702
703static PyObject *
704fileio_isatty(PyFileIOObject *self)
705{
706 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000707
Guido van Rossuma9e20242007-03-08 00:43:48 +0000708 if (self->fd < 0)
709 return err_closed();
710 Py_BEGIN_ALLOW_THREADS
711 res = isatty(self->fd);
712 Py_END_ALLOW_THREADS
713 return PyBool_FromLong(res);
714}
715
Guido van Rossuma9e20242007-03-08 00:43:48 +0000716
717PyDoc_STRVAR(fileio_doc,
718"file(name: str[, mode: str]) -> file IO object\n"
719"\n"
720"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
721"writing or appending. The file will be created if it doesn't exist\n"
722"when opened for writing or appending; it will be truncated when\n"
723"opened for writing. Add a '+' to the mode to allow simultaneous\n"
724"reading and writing.");
725
726PyDoc_STRVAR(read_doc,
727"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
728"\n"
729"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +0000730"In non-blocking mode, returns None if no data is available.\n"
731"On end-of-file, returns ''.");
732
733PyDoc_STRVAR(readall_doc,
734"readall() -> bytes. read all data from the file, returned as bytes.\n"
735"\n"
736"In non-blocking mode, returns as much as is immediately available,\n"
737"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000738
739PyDoc_STRVAR(write_doc,
740"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
741"\n"
742"Only makes one system call, so not all of the data may be written.\n"
743"The number of bytes actually written is returned.");
744
745PyDoc_STRVAR(fileno_doc,
746"fileno() -> int. \"file descriptor\".\n"
747"\n"
748"This is needed for lower-level file interfaces, such the fcntl module.");
749
750PyDoc_STRVAR(seek_doc,
751"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
752"\n"
753"Argument offset is a byte count. Optional argument whence defaults to\n"
754"0 (offset from start of file, offset should be >= 0); other values are 1\n"
755"(move relative to current position, positive or negative), and 2 (move\n"
756"relative to end of file, usually negative, although many platforms allow\n"
757"seeking beyond the end of a file)."
758"\n"
759"Note that not all file objects are seekable.");
760
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000761#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000762PyDoc_STRVAR(truncate_doc,
763"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
764"\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000765"Size defaults to the current file position, as returned by tell()."
766"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000767#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000768
769PyDoc_STRVAR(tell_doc,
770"tell() -> int. Current file position");
771
772PyDoc_STRVAR(readinto_doc,
773"readinto() -> Undocumented. Don't use this; it may go away.");
774
775PyDoc_STRVAR(close_doc,
776"close() -> None. Close the file.\n"
777"\n"
778"A closed file cannot be used for further I/O operations. close() may be\n"
779"called more than once without error. Changes the fileno to -1.");
780
781PyDoc_STRVAR(isatty_doc,
782"isatty() -> bool. True if the file is connected to a tty device.");
783
Guido van Rossuma9e20242007-03-08 00:43:48 +0000784PyDoc_STRVAR(seekable_doc,
785"seekable() -> bool. True if file supports random-access.");
786
787PyDoc_STRVAR(readable_doc,
788"readable() -> bool. True if file was opened in a read mode.");
789
790PyDoc_STRVAR(writable_doc,
791"writable() -> bool. True if file was opened in a write mode.");
792
793static PyMethodDef fileio_methods[] = {
794 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
Guido van Rossum7165cb12007-07-10 06:54:34 +0000795 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000796 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
797 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
798 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
799 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000800#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000801 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000802#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000803 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
804 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
805 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
806 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
807 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
808 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
Guido van Rossuma9e20242007-03-08 00:43:48 +0000809 {NULL, NULL} /* sentinel */
810};
811
Guido van Rossum53807da2007-04-10 19:01:47 +0000812/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
813
Guido van Rossumb0428152007-04-08 17:44:42 +0000814static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000815get_closed(PyFileIOObject *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +0000816{
Guido van Rossum53807da2007-04-10 19:01:47 +0000817 return PyBool_FromLong((long)(self->fd < 0));
818}
819
820static PyObject *
Christian Heimesecc42a22008-11-05 19:30:32 +0000821get_closefd(PyFileIOObject *self, void *closure)
822{
823 return PyBool_FromLong((long)(self->closefd));
824}
825
826static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000827get_mode(PyFileIOObject *self, void *closure)
828{
Guido van Rossumc43e79f2007-06-18 18:26:36 +0000829 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +0000830}
831
832static PyGetSetDef fileio_getsetlist[] = {
833 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Christian Heimesecc42a22008-11-05 19:30:32 +0000834 {"closefd", (getter)get_closefd, NULL,
835 "True if the file descriptor will be closed"},
Guido van Rossum53807da2007-04-10 19:01:47 +0000836 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
Guido van Rossumb0428152007-04-08 17:44:42 +0000837 {0},
838};
839
Guido van Rossuma9e20242007-03-08 00:43:48 +0000840PyTypeObject PyFileIO_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000841 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +0000842 "_FileIO",
Guido van Rossuma9e20242007-03-08 00:43:48 +0000843 sizeof(PyFileIOObject),
844 0,
845 (destructor)fileio_dealloc, /* tp_dealloc */
846 0, /* tp_print */
847 0, /* tp_getattr */
848 0, /* tp_setattr */
849 0, /* tp_compare */
850 (reprfunc)fileio_repr, /* tp_repr */
851 0, /* tp_as_number */
852 0, /* tp_as_sequence */
853 0, /* tp_as_mapping */
854 0, /* tp_hash */
855 0, /* tp_call */
856 0, /* tp_str */
857 PyObject_GenericGetAttr, /* tp_getattro */
858 0, /* tp_setattro */
859 0, /* tp_as_buffer */
860 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
861 fileio_doc, /* tp_doc */
862 0, /* tp_traverse */
863 0, /* tp_clear */
864 0, /* tp_richcompare */
865 offsetof(PyFileIOObject, weakreflist), /* tp_weaklistoffset */
866 0, /* tp_iter */
867 0, /* tp_iternext */
868 fileio_methods, /* tp_methods */
869 0, /* tp_members */
Guido van Rossumb0428152007-04-08 17:44:42 +0000870 fileio_getsetlist, /* tp_getset */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000871 0, /* tp_base */
872 0, /* tp_dict */
873 0, /* tp_descr_get */
874 0, /* tp_descr_set */
875 0, /* tp_dictoffset */
876 fileio_init, /* tp_init */
877 PyType_GenericAlloc, /* tp_alloc */
878 fileio_new, /* tp_new */
879 PyObject_Del, /* tp_free */
880};
881
882static PyMethodDef module_methods[] = {
883 {NULL, NULL}
884};
885
Martin v. Löwis1a214512008-06-11 05:26:20 +0000886static struct PyModuleDef fileiomodule = {
887 PyModuleDef_HEAD_INIT,
888 "_fileio",
889 "Fast implementation of io.FileIO.",
890 -1,
891 module_methods,
892 NULL,
893 NULL,
894 NULL,
895 NULL
896};
897
Guido van Rossuma9e20242007-03-08 00:43:48 +0000898PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000899PyInit__fileio(void)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000900{
901 PyObject *m; /* a module object */
902
Martin v. Löwis1a214512008-06-11 05:26:20 +0000903 m = PyModule_Create(&fileiomodule);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000904 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000905 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000906 if (PyType_Ready(&PyFileIO_Type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000907 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000908 Py_INCREF(&PyFileIO_Type);
909 PyModule_AddObject(m, "_FileIO", (PyObject *) &PyFileIO_Type);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000910 return m;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000911}