blob: f332ee4633372500b530be445cce4070937894f7 [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 */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00009#include "_iomodule.h"
Guido van Rossuma9e20242007-03-08 00:43:48 +000010
11/*
12 * Known likely problems:
13 *
14 * - Files larger then 2**32-1
15 * - Files with unicode filenames
16 * - Passing numbers greater than 2**32-1 when an integer is expected
17 * - Making it work on Windows and other oddball platforms
18 *
19 * To Do:
20 *
21 * - autoconfify header file inclusion
Guido van Rossuma9e20242007-03-08 00:43:48 +000022 */
23
24#ifdef MS_WINDOWS
25/* can simulate truncate with Win32 API functions; see file_truncate */
Thomas Hellerfdeee3a2007-07-12 11:21:36 +000026#define HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +000027#define WIN32_LEAN_AND_MEAN
28#include <windows.h>
29#endif
30
Christian Heimesa872de52008-12-05 08:26:55 +000031#if BUFSIZ < (8*1024)
32#define SMALLCHUNK (8*1024)
33#elif (BUFSIZ >= (2 << 25))
34#error "unreasonable BUFSIZ > 64MB defined"
35#else
36#define SMALLCHUNK BUFSIZ
37#endif
38
39#if SIZEOF_INT < 4
40#define BIGCHUNK (512 * 32)
41#else
42#define BIGCHUNK (512 * 1024)
43#endif
44
Guido van Rossuma9e20242007-03-08 00:43:48 +000045typedef struct {
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +000046 PyObject_HEAD
47 int fd;
48 unsigned int readable : 1;
49 unsigned int writable : 1;
50 signed int seekable : 2; /* -1 means unknown */
51 unsigned int closefd : 1;
52 PyObject *weakreflist;
53 PyObject *dict;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000054} fileio;
Guido van Rossuma9e20242007-03-08 00:43:48 +000055
Collin Winteraf334382007-03-08 21:46:15 +000056PyTypeObject PyFileIO_Type;
57
Guido van Rossuma9e20242007-03-08 00:43:48 +000058#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
59
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000060int
61_PyFileIO_closed(PyObject *self)
62{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +000063 return ((fileio *)self)->fd < 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000064}
Antoine Pitrou08838b62009-01-21 00:55:13 +000065
Antoine Pitrou7fb111b2009-03-04 11:14:01 +000066static PyObject *
67portable_lseek(int fd, PyObject *posobj, int whence);
68
Antoine Pitroua28fcfd2009-03-13 23:42:55 +000069static PyObject *portable_lseek(int fd, PyObject *posobj, int whence);
70
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000071/* Returns 0 on success, -1 with exception set on failure. */
Neal Norwitz88b44da2007-08-12 17:23:54 +000072static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000073internal_close(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +000074{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +000075 int err = 0;
76 int save_errno = 0;
77 if (self->fd >= 0) {
78 int fd = self->fd;
79 self->fd = -1;
80 /* fd is accessible and someone else may have closed it */
81 if (_PyVerify_fd(fd)) {
82 Py_BEGIN_ALLOW_THREADS
83 err = close(fd);
84 if (err < 0)
85 save_errno = errno;
86 Py_END_ALLOW_THREADS
87 } else {
88 save_errno = errno;
89 err = -1;
90 }
91 }
92 if (err < 0) {
93 errno = save_errno;
94 PyErr_SetFromErrno(PyExc_IOError);
95 return -1;
96 }
97 return 0;
Neal Norwitz88b44da2007-08-12 17:23:54 +000098}
99
100static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000101fileio_close(fileio *self)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000102{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000103 if (!self->closefd) {
104 self->fd = -1;
105 Py_RETURN_NONE;
106 }
107 errno = internal_close(self);
108 if (errno < 0)
109 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000110
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000111 return PyObject_CallMethod((PyObject*)&PyRawIOBase_Type,
112 "close", "O", self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000113}
114
115static PyObject *
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000116fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000117{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000118 fileio *self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000119
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000120 assert(type != NULL && type->tp_alloc != NULL);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000121
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000122 self = (fileio *) type->tp_alloc(type, 0);
123 if (self != NULL) {
124 self->fd = -1;
125 self->readable = 0;
126 self->writable = 0;
127 self->seekable = -1;
128 self->closefd = 1;
129 self->weakreflist = NULL;
130 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000131
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000132 return (PyObject *) self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000133}
134
135/* On Unix, open will succeed for directories.
136 In Python, there should be no file objects referring to
137 directories, so we need a check. */
138
139static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000140dircheck(fileio* self, const char *name)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000141{
142#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000143 struct stat buf;
144 if (self->fd < 0)
145 return 0;
146 if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
147 char *msg = strerror(EISDIR);
148 PyObject *exc;
149 if (internal_close(self))
150 return -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000151
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000152 exc = PyObject_CallFunction(PyExc_IOError, "(iss)",
153 EISDIR, msg, name);
154 PyErr_SetObject(PyExc_IOError, exc);
155 Py_XDECREF(exc);
156 return -1;
157 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000158#endif
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000159 return 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000160}
161
Benjamin Peterson806d4022009-01-19 15:11:51 +0000162static int
163check_fd(int fd)
164{
165#if defined(HAVE_FSTAT)
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000166 struct stat buf;
167 if (!_PyVerify_fd(fd) || (fstat(fd, &buf) < 0 && errno == EBADF)) {
168 PyObject *exc;
169 char *msg = strerror(EBADF);
170 exc = PyObject_CallFunction(PyExc_OSError, "(is)",
171 EBADF, msg);
172 PyErr_SetObject(PyExc_OSError, exc);
173 Py_XDECREF(exc);
174 return -1;
175 }
Benjamin Peterson806d4022009-01-19 15:11:51 +0000176#endif
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000177 return 0;
Benjamin Peterson806d4022009-01-19 15:11:51 +0000178}
179
Guido van Rossuma9e20242007-03-08 00:43:48 +0000180
181static int
182fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
183{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000184 fileio *self = (fileio *) oself;
185 static char *kwlist[] = {"file", "mode", "closefd", NULL};
186 const char *name = NULL;
187 PyObject *nameobj, *stringobj = NULL;
188 char *mode = "r";
189 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000190#ifdef MS_WINDOWS
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000191 Py_UNICODE *widename = NULL;
Thomas Helleraf2be262007-07-12 11:03:13 +0000192#endif
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000193 int ret = 0;
194 int rwa = 0, plus = 0, append = 0;
195 int flags = 0;
196 int fd = -1;
197 int closefd = 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000198
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000199 assert(PyFileIO_Check(oself));
200 if (self->fd >= 0) {
201 /* Have to close the existing file first. */
202 if (internal_close(self) < 0)
203 return -1;
204 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000205
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000206 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:fileio",
207 kwlist, &nameobj, &mode, &closefd))
208 return -1;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000209
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000210 if (PyFloat_Check(nameobj)) {
211 PyErr_SetString(PyExc_TypeError,
212 "integer argument expected, got float");
213 return -1;
214 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000215
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000216 fd = PyLong_AsLong(nameobj);
217 if (fd < 0) {
218 if (!PyErr_Occurred()) {
219 PyErr_SetString(PyExc_ValueError,
220 "Negative filedescriptor");
221 return -1;
222 }
223 PyErr_Clear();
224 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000225
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000226#ifdef MS_WINDOWS
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000227 if (GetVersion() < 0x80000000) {
228 /* On NT, so wide API available */
229 if (PyUnicode_Check(nameobj))
230 widename = PyUnicode_AS_UNICODE(nameobj);
231 }
232 if (widename == NULL)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000233#endif
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000234 if (fd < 0)
235 {
236 if (PyBytes_Check(nameobj) || PyByteArray_Check(nameobj)) {
237 Py_ssize_t namelen;
238 if (PyObject_AsCharBuffer(nameobj, &name, &namelen) < 0)
239 return -1;
240 }
241 else {
242 PyObject *u = PyUnicode_FromObject(nameobj);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000243
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000244 if (u == NULL)
245 return -1;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000246
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000247 stringobj = PyUnicode_AsEncodedString(
248 u, Py_FileSystemDefaultEncoding, "surrogateescape");
249 Py_DECREF(u);
250 if (stringobj == NULL)
251 return -1;
252 if (!PyBytes_Check(stringobj)) {
253 PyErr_SetString(PyExc_TypeError,
254 "encoder failed to return bytes");
255 goto error;
256 }
257 name = PyBytes_AS_STRING(stringobj);
258 }
259 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000260
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000261 s = mode;
262 while (*s) {
263 switch (*s++) {
264 case 'r':
265 if (rwa) {
266 bad_mode:
267 PyErr_SetString(PyExc_ValueError,
268 "Must have exactly one of read/write/append mode");
269 goto error;
270 }
271 rwa = 1;
272 self->readable = 1;
273 break;
274 case 'w':
275 if (rwa)
276 goto bad_mode;
277 rwa = 1;
278 self->writable = 1;
279 flags |= O_CREAT | O_TRUNC;
280 break;
281 case 'a':
282 if (rwa)
283 goto bad_mode;
284 rwa = 1;
285 self->writable = 1;
286 flags |= O_CREAT;
287 append = 1;
288 break;
289 case 'b':
290 break;
291 case '+':
292 if (plus)
293 goto bad_mode;
294 self->readable = self->writable = 1;
295 plus = 1;
296 break;
297 default:
298 PyErr_Format(PyExc_ValueError,
299 "invalid mode: %.200s", mode);
300 goto error;
301 }
302 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000303
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000304 if (!rwa)
305 goto bad_mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000306
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000307 if (self->readable && self->writable)
308 flags |= O_RDWR;
309 else if (self->readable)
310 flags |= O_RDONLY;
311 else
312 flags |= O_WRONLY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000313
314#ifdef O_BINARY
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000315 flags |= O_BINARY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000316#endif
317
Walter Dörwald0e411482007-06-06 16:55:38 +0000318#ifdef O_APPEND
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000319 if (append)
320 flags |= O_APPEND;
Walter Dörwald0e411482007-06-06 16:55:38 +0000321#endif
322
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000323 if (fd >= 0) {
324 if (check_fd(fd))
325 goto error;
326 self->fd = fd;
327 self->closefd = closefd;
328 }
329 else {
330 self->closefd = 1;
331 if (!closefd) {
332 PyErr_SetString(PyExc_ValueError,
333 "Cannot use closefd=False with file name");
334 goto error;
335 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000336
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000337 Py_BEGIN_ALLOW_THREADS
338 errno = 0;
Thomas Helleraf2be262007-07-12 11:03:13 +0000339#ifdef MS_WINDOWS
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000340 if (widename != NULL)
341 self->fd = _wopen(widename, flags, 0666);
342 else
Thomas Helleraf2be262007-07-12 11:03:13 +0000343#endif
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000344 self->fd = open(name, flags, 0666);
345 Py_END_ALLOW_THREADS
346 if (self->fd < 0) {
Christian Heimes0b489542007-10-31 19:20:48 +0000347#ifdef MS_WINDOWS
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000348 if (widename != NULL)
349 PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename);
350 else
Christian Heimes0b489542007-10-31 19:20:48 +0000351#endif
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000352 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
353 goto error;
354 }
355 if(dircheck(self, name) < 0)
356 goto error;
357 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000358
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000359 if (PyObject_SetAttrString((PyObject *)self, "name", nameobj) < 0)
360 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000361
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000362 if (append) {
363 /* For consistent behaviour, we explicitly seek to the
364 end of file (otherwise, it might be done only on the
365 first write()). */
366 PyObject *pos = portable_lseek(self->fd, NULL, 2);
367 if (pos == NULL)
368 goto error;
369 Py_DECREF(pos);
370 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000371
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000372 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000373
374 error:
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000375 ret = -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000376
Guido van Rossuma9e20242007-03-08 00:43:48 +0000377 done:
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000378 Py_CLEAR(stringobj);
379 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000380}
381
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000382static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000383fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000384{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000385 Py_VISIT(self->dict);
386 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000387}
388
389static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000390fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000391{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000392 Py_CLEAR(self->dict);
393 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000394}
395
Guido van Rossuma9e20242007-03-08 00:43:48 +0000396static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000397fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000398{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000399 if (_PyIOBase_finalize((PyObject *) self) < 0)
400 return;
401 _PyObject_GC_UNTRACK(self);
402 if (self->weakreflist != NULL)
403 PyObject_ClearWeakRefs((PyObject *) self);
404 Py_CLEAR(self->dict);
405 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000406}
407
408static PyObject *
409err_closed(void)
410{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000411 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
412 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000413}
414
415static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000416err_mode(char *action)
417{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000418 PyErr_Format(PyExc_ValueError, "File not open for %s", action);
419 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000420}
421
422static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000423fileio_fileno(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000424{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000425 if (self->fd < 0)
426 return err_closed();
427 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000428}
429
430static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000431fileio_readable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000432{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000433 if (self->fd < 0)
434 return err_closed();
435 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000436}
437
438static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000439fileio_writable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000440{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000441 if (self->fd < 0)
442 return err_closed();
443 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000444}
445
446static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000447fileio_seekable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000448{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000449 if (self->fd < 0)
450 return err_closed();
451 if (self->seekable < 0) {
452 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
453 if (pos == NULL) {
454 PyErr_Clear();
455 self->seekable = 0;
456 } else {
457 Py_DECREF(pos);
458 self->seekable = 1;
459 }
460 }
461 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000462}
463
464static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000465fileio_readinto(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000466{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000467 Py_buffer pbuf;
468 Py_ssize_t n;
Guido van Rossum53807da2007-04-10 19:01:47 +0000469
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000470 if (self->fd < 0)
471 return err_closed();
472 if (!self->readable)
473 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000474
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000475 if (!PyArg_ParseTuple(args, "w*", &pbuf))
476 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000477
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000478 if (_PyVerify_fd(self->fd)) {
479 Py_BEGIN_ALLOW_THREADS
480 errno = 0;
481 n = read(self->fd, pbuf.buf, pbuf.len);
482 Py_END_ALLOW_THREADS
483 } else
484 n = -1;
485 PyBuffer_Release(&pbuf);
486 if (n < 0) {
487 if (errno == EAGAIN)
488 Py_RETURN_NONE;
489 PyErr_SetFromErrno(PyExc_IOError);
490 return NULL;
491 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000492
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000493 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000494}
495
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000496static size_t
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000497new_buffersize(fileio *self, size_t currentsize)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000498{
499#ifdef HAVE_FSTAT
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000500 off_t pos, end;
501 struct stat st;
502 if (fstat(self->fd, &st) == 0) {
503 end = st.st_size;
504 pos = lseek(self->fd, 0L, SEEK_CUR);
505 /* Files claiming a size smaller than SMALLCHUNK may
506 actually be streaming pseudo-files. In this case, we
507 apply the more aggressive algorithm below.
508 */
509 if (end >= SMALLCHUNK && end >= pos && pos >= 0) {
510 /* Add 1 so if the file were to grow we'd notice. */
511 return currentsize + end - pos + 1;
512 }
513 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000514#endif
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000515 if (currentsize > SMALLCHUNK) {
516 /* Keep doubling until we reach BIGCHUNK;
517 then keep adding BIGCHUNK. */
518 if (currentsize <= BIGCHUNK)
519 return currentsize + currentsize;
520 else
521 return currentsize + BIGCHUNK;
522 }
523 return currentsize + SMALLCHUNK;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000524}
525
Guido van Rossum7165cb12007-07-10 06:54:34 +0000526static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000527fileio_readall(fileio *self)
Guido van Rossum7165cb12007-07-10 06:54:34 +0000528{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000529 PyObject *result;
530 Py_ssize_t total = 0;
531 int n;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000532
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000533 if (!_PyVerify_fd(self->fd))
534 return PyErr_SetFromErrno(PyExc_IOError);
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000535
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000536 result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK);
537 if (result == NULL)
538 return NULL;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000539
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000540 while (1) {
541 size_t newsize = new_buffersize(self, total);
542 if (newsize > PY_SSIZE_T_MAX || newsize <= 0) {
543 PyErr_SetString(PyExc_OverflowError,
544 "unbounded read returned more bytes "
545 "than a Python string can hold ");
546 Py_DECREF(result);
547 return NULL;
548 }
Christian Heimesa872de52008-12-05 08:26:55 +0000549
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000550 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)newsize) {
551 if (_PyBytes_Resize(&result, newsize) < 0) {
552 if (total == 0) {
553 Py_DECREF(result);
554 return NULL;
555 }
556 PyErr_Clear();
557 break;
558 }
559 }
560 Py_BEGIN_ALLOW_THREADS
561 errno = 0;
562 n = read(self->fd,
563 PyBytes_AS_STRING(result) + total,
564 newsize - total);
565 Py_END_ALLOW_THREADS
566 if (n == 0)
567 break;
568 if (n < 0) {
569 if (total > 0)
570 break;
571 if (errno == EAGAIN) {
572 Py_DECREF(result);
573 Py_RETURN_NONE;
574 }
575 Py_DECREF(result);
576 PyErr_SetFromErrno(PyExc_IOError);
577 return NULL;
578 }
579 total += n;
580 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000581
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000582 if (PyBytes_GET_SIZE(result) > total) {
583 if (_PyBytes_Resize(&result, total) < 0) {
584 /* This should never happen, but just in case */
585 Py_DECREF(result);
586 return NULL;
587 }
588 }
589 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000590}
591
Guido van Rossuma9e20242007-03-08 00:43:48 +0000592static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000593fileio_read(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000594{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000595 char *ptr;
596 Py_ssize_t n;
597 Py_ssize_t size = -1;
598 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000599
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000600 if (self->fd < 0)
601 return err_closed();
602 if (!self->readable)
603 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000604
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000605 if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size))
606 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000607
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000608 if (size < 0) {
609 return fileio_readall(self);
610 }
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000611
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000612 bytes = PyBytes_FromStringAndSize(NULL, size);
613 if (bytes == NULL)
614 return NULL;
615 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000616
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000617 if (_PyVerify_fd(self->fd)) {
618 Py_BEGIN_ALLOW_THREADS
619 errno = 0;
620 n = read(self->fd, ptr, size);
621 Py_END_ALLOW_THREADS
622 } else
623 n = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000624
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000625 if (n < 0) {
626 Py_DECREF(bytes);
627 if (errno == EAGAIN)
628 Py_RETURN_NONE;
629 PyErr_SetFromErrno(PyExc_IOError);
630 return NULL;
631 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000632
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000633 if (n != size) {
634 if (_PyBytes_Resize(&bytes, n) < 0) {
635 Py_DECREF(bytes);
636 return NULL;
637 }
638 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000639
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000640 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000641}
642
643static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000644fileio_write(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000645{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000646 Py_buffer pbuf;
647 Py_ssize_t n;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000648
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000649 if (self->fd < 0)
650 return err_closed();
651 if (!self->writable)
652 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000653
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000654 if (!PyArg_ParseTuple(args, "y*", &pbuf))
655 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000656
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000657 if (_PyVerify_fd(self->fd)) {
658 Py_BEGIN_ALLOW_THREADS
659 errno = 0;
660 n = write(self->fd, pbuf.buf, pbuf.len);
661 Py_END_ALLOW_THREADS
662 } else
663 n = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000664
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000665 PyBuffer_Release(&pbuf);
Martin v. Löwis423be952008-08-13 15:53:07 +0000666
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000667 if (n < 0) {
668 if (errno == EAGAIN)
669 Py_RETURN_NONE;
670 PyErr_SetFromErrno(PyExc_IOError);
671 return NULL;
672 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000673
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000674 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000675}
676
Guido van Rossum53807da2007-04-10 19:01:47 +0000677/* XXX Windows support below is likely incomplete */
678
Guido van Rossum53807da2007-04-10 19:01:47 +0000679/* Cribbed from posix_lseek() */
680static PyObject *
681portable_lseek(int fd, PyObject *posobj, int whence)
682{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000683 Py_off_t pos, res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000684
685#ifdef SEEK_SET
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000686 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
687 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000688#if SEEK_SET != 0
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000689 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000690#endif
691#if SEEK_CUR != 1
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000692 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000693#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000694#if SEEK_END != 2
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000695 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000696#endif
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000697 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000698#endif /* SEEK_SET */
699
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000700 if (posobj == NULL)
701 pos = 0;
702 else {
703 if(PyFloat_Check(posobj)) {
704 PyErr_SetString(PyExc_TypeError, "an integer is required");
705 return NULL;
706 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000707#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000708 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000709#else
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000710 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000711#endif
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000712 if (PyErr_Occurred())
713 return NULL;
714 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000715
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000716 if (_PyVerify_fd(fd)) {
717 Py_BEGIN_ALLOW_THREADS
Guido van Rossum53807da2007-04-10 19:01:47 +0000718#if defined(MS_WIN64) || defined(MS_WINDOWS)
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000719 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000720#else
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000721 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000722#endif
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000723 Py_END_ALLOW_THREADS
724 } else
725 res = -1;
726 if (res < 0)
727 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000728
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000729#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000730 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000731#else
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000732 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000733#endif
734}
735
Guido van Rossuma9e20242007-03-08 00:43:48 +0000736static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000737fileio_seek(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000738{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000739 PyObject *posobj;
740 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000741
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000742 if (self->fd < 0)
743 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000744
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000745 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
746 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000747
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000748 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000749}
750
751static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000752fileio_tell(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000753{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000754 if (self->fd < 0)
755 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000756
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000757 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000758}
759
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000760#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000761static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000762fileio_truncate(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000763{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000764 PyObject *posobj = NULL; /* the new size wanted by the user */
Antoine Pitrou66f9fea2010-01-31 23:20:26 +0000765#ifndef MS_WINDOWS
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000766 Py_off_t pos;
Antoine Pitrou66f9fea2010-01-31 23:20:26 +0000767#endif
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000768 int ret;
769 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000770
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000771 fd = self->fd;
772 if (fd < 0)
773 return err_closed();
774 if (!self->writable)
775 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000776
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000777 if (!PyArg_ParseTuple(args, "|O", &posobj))
778 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000779
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000780 if (posobj == Py_None || posobj == NULL) {
781 /* Get the current position. */
782 posobj = portable_lseek(fd, NULL, 1);
783 if (posobj == NULL)
784 return NULL;
785 }
786 else {
787 Py_INCREF(posobj);
788 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000789
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000790#ifdef MS_WINDOWS
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000791 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
792 so don't even try using it. */
793 {
794 PyObject *oldposobj, *tempposobj;
795 HANDLE hFile;
Antoine Pitrou66f9fea2010-01-31 23:20:26 +0000796
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000797 /* we save the file pointer position */
798 oldposobj = portable_lseek(fd, NULL, 1);
799 if (oldposobj == NULL) {
800 Py_DECREF(posobj);
801 return NULL;
802 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000803
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000804 /* we then move to the truncation position */
805 tempposobj = portable_lseek(fd, posobj, 0);
806 if (tempposobj == NULL) {
807 Py_DECREF(oldposobj);
808 Py_DECREF(posobj);
809 return NULL;
810 }
811 Py_DECREF(tempposobj);
Antoine Pitrou66f9fea2010-01-31 23:20:26 +0000812
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000813 /* Truncate. Note that this may grow the file! */
814 Py_BEGIN_ALLOW_THREADS
815 errno = 0;
816 hFile = (HANDLE)_get_osfhandle(fd);
817 ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
818 if (ret == 0) {
819 ret = SetEndOfFile(hFile) == 0;
820 if (ret)
821 errno = EACCES;
822 }
823 Py_END_ALLOW_THREADS
824
825 /* we restore the file pointer position in any case */
826 tempposobj = portable_lseek(fd, oldposobj, 0);
827 Py_DECREF(oldposobj);
828 if (tempposobj == NULL) {
829 Py_DECREF(posobj);
830 return NULL;
831 }
832 Py_DECREF(tempposobj);
833 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000834#else
Antoine Pitrou66f9fea2010-01-31 23:20:26 +0000835
836#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000837 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou66f9fea2010-01-31 23:20:26 +0000838#else
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000839 pos = PyLong_AsLong(posobj);
Antoine Pitrou66f9fea2010-01-31 23:20:26 +0000840#endif
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000841 if (PyErr_Occurred()){
842 Py_DECREF(posobj);
843 return NULL;
844 }
Antoine Pitrou66f9fea2010-01-31 23:20:26 +0000845
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000846 Py_BEGIN_ALLOW_THREADS
847 errno = 0;
848 ret = ftruncate(fd, pos);
849 Py_END_ALLOW_THREADS
Antoine Pitrou66f9fea2010-01-31 23:20:26 +0000850
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000851#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000852
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000853 if (ret != 0) {
854 Py_DECREF(posobj);
855 PyErr_SetFromErrno(PyExc_IOError);
856 return NULL;
857 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000858
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000859 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000860}
Antoine Pitrou66f9fea2010-01-31 23:20:26 +0000861#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +0000862
863static char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000864mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +0000865{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000866 if (self->readable) {
867 if (self->writable)
868 return "rb+";
869 else
870 return "rb";
871 }
872 else
873 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000874}
Guido van Rossuma9e20242007-03-08 00:43:48 +0000875
876static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000877fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000878{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000879 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000880
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000881 if (self->fd < 0)
882 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +0000883
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000884 nameobj = PyObject_GetAttrString((PyObject *) self, "name");
885 if (nameobj == NULL) {
886 if (PyErr_ExceptionMatches(PyExc_AttributeError))
887 PyErr_Clear();
888 else
889 return NULL;
890 res = PyUnicode_FromFormat("<_io.FileIO fd=%d mode='%s'>",
891 self->fd, mode_string(self));
892 }
893 else {
894 res = PyUnicode_FromFormat("<_io.FileIO name=%R mode='%s'>",
895 nameobj, mode_string(self));
896 Py_DECREF(nameobj);
897 }
898 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000899}
900
901static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000902fileio_isatty(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000903{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000904 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000905
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000906 if (self->fd < 0)
907 return err_closed();
908 Py_BEGIN_ALLOW_THREADS
909 res = isatty(self->fd);
910 Py_END_ALLOW_THREADS
911 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000912}
913
Guido van Rossuma9e20242007-03-08 00:43:48 +0000914
915PyDoc_STRVAR(fileio_doc,
916"file(name: str[, mode: str]) -> file IO object\n"
917"\n"
918"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000919"writing or appending. The file will be created if it doesn't exist\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +0000920"when opened for writing or appending; it will be truncated when\n"
921"opened for writing. Add a '+' to the mode to allow simultaneous\n"
922"reading and writing.");
923
924PyDoc_STRVAR(read_doc,
925"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
926"\n"
927"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +0000928"In non-blocking mode, returns None if no data is available.\n"
929"On end-of-file, returns ''.");
930
931PyDoc_STRVAR(readall_doc,
932"readall() -> bytes. read all data from the file, returned as bytes.\n"
933"\n"
934"In non-blocking mode, returns as much as is immediately available,\n"
935"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000936
937PyDoc_STRVAR(write_doc,
938"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
939"\n"
940"Only makes one system call, so not all of the data may be written.\n"
941"The number of bytes actually written is returned.");
942
943PyDoc_STRVAR(fileno_doc,
944"fileno() -> int. \"file descriptor\".\n"
945"\n"
946"This is needed for lower-level file interfaces, such the fcntl module.");
947
948PyDoc_STRVAR(seek_doc,
949"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
950"\n"
951"Argument offset is a byte count. Optional argument whence defaults to\n"
952"0 (offset from start of file, offset should be >= 0); other values are 1\n"
953"(move relative to current position, positive or negative), and 2 (move\n"
954"relative to end of file, usually negative, although many platforms allow\n"
955"seeking beyond the end of a file)."
956"\n"
957"Note that not all file objects are seekable.");
958
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000959#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000960PyDoc_STRVAR(truncate_doc,
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000961"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +0000962"\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000963"Size defaults to the current file position, as returned by tell()."
964"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000965#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000966
967PyDoc_STRVAR(tell_doc,
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000968"tell() -> int. Current file position");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000969
970PyDoc_STRVAR(readinto_doc,
Benjamin Peterson9a8082f2009-03-05 00:55:56 +0000971"readinto() -> Same as RawIOBase.readinto().");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000972
973PyDoc_STRVAR(close_doc,
974"close() -> None. Close the file.\n"
975"\n"
976"A closed file cannot be used for further I/O operations. close() may be\n"
977"called more than once without error. Changes the fileno to -1.");
978
979PyDoc_STRVAR(isatty_doc,
980"isatty() -> bool. True if the file is connected to a tty device.");
981
Guido van Rossuma9e20242007-03-08 00:43:48 +0000982PyDoc_STRVAR(seekable_doc,
983"seekable() -> bool. True if file supports random-access.");
984
985PyDoc_STRVAR(readable_doc,
986"readable() -> bool. True if file was opened in a read mode.");
987
988PyDoc_STRVAR(writable_doc,
989"writable() -> bool. True if file was opened in a write mode.");
990
991static PyMethodDef fileio_methods[] = {
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000992 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
993 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
994 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
995 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
996 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
997 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000998#ifdef HAVE_FTRUNCATE
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +0000999 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001000#endif
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +00001001 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
1002 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
1003 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
1004 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
1005 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
1006 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
1007 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001008};
1009
Guido van Rossum53807da2007-04-10 19:01:47 +00001010/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1011
Guido van Rossumb0428152007-04-08 17:44:42 +00001012static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001013get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001014{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +00001015 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001016}
1017
1018static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001019get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001020{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +00001021 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001022}
1023
1024static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001025get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001026{
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +00001027 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001028}
1029
1030static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +00001031 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1032 {"closefd", (getter)get_closefd, NULL,
1033 "True if the file descriptor will be closed"},
1034 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1035 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001036};
1037
Guido van Rossuma9e20242007-03-08 00:43:48 +00001038PyTypeObject PyFileIO_Type = {
Antoine Pitrouf7ec1fd2010-05-05 16:34:37 +00001039 PyVarObject_HEAD_INIT(NULL, 0)
1040 "_io.FileIO",
1041 sizeof(fileio),
1042 0,
1043 (destructor)fileio_dealloc, /* tp_dealloc */
1044 0, /* tp_print */
1045 0, /* tp_getattr */
1046 0, /* tp_setattr */
1047 0, /* tp_reserved */
1048 (reprfunc)fileio_repr, /* tp_repr */
1049 0, /* tp_as_number */
1050 0, /* tp_as_sequence */
1051 0, /* tp_as_mapping */
1052 0, /* tp_hash */
1053 0, /* tp_call */
1054 0, /* tp_str */
1055 PyObject_GenericGetAttr, /* tp_getattro */
1056 0, /* tp_setattro */
1057 0, /* tp_as_buffer */
1058 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
1059 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1060 fileio_doc, /* tp_doc */
1061 (traverseproc)fileio_traverse, /* tp_traverse */
1062 (inquiry)fileio_clear, /* tp_clear */
1063 0, /* tp_richcompare */
1064 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1065 0, /* tp_iter */
1066 0, /* tp_iternext */
1067 fileio_methods, /* tp_methods */
1068 0, /* tp_members */
1069 fileio_getsetlist, /* tp_getset */
1070 0, /* tp_base */
1071 0, /* tp_dict */
1072 0, /* tp_descr_get */
1073 0, /* tp_descr_set */
1074 offsetof(fileio, dict), /* tp_dictoffset */
1075 fileio_init, /* tp_init */
1076 PyType_GenericAlloc, /* tp_alloc */
1077 fileio_new, /* tp_new */
1078 PyObject_GC_Del, /* tp_free */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001079};