blob: 4f450da636a813ca732e2294eb5454c602e677a2 [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,
270 "Must have exactly one of read/write/append mode");
271 goto error;
272 }
273 rwa = 1;
274 self->readable = 1;
275 break;
276 case 'w':
277 if (rwa)
278 goto bad_mode;
279 rwa = 1;
280 self->writable = 1;
281 flags |= O_CREAT | O_TRUNC;
282 break;
283 case 'a':
284 if (rwa)
285 goto bad_mode;
286 rwa = 1;
287 self->writable = 1;
288 flags |= O_CREAT;
289 append = 1;
290 break;
291 case 'b':
292 break;
293 case '+':
294 if (plus)
295 goto bad_mode;
296 self->readable = self->writable = 1;
297 plus = 1;
298 break;
299 default:
300 PyErr_Format(PyExc_ValueError,
301 "invalid mode: %.200s", mode);
302 goto error;
303 }
304 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000305
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000306 if (!rwa)
307 goto bad_mode;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000308
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000309 if (self->readable && self->writable)
310 flags |= O_RDWR;
311 else if (self->readable)
312 flags |= O_RDONLY;
313 else
314 flags |= O_WRONLY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000315
316#ifdef O_BINARY
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000317 flags |= O_BINARY;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000318#endif
319
Walter Dörwald0e411482007-06-06 16:55:38 +0000320#ifdef O_APPEND
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000321 if (append)
322 flags |= O_APPEND;
Walter Dörwald0e411482007-06-06 16:55:38 +0000323#endif
324
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000325 if (fd >= 0) {
326 if (check_fd(fd))
327 goto error;
328 self->fd = fd;
329 self->closefd = closefd;
330 }
331 else {
332 self->closefd = 1;
333 if (!closefd) {
334 PyErr_SetString(PyExc_ValueError,
335 "Cannot use closefd=False with file name");
336 goto error;
337 }
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000338
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000339 Py_BEGIN_ALLOW_THREADS
340 errno = 0;
Thomas Helleraf2be262007-07-12 11:03:13 +0000341#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000342 if (widename != NULL)
343 self->fd = _wopen(widename, flags, 0666);
344 else
Thomas Helleraf2be262007-07-12 11:03:13 +0000345#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000346 self->fd = open(name, flags, 0666);
347 Py_END_ALLOW_THREADS
348 if (self->fd < 0) {
Christian Heimes0b489542007-10-31 19:20:48 +0000349#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000350 if (widename != NULL)
351 PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename);
352 else
Christian Heimes0b489542007-10-31 19:20:48 +0000353#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000354 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
355 goto error;
356 }
357 if(dircheck(self, name) < 0)
358 goto error;
359 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000360
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000361 if (PyObject_SetAttrString((PyObject *)self, "name", nameobj) < 0)
362 goto error;
Antoine Pitrou08838b62009-01-21 00:55:13 +0000363
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000364 if (append) {
365 /* For consistent behaviour, we explicitly seek to the
366 end of file (otherwise, it might be done only on the
367 first write()). */
368 PyObject *pos = portable_lseek(self->fd, NULL, 2);
369 if (pos == NULL)
370 goto error;
371 Py_DECREF(pos);
372 }
Antoine Pitrou7fb111b2009-03-04 11:14:01 +0000373
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000374 goto done;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000375
376 error:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000377 ret = -1;
Guido van Rossum53807da2007-04-10 19:01:47 +0000378
Guido van Rossuma9e20242007-03-08 00:43:48 +0000379 done:
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000380 Py_CLEAR(stringobj);
381 return ret;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000382}
383
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000384static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000385fileio_traverse(fileio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000386{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000387 Py_VISIT(self->dict);
388 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000389}
390
391static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000392fileio_clear(fileio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000393{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000394 Py_CLEAR(self->dict);
395 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000396}
397
Guido van Rossuma9e20242007-03-08 00:43:48 +0000398static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000399fileio_dealloc(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000400{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000401 if (_PyIOBase_finalize((PyObject *) self) < 0)
402 return;
403 _PyObject_GC_UNTRACK(self);
404 if (self->weakreflist != NULL)
405 PyObject_ClearWeakRefs((PyObject *) self);
406 Py_CLEAR(self->dict);
407 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000408}
409
410static PyObject *
411err_closed(void)
412{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000413 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
414 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000415}
416
417static PyObject *
Guido van Rossum53807da2007-04-10 19:01:47 +0000418err_mode(char *action)
419{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000420 PyErr_Format(PyExc_ValueError, "File not open for %s", action);
421 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000422}
423
424static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000425fileio_fileno(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000426{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000427 if (self->fd < 0)
428 return err_closed();
429 return PyLong_FromLong((long) self->fd);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000430}
431
432static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000433fileio_readable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000434{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000435 if (self->fd < 0)
436 return err_closed();
437 return PyBool_FromLong((long) self->readable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000438}
439
440static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000441fileio_writable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000442{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000443 if (self->fd < 0)
444 return err_closed();
445 return PyBool_FromLong((long) self->writable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000446}
447
448static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000449fileio_seekable(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000450{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000451 if (self->fd < 0)
452 return err_closed();
453 if (self->seekable < 0) {
454 PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
455 if (pos == NULL) {
456 PyErr_Clear();
457 self->seekable = 0;
458 } else {
459 Py_DECREF(pos);
460 self->seekable = 1;
461 }
462 }
463 return PyBool_FromLong((long) self->seekable);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000464}
465
466static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000467fileio_readinto(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000468{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000469 Py_buffer pbuf;
470 Py_ssize_t n;
Guido van Rossum53807da2007-04-10 19:01:47 +0000471
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000472 if (self->fd < 0)
473 return err_closed();
474 if (!self->readable)
475 return err_mode("reading");
Guido van Rossum53807da2007-04-10 19:01:47 +0000476
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000477 if (!PyArg_ParseTuple(args, "w*", &pbuf))
478 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000479
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000480 if (_PyVerify_fd(self->fd)) {
481 Py_BEGIN_ALLOW_THREADS
482 errno = 0;
483 n = read(self->fd, pbuf.buf, pbuf.len);
484 Py_END_ALLOW_THREADS
485 } else
486 n = -1;
487 PyBuffer_Release(&pbuf);
488 if (n < 0) {
489 if (errno == EAGAIN)
490 Py_RETURN_NONE;
491 PyErr_SetFromErrno(PyExc_IOError);
492 return NULL;
493 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000494
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000495 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000496}
497
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000498static size_t
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000499new_buffersize(fileio *self, size_t currentsize)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000500{
501#ifdef HAVE_FSTAT
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000502 off_t pos, end;
503 struct stat st;
504 if (fstat(self->fd, &st) == 0) {
505 end = st.st_size;
506 pos = lseek(self->fd, 0L, SEEK_CUR);
507 /* Files claiming a size smaller than SMALLCHUNK may
508 actually be streaming pseudo-files. In this case, we
509 apply the more aggressive algorithm below.
510 */
511 if (end >= SMALLCHUNK && end >= pos && pos >= 0) {
512 /* Add 1 so if the file were to grow we'd notice. */
513 return currentsize + end - pos + 1;
514 }
515 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000516#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000517 if (currentsize > SMALLCHUNK) {
518 /* Keep doubling until we reach BIGCHUNK;
519 then keep adding BIGCHUNK. */
520 if (currentsize <= BIGCHUNK)
521 return currentsize + currentsize;
522 else
523 return currentsize + BIGCHUNK;
524 }
525 return currentsize + SMALLCHUNK;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000526}
527
Guido van Rossum7165cb12007-07-10 06:54:34 +0000528static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000529fileio_readall(fileio *self)
Guido van Rossum7165cb12007-07-10 06:54:34 +0000530{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000531 PyObject *result;
532 Py_ssize_t total = 0;
533 int n;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000534
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000535 if (!_PyVerify_fd(self->fd))
536 return PyErr_SetFromErrno(PyExc_IOError);
Kristján Valur Jónssona8abe862009-03-24 15:27:42 +0000537
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000538 result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK);
539 if (result == NULL)
540 return NULL;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000541
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000542 while (1) {
543 size_t newsize = new_buffersize(self, total);
544 if (newsize > PY_SSIZE_T_MAX || newsize <= 0) {
545 PyErr_SetString(PyExc_OverflowError,
546 "unbounded read returned more bytes "
547 "than a Python string can hold ");
548 Py_DECREF(result);
549 return NULL;
550 }
Christian Heimesa872de52008-12-05 08:26:55 +0000551
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000552 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)newsize) {
553 if (_PyBytes_Resize(&result, newsize) < 0) {
554 if (total == 0) {
555 Py_DECREF(result);
556 return NULL;
557 }
558 PyErr_Clear();
559 break;
560 }
561 }
562 Py_BEGIN_ALLOW_THREADS
563 errno = 0;
564 n = read(self->fd,
565 PyBytes_AS_STRING(result) + total,
566 newsize - total);
567 Py_END_ALLOW_THREADS
568 if (n == 0)
569 break;
570 if (n < 0) {
571 if (total > 0)
572 break;
573 if (errno == EAGAIN) {
574 Py_DECREF(result);
575 Py_RETURN_NONE;
576 }
577 Py_DECREF(result);
578 PyErr_SetFromErrno(PyExc_IOError);
579 return NULL;
580 }
581 total += n;
582 }
Guido van Rossum7165cb12007-07-10 06:54:34 +0000583
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000584 if (PyBytes_GET_SIZE(result) > total) {
585 if (_PyBytes_Resize(&result, total) < 0) {
586 /* This should never happen, but just in case */
587 Py_DECREF(result);
588 return NULL;
589 }
590 }
591 return result;
Guido van Rossum7165cb12007-07-10 06:54:34 +0000592}
593
Guido van Rossuma9e20242007-03-08 00:43:48 +0000594static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000595fileio_read(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000596{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000597 char *ptr;
598 Py_ssize_t n;
599 Py_ssize_t size = -1;
600 PyObject *bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000601
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000602 if (self->fd < 0)
603 return err_closed();
604 if (!self->readable)
605 return err_mode("reading");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000606
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000607 if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size))
608 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000609
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000610 if (size < 0) {
611 return fileio_readall(self);
612 }
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000613
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000614 bytes = PyBytes_FromStringAndSize(NULL, size);
615 if (bytes == NULL)
616 return NULL;
617 ptr = PyBytes_AS_STRING(bytes);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000618
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000619 if (_PyVerify_fd(self->fd)) {
620 Py_BEGIN_ALLOW_THREADS
621 errno = 0;
622 n = read(self->fd, ptr, size);
623 Py_END_ALLOW_THREADS
624 } else
625 n = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000626
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000627 if (n < 0) {
628 Py_DECREF(bytes);
629 if (errno == EAGAIN)
630 Py_RETURN_NONE;
631 PyErr_SetFromErrno(PyExc_IOError);
632 return NULL;
633 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000634
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000635 if (n != size) {
636 if (_PyBytes_Resize(&bytes, n) < 0) {
637 Py_DECREF(bytes);
638 return NULL;
639 }
640 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000641
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000642 return (PyObject *) bytes;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000643}
644
645static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000646fileio_write(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000647{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000648 Py_buffer pbuf;
649 Py_ssize_t n;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000650
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000651 if (self->fd < 0)
652 return err_closed();
653 if (!self->writable)
654 return err_mode("writing");
Guido van Rossum53807da2007-04-10 19:01:47 +0000655
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000656 if (!PyArg_ParseTuple(args, "y*", &pbuf))
657 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000658
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000659 if (_PyVerify_fd(self->fd)) {
660 Py_BEGIN_ALLOW_THREADS
661 errno = 0;
662 n = write(self->fd, pbuf.buf, pbuf.len);
663 Py_END_ALLOW_THREADS
664 } else
665 n = -1;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000666
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000667 PyBuffer_Release(&pbuf);
Martin v. Löwis423be952008-08-13 15:53:07 +0000668
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000669 if (n < 0) {
670 if (errno == EAGAIN)
671 Py_RETURN_NONE;
672 PyErr_SetFromErrno(PyExc_IOError);
673 return NULL;
674 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000675
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000676 return PyLong_FromSsize_t(n);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000677}
678
Guido van Rossum53807da2007-04-10 19:01:47 +0000679/* XXX Windows support below is likely incomplete */
680
Guido van Rossum53807da2007-04-10 19:01:47 +0000681/* Cribbed from posix_lseek() */
682static PyObject *
683portable_lseek(int fd, PyObject *posobj, int whence)
684{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000685 Py_off_t pos, res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000686
687#ifdef SEEK_SET
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000688 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
689 switch (whence) {
Guido van Rossum53807da2007-04-10 19:01:47 +0000690#if SEEK_SET != 0
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000691 case 0: whence = SEEK_SET; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000692#endif
693#if SEEK_CUR != 1
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000694 case 1: whence = SEEK_CUR; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000695#endif
Antoine Pitrou4f7945f2009-01-20 11:42:11 +0000696#if SEEK_END != 2
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000697 case 2: whence = SEEK_END; break;
Guido van Rossum53807da2007-04-10 19:01:47 +0000698#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000699 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000700#endif /* SEEK_SET */
701
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000702 if (posobj == NULL)
703 pos = 0;
704 else {
705 if(PyFloat_Check(posobj)) {
706 PyErr_SetString(PyExc_TypeError, "an integer is required");
707 return NULL;
708 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000709#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000710 pos = PyLong_AsLongLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000711#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000712 pos = PyLong_AsLong(posobj);
Guido van Rossum53807da2007-04-10 19:01:47 +0000713#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000714 if (PyErr_Occurred())
715 return NULL;
716 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000717
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000718 if (_PyVerify_fd(fd)) {
719 Py_BEGIN_ALLOW_THREADS
Guido van Rossum53807da2007-04-10 19:01:47 +0000720#if defined(MS_WIN64) || defined(MS_WINDOWS)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000721 res = _lseeki64(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000722#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000723 res = lseek(fd, pos, whence);
Guido van Rossum53807da2007-04-10 19:01:47 +0000724#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000725 Py_END_ALLOW_THREADS
726 } else
727 res = -1;
728 if (res < 0)
729 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum53807da2007-04-10 19:01:47 +0000730
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000731#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000732 return PyLong_FromLongLong(res);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000733#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000734 return PyLong_FromLong(res);
Guido van Rossum53807da2007-04-10 19:01:47 +0000735#endif
736}
737
Guido van Rossuma9e20242007-03-08 00:43:48 +0000738static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000739fileio_seek(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000740{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000741 PyObject *posobj;
742 int whence = 0;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000743
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000744 if (self->fd < 0)
745 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000746
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000747 if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence))
748 return NULL;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000749
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000750 return portable_lseek(self->fd, posobj, whence);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000751}
752
753static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000754fileio_tell(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000755{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000756 if (self->fd < 0)
757 return err_closed();
Guido van Rossuma9e20242007-03-08 00:43:48 +0000758
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000759 return portable_lseek(self->fd, NULL, 1);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000760}
761
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000762#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000763static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000764fileio_truncate(fileio *self, PyObject *args)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000765{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000766 PyObject *posobj = NULL; /* the new size wanted by the user */
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000767#ifndef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000768 Py_off_t pos;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000769#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000770 int ret;
771 int fd;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000772
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000773 fd = self->fd;
774 if (fd < 0)
775 return err_closed();
776 if (!self->writable)
777 return err_mode("writing");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000778
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000779 if (!PyArg_ParseTuple(args, "|O", &posobj))
780 return NULL;
Guido van Rossum53807da2007-04-10 19:01:47 +0000781
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000782 if (posobj == Py_None || posobj == NULL) {
783 /* Get the current position. */
784 posobj = portable_lseek(fd, NULL, 1);
785 if (posobj == NULL)
786 return NULL;
787 }
788 else {
789 Py_INCREF(posobj);
790 }
Guido van Rossum53807da2007-04-10 19:01:47 +0000791
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000792#ifdef MS_WINDOWS
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000793 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
794 so don't even try using it. */
795 {
796 PyObject *oldposobj, *tempposobj;
797 HANDLE hFile;
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000798
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000799 /* we save the file pointer position */
800 oldposobj = portable_lseek(fd, NULL, 1);
801 if (oldposobj == NULL) {
802 Py_DECREF(posobj);
803 return NULL;
804 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000805
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000806 /* we then move to the truncation position */
807 tempposobj = portable_lseek(fd, posobj, 0);
808 if (tempposobj == NULL) {
809 Py_DECREF(oldposobj);
810 Py_DECREF(posobj);
811 return NULL;
812 }
813 Py_DECREF(tempposobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000814
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000815 /* Truncate. Note that this may grow the file! */
816 Py_BEGIN_ALLOW_THREADS
817 errno = 0;
818 hFile = (HANDLE)_get_osfhandle(fd);
819 ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
820 if (ret == 0) {
821 ret = SetEndOfFile(hFile) == 0;
822 if (ret)
823 errno = EACCES;
824 }
825 Py_END_ALLOW_THREADS
826
827 /* we restore the file pointer position in any case */
828 tempposobj = portable_lseek(fd, oldposobj, 0);
829 Py_DECREF(oldposobj);
830 if (tempposobj == NULL) {
831 Py_DECREF(posobj);
832 return NULL;
833 }
834 Py_DECREF(tempposobj);
835 }
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000836#else
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000837
838#if defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000839 pos = PyLong_AsLongLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000840#else
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000841 pos = PyLong_AsLong(posobj);
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000842#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000843 if (PyErr_Occurred()){
844 Py_DECREF(posobj);
845 return NULL;
846 }
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000847
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000848 Py_BEGIN_ALLOW_THREADS
849 errno = 0;
850 ret = ftruncate(fd, pos);
851 Py_END_ALLOW_THREADS
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000852
Thomas Hellerfdeee3a2007-07-12 11:21:36 +0000853#endif /* !MS_WINDOWS */
Guido van Rossuma9e20242007-03-08 00:43:48 +0000854
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000855 if (ret != 0) {
856 Py_DECREF(posobj);
857 PyErr_SetFromErrno(PyExc_IOError);
858 return NULL;
859 }
Guido van Rossuma9e20242007-03-08 00:43:48 +0000860
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000861 return posobj;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000862}
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000863#endif /* HAVE_FTRUNCATE */
Guido van Rossum53807da2007-04-10 19:01:47 +0000864
865static char *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000866mode_string(fileio *self)
Guido van Rossum53807da2007-04-10 19:01:47 +0000867{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000868 if (self->readable) {
869 if (self->writable)
870 return "rb+";
871 else
872 return "rb";
873 }
874 else
875 return "wb";
Guido van Rossum53807da2007-04-10 19:01:47 +0000876}
Guido van Rossuma9e20242007-03-08 00:43:48 +0000877
878static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000879fileio_repr(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000880{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000881 PyObject *nameobj, *res;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000882
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000883 if (self->fd < 0)
884 return PyUnicode_FromFormat("<_io.FileIO [closed]>");
Antoine Pitrou716c4442009-05-23 19:04:03 +0000885
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000886 nameobj = PyObject_GetAttrString((PyObject *) self, "name");
887 if (nameobj == NULL) {
888 if (PyErr_ExceptionMatches(PyExc_AttributeError))
889 PyErr_Clear();
890 else
891 return NULL;
892 res = PyUnicode_FromFormat("<_io.FileIO fd=%d mode='%s'>",
893 self->fd, mode_string(self));
894 }
895 else {
896 res = PyUnicode_FromFormat("<_io.FileIO name=%R mode='%s'>",
897 nameobj, mode_string(self));
898 Py_DECREF(nameobj);
899 }
900 return res;
Guido van Rossuma9e20242007-03-08 00:43:48 +0000901}
902
903static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000904fileio_isatty(fileio *self)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000905{
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000906 long res;
Guido van Rossum53807da2007-04-10 19:01:47 +0000907
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000908 if (self->fd < 0)
909 return err_closed();
910 Py_BEGIN_ALLOW_THREADS
911 res = isatty(self->fd);
912 Py_END_ALLOW_THREADS
913 return PyBool_FromLong(res);
Guido van Rossuma9e20242007-03-08 00:43:48 +0000914}
915
Guido van Rossuma9e20242007-03-08 00:43:48 +0000916
917PyDoc_STRVAR(fileio_doc,
918"file(name: str[, mode: str]) -> file IO object\n"
919"\n"
920"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000921"writing or appending. The file will be created if it doesn't exist\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +0000922"when opened for writing or appending; it will be truncated when\n"
923"opened for writing. Add a '+' to the mode to allow simultaneous\n"
924"reading and writing.");
925
926PyDoc_STRVAR(read_doc,
927"read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
928"\n"
929"Only makes one system call, so less data may be returned than requested\n"
Guido van Rossum7165cb12007-07-10 06:54:34 +0000930"In non-blocking mode, returns None if no data is available.\n"
931"On end-of-file, returns ''.");
932
933PyDoc_STRVAR(readall_doc,
934"readall() -> bytes. read all data from the file, returned as bytes.\n"
935"\n"
936"In non-blocking mode, returns as much as is immediately available,\n"
937"or None if no data is available. On end-of-file, returns ''.");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000938
939PyDoc_STRVAR(write_doc,
940"write(b: bytes) -> int. Write bytes b to file, return number written.\n"
941"\n"
942"Only makes one system call, so not all of the data may be written.\n"
943"The number of bytes actually written is returned.");
944
945PyDoc_STRVAR(fileno_doc,
946"fileno() -> int. \"file descriptor\".\n"
947"\n"
948"This is needed for lower-level file interfaces, such the fcntl module.");
949
950PyDoc_STRVAR(seek_doc,
951"seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
952"\n"
953"Argument offset is a byte count. Optional argument whence defaults to\n"
954"0 (offset from start of file, offset should be >= 0); other values are 1\n"
955"(move relative to current position, positive or negative), and 2 (move\n"
956"relative to end of file, usually negative, although many platforms allow\n"
957"seeking beyond the end of a file)."
958"\n"
959"Note that not all file objects are seekable.");
960
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000961#ifdef HAVE_FTRUNCATE
Guido van Rossuma9e20242007-03-08 00:43:48 +0000962PyDoc_STRVAR(truncate_doc,
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000963"truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
Guido van Rossuma9e20242007-03-08 00:43:48 +0000964"\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000965"Size defaults to the current file position, as returned by tell()."
966"The current file position is changed to the value of size.");
Thomas Hellerc6a55ee2007-07-11 12:45:46 +0000967#endif
Guido van Rossuma9e20242007-03-08 00:43:48 +0000968
969PyDoc_STRVAR(tell_doc,
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000970"tell() -> int. Current file position");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000971
972PyDoc_STRVAR(readinto_doc,
Benjamin Peterson9a8082f2009-03-05 00:55:56 +0000973"readinto() -> Same as RawIOBase.readinto().");
Guido van Rossuma9e20242007-03-08 00:43:48 +0000974
975PyDoc_STRVAR(close_doc,
976"close() -> None. Close the file.\n"
977"\n"
978"A closed file cannot be used for further I/O operations. close() may be\n"
979"called more than once without error. Changes the fileno to -1.");
980
981PyDoc_STRVAR(isatty_doc,
982"isatty() -> bool. True if the file is connected to a tty device.");
983
Guido van Rossuma9e20242007-03-08 00:43:48 +0000984PyDoc_STRVAR(seekable_doc,
985"seekable() -> bool. True if file supports random-access.");
986
987PyDoc_STRVAR(readable_doc,
988"readable() -> bool. True if file was opened in a read mode.");
989
990PyDoc_STRVAR(writable_doc,
991"writable() -> bool. True if file was opened in a write mode.");
992
993static PyMethodDef fileio_methods[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +0000994 {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc},
995 {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc},
996 {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc},
997 {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc},
998 {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc},
999 {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001000#ifdef HAVE_FTRUNCATE
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001001 {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc},
Thomas Hellerc6a55ee2007-07-11 12:45:46 +00001002#endif
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001003 {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc},
1004 {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc},
1005 {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc},
1006 {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},
1007 {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc},
1008 {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc},
1009 {NULL, NULL} /* sentinel */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001010};
1011
Guido van Rossum53807da2007-04-10 19:01:47 +00001012/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1013
Guido van Rossumb0428152007-04-08 17:44:42 +00001014static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001015get_closed(fileio *self, void *closure)
Guido van Rossumb0428152007-04-08 17:44:42 +00001016{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001017 return PyBool_FromLong((long)(self->fd < 0));
Guido van Rossum53807da2007-04-10 19:01:47 +00001018}
1019
1020static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001021get_closefd(fileio *self, void *closure)
Christian Heimesecc42a22008-11-05 19:30:32 +00001022{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001023 return PyBool_FromLong((long)(self->closefd));
Christian Heimesecc42a22008-11-05 19:30:32 +00001024}
1025
1026static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001027get_mode(fileio *self, void *closure)
Guido van Rossum53807da2007-04-10 19:01:47 +00001028{
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001029 return PyUnicode_FromString(mode_string(self));
Guido van Rossumb0428152007-04-08 17:44:42 +00001030}
1031
1032static PyGetSetDef fileio_getsetlist[] = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001033 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1034 {"closefd", (getter)get_closefd, NULL,
1035 "True if the file descriptor will be closed"},
1036 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
1037 {NULL},
Guido van Rossumb0428152007-04-08 17:44:42 +00001038};
1039
Guido van Rossuma9e20242007-03-08 00:43:48 +00001040PyTypeObject PyFileIO_Type = {
Antoine Pitrouae4b4722010-05-05 16:31:07 +00001041 PyVarObject_HEAD_INIT(NULL, 0)
1042 "_io.FileIO",
1043 sizeof(fileio),
1044 0,
1045 (destructor)fileio_dealloc, /* tp_dealloc */
1046 0, /* tp_print */
1047 0, /* tp_getattr */
1048 0, /* tp_setattr */
1049 0, /* tp_reserved */
1050 (reprfunc)fileio_repr, /* tp_repr */
1051 0, /* tp_as_number */
1052 0, /* tp_as_sequence */
1053 0, /* tp_as_mapping */
1054 0, /* tp_hash */
1055 0, /* tp_call */
1056 0, /* tp_str */
1057 PyObject_GenericGetAttr, /* tp_getattro */
1058 0, /* tp_setattro */
1059 0, /* tp_as_buffer */
1060 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
1061 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1062 fileio_doc, /* tp_doc */
1063 (traverseproc)fileio_traverse, /* tp_traverse */
1064 (inquiry)fileio_clear, /* tp_clear */
1065 0, /* tp_richcompare */
1066 offsetof(fileio, weakreflist), /* tp_weaklistoffset */
1067 0, /* tp_iter */
1068 0, /* tp_iternext */
1069 fileio_methods, /* tp_methods */
1070 0, /* tp_members */
1071 fileio_getsetlist, /* tp_getset */
1072 0, /* tp_base */
1073 0, /* tp_dict */
1074 0, /* tp_descr_get */
1075 0, /* tp_descr_set */
1076 offsetof(fileio, dict), /* tp_dictoffset */
1077 fileio_init, /* tp_init */
1078 PyType_GenericAlloc, /* tp_alloc */
1079 fileio_new, /* tp_new */
1080 PyObject_GC_Del, /* tp_free */
Guido van Rossuma9e20242007-03-08 00:43:48 +00001081};