Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1 | /* Author: Daniel Stutzbach */ |
| 2 | |
| 3 | #define PY_SSIZE_T_CLEAN |
| 4 | #include "Python.h" |
Benjamin Peterson | 2614cda | 2010-03-21 22:36:19 +0000 | [diff] [blame] | 5 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 6 | #include <sys/types.h> |
Benjamin Peterson | 2614cda | 2010-03-21 22:36:19 +0000 | [diff] [blame] | 7 | #endif |
| 8 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 9 | #include <sys/stat.h> |
Benjamin Peterson | 2614cda | 2010-03-21 22:36:19 +0000 | [diff] [blame] | 10 | #endif |
| 11 | #ifdef HAVE_FCNTL_H |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 12 | #include <fcntl.h> |
Benjamin Peterson | 2614cda | 2010-03-21 22:36:19 +0000 | [diff] [blame] | 13 | #endif |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 14 | #include <stddef.h> /* For offsetof */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 15 | #include "_iomodule.h" |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 16 | |
| 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | #ifdef MS_WINDOWS |
| 31 | /* can simulate truncate with Win32 API functions; see file_truncate */ |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 32 | #define HAVE_FTRUNCATE |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 33 | #define WIN32_LEAN_AND_MEAN |
| 34 | #include <windows.h> |
| 35 | #endif |
| 36 | |
Christian Heimes | a872de5 | 2008-12-05 08:26:55 +0000 | [diff] [blame] | 37 | #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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 51 | typedef struct { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 52 | 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 Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 60 | } fileio; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 61 | |
Collin Winter | af33438 | 2007-03-08 21:46:15 +0000 | [diff] [blame] | 62 | PyTypeObject PyFileIO_Type; |
| 63 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 64 | #define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type)) |
| 65 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 66 | int |
| 67 | _PyFileIO_closed(PyObject *self) |
| 68 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 69 | return ((fileio *)self)->fd < 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 70 | } |
Antoine Pitrou | 08838b6 | 2009-01-21 00:55:13 +0000 | [diff] [blame] | 71 | |
Antoine Pitrou | 7fb111b | 2009-03-04 11:14:01 +0000 | [diff] [blame] | 72 | static PyObject * |
| 73 | portable_lseek(int fd, PyObject *posobj, int whence); |
| 74 | |
Antoine Pitrou | a28fcfd | 2009-03-13 23:42:55 +0000 | [diff] [blame] | 75 | static PyObject *portable_lseek(int fd, PyObject *posobj, int whence); |
| 76 | |
Kristján Valur Jónsson | 19288c2 | 2008-12-18 17:15:54 +0000 | [diff] [blame] | 77 | /* Returns 0 on success, -1 with exception set on failure. */ |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 78 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 79 | internal_close(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 80 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 81 | 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 Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 107 | fileio_close(fileio *self) |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 108 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 109 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 116 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 117 | return PyObject_CallMethod((PyObject*)&PyRawIOBase_Type, |
| 118 | "close", "O", self); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static PyObject * |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 122 | fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 123 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 124 | fileio *self; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 125 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 126 | assert(type != NULL && type->tp_alloc != NULL); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 127 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 128 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 137 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 138 | return (PyObject *) self; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 139 | } |
| 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 | |
| 145 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 146 | dircheck(fileio* self, const char *name) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 147 | { |
| 148 | #if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR) |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 149 | 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 Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 157 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 158 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 164 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 165 | return 0; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Benjamin Peterson | 806d402 | 2009-01-19 15:11:51 +0000 | [diff] [blame] | 168 | static int |
| 169 | check_fd(int fd) |
| 170 | { |
| 171 | #if defined(HAVE_FSTAT) |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 172 | 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 Peterson | 806d402 | 2009-01-19 15:11:51 +0000 | [diff] [blame] | 182 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 183 | return 0; |
Benjamin Peterson | 806d402 | 2009-01-19 15:11:51 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 186 | |
| 187 | static int |
| 188 | fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) |
| 189 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 190 | 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 Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 196 | #ifdef MS_WINDOWS |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 197 | Py_UNICODE *widename = NULL; |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 198 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 199 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 204 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 205 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 211 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 212 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:fileio", |
| 213 | kwlist, &nameobj, &mode, &closefd)) |
| 214 | return -1; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 215 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 216 | if (PyFloat_Check(nameobj)) { |
| 217 | PyErr_SetString(PyExc_TypeError, |
| 218 | "integer argument expected, got float"); |
| 219 | return -1; |
| 220 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 221 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 222 | 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 Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 231 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 232 | #ifdef MS_WINDOWS |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 233 | if (PyUnicode_Check(nameobj)) |
| 234 | widename = PyUnicode_AS_UNICODE(nameobj); |
| 235 | if (widename == NULL) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 236 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 237 | 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 Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 246 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 247 | if (u == NULL) |
| 248 | return -1; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 249 | |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 250 | stringobj = PyUnicode_EncodeFSDefault(u); |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 251 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 262 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 263 | s = mode; |
| 264 | while (*s) { |
| 265 | switch (*s++) { |
| 266 | case 'r': |
| 267 | if (rwa) { |
| 268 | bad_mode: |
| 269 | PyErr_SetString(PyExc_ValueError, |
Georg Brandl | 28928ae | 2010-10-21 13:45:52 +0000 | [diff] [blame^] | 270 | "Must have exactly one of read/write/append " |
| 271 | "mode and at most one plus"); |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 272 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 306 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 307 | if (!rwa) |
| 308 | goto bad_mode; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 309 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 310 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 316 | |
| 317 | #ifdef O_BINARY |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 318 | flags |= O_BINARY; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 319 | #endif |
| 320 | |
Walter Dörwald | 0e41148 | 2007-06-06 16:55:38 +0000 | [diff] [blame] | 321 | #ifdef O_APPEND |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 322 | if (append) |
| 323 | flags |= O_APPEND; |
Walter Dörwald | 0e41148 | 2007-06-06 16:55:38 +0000 | [diff] [blame] | 324 | #endif |
| 325 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 326 | 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 Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 339 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 340 | Py_BEGIN_ALLOW_THREADS |
| 341 | errno = 0; |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 342 | #ifdef MS_WINDOWS |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 343 | if (widename != NULL) |
| 344 | self->fd = _wopen(widename, flags, 0666); |
| 345 | else |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 346 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 347 | self->fd = open(name, flags, 0666); |
| 348 | Py_END_ALLOW_THREADS |
| 349 | if (self->fd < 0) { |
Christian Heimes | 0b48954 | 2007-10-31 19:20:48 +0000 | [diff] [blame] | 350 | #ifdef MS_WINDOWS |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 351 | if (widename != NULL) |
| 352 | PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename); |
| 353 | else |
Christian Heimes | 0b48954 | 2007-10-31 19:20:48 +0000 | [diff] [blame] | 354 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 355 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
| 356 | goto error; |
| 357 | } |
| 358 | if(dircheck(self, name) < 0) |
| 359 | goto error; |
| 360 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 361 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 362 | if (PyObject_SetAttrString((PyObject *)self, "name", nameobj) < 0) |
| 363 | goto error; |
Antoine Pitrou | 08838b6 | 2009-01-21 00:55:13 +0000 | [diff] [blame] | 364 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 365 | 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 Pitrou | 7fb111b | 2009-03-04 11:14:01 +0000 | [diff] [blame] | 374 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 375 | goto done; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 376 | |
| 377 | error: |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 378 | ret = -1; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 379 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 380 | done: |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 381 | Py_CLEAR(stringobj); |
| 382 | return ret; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 385 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 386 | fileio_traverse(fileio *self, visitproc visit, void *arg) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 387 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 388 | Py_VISIT(self->dict); |
| 389 | return 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 393 | fileio_clear(fileio *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 394 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 395 | Py_CLEAR(self->dict); |
| 396 | return 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 399 | static void |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 400 | fileio_dealloc(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 401 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 402 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | static PyObject * |
| 412 | err_closed(void) |
| 413 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 414 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); |
| 415 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static PyObject * |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 419 | err_mode(char *action) |
| 420 | { |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 421 | PyErr_Format(IO_STATE->unsupported_operation, |
| 422 | "File not open for %s", action); |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 423 | return NULL; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 427 | fileio_fileno(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 428 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 429 | if (self->fd < 0) |
| 430 | return err_closed(); |
| 431 | return PyLong_FromLong((long) self->fd); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 435 | fileio_readable(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 436 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 437 | if (self->fd < 0) |
| 438 | return err_closed(); |
| 439 | return PyBool_FromLong((long) self->readable); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 443 | fileio_writable(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 444 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 445 | if (self->fd < 0) |
| 446 | return err_closed(); |
| 447 | return PyBool_FromLong((long) self->writable); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 451 | fileio_seekable(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 452 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 453 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 469 | fileio_readinto(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 470 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 471 | Py_buffer pbuf; |
| 472 | Py_ssize_t n; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 473 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 474 | if (self->fd < 0) |
| 475 | return err_closed(); |
| 476 | if (!self->readable) |
| 477 | return err_mode("reading"); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 478 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 479 | if (!PyArg_ParseTuple(args, "w*", &pbuf)) |
| 480 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 481 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 482 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 496 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 497 | return PyLong_FromSsize_t(n); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 500 | static size_t |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 501 | new_buffersize(fileio *self, size_t currentsize) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 502 | { |
| 503 | #ifdef HAVE_FSTAT |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 504 | 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 Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 518 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 519 | 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 Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 530 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 531 | fileio_readall(fileio *self) |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 532 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 533 | PyObject *result; |
| 534 | Py_ssize_t total = 0; |
| 535 | int n; |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 536 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 537 | if (!_PyVerify_fd(self->fd)) |
| 538 | return PyErr_SetFromErrno(PyExc_IOError); |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 539 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 540 | result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK); |
| 541 | if (result == NULL) |
| 542 | return NULL; |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 543 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 544 | 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 Heimes | a872de5 | 2008-12-05 08:26:55 +0000 | [diff] [blame] | 553 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 554 | 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 Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 585 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 586 | 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 Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 596 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 597 | fileio_read(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 598 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 599 | char *ptr; |
| 600 | Py_ssize_t n; |
| 601 | Py_ssize_t size = -1; |
| 602 | PyObject *bytes; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 603 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 604 | if (self->fd < 0) |
| 605 | return err_closed(); |
| 606 | if (!self->readable) |
| 607 | return err_mode("reading"); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 608 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 609 | if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size)) |
| 610 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 611 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 612 | if (size < 0) { |
| 613 | return fileio_readall(self); |
| 614 | } |
Guido van Rossum | c2f93dc | 2007-05-24 00:50:02 +0000 | [diff] [blame] | 615 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 616 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 617 | if (bytes == NULL) |
| 618 | return NULL; |
| 619 | ptr = PyBytes_AS_STRING(bytes); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 620 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 621 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 628 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 629 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 636 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 637 | if (n != size) { |
| 638 | if (_PyBytes_Resize(&bytes, n) < 0) { |
| 639 | Py_DECREF(bytes); |
| 640 | return NULL; |
| 641 | } |
| 642 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 643 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 644 | return (PyObject *) bytes; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 648 | fileio_write(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 649 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 650 | Py_buffer pbuf; |
| 651 | Py_ssize_t n; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 652 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 653 | if (self->fd < 0) |
| 654 | return err_closed(); |
| 655 | if (!self->writable) |
| 656 | return err_mode("writing"); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 657 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 658 | if (!PyArg_ParseTuple(args, "y*", &pbuf)) |
| 659 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 660 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 661 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 668 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 669 | PyBuffer_Release(&pbuf); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 670 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 671 | if (n < 0) { |
| 672 | if (errno == EAGAIN) |
| 673 | Py_RETURN_NONE; |
| 674 | PyErr_SetFromErrno(PyExc_IOError); |
| 675 | return NULL; |
| 676 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 677 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 678 | return PyLong_FromSsize_t(n); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 681 | /* XXX Windows support below is likely incomplete */ |
| 682 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 683 | /* Cribbed from posix_lseek() */ |
| 684 | static PyObject * |
| 685 | portable_lseek(int fd, PyObject *posobj, int whence) |
| 686 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 687 | Py_off_t pos, res; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 688 | |
| 689 | #ifdef SEEK_SET |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 690 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 691 | switch (whence) { |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 692 | #if SEEK_SET != 0 |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 693 | case 0: whence = SEEK_SET; break; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 694 | #endif |
| 695 | #if SEEK_CUR != 1 |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 696 | case 1: whence = SEEK_CUR; break; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 697 | #endif |
Antoine Pitrou | 4f7945f | 2009-01-20 11:42:11 +0000 | [diff] [blame] | 698 | #if SEEK_END != 2 |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 699 | case 2: whence = SEEK_END; break; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 700 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 701 | } |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 702 | #endif /* SEEK_SET */ |
| 703 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 704 | 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 Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 711 | #if defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 712 | pos = PyLong_AsLongLong(posobj); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 713 | #else |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 714 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 715 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 716 | if (PyErr_Occurred()) |
| 717 | return NULL; |
| 718 | } |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 719 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 720 | if (_PyVerify_fd(fd)) { |
| 721 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 722 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 723 | res = _lseeki64(fd, pos, whence); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 724 | #else |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 725 | res = lseek(fd, pos, whence); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 726 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 727 | Py_END_ALLOW_THREADS |
| 728 | } else |
| 729 | res = -1; |
| 730 | if (res < 0) |
| 731 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 732 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 733 | #if defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 734 | return PyLong_FromLongLong(res); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 735 | #else |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 736 | return PyLong_FromLong(res); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 737 | #endif |
| 738 | } |
| 739 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 740 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 741 | fileio_seek(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 742 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 743 | PyObject *posobj; |
| 744 | int whence = 0; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 745 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 746 | if (self->fd < 0) |
| 747 | return err_closed(); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 748 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 749 | if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence)) |
| 750 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 751 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 752 | return portable_lseek(self->fd, posobj, whence); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 756 | fileio_tell(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 757 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 758 | if (self->fd < 0) |
| 759 | return err_closed(); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 760 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 761 | return portable_lseek(self->fd, NULL, 1); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 764 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 765 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 766 | fileio_truncate(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 767 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 768 | PyObject *posobj = NULL; /* the new size wanted by the user */ |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 769 | #ifndef MS_WINDOWS |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 770 | Py_off_t pos; |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 771 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 772 | int ret; |
| 773 | int fd; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 774 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 775 | fd = self->fd; |
| 776 | if (fd < 0) |
| 777 | return err_closed(); |
| 778 | if (!self->writable) |
| 779 | return err_mode("writing"); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 780 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 781 | if (!PyArg_ParseTuple(args, "|O", &posobj)) |
| 782 | return NULL; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 783 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 784 | 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 Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 793 | |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 794 | #ifdef MS_WINDOWS |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 795 | /* 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 Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 800 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 801 | /* 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 Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 807 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 808 | /* 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 Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 816 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 817 | /* 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 Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 838 | #else |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 839 | |
| 840 | #if defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 841 | pos = PyLong_AsLongLong(posobj); |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 842 | #else |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 843 | pos = PyLong_AsLong(posobj); |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 844 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 845 | if (PyErr_Occurred()){ |
| 846 | Py_DECREF(posobj); |
| 847 | return NULL; |
| 848 | } |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 849 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 850 | Py_BEGIN_ALLOW_THREADS |
| 851 | errno = 0; |
| 852 | ret = ftruncate(fd, pos); |
| 853 | Py_END_ALLOW_THREADS |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 854 | |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 855 | #endif /* !MS_WINDOWS */ |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 856 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 857 | if (ret != 0) { |
| 858 | Py_DECREF(posobj); |
| 859 | PyErr_SetFromErrno(PyExc_IOError); |
| 860 | return NULL; |
| 861 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 862 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 863 | return posobj; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 864 | } |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 865 | #endif /* HAVE_FTRUNCATE */ |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 866 | |
| 867 | static char * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 868 | mode_string(fileio *self) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 869 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 870 | if (self->readable) { |
| 871 | if (self->writable) |
| 872 | return "rb+"; |
| 873 | else |
| 874 | return "rb"; |
| 875 | } |
| 876 | else |
| 877 | return "wb"; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 878 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 879 | |
| 880 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 881 | fileio_repr(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 882 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 883 | PyObject *nameobj, *res; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 884 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 885 | if (self->fd < 0) |
| 886 | return PyUnicode_FromFormat("<_io.FileIO [closed]>"); |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 887 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 888 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 906 | fileio_isatty(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 907 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 908 | long res; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 909 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 910 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 918 | |
| 919 | PyDoc_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 Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 923 | "writing or appending. The file will be created if it doesn't exist\n" |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 924 | "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 | |
| 928 | PyDoc_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 Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 932 | "In non-blocking mode, returns None if no data is available.\n" |
| 933 | "On end-of-file, returns ''."); |
| 934 | |
| 935 | PyDoc_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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 940 | |
| 941 | PyDoc_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 | |
| 947 | PyDoc_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 | |
| 952 | PyDoc_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 Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 963 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 964 | PyDoc_STRVAR(truncate_doc, |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 965 | "truncate([size: int]) -> None. Truncate the file to at most size bytes.\n" |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 966 | "\n" |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 967 | "Size defaults to the current file position, as returned by tell()." |
| 968 | "The current file position is changed to the value of size."); |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 969 | #endif |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 970 | |
| 971 | PyDoc_STRVAR(tell_doc, |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 972 | "tell() -> int. Current file position"); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 973 | |
| 974 | PyDoc_STRVAR(readinto_doc, |
Benjamin Peterson | 9a8082f | 2009-03-05 00:55:56 +0000 | [diff] [blame] | 975 | "readinto() -> Same as RawIOBase.readinto()."); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 976 | |
| 977 | PyDoc_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 | |
| 983 | PyDoc_STRVAR(isatty_doc, |
| 984 | "isatty() -> bool. True if the file is connected to a tty device."); |
| 985 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 986 | PyDoc_STRVAR(seekable_doc, |
| 987 | "seekable() -> bool. True if file supports random-access."); |
| 988 | |
| 989 | PyDoc_STRVAR(readable_doc, |
| 990 | "readable() -> bool. True if file was opened in a read mode."); |
| 991 | |
| 992 | PyDoc_STRVAR(writable_doc, |
| 993 | "writable() -> bool. True if file was opened in a write mode."); |
| 994 | |
| 995 | static PyMethodDef fileio_methods[] = { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 996 | {"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 Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 1002 | #ifdef HAVE_FTRUNCATE |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1003 | {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc}, |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 1004 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1005 | {"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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1012 | }; |
| 1013 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 1014 | /* 'closed' and 'mode' are attributes for backwards compatibility reasons. */ |
| 1015 | |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 1016 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1017 | get_closed(fileio *self, void *closure) |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 1018 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1019 | return PyBool_FromLong((long)(self->fd < 0)); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1023 | get_closefd(fileio *self, void *closure) |
Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 1024 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1025 | return PyBool_FromLong((long)(self->closefd)); |
Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1029 | get_mode(fileio *self, void *closure) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 1030 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1031 | return PyUnicode_FromString(mode_string(self)); |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | static PyGetSetDef fileio_getsetlist[] = { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1035 | {"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 Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 1040 | }; |
| 1041 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1042 | PyTypeObject PyFileIO_Type = { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1043 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1083 | }; |