blob: 74009e3657dd0aeab9b91fdef78ae87359d99271 [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"
Benjamin Peterson2614cda2010-03-21 22:36:19 +00005#ifdef HAVE_SYS_TYPES_H
Guido van Rossuma9e20242007-03-08 00:43:48 +00006#include <sys/types.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +00007#endif
8#ifdef HAVE_SYS_STAT_H
Guido van Rossuma9e20242007-03-08 00:43:48 +00009#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000010#endif
11#ifdef HAVE_FCNTL_H
Guido van Rossuma9e20242007-03-08 00:43:48 +000012#include <fcntl.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000013#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +000014#include <stddef.h> /* For offsetof */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000015#include "_iomodule.h"
Guido van Rossuma9e20242007-03-08 00:43:48 +000016
17/*
18 * Known likely problems:
19 *
20 * - Files larger then 2**32-1
21 * - Files with unicode filenames
22 * - Passing numbers greater than 2**32-1 when an integer is expected
23 * - Making it work on Windows and other oddball platforms
24 *
25 * To Do:
26 *
27 * - autoconfify header file inclusion
Guido van Rossuma9e20242007-03-08 00:43:48 +000028 */
29
30#ifdef MS_WINDOWS
31/* can simulate truncate with Win32 API functions; see file_truncate */
Thomas Hellerfdeee3a2007-07-12 11:21:36 +000032#define HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +000033#define WIN32_LEAN_AND_MEAN
34#include <windows.h>
35#endif
36
Christian Heimesa872de52008-12-05 08:26:55 +000037#if BUFSIZ < (8*1024)
38#define SMALLCHUNK (8*1024)
39#elif (BUFSIZ >= (2 << 25))
40#error "unreasonable BUFSIZ > 64MB defined"
41#else
42#define SMALLCHUNK BUFSIZ
43#endif
44
45#if SIZEOF_INT < 4
46#define BIGCHUNK (512 * 32)
47#else
48#define BIGCHUNK (512 * 1024)
49#endif
50
Guido van Rossuma9e20242007-03-08 00:43:48 +000051typedef struct {
Antoine Pitrouae4b4722010-05-05 16:31:07 +000052 PyObject_HEAD
53 int fd;
54 unsigned int readable : 1;
55 unsigned int writable : 1;
56 signed int seekable : 2; /* -1 means unknown */
57 unsigned int closefd : 1;
58 PyObject *weakreflist;
59 PyObject *dict;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000060} fileio;
Guido van Rossuma9e20242007-03-08 00:43:48 +000061
Collin Winteraf334382007-03-08 21:46:15 +000062PyTypeObject PyFileIO_Type;
63
Guido van Rossuma9e20242007-03-08 00:43:48 +000064#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
65
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000066int
67_PyFileIO_closed(PyObject *self)
68{
Antoine Pitrouae4b4722010-05-05 16:31:07 +000069 return ((fileio *)self)->fd < 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000070}
Antoine Pitrou08838b62009-01-21 00:55:13 +000071
Antoine Pitrou7fb111b2009-03-04 11:14:01 +000072static PyObject *
73portable_lseek(int fd, PyObject *posobj, int whence);
74
Antoine Pitroua28fcfd2009-03-13 23:42:55 +000075static PyObject *portable_lseek(int fd, PyObject *posobj, int whence);
76
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +000077/* Returns 0 on success, -1 with exception set on failure. */
Neal Norwitz88b44da2007-08-12 17:23:54 +000078static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000079internal_close(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +000080{
Antoine Pitrouae4b4722010-05-05 16:31:07 +000081 int err = 0;
82 int save_errno = 0;
83 if (self->fd >= 0) {
84 int fd = self->fd;
85 self->fd = -1;
86 /* fd is accessible and someone else may have closed it */
87 if (_PyVerify_fd(fd)) {
88 Py_BEGIN_ALLOW_THREADS
89 err = close(fd);
90 if (err < 0)
91 save_errno = errno;
92 Py_END_ALLOW_THREADS
93 } else {
94 save_errno = errno;
95 err = -1;
96 }
97 }
98 if (err < 0) {
99 errno = save_errno;
100 PyErr_SetFromErrno(PyExc_IOError);
101 return -1;
102 }
103 return 0;
Neal Norwitz88b44da2007-08-12 17:23:54 +0000104}
105
106static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000107fileio_close(fileio *self)
Neal Norwitz88b44da2007-08-12 17:23:54 +0000108{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000109 if (!self->closefd) {
110 self->fd = -1;
111 Py_RETURN_NONE;
112 }
113 errno = internal_close(self);
114 if (errno < 0)
115 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000116
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000117 return PyObject_CallMethod((PyObject*)&PyRawIOBase_Type,
118 "close", "O", self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000119}
120
121static PyObject *
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000122fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000123{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000124 fileio *self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000125
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000126 assert(type != NULL && type->tp_alloc != NULL);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000127
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000128 self = (fileio *) type->tp_alloc(type, 0);
129 if (self != NULL) {
130 self->fd = -1;
131 self->readable = 0;
132 self->writable = 0;
133 self->seekable = -1;
134 self->closefd = 1;
135 self->weakreflist = NULL;
136 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000137
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000138 return (PyObject *) self;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000139}
140
141/* On Unix, open will succeed for directories.
142 In Python, there should be no file objects referring to
143 directories, so we need a check. */
144
145static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000146dircheck(fileio* self, const char *name)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000147{
148#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000149 struct stat buf;
150 if (self->fd < 0)
151 return 0;
152 if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
153 char *msg = strerror(EISDIR);
154 PyObject *exc;
155 if (internal_close(self))
156 return -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000157
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000158 exc = PyObject_CallFunction(PyExc_IOError, "(iss)",
159 EISDIR, msg, name);
160 PyErr_SetObject(PyExc_IOError, exc);
161 Py_XDECREF(exc);
162 return -1;
163 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000164#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000165 return 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000166}
167
Benjamin Peterson806d4022009-01-19 15:11:51 +0000168static int
169check_fd(int fd)
170{
171#if defined(HAVE_FSTAT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000172 struct stat buf;
173 if (!_PyVerify_fd(fd) || (fstat(fd, &buf) < 0 && errno == EBADF)) {
174 PyObject *exc;
175 char *msg = strerror(EBADF);
176 exc = PyObject_CallFunction(PyExc_OSError, "(is)",
177 EBADF, msg);
178 PyErr_SetObject(PyExc_OSError, exc);
179 Py_XDECREF(exc);
180 return -1;
181 }
Benjamin Peterson806d4022009-01-19 15:11:51 +0000182#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000183 return 0;
Benjamin Peterson806d4022009-01-19 15:11:51 +0000184}
185
Guido van Rossuma9e20242007-03-08 00:43:48 +0000186
187static int
188fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
189{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000190 fileio *self = (fileio *) oself;
191 static char *kwlist[] = {"file", "mode", "closefd", NULL};
192 const char *name = NULL;
193 PyObject *nameobj, *stringobj = NULL;
194 char *mode = "r";
195 char *s;
Thomas Helleraf2be262007-07-12 11:03:13 +0000196#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000197 Py_UNICODE *widename = NULL;
Thomas Helleraf2be262007-07-12 11:03:13 +0000198#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000199 int ret = 0;
200 int rwa = 0, plus = 0, append = 0;
201 int flags = 0;
202 int fd = -1;
203 int closefd = 1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000204
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000205 assert(PyFileIO_Check(oself));
206 if (self->fd >= 0) {
207 /* Have to close the existing file first. */
208 if (internal_close(self) < 0)
209 return -1;
210 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000211
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000212 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:fileio",
213 kwlist, &nameobj, &mode, &closefd))
214 return -1;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000215
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000216 if (PyFloat_Check(nameobj)) {
217 PyErr_SetString(PyExc_TypeError,
218 "integer argument expected, got float");
219 return -1;
220 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000221
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000222 fd = PyLong_AsLong(nameobj);
223 if (fd < 0) {
224 if (!PyErr_Occurred()) {
225 PyErr_SetString(PyExc_ValueError,
226 "Negative filedescriptor");
227 return -1;
228 }
229 PyErr_Clear();
230 }
Guido van Rossumb0428152007-04-08 17:44:42 +0000231
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000232#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000233 if (PyUnicode_Check(nameobj))
234 widename = PyUnicode_AS_UNICODE(nameobj);
235 if (widename == NULL)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000236#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000237 if (fd < 0)
238 {
239 if (PyBytes_Check(nameobj) || PyByteArray_Check(nameobj)) {
240 Py_ssize_t namelen;
241 if (PyObject_AsCharBuffer(nameobj, &name, &namelen) < 0)
242 return -1;
243 }
244 else {
245 PyObject *u = PyUnicode_FromObject(nameobj);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000246
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000247 if (u == NULL)
248 return -1;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000249
Victor Stinnerae6265f2010-05-15 16:27:27 +0000250 stringobj = PyUnicode_EncodeFSDefault(u);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000251 Py_DECREF(u);
252 if (stringobj == NULL)
253 return -1;
254 if (!PyBytes_Check(stringobj)) {
255 PyErr_SetString(PyExc_TypeError,
256 "encoder failed to return bytes");
257 goto error;
258 }
259 name = PyBytes_AS_STRING(stringobj);
260 }
261 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000262
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000263 s = mode;
264 while (*s) {
265 switch (*s++) {
266 case 'r':
267 if (rwa) {
268 bad_mode:
269 PyErr_SetString(PyExc_ValueError,
Georg Brandl28928ae2010-10-21 13:45:52 +0000270 "Must have exactly one of read/write/append "
271 "mode and at most one plus");
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000272 goto error;
273 }
274 rwa = 1;
275 self->readable = 1;
276 break;
277 case 'w':
278 if (rwa)
279 goto bad_mode;
280 rwa = 1;
281 self->writable = 1;
282 flags |= O_CREAT | O_TRUNC;
283 break;
284 case 'a':
285 if (rwa)
286 goto bad_mode;
287 rwa = 1;
288 self->writable = 1;
289 flags |= O_CREAT;
290 append = 1;
291 break;
292 case 'b':
293 break;
294 case '+':
295 if (plus)
296 goto bad_mode;
297 self->readable = self->writable = 1;
298 plus = 1;
299 break;
300 default:
301 PyErr_Format(PyExc_ValueError,
302 "invalid mode: %.200s", mode);
303 goto error;
304 }
305 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000306
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000307 if (!rwa)
308 goto bad_mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000309
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000310 if (self->readable && self->writable)
311 flags |= O_RDWR;
312 else if (self->readable)
313 flags |= O_RDONLY;
314 else
315 flags |= O_WRONLY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000316
317#ifdef O_BINARY
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000318 flags |= O_BINARY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000319#endif
320
Walter Dörwald0e411482007-06-06 16:55:38 +0000321#ifdef O_APPEND
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000322 if (append)
323 flags |= O_APPEND;
Walter Dörwald0e411482007-06-06 16:55:38 +0000324#endif
325
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000326 if (fd >= 0) {
327 if (check_fd(fd))
328 goto error;
329 self->fd = fd;
330 self->closefd = closefd;
331 }
332 else {
333 self->closefd = 1;
334 if (!closefd) {
335 PyErr_SetString(PyExc_ValueError,
336 "Cannot use closefd=False with file name");
337 goto error;
338 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000339
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000340 Py_BEGIN_ALLOW_THREADS
341 errno = 0;
Thomas Helleraf2be262007-07-12 11:03:13 +0000342#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000343 if (widename != NULL)
344 self->fd = _wopen(widename, flags, 0666);
345 else
Thomas Helleraf2be262007-07-12 11:03:13 +0000346#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000347 self->fd = open(name, flags, 0666);
348 Py_END_ALLOW_THREADS
349 if (self->fd < 0) {
Christian Heimes0b489542007-10-31 19:20:48 +0000350#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000351 if (widename != NULL)
352 PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename);
353 else
Christian Heimes0b489542007-10-31 19:20:48 +0000354#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000355 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
356 goto error;
357 }
358 if(dircheck(self, name) < 0)
359 goto error;
360 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000361
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000362 if (PyObject_SetAttrString((PyObject *)self, "name", nameobj) < 0)
363 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000364
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000365 if (append) {
366 /* For consistent behaviour, we explicitly seek to the
367 end of file (otherwise, it might be done only on the
368 first write()). */
369 PyObject *pos = portable_lseek(self->fd, NULL, 2);
370 if (pos == NULL)
371 goto error;
372 Py_DECREF(pos);
373 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000374
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000375 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000376
377 error:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000378 ret = -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000379
Guido van Rossuma9e20242007-03-08 00:43:48 +0000380 done:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000381 Py_CLEAR(stringobj);
382 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000383}
384
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000385static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000386fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000387{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000388 Py_VISIT(self->dict);
389 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000390}
391
392static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000393fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000394{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000395 Py_CLEAR(self->dict);
396 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000397}
398
Guido van Rossuma9e20242007-03-08 00:43:48 +0000399static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000400fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000401{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000402 if (_PyIOBase_finalize((PyObject *) self) < 0)
403 return;
404 _PyObject_GC_UNTRACK(self);
405 if (self->weakreflist != NULL)
406 PyObject_ClearWeakRefs((PyObject *) self);
407 Py_CLEAR(self->dict);
408 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000409}
410
411static PyObject *
412err_closed(void)
413{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000414 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
415 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000416}
417
418static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000419err_mode(char *action)
420{
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000421 PyErr_Format(IO_STATE->unsupported_operation,
422 "File not open for %s", action);
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000423 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000424}
425
426static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000427fileio_fileno(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000428{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000429 if (self->fd < 0)
430 return err_closed();
431 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000432}
433
434static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000435fileio_readable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000436{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000437 if (self->fd < 0)
438 return err_closed();
439 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000440}
441
442static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000443fileio_writable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000444{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000445 if (self->fd < 0)
446 return err_closed();
447 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000448}
449
450static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000451fileio_seekable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000452{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000453 if (self->fd < 0)
454 return err_closed();
455 if (self->seekable < 0) {
456 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
457 if (pos == NULL) {
458 PyErr_Clear();
459 self->seekable = 0;
460 } else {
461 Py_DECREF(pos);
462 self->seekable = 1;
463 }
464 }
465 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000466}
467
468static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000469fileio_readinto(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000470{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000471 Py_buffer pbuf;
472 Py_ssize_t n;
Guido van Rossum53807da2007-04-10 19:01:47 +0000473
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000474 if (self->fd < 0)
475 return err_closed();
476 if (!self->readable)
477 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000478
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000479 if (!PyArg_ParseTuple(args, "w*", &pbuf))
480 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000481
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000482 if (_PyVerify_fd(self->fd)) {
483 Py_BEGIN_ALLOW_THREADS
484 errno = 0;
485 n = read(self->fd, pbuf.buf, pbuf.len);
486 Py_END_ALLOW_THREADS
487 } else
488 n = -1;
489 PyBuffer_Release(&pbuf);
490 if (n < 0) {
491 if (errno == EAGAIN)
492 Py_RETURN_NONE;
493 PyErr_SetFromErrno(PyExc_IOError);
494 return NULL;
495 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000496
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000497 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000498}
499
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000500static size_t
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000501new_buffersize(fileio *self, size_t currentsize)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000502{
503#ifdef HAVE_FSTAT
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000504 off_t pos, end;
505 struct stat st;
506 if (fstat(self->fd, &st) == 0) {
507 end = st.st_size;
508 pos = lseek(self->fd, 0L, SEEK_CUR);
509 /* Files claiming a size smaller than SMALLCHUNK may
510 actually be streaming pseudo-files. In this case, we
511 apply the more aggressive algorithm below.
512 */
513 if (end >= SMALLCHUNK && end >= pos && pos >= 0) {
514 /* Add 1 so if the file were to grow we'd notice. */
515 return currentsize + end - pos + 1;
516 }
517 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000518#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000519 if (currentsize > SMALLCHUNK) {
520 /* Keep doubling until we reach BIGCHUNK;
521 then keep adding BIGCHUNK. */
522 if (currentsize <= BIGCHUNK)
523 return currentsize + currentsize;
524 else
525 return currentsize + BIGCHUNK;
526 }
527 return currentsize + SMALLCHUNK;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000528}
529
Guido van Rossum7165cb12007-07-10 06:54:34 +0000530static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000531fileio_readall(fileio *self)
Guido van Rossum7165cb12007-07-10 06:54:34 +0000532{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000533 PyObject *result;
534 Py_ssize_t total = 0;
535 int n;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000536
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000537 if (!_PyVerify_fd(self->fd))
538 return PyErr_SetFromErrno(PyExc_IOError);
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000539
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000540 result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK);
541 if (result == NULL)
542 return NULL;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000543
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000544 while (1) {
545 size_t newsize = new_buffersize(self, total);
546 if (newsize > PY_SSIZE_T_MAX || newsize <= 0) {
547 PyErr_SetString(PyExc_OverflowError,
548 "unbounded read returned more bytes "
549 "than a Python string can hold ");
550 Py_DECREF(result);
551 return NULL;
552 }
Christian Heimesa872de52008-12-05 08:26:55 +0000553
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000554 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)newsize) {
555 if (_PyBytes_Resize(&result, newsize) < 0) {
556 if (total == 0) {
557 Py_DECREF(result);
558 return NULL;
559 }
560 PyErr_Clear();
561 break;
562 }
563 }
564 Py_BEGIN_ALLOW_THREADS
565 errno = 0;
566 n = read(self->fd,
567 PyBytes_AS_STRING(result) + total,
568 newsize - total);
569 Py_END_ALLOW_THREADS
570 if (n == 0)
571 break;
572 if (n < 0) {
573 if (total > 0)
574 break;
575 if (errno == EAGAIN) {
576 Py_DECREF(result);
577 Py_RETURN_NONE;
578 }
579 Py_DECREF(result);
580 PyErr_SetFromErrno(PyExc_IOError);
581 return NULL;
582 }
583 total += n;
584 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000585
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000586 if (PyBytes_GET_SIZE(result) > total) {
587 if (_PyBytes_Resize(&result, total) < 0) {
588 /* This should never happen, but just in case */
589 Py_DECREF(result);
590 return NULL;
591 }
592 }
593 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000594}
595
Guido van Rossuma9e20242007-03-08 00:43:48 +0000596static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000597fileio_read(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000598{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000599 char *ptr;
600 Py_ssize_t n;
601 Py_ssize_t size = -1;
602 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000603
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000604 if (self->fd < 0)
605 return err_closed();
606 if (!self->readable)
607 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000608
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000609 if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size))
610 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000611
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000612 if (size < 0) {
613 return fileio_readall(self);
614 }
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000615
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000616 bytes = PyBytes_FromStringAndSize(NULL, size);
617 if (bytes == NULL)
618 return NULL;
619 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000620
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000621 if (_PyVerify_fd(self->fd)) {
622 Py_BEGIN_ALLOW_THREADS
623 errno = 0;
624 n = read(self->fd, ptr, size);
625 Py_END_ALLOW_THREADS
626 } else
627 n = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000628
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000629 if (n < 0) {
630 Py_DECREF(bytes);
631 if (errno == EAGAIN)
632 Py_RETURN_NONE;
633 PyErr_SetFromErrno(PyExc_IOError);
634 return NULL;
635 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000636
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000637 if (n != size) {
638 if (_PyBytes_Resize(&bytes, n) < 0) {
639 Py_DECREF(bytes);
640 return NULL;
641 }
642 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000643
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000644 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000645}
646
647static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000648fileio_write(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000649{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000650 Py_buffer pbuf;
651 Py_ssize_t n;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000652
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000653 if (self->fd < 0)
654 return err_closed();
655 if (!self->writable)
656 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000657
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000658 if (!PyArg_ParseTuple(args, "y*", &pbuf))
659 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000660
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000661 if (_PyVerify_fd(self->fd)) {
662 Py_BEGIN_ALLOW_THREADS
663 errno = 0;
664 n = write(self->fd, pbuf.buf, pbuf.len);
665 Py_END_ALLOW_THREADS
666 } else
667 n = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000668
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000669 PyBuffer_Release(&pbuf);
Martin v. Löwis423be952008-08-13 15:53:07 +0000670
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000671 if (n < 0) {
672 if (errno == EAGAIN)
673 Py_RETURN_NONE;
674 PyErr_SetFromErrno(PyExc_IOError);
675 return NULL;
676 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000677
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000678 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000679}
680
Guido van Rossum53807da2007-04-10 19:01:47 +0000681/* XXX Windows support below is likely incomplete */
682
Guido van Rossum53807da2007-04-10 19:01:47 +0000683/* Cribbed from posix_lseek() */
684static PyObject *
685portable_lseek(int fd, PyObject *posobj, int whence)
686{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000687 Py_off_t pos, res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000688
689#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000690 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
691 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000692#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000693 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000694#endif
695#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000696 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000697#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000698#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000699 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000700#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000701 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000702#endif /* SEEK_SET */
703
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000704 if (posobj == NULL)
705 pos = 0;
706 else {
707 if(PyFloat_Check(posobj)) {
708 PyErr_SetString(PyExc_TypeError, "an integer is required");
709 return NULL;
710 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000711#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000712 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000713#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000714 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000715#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000716 if (PyErr_Occurred())
717 return NULL;
718 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000719
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000720 if (_PyVerify_fd(fd)) {
721 Py_BEGIN_ALLOW_THREADS
Guido van Rossum53807da2007-04-10 19:01:47 +0000722#if defined(MS_WIN64) || defined(MS_WINDOWS)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000723 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000724#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000725 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000726#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000727 Py_END_ALLOW_THREADS
728 } else
729 res = -1;
730 if (res < 0)
731 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000732
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000733#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000734 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000735#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000736 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000737#endif
738}
739
Guido van Rossuma9e20242007-03-08 00:43:48 +0000740static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000741fileio_seek(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000742{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000743 PyObject *posobj;
744 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000745
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000746 if (self->fd < 0)
747 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000748
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000749 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
750 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000751
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000752 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000753}
754
755static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000756fileio_tell(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000757{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000758 if (self->fd < 0)
759 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000760
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000761 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000762}
763
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000764#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000765static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000766fileio_truncate(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000767{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000768 PyObject *posobj = NULL; /* the new size wanted by the user */
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000769#ifndef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000770 Py_off_t pos;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000771#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000772 int ret;
773 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000774
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000775 fd = self->fd;
776 if (fd < 0)
777 return err_closed();
778 if (!self->writable)
779 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000780
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000781 if (!PyArg_ParseTuple(args, "|O", &posobj))
782 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000783
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000784 if (posobj == Py_None || posobj == NULL) {
785 /* Get the current position. */
786 posobj = portable_lseek(fd, NULL, 1);
787 if (posobj == NULL)
788 return NULL;
789 }
790 else {
791 Py_INCREF(posobj);
792 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000793
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000794#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000795 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
796 so don't even try using it. */
797 {
798 PyObject *oldposobj, *tempposobj;
799 HANDLE hFile;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000800
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000801 /* we save the file pointer position */
802 oldposobj = portable_lseek(fd, NULL, 1);
803 if (oldposobj == NULL) {
804 Py_DECREF(posobj);
805 return NULL;
806 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000807
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000808 /* we then move to the truncation position */
809 tempposobj = portable_lseek(fd, posobj, 0);
810 if (tempposobj == NULL) {
811 Py_DECREF(oldposobj);
812 Py_DECREF(posobj);
813 return NULL;
814 }
815 Py_DECREF(tempposobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000816
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000817 /* Truncate. Note that this may grow the file! */
818 Py_BEGIN_ALLOW_THREADS
819 errno = 0;
820 hFile = (HANDLE)_get_osfhandle(fd);
821 ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
822 if (ret == 0) {
823 ret = SetEndOfFile(hFile) == 0;
824 if (ret)
825 errno = EACCES;
826 }
827 Py_END_ALLOW_THREADS
828
829 /* we restore the file pointer position in any case */
830 tempposobj = portable_lseek(fd, oldposobj, 0);
831 Py_DECREF(oldposobj);
832 if (tempposobj == NULL) {
833 Py_DECREF(posobj);
834 return NULL;
835 }
836 Py_DECREF(tempposobj);
837 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000838#else
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000839
840#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000841 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000842#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000843 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000844#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000845 if (PyErr_Occurred()){
846 Py_DECREF(posobj);
847 return NULL;
848 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000849
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000850 Py_BEGIN_ALLOW_THREADS
851 errno = 0;
852 ret = ftruncate(fd, pos);
853 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000854
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000855#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000856
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000857 if (ret != 0) {
858 Py_DECREF(posobj);
859 PyErr_SetFromErrno(PyExc_IOError);
860 return NULL;
861 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000862
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000863 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000864}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000865#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +0000866
867static char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000868mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +0000869{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000870 if (self->readable) {
871 if (self->writable)
872 return "rb+";
873 else
874 return "rb";
875 }
876 else
877 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000878}
Guido van Rossuma9e20242007-03-08 00:43:48 +0000879
880static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000881fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000882{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000883 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000884
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000885 if (self->fd < 0)
886 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +0000887
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000888 nameobj = PyObject_GetAttrString((PyObject *) self, "name");
889 if (nameobj == NULL) {
890 if (PyErr_ExceptionMatches(PyExc_AttributeError))
891 PyErr_Clear();
892 else
893 return NULL;
894 res = PyUnicode_FromFormat("<_io.FileIO fd=%d mode='%s'>",
895 self->fd, mode_string(self));
896 }
897 else {
898 res = PyUnicode_FromFormat("<_io.FileIO name=%R mode='%s'>",
899 nameobj, mode_string(self));
900 Py_DECREF(nameobj);
901 }
902 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000903}
904
905static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000906fileio_isatty(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000907{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000908 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000909
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000910 if (self->fd < 0)
911 return err_closed();
912 Py_BEGIN_ALLOW_THREADS
913 res = isatty(self->fd);
914 Py_END_ALLOW_THREADS
915 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000916}
917
Guido van Rossuma9e20242007-03-08 00:43:48 +0000918
919PyDoc_STRVAR(fileio_doc,
920"file(name: str[, mode: str]) -> file IO object\n"
921"\n"
922"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000923"writing or appending. The file will be created if it doesn't exist\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +0000924"when opened for writing or appending; it will be truncated when\n"
925"opened for writing. Add a '+' to the mode to allow simultaneous\n"
926"reading and writing.");
927
928PyDoc_STRVAR(read_doc,
929"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
930"\n"
931"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +0000932"In non-blocking mode, returns None if no data is available.\n"
933"On end-of-file, returns ''.");
934
935PyDoc_STRVAR(readall_doc,
936"readall() -> bytes. read all data from the file, returned as bytes.\n"
937"\n"
938"In non-blocking mode, returns as much as is immediately available,\n"
939"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000940
941PyDoc_STRVAR(write_doc,
942"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
943"\n"
944"Only makes one system call, so not all of the data may be written.\n"
945"The number of bytes actually written is returned.");
946
947PyDoc_STRVAR(fileno_doc,
948"fileno() -> int. \"file descriptor\".\n"
949"\n"
950"This is needed for lower-level file interfaces, such the fcntl module.");
951
952PyDoc_STRVAR(seek_doc,
953"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
954"\n"
955"Argument offset is a byte count. Optional argument whence defaults to\n"
956"0 (offset from start of file, offset should be >= 0); other values are 1\n"
957"(move relative to current position, positive or negative), and 2 (move\n"
958"relative to end of file, usually negative, although many platforms allow\n"
959"seeking beyond the end of a file)."
960"\n"
961"Note that not all file objects are seekable.");
962
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000963#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000964PyDoc_STRVAR(truncate_doc,
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000965"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +0000966"\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000967"Size defaults to the current file position, as returned by tell()."
968"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000969#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000970
971PyDoc_STRVAR(tell_doc,
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000972"tell() -> int. Current file position");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000973
974PyDoc_STRVAR(readinto_doc,
Benjamin Peterson9a8082f2009-03-05 00:55:56 +0000975"readinto() -> Same as RawIOBase.readinto().");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000976
977PyDoc_STRVAR(close_doc,
978"close() -> None. Close the file.\n"
979"\n"
980"A closed file cannot be used for further I/O operations. close() may be\n"
981"called more than once without error. Changes the fileno to -1.");
982
983PyDoc_STRVAR(isatty_doc,
984"isatty() -> bool. True if the file is connected to a tty device.");
985
Guido van Rossuma9e20242007-03-08 00:43:48 +0000986PyDoc_STRVAR(seekable_doc,
987"seekable() -> bool. True if file supports random-access.");
988
989PyDoc_STRVAR(readable_doc,
990"readable() -> bool. True if file was opened in a read mode.");
991
992PyDoc_STRVAR(writable_doc,
993"writable() -> bool. True if file was opened in a write mode.");
994
995static PyMethodDef fileio_methods[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000996 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
997 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
998 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
999 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
1000 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
1001 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001002#ifdef HAVE_FTRUNCATE
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001003 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001004#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001005 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
1006 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
1007 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
1008 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
1009 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
1010 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
1011 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001012};
1013
Guido van Rossum53807da2007-04-10 19:01:47 +00001014/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1015
Guido van Rossumb0428152007-04-08 17:44:42 +00001016static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001017get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001018{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001019 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001020}
1021
1022static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001023get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001024{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001025 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001026}
1027
1028static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001029get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001030{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001031 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001032}
1033
1034static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001035 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1036 {"closefd", (getter)get_closefd, NULL,
1037 "True if the file descriptor will be closed"},
1038 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1039 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001040};
1041
Guido van Rossuma9e20242007-03-08 00:43:48 +00001042PyTypeObject PyFileIO_Type = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001043 PyVarObject_HEAD_INIT(NULL, 0)
1044 "_io.FileIO",
1045 sizeof(fileio),
1046 0,
1047 (destructor)fileio_dealloc, /* tp_dealloc */
1048 0, /* tp_print */
1049 0, /* tp_getattr */
1050 0, /* tp_setattr */
1051 0, /* tp_reserved */
1052 (reprfunc)fileio_repr, /* tp_repr */
1053 0, /* tp_as_number */
1054 0, /* tp_as_sequence */
1055 0, /* tp_as_mapping */
1056 0, /* tp_hash */
1057 0, /* tp_call */
1058 0, /* tp_str */
1059 PyObject_GenericGetAttr, /* tp_getattro */
1060 0, /* tp_setattro */
1061 0, /* tp_as_buffer */
1062 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
1063 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1064 fileio_doc, /* tp_doc */
1065 (traverseproc)fileio_traverse, /* tp_traverse */
1066 (inquiry)fileio_clear, /* tp_clear */
1067 0, /* tp_richcompare */
1068 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1069 0, /* tp_iter */
1070 0, /* tp_iternext */
1071 fileio_methods, /* tp_methods */
1072 0, /* tp_members */
1073 fileio_getsetlist, /* tp_getset */
1074 0, /* tp_base */
1075 0, /* tp_dict */
1076 0, /* tp_descr_get */
1077 0, /* tp_descr_set */
1078 offsetof(fileio, dict), /* tp_dictoffset */
1079 fileio_init, /* tp_init */
1080 PyType_GenericAlloc, /* tp_alloc */
1081 fileio_new, /* tp_new */
1082 PyObject_GC_Del, /* tp_free */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001083};