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, |
| 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 305 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 306 | if (!rwa) |
| 307 | goto bad_mode; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 308 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 309 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 315 | |
| 316 | #ifdef O_BINARY |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 317 | flags |= O_BINARY; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 318 | #endif |
| 319 | |
Walter Dörwald | 0e41148 | 2007-06-06 16:55:38 +0000 | [diff] [blame] | 320 | #ifdef O_APPEND |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 321 | if (append) |
| 322 | flags |= O_APPEND; |
Walter Dörwald | 0e41148 | 2007-06-06 16:55:38 +0000 | [diff] [blame] | 323 | #endif |
| 324 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 325 | 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 Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 338 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 339 | Py_BEGIN_ALLOW_THREADS |
| 340 | errno = 0; |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 341 | #ifdef MS_WINDOWS |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 342 | if (widename != NULL) |
| 343 | self->fd = _wopen(widename, flags, 0666); |
| 344 | else |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 345 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 346 | self->fd = open(name, flags, 0666); |
| 347 | Py_END_ALLOW_THREADS |
| 348 | if (self->fd < 0) { |
Christian Heimes | 0b48954 | 2007-10-31 19:20:48 +0000 | [diff] [blame] | 349 | #ifdef MS_WINDOWS |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 350 | if (widename != NULL) |
| 351 | PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename); |
| 352 | else |
Christian Heimes | 0b48954 | 2007-10-31 19:20:48 +0000 | [diff] [blame] | 353 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 354 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
| 355 | goto error; |
| 356 | } |
| 357 | if(dircheck(self, name) < 0) |
| 358 | goto error; |
| 359 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 360 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 361 | if (PyObject_SetAttrString((PyObject *)self, "name", nameobj) < 0) |
| 362 | goto error; |
Antoine Pitrou | 08838b6 | 2009-01-21 00:55:13 +0000 | [diff] [blame] | 363 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 364 | 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 Pitrou | 7fb111b | 2009-03-04 11:14:01 +0000 | [diff] [blame] | 373 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 374 | goto done; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 375 | |
| 376 | error: |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 377 | ret = -1; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 378 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 379 | done: |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 380 | Py_CLEAR(stringobj); |
| 381 | return ret; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 384 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 385 | fileio_traverse(fileio *self, visitproc visit, void *arg) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 386 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 387 | Py_VISIT(self->dict); |
| 388 | return 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 392 | fileio_clear(fileio *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 393 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 394 | Py_CLEAR(self->dict); |
| 395 | return 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 398 | static void |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 399 | fileio_dealloc(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 400 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 401 | 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 Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | static PyObject * |
| 411 | err_closed(void) |
| 412 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 413 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); |
| 414 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | static PyObject * |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 418 | err_mode(char *action) |
| 419 | { |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 420 | PyErr_Format(IO_STATE->unsupported_operation, |
| 421 | "File not open for %s", action); |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 422 | return NULL; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 426 | fileio_fileno(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 427 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 428 | if (self->fd < 0) |
| 429 | return err_closed(); |
| 430 | return PyLong_FromLong((long) self->fd); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 434 | fileio_readable(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 435 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 436 | if (self->fd < 0) |
| 437 | return err_closed(); |
| 438 | return PyBool_FromLong((long) self->readable); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 442 | fileio_writable(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 443 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 444 | if (self->fd < 0) |
| 445 | return err_closed(); |
| 446 | return PyBool_FromLong((long) self->writable); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 450 | fileio_seekable(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 451 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 452 | if (self->fd < 0) |
| 453 | return err_closed(); |
| 454 | if (self->seekable < 0) { |
| 455 | PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR); |
| 456 | if (pos == NULL) { |
| 457 | PyErr_Clear(); |
| 458 | self->seekable = 0; |
| 459 | } else { |
| 460 | Py_DECREF(pos); |
| 461 | self->seekable = 1; |
| 462 | } |
| 463 | } |
| 464 | return PyBool_FromLong((long) self->seekable); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 468 | fileio_readinto(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 469 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 470 | Py_buffer pbuf; |
| 471 | Py_ssize_t n; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 472 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 473 | if (self->fd < 0) |
| 474 | return err_closed(); |
| 475 | if (!self->readable) |
| 476 | return err_mode("reading"); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 477 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 478 | if (!PyArg_ParseTuple(args, "w*", &pbuf)) |
| 479 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 480 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 481 | if (_PyVerify_fd(self->fd)) { |
| 482 | Py_BEGIN_ALLOW_THREADS |
| 483 | errno = 0; |
| 484 | n = read(self->fd, pbuf.buf, pbuf.len); |
| 485 | Py_END_ALLOW_THREADS |
| 486 | } else |
| 487 | n = -1; |
| 488 | PyBuffer_Release(&pbuf); |
| 489 | if (n < 0) { |
| 490 | if (errno == EAGAIN) |
| 491 | Py_RETURN_NONE; |
| 492 | PyErr_SetFromErrno(PyExc_IOError); |
| 493 | return NULL; |
| 494 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 495 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 496 | return PyLong_FromSsize_t(n); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 499 | static size_t |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 500 | new_buffersize(fileio *self, size_t currentsize) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 501 | { |
| 502 | #ifdef HAVE_FSTAT |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 503 | off_t pos, end; |
| 504 | struct stat st; |
| 505 | if (fstat(self->fd, &st) == 0) { |
| 506 | end = st.st_size; |
| 507 | pos = lseek(self->fd, 0L, SEEK_CUR); |
| 508 | /* Files claiming a size smaller than SMALLCHUNK may |
| 509 | actually be streaming pseudo-files. In this case, we |
| 510 | apply the more aggressive algorithm below. |
| 511 | */ |
| 512 | if (end >= SMALLCHUNK && end >= pos && pos >= 0) { |
| 513 | /* Add 1 so if the file were to grow we'd notice. */ |
| 514 | return currentsize + end - pos + 1; |
| 515 | } |
| 516 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 517 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 518 | if (currentsize > SMALLCHUNK) { |
| 519 | /* Keep doubling until we reach BIGCHUNK; |
| 520 | then keep adding BIGCHUNK. */ |
| 521 | if (currentsize <= BIGCHUNK) |
| 522 | return currentsize + currentsize; |
| 523 | else |
| 524 | return currentsize + BIGCHUNK; |
| 525 | } |
| 526 | return currentsize + SMALLCHUNK; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 529 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 530 | fileio_readall(fileio *self) |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 531 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 532 | PyObject *result; |
| 533 | Py_ssize_t total = 0; |
| 534 | int n; |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 535 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 536 | if (!_PyVerify_fd(self->fd)) |
| 537 | return PyErr_SetFromErrno(PyExc_IOError); |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 538 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 539 | result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK); |
| 540 | if (result == NULL) |
| 541 | return NULL; |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 542 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 543 | while (1) { |
| 544 | size_t newsize = new_buffersize(self, total); |
| 545 | if (newsize > PY_SSIZE_T_MAX || newsize <= 0) { |
| 546 | PyErr_SetString(PyExc_OverflowError, |
| 547 | "unbounded read returned more bytes " |
| 548 | "than a Python string can hold "); |
| 549 | Py_DECREF(result); |
| 550 | return NULL; |
| 551 | } |
Christian Heimes | a872de5 | 2008-12-05 08:26:55 +0000 | [diff] [blame] | 552 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 553 | if (PyBytes_GET_SIZE(result) < (Py_ssize_t)newsize) { |
| 554 | if (_PyBytes_Resize(&result, newsize) < 0) { |
| 555 | if (total == 0) { |
| 556 | Py_DECREF(result); |
| 557 | return NULL; |
| 558 | } |
| 559 | PyErr_Clear(); |
| 560 | break; |
| 561 | } |
| 562 | } |
| 563 | Py_BEGIN_ALLOW_THREADS |
| 564 | errno = 0; |
| 565 | n = read(self->fd, |
| 566 | PyBytes_AS_STRING(result) + total, |
| 567 | newsize - total); |
| 568 | Py_END_ALLOW_THREADS |
| 569 | if (n == 0) |
| 570 | break; |
| 571 | if (n < 0) { |
| 572 | if (total > 0) |
| 573 | break; |
| 574 | if (errno == EAGAIN) { |
| 575 | Py_DECREF(result); |
| 576 | Py_RETURN_NONE; |
| 577 | } |
| 578 | Py_DECREF(result); |
| 579 | PyErr_SetFromErrno(PyExc_IOError); |
| 580 | return NULL; |
| 581 | } |
| 582 | total += n; |
| 583 | } |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 584 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 585 | if (PyBytes_GET_SIZE(result) > total) { |
| 586 | if (_PyBytes_Resize(&result, total) < 0) { |
| 587 | /* This should never happen, but just in case */ |
| 588 | Py_DECREF(result); |
| 589 | return NULL; |
| 590 | } |
| 591 | } |
| 592 | return result; |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 595 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 596 | fileio_read(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 597 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 598 | char *ptr; |
| 599 | Py_ssize_t n; |
| 600 | Py_ssize_t size = -1; |
| 601 | PyObject *bytes; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 602 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 603 | if (self->fd < 0) |
| 604 | return err_closed(); |
| 605 | if (!self->readable) |
| 606 | return err_mode("reading"); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 607 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 608 | if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size)) |
| 609 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 610 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 611 | if (size < 0) { |
| 612 | return fileio_readall(self); |
| 613 | } |
Guido van Rossum | c2f93dc | 2007-05-24 00:50:02 +0000 | [diff] [blame] | 614 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 615 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 616 | if (bytes == NULL) |
| 617 | return NULL; |
| 618 | ptr = PyBytes_AS_STRING(bytes); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 619 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 620 | if (_PyVerify_fd(self->fd)) { |
| 621 | Py_BEGIN_ALLOW_THREADS |
| 622 | errno = 0; |
| 623 | n = read(self->fd, ptr, size); |
| 624 | Py_END_ALLOW_THREADS |
| 625 | } else |
| 626 | n = -1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 627 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 628 | if (n < 0) { |
| 629 | Py_DECREF(bytes); |
| 630 | if (errno == EAGAIN) |
| 631 | Py_RETURN_NONE; |
| 632 | PyErr_SetFromErrno(PyExc_IOError); |
| 633 | return NULL; |
| 634 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 635 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 636 | if (n != size) { |
| 637 | if (_PyBytes_Resize(&bytes, n) < 0) { |
| 638 | Py_DECREF(bytes); |
| 639 | return NULL; |
| 640 | } |
| 641 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 642 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 643 | return (PyObject *) bytes; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 647 | fileio_write(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 648 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 649 | Py_buffer pbuf; |
| 650 | Py_ssize_t n; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 651 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 652 | if (self->fd < 0) |
| 653 | return err_closed(); |
| 654 | if (!self->writable) |
| 655 | return err_mode("writing"); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 656 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 657 | if (!PyArg_ParseTuple(args, "y*", &pbuf)) |
| 658 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 659 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 660 | if (_PyVerify_fd(self->fd)) { |
| 661 | Py_BEGIN_ALLOW_THREADS |
| 662 | errno = 0; |
| 663 | n = write(self->fd, pbuf.buf, pbuf.len); |
| 664 | Py_END_ALLOW_THREADS |
| 665 | } else |
| 666 | n = -1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 667 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 668 | PyBuffer_Release(&pbuf); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 669 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 670 | if (n < 0) { |
| 671 | if (errno == EAGAIN) |
| 672 | Py_RETURN_NONE; |
| 673 | PyErr_SetFromErrno(PyExc_IOError); |
| 674 | return NULL; |
| 675 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 676 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 677 | return PyLong_FromSsize_t(n); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 680 | /* XXX Windows support below is likely incomplete */ |
| 681 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 682 | /* Cribbed from posix_lseek() */ |
| 683 | static PyObject * |
| 684 | portable_lseek(int fd, PyObject *posobj, int whence) |
| 685 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 686 | Py_off_t pos, res; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 687 | |
| 688 | #ifdef SEEK_SET |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 689 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 690 | switch (whence) { |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 691 | #if SEEK_SET != 0 |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 692 | case 0: whence = SEEK_SET; break; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 693 | #endif |
| 694 | #if SEEK_CUR != 1 |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 695 | case 1: whence = SEEK_CUR; break; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 696 | #endif |
Antoine Pitrou | 4f7945f | 2009-01-20 11:42:11 +0000 | [diff] [blame] | 697 | #if SEEK_END != 2 |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 698 | case 2: whence = SEEK_END; break; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 699 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 700 | } |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 701 | #endif /* SEEK_SET */ |
| 702 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 703 | if (posobj == NULL) |
| 704 | pos = 0; |
| 705 | else { |
| 706 | if(PyFloat_Check(posobj)) { |
| 707 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 708 | return NULL; |
| 709 | } |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 710 | #if defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 711 | pos = PyLong_AsLongLong(posobj); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 712 | #else |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 713 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 714 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 715 | if (PyErr_Occurred()) |
| 716 | return NULL; |
| 717 | } |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 718 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 719 | if (_PyVerify_fd(fd)) { |
| 720 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 721 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 722 | res = _lseeki64(fd, pos, whence); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 723 | #else |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 724 | res = lseek(fd, pos, whence); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 725 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 726 | Py_END_ALLOW_THREADS |
| 727 | } else |
| 728 | res = -1; |
| 729 | if (res < 0) |
| 730 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 731 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 732 | #if defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 733 | return PyLong_FromLongLong(res); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 734 | #else |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 735 | return PyLong_FromLong(res); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 736 | #endif |
| 737 | } |
| 738 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 739 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 740 | fileio_seek(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 741 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 742 | PyObject *posobj; |
| 743 | int whence = 0; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 744 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 745 | if (self->fd < 0) |
| 746 | return err_closed(); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 747 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 748 | if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence)) |
| 749 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 750 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 751 | return portable_lseek(self->fd, posobj, whence); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 755 | fileio_tell(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 756 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 757 | if (self->fd < 0) |
| 758 | return err_closed(); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 759 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 760 | return portable_lseek(self->fd, NULL, 1); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 763 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 764 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 765 | fileio_truncate(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 766 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 767 | PyObject *posobj = NULL; /* the new size wanted by the user */ |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 768 | #ifndef MS_WINDOWS |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 769 | Py_off_t pos; |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 770 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 771 | int ret; |
| 772 | int fd; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 773 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 774 | fd = self->fd; |
| 775 | if (fd < 0) |
| 776 | return err_closed(); |
| 777 | if (!self->writable) |
| 778 | return err_mode("writing"); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 779 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 780 | if (!PyArg_ParseTuple(args, "|O", &posobj)) |
| 781 | return NULL; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 782 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 783 | if (posobj == Py_None || posobj == NULL) { |
| 784 | /* Get the current position. */ |
| 785 | posobj = portable_lseek(fd, NULL, 1); |
| 786 | if (posobj == NULL) |
| 787 | return NULL; |
| 788 | } |
| 789 | else { |
| 790 | Py_INCREF(posobj); |
| 791 | } |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 792 | |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 793 | #ifdef MS_WINDOWS |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 794 | /* MS _chsize doesn't work if newsize doesn't fit in 32 bits, |
| 795 | so don't even try using it. */ |
| 796 | { |
| 797 | PyObject *oldposobj, *tempposobj; |
| 798 | HANDLE hFile; |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 799 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 800 | /* we save the file pointer position */ |
| 801 | oldposobj = portable_lseek(fd, NULL, 1); |
| 802 | if (oldposobj == NULL) { |
| 803 | Py_DECREF(posobj); |
| 804 | return NULL; |
| 805 | } |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 806 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 807 | /* we then move to the truncation position */ |
| 808 | tempposobj = portable_lseek(fd, posobj, 0); |
| 809 | if (tempposobj == NULL) { |
| 810 | Py_DECREF(oldposobj); |
| 811 | Py_DECREF(posobj); |
| 812 | return NULL; |
| 813 | } |
| 814 | Py_DECREF(tempposobj); |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 815 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 816 | /* Truncate. Note that this may grow the file! */ |
| 817 | Py_BEGIN_ALLOW_THREADS |
| 818 | errno = 0; |
| 819 | hFile = (HANDLE)_get_osfhandle(fd); |
| 820 | ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */ |
| 821 | if (ret == 0) { |
| 822 | ret = SetEndOfFile(hFile) == 0; |
| 823 | if (ret) |
| 824 | errno = EACCES; |
| 825 | } |
| 826 | Py_END_ALLOW_THREADS |
| 827 | |
| 828 | /* we restore the file pointer position in any case */ |
| 829 | tempposobj = portable_lseek(fd, oldposobj, 0); |
| 830 | Py_DECREF(oldposobj); |
| 831 | if (tempposobj == NULL) { |
| 832 | Py_DECREF(posobj); |
| 833 | return NULL; |
| 834 | } |
| 835 | Py_DECREF(tempposobj); |
| 836 | } |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 837 | #else |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 838 | |
| 839 | #if defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 840 | pos = PyLong_AsLongLong(posobj); |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 841 | #else |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 842 | pos = PyLong_AsLong(posobj); |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 843 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 844 | if (PyErr_Occurred()){ |
| 845 | Py_DECREF(posobj); |
| 846 | return NULL; |
| 847 | } |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 848 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 849 | Py_BEGIN_ALLOW_THREADS |
| 850 | errno = 0; |
| 851 | ret = ftruncate(fd, pos); |
| 852 | Py_END_ALLOW_THREADS |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 853 | |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 854 | #endif /* !MS_WINDOWS */ |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 855 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 856 | if (ret != 0) { |
| 857 | Py_DECREF(posobj); |
| 858 | PyErr_SetFromErrno(PyExc_IOError); |
| 859 | return NULL; |
| 860 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 861 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 862 | return posobj; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 863 | } |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 864 | #endif /* HAVE_FTRUNCATE */ |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 865 | |
| 866 | static char * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 867 | mode_string(fileio *self) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 868 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 869 | if (self->readable) { |
| 870 | if (self->writable) |
| 871 | return "rb+"; |
| 872 | else |
| 873 | return "rb"; |
| 874 | } |
| 875 | else |
| 876 | return "wb"; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 877 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 878 | |
| 879 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 880 | fileio_repr(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 881 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 882 | PyObject *nameobj, *res; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 883 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 884 | if (self->fd < 0) |
| 885 | return PyUnicode_FromFormat("<_io.FileIO [closed]>"); |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 886 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 887 | nameobj = PyObject_GetAttrString((PyObject *) self, "name"); |
| 888 | if (nameobj == NULL) { |
| 889 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 890 | PyErr_Clear(); |
| 891 | else |
| 892 | return NULL; |
| 893 | res = PyUnicode_FromFormat("<_io.FileIO fd=%d mode='%s'>", |
| 894 | self->fd, mode_string(self)); |
| 895 | } |
| 896 | else { |
| 897 | res = PyUnicode_FromFormat("<_io.FileIO name=%R mode='%s'>", |
| 898 | nameobj, mode_string(self)); |
| 899 | Py_DECREF(nameobj); |
| 900 | } |
| 901 | return res; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 905 | fileio_isatty(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 906 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 907 | long res; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 908 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 909 | if (self->fd < 0) |
| 910 | return err_closed(); |
| 911 | Py_BEGIN_ALLOW_THREADS |
| 912 | res = isatty(self->fd); |
| 913 | Py_END_ALLOW_THREADS |
| 914 | return PyBool_FromLong(res); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 917 | |
| 918 | PyDoc_STRVAR(fileio_doc, |
| 919 | "file(name: str[, mode: str]) -> file IO object\n" |
| 920 | "\n" |
| 921 | "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] | 922 | "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] | 923 | "when opened for writing or appending; it will be truncated when\n" |
| 924 | "opened for writing. Add a '+' to the mode to allow simultaneous\n" |
| 925 | "reading and writing."); |
| 926 | |
| 927 | PyDoc_STRVAR(read_doc, |
| 928 | "read(size: int) -> bytes. read at most size bytes, returned as bytes.\n" |
| 929 | "\n" |
| 930 | "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] | 931 | "In non-blocking mode, returns None if no data is available.\n" |
| 932 | "On end-of-file, returns ''."); |
| 933 | |
| 934 | PyDoc_STRVAR(readall_doc, |
| 935 | "readall() -> bytes. read all data from the file, returned as bytes.\n" |
| 936 | "\n" |
| 937 | "In non-blocking mode, returns as much as is immediately available,\n" |
| 938 | "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] | 939 | |
| 940 | PyDoc_STRVAR(write_doc, |
| 941 | "write(b: bytes) -> int. Write bytes b to file, return number written.\n" |
| 942 | "\n" |
| 943 | "Only makes one system call, so not all of the data may be written.\n" |
| 944 | "The number of bytes actually written is returned."); |
| 945 | |
| 946 | PyDoc_STRVAR(fileno_doc, |
| 947 | "fileno() -> int. \"file descriptor\".\n" |
| 948 | "\n" |
| 949 | "This is needed for lower-level file interfaces, such the fcntl module."); |
| 950 | |
| 951 | PyDoc_STRVAR(seek_doc, |
| 952 | "seek(offset: int[, whence: int]) -> None. Move to new file position.\n" |
| 953 | "\n" |
| 954 | "Argument offset is a byte count. Optional argument whence defaults to\n" |
| 955 | "0 (offset from start of file, offset should be >= 0); other values are 1\n" |
| 956 | "(move relative to current position, positive or negative), and 2 (move\n" |
| 957 | "relative to end of file, usually negative, although many platforms allow\n" |
| 958 | "seeking beyond the end of a file)." |
| 959 | "\n" |
| 960 | "Note that not all file objects are seekable."); |
| 961 | |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 962 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 963 | PyDoc_STRVAR(truncate_doc, |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 964 | "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] | 965 | "\n" |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 966 | "Size defaults to the current file position, as returned by tell()." |
| 967 | "The current file position is changed to the value of size."); |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 968 | #endif |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 969 | |
| 970 | PyDoc_STRVAR(tell_doc, |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 971 | "tell() -> int. Current file position"); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 972 | |
| 973 | PyDoc_STRVAR(readinto_doc, |
Benjamin Peterson | 9a8082f | 2009-03-05 00:55:56 +0000 | [diff] [blame] | 974 | "readinto() -> Same as RawIOBase.readinto()."); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 975 | |
| 976 | PyDoc_STRVAR(close_doc, |
| 977 | "close() -> None. Close the file.\n" |
| 978 | "\n" |
| 979 | "A closed file cannot be used for further I/O operations. close() may be\n" |
| 980 | "called more than once without error. Changes the fileno to -1."); |
| 981 | |
| 982 | PyDoc_STRVAR(isatty_doc, |
| 983 | "isatty() -> bool. True if the file is connected to a tty device."); |
| 984 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 985 | PyDoc_STRVAR(seekable_doc, |
| 986 | "seekable() -> bool. True if file supports random-access."); |
| 987 | |
| 988 | PyDoc_STRVAR(readable_doc, |
| 989 | "readable() -> bool. True if file was opened in a read mode."); |
| 990 | |
| 991 | PyDoc_STRVAR(writable_doc, |
| 992 | "writable() -> bool. True if file was opened in a write mode."); |
| 993 | |
| 994 | static PyMethodDef fileio_methods[] = { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 995 | {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc}, |
| 996 | {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc}, |
| 997 | {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc}, |
| 998 | {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc}, |
| 999 | {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc}, |
| 1000 | {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc}, |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 1001 | #ifdef HAVE_FTRUNCATE |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1002 | {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc}, |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 1003 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1004 | {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc}, |
| 1005 | {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc}, |
| 1006 | {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc}, |
| 1007 | {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc}, |
| 1008 | {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc}, |
| 1009 | {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc}, |
| 1010 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1011 | }; |
| 1012 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 1013 | /* 'closed' and 'mode' are attributes for backwards compatibility reasons. */ |
| 1014 | |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 1015 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1016 | get_closed(fileio *self, void *closure) |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 1017 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1018 | return PyBool_FromLong((long)(self->fd < 0)); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1022 | get_closefd(fileio *self, void *closure) |
Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 1023 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1024 | return PyBool_FromLong((long)(self->closefd)); |
Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1028 | get_mode(fileio *self, void *closure) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 1029 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1030 | return PyUnicode_FromString(mode_string(self)); |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | static PyGetSetDef fileio_getsetlist[] = { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1034 | {"closed", (getter)get_closed, NULL, "True if the file is closed"}, |
| 1035 | {"closefd", (getter)get_closefd, NULL, |
| 1036 | "True if the file descriptor will be closed"}, |
| 1037 | {"mode", (getter)get_mode, NULL, "String giving the file mode"}, |
| 1038 | {NULL}, |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 1039 | }; |
| 1040 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1041 | PyTypeObject PyFileIO_Type = { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1042 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1043 | "_io.FileIO", |
| 1044 | sizeof(fileio), |
| 1045 | 0, |
| 1046 | (destructor)fileio_dealloc, /* tp_dealloc */ |
| 1047 | 0, /* tp_print */ |
| 1048 | 0, /* tp_getattr */ |
| 1049 | 0, /* tp_setattr */ |
| 1050 | 0, /* tp_reserved */ |
| 1051 | (reprfunc)fileio_repr, /* tp_repr */ |
| 1052 | 0, /* tp_as_number */ |
| 1053 | 0, /* tp_as_sequence */ |
| 1054 | 0, /* tp_as_mapping */ |
| 1055 | 0, /* tp_hash */ |
| 1056 | 0, /* tp_call */ |
| 1057 | 0, /* tp_str */ |
| 1058 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1059 | 0, /* tp_setattro */ |
| 1060 | 0, /* tp_as_buffer */ |
| 1061 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
| 1062 | | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 1063 | fileio_doc, /* tp_doc */ |
| 1064 | (traverseproc)fileio_traverse, /* tp_traverse */ |
| 1065 | (inquiry)fileio_clear, /* tp_clear */ |
| 1066 | 0, /* tp_richcompare */ |
| 1067 | offsetof(fileio, weakreflist), /* tp_weaklistoffset */ |
| 1068 | 0, /* tp_iter */ |
| 1069 | 0, /* tp_iternext */ |
| 1070 | fileio_methods, /* tp_methods */ |
| 1071 | 0, /* tp_members */ |
| 1072 | fileio_getsetlist, /* tp_getset */ |
| 1073 | 0, /* tp_base */ |
| 1074 | 0, /* tp_dict */ |
| 1075 | 0, /* tp_descr_get */ |
| 1076 | 0, /* tp_descr_set */ |
| 1077 | offsetof(fileio, dict), /* tp_dictoffset */ |
| 1078 | fileio_init, /* tp_init */ |
| 1079 | PyType_GenericAlloc, /* tp_alloc */ |
| 1080 | fileio_new, /* tp_new */ |
| 1081 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1082 | }; |