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" |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 5 | #include "structmember.h" |
Benjamin Peterson | 2614cda | 2010-03-21 22:36:19 +0000 | [diff] [blame] | 6 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 7 | #include <sys/types.h> |
Benjamin Peterson | 2614cda | 2010-03-21 22:36:19 +0000 | [diff] [blame] | 8 | #endif |
| 9 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 10 | #include <sys/stat.h> |
Benjamin Peterson | 2614cda | 2010-03-21 22:36:19 +0000 | [diff] [blame] | 11 | #endif |
| 12 | #ifdef HAVE_FCNTL_H |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 13 | #include <fcntl.h> |
Benjamin Peterson | 2614cda | 2010-03-21 22:36:19 +0000 | [diff] [blame] | 14 | #endif |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 15 | #include <stddef.h> /* For offsetof */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 16 | #include "_iomodule.h" |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * Known likely problems: |
| 20 | * |
| 21 | * - Files larger then 2**32-1 |
| 22 | * - Files with unicode filenames |
| 23 | * - Passing numbers greater than 2**32-1 when an integer is expected |
| 24 | * - Making it work on Windows and other oddball platforms |
| 25 | * |
| 26 | * To Do: |
| 27 | * |
| 28 | * - autoconfify header file inclusion |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | #ifdef MS_WINDOWS |
| 32 | /* can simulate truncate with Win32 API functions; see file_truncate */ |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 33 | #define HAVE_FTRUNCATE |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 34 | #define WIN32_LEAN_AND_MEAN |
| 35 | #include <windows.h> |
| 36 | #endif |
| 37 | |
Christian Heimes | a872de5 | 2008-12-05 08:26:55 +0000 | [diff] [blame] | 38 | #if BUFSIZ < (8*1024) |
| 39 | #define SMALLCHUNK (8*1024) |
| 40 | #elif (BUFSIZ >= (2 << 25)) |
| 41 | #error "unreasonable BUFSIZ > 64MB defined" |
| 42 | #else |
| 43 | #define SMALLCHUNK BUFSIZ |
| 44 | #endif |
| 45 | |
| 46 | #if SIZEOF_INT < 4 |
| 47 | #define BIGCHUNK (512 * 32) |
| 48 | #else |
| 49 | #define BIGCHUNK (512 * 1024) |
| 50 | #endif |
| 51 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 52 | typedef struct { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 53 | PyObject_HEAD |
| 54 | int fd; |
| 55 | unsigned int readable : 1; |
| 56 | unsigned int writable : 1; |
| 57 | signed int seekable : 2; /* -1 means unknown */ |
| 58 | unsigned int closefd : 1; |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 59 | unsigned int deallocating: 1; |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 60 | PyObject *weakreflist; |
| 61 | PyObject *dict; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 62 | } fileio; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 63 | |
Collin Winter | af33438 | 2007-03-08 21:46:15 +0000 | [diff] [blame] | 64 | PyTypeObject PyFileIO_Type; |
| 65 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 66 | #define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type)) |
| 67 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 68 | int |
| 69 | _PyFileIO_closed(PyObject *self) |
| 70 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 71 | return ((fileio *)self)->fd < 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 72 | } |
Antoine Pitrou | 08838b6 | 2009-01-21 00:55:13 +0000 | [diff] [blame] | 73 | |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 74 | /* Because this can call arbitrary code, it shouldn't be called when |
| 75 | the refcount is 0 (that is, not directly from tp_dealloc unless |
| 76 | the refcount has been temporarily re-incremented). */ |
| 77 | static PyObject * |
| 78 | fileio_dealloc_warn(fileio *self, PyObject *source) |
| 79 | { |
| 80 | if (self->fd >= 0 && self->closefd) { |
| 81 | PyObject *exc, *val, *tb; |
| 82 | PyErr_Fetch(&exc, &val, &tb); |
| 83 | if (PyErr_WarnFormat(PyExc_ResourceWarning, 1, |
| 84 | "unclosed file %R", source)) { |
| 85 | /* Spurious errors can appear at shutdown */ |
| 86 | if (PyErr_ExceptionMatches(PyExc_Warning)) |
| 87 | PyErr_WriteUnraisable((PyObject *) self); |
| 88 | } |
| 89 | PyErr_Restore(exc, val, tb); |
| 90 | } |
| 91 | Py_RETURN_NONE; |
| 92 | } |
| 93 | |
Antoine Pitrou | 7fb111b | 2009-03-04 11:14:01 +0000 | [diff] [blame] | 94 | static PyObject * |
| 95 | portable_lseek(int fd, PyObject *posobj, int whence); |
| 96 | |
Antoine Pitrou | a28fcfd | 2009-03-13 23:42:55 +0000 | [diff] [blame] | 97 | static PyObject *portable_lseek(int fd, PyObject *posobj, int whence); |
| 98 | |
Kristján Valur Jónsson | 19288c2 | 2008-12-18 17:15:54 +0000 | [diff] [blame] | 99 | /* Returns 0 on success, -1 with exception set on failure. */ |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 100 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 101 | internal_close(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 102 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 103 | int err = 0; |
| 104 | int save_errno = 0; |
| 105 | if (self->fd >= 0) { |
| 106 | int fd = self->fd; |
| 107 | self->fd = -1; |
| 108 | /* fd is accessible and someone else may have closed it */ |
| 109 | if (_PyVerify_fd(fd)) { |
| 110 | Py_BEGIN_ALLOW_THREADS |
| 111 | err = close(fd); |
| 112 | if (err < 0) |
| 113 | save_errno = errno; |
| 114 | Py_END_ALLOW_THREADS |
| 115 | } else { |
| 116 | save_errno = errno; |
| 117 | err = -1; |
| 118 | } |
| 119 | } |
| 120 | if (err < 0) { |
| 121 | errno = save_errno; |
| 122 | PyErr_SetFromErrno(PyExc_IOError); |
| 123 | return -1; |
| 124 | } |
| 125 | return 0; |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 129 | fileio_close(fileio *self) |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 130 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 131 | if (!self->closefd) { |
| 132 | self->fd = -1; |
| 133 | Py_RETURN_NONE; |
| 134 | } |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 135 | if (self->deallocating) { |
| 136 | PyObject *r = fileio_dealloc_warn(self, (PyObject *) self); |
| 137 | if (r) |
| 138 | Py_DECREF(r); |
| 139 | else |
| 140 | PyErr_Clear(); |
| 141 | } |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 142 | errno = internal_close(self); |
| 143 | if (errno < 0) |
| 144 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 145 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 146 | return PyObject_CallMethod((PyObject*)&PyRawIOBase_Type, |
| 147 | "close", "O", self); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static PyObject * |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 151 | fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 152 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 153 | fileio *self; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 154 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 155 | assert(type != NULL && type->tp_alloc != NULL); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 156 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 157 | self = (fileio *) type->tp_alloc(type, 0); |
| 158 | if (self != NULL) { |
| 159 | self->fd = -1; |
| 160 | self->readable = 0; |
| 161 | self->writable = 0; |
| 162 | self->seekable = -1; |
| 163 | self->closefd = 1; |
| 164 | self->weakreflist = NULL; |
| 165 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 166 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 167 | return (PyObject *) self; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* On Unix, open will succeed for directories. |
| 171 | In Python, there should be no file objects referring to |
| 172 | directories, so we need a check. */ |
| 173 | |
| 174 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 175 | dircheck(fileio* self, const char *name) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 176 | { |
| 177 | #if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR) |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 178 | struct stat buf; |
| 179 | if (self->fd < 0) |
| 180 | return 0; |
| 181 | if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) { |
| 182 | char *msg = strerror(EISDIR); |
| 183 | PyObject *exc; |
| 184 | if (internal_close(self)) |
| 185 | return -1; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 186 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 187 | exc = PyObject_CallFunction(PyExc_IOError, "(iss)", |
| 188 | EISDIR, msg, name); |
| 189 | PyErr_SetObject(PyExc_IOError, exc); |
| 190 | Py_XDECREF(exc); |
| 191 | return -1; |
| 192 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 193 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 194 | return 0; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Benjamin Peterson | 806d402 | 2009-01-19 15:11:51 +0000 | [diff] [blame] | 197 | static int |
| 198 | check_fd(int fd) |
| 199 | { |
| 200 | #if defined(HAVE_FSTAT) |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 201 | struct stat buf; |
| 202 | if (!_PyVerify_fd(fd) || (fstat(fd, &buf) < 0 && errno == EBADF)) { |
| 203 | PyObject *exc; |
| 204 | char *msg = strerror(EBADF); |
| 205 | exc = PyObject_CallFunction(PyExc_OSError, "(is)", |
| 206 | EBADF, msg); |
| 207 | PyErr_SetObject(PyExc_OSError, exc); |
| 208 | Py_XDECREF(exc); |
| 209 | return -1; |
| 210 | } |
Benjamin Peterson | 806d402 | 2009-01-19 15:11:51 +0000 | [diff] [blame] | 211 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 212 | return 0; |
Benjamin Peterson | 806d402 | 2009-01-19 15:11:51 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 215 | |
| 216 | static int |
| 217 | fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) |
| 218 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 219 | fileio *self = (fileio *) oself; |
| 220 | static char *kwlist[] = {"file", "mode", "closefd", NULL}; |
| 221 | const char *name = NULL; |
| 222 | PyObject *nameobj, *stringobj = NULL; |
| 223 | char *mode = "r"; |
| 224 | char *s; |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 225 | #ifdef MS_WINDOWS |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 226 | Py_UNICODE *widename = NULL; |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 227 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 228 | int ret = 0; |
| 229 | int rwa = 0, plus = 0, append = 0; |
| 230 | int flags = 0; |
| 231 | int fd = -1; |
| 232 | int closefd = 1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 233 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 234 | assert(PyFileIO_Check(oself)); |
| 235 | if (self->fd >= 0) { |
| 236 | /* Have to close the existing file first. */ |
| 237 | if (internal_close(self) < 0) |
| 238 | return -1; |
| 239 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 240 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 241 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:fileio", |
| 242 | kwlist, &nameobj, &mode, &closefd)) |
| 243 | return -1; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 244 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 245 | if (PyFloat_Check(nameobj)) { |
| 246 | PyErr_SetString(PyExc_TypeError, |
| 247 | "integer argument expected, got float"); |
| 248 | return -1; |
| 249 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 250 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 251 | fd = PyLong_AsLong(nameobj); |
| 252 | if (fd < 0) { |
| 253 | if (!PyErr_Occurred()) { |
| 254 | PyErr_SetString(PyExc_ValueError, |
| 255 | "Negative filedescriptor"); |
| 256 | return -1; |
| 257 | } |
| 258 | PyErr_Clear(); |
| 259 | } |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 260 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 261 | #ifdef MS_WINDOWS |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 262 | if (PyUnicode_Check(nameobj)) |
| 263 | widename = PyUnicode_AS_UNICODE(nameobj); |
| 264 | if (widename == NULL) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 265 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 266 | if (fd < 0) |
| 267 | { |
| 268 | if (PyBytes_Check(nameobj) || PyByteArray_Check(nameobj)) { |
| 269 | Py_ssize_t namelen; |
| 270 | if (PyObject_AsCharBuffer(nameobj, &name, &namelen) < 0) |
| 271 | return -1; |
| 272 | } |
| 273 | else { |
| 274 | PyObject *u = PyUnicode_FromObject(nameobj); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 275 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 276 | if (u == NULL) |
| 277 | return -1; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 278 | |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 279 | stringobj = PyUnicode_EncodeFSDefault(u); |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 280 | Py_DECREF(u); |
| 281 | if (stringobj == NULL) |
| 282 | return -1; |
| 283 | if (!PyBytes_Check(stringobj)) { |
| 284 | PyErr_SetString(PyExc_TypeError, |
| 285 | "encoder failed to return bytes"); |
| 286 | goto error; |
| 287 | } |
| 288 | name = PyBytes_AS_STRING(stringobj); |
| 289 | } |
| 290 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 291 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 292 | s = mode; |
| 293 | while (*s) { |
| 294 | switch (*s++) { |
| 295 | case 'r': |
| 296 | if (rwa) { |
| 297 | bad_mode: |
| 298 | PyErr_SetString(PyExc_ValueError, |
Georg Brandl | 28928ae | 2010-10-21 13:45:52 +0000 | [diff] [blame] | 299 | "Must have exactly one of read/write/append " |
| 300 | "mode and at most one plus"); |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 301 | goto error; |
| 302 | } |
| 303 | rwa = 1; |
| 304 | self->readable = 1; |
| 305 | break; |
| 306 | case 'w': |
| 307 | if (rwa) |
| 308 | goto bad_mode; |
| 309 | rwa = 1; |
| 310 | self->writable = 1; |
| 311 | flags |= O_CREAT | O_TRUNC; |
| 312 | break; |
| 313 | case 'a': |
| 314 | if (rwa) |
| 315 | goto bad_mode; |
| 316 | rwa = 1; |
| 317 | self->writable = 1; |
| 318 | flags |= O_CREAT; |
| 319 | append = 1; |
| 320 | break; |
| 321 | case 'b': |
| 322 | break; |
| 323 | case '+': |
| 324 | if (plus) |
| 325 | goto bad_mode; |
| 326 | self->readable = self->writable = 1; |
| 327 | plus = 1; |
| 328 | break; |
| 329 | default: |
| 330 | PyErr_Format(PyExc_ValueError, |
| 331 | "invalid mode: %.200s", mode); |
| 332 | goto error; |
| 333 | } |
| 334 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 335 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 336 | if (!rwa) |
| 337 | goto bad_mode; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 338 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 339 | if (self->readable && self->writable) |
| 340 | flags |= O_RDWR; |
| 341 | else if (self->readable) |
| 342 | flags |= O_RDONLY; |
| 343 | else |
| 344 | flags |= O_WRONLY; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 345 | |
| 346 | #ifdef O_BINARY |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 347 | flags |= O_BINARY; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 348 | #endif |
| 349 | |
Walter Dörwald | 0e41148 | 2007-06-06 16:55:38 +0000 | [diff] [blame] | 350 | #ifdef O_APPEND |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 351 | if (append) |
| 352 | flags |= O_APPEND; |
Walter Dörwald | 0e41148 | 2007-06-06 16:55:38 +0000 | [diff] [blame] | 353 | #endif |
| 354 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 355 | if (fd >= 0) { |
| 356 | if (check_fd(fd)) |
| 357 | goto error; |
| 358 | self->fd = fd; |
| 359 | self->closefd = closefd; |
| 360 | } |
| 361 | else { |
| 362 | self->closefd = 1; |
| 363 | if (!closefd) { |
| 364 | PyErr_SetString(PyExc_ValueError, |
| 365 | "Cannot use closefd=False with file name"); |
| 366 | goto error; |
| 367 | } |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 368 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 369 | Py_BEGIN_ALLOW_THREADS |
| 370 | errno = 0; |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 371 | #ifdef MS_WINDOWS |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 372 | if (widename != NULL) |
| 373 | self->fd = _wopen(widename, flags, 0666); |
| 374 | else |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 375 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 376 | self->fd = open(name, flags, 0666); |
| 377 | Py_END_ALLOW_THREADS |
| 378 | if (self->fd < 0) { |
Christian Heimes | 0b48954 | 2007-10-31 19:20:48 +0000 | [diff] [blame] | 379 | #ifdef MS_WINDOWS |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 380 | if (widename != NULL) |
| 381 | PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename); |
| 382 | else |
Christian Heimes | 0b48954 | 2007-10-31 19:20:48 +0000 | [diff] [blame] | 383 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 384 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
| 385 | goto error; |
| 386 | } |
Benjamin Peterson | 430d469 | 2010-10-30 23:13:57 +0000 | [diff] [blame] | 387 | if (dircheck(self, name) < 0) |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 388 | goto error; |
| 389 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 390 | |
Victor Stinner | 89e3436 | 2011-01-07 18:47:22 +0000 | [diff] [blame] | 391 | #if defined(MS_WINDOWS) || defined(__CYGWIN__) |
| 392 | /* don't translate newlines (\r\n <=> \n) */ |
| 393 | _setmode(self->fd, O_BINARY); |
| 394 | #endif |
| 395 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 396 | if (PyObject_SetAttrString((PyObject *)self, "name", nameobj) < 0) |
| 397 | goto error; |
Antoine Pitrou | 08838b6 | 2009-01-21 00:55:13 +0000 | [diff] [blame] | 398 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 399 | if (append) { |
| 400 | /* For consistent behaviour, we explicitly seek to the |
| 401 | end of file (otherwise, it might be done only on the |
| 402 | first write()). */ |
| 403 | PyObject *pos = portable_lseek(self->fd, NULL, 2); |
Antoine Pitrou | 8d2b51b | 2010-10-30 16:19:14 +0000 | [diff] [blame] | 404 | if (pos == NULL) { |
| 405 | if (closefd) { |
| 406 | close(self->fd); |
| 407 | self->fd = -1; |
| 408 | } |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 409 | goto error; |
Antoine Pitrou | 8d2b51b | 2010-10-30 16:19:14 +0000 | [diff] [blame] | 410 | } |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 411 | Py_DECREF(pos); |
| 412 | } |
Antoine Pitrou | 7fb111b | 2009-03-04 11:14:01 +0000 | [diff] [blame] | 413 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 414 | goto done; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 415 | |
| 416 | error: |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 417 | ret = -1; |
Benjamin Peterson | bbb0412 | 2010-10-30 23:16:28 +0000 | [diff] [blame] | 418 | if (self->fd >= 0) |
| 419 | internal_close(self); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 420 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 421 | done: |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 422 | Py_CLEAR(stringobj); |
| 423 | return ret; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 426 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 427 | fileio_traverse(fileio *self, visitproc visit, void *arg) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 428 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 429 | Py_VISIT(self->dict); |
| 430 | return 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 434 | fileio_clear(fileio *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 435 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 436 | Py_CLEAR(self->dict); |
| 437 | return 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 440 | static void |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 441 | fileio_dealloc(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 442 | { |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 443 | self->deallocating = 1; |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 444 | if (_PyIOBase_finalize((PyObject *) self) < 0) |
| 445 | return; |
| 446 | _PyObject_GC_UNTRACK(self); |
| 447 | if (self->weakreflist != NULL) |
| 448 | PyObject_ClearWeakRefs((PyObject *) self); |
| 449 | Py_CLEAR(self->dict); |
| 450 | Py_TYPE(self)->tp_free((PyObject *)self); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static PyObject * |
| 454 | err_closed(void) |
| 455 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 456 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); |
| 457 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | static PyObject * |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 461 | err_mode(char *action) |
| 462 | { |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 463 | PyErr_Format(IO_STATE->unsupported_operation, |
| 464 | "File not open for %s", action); |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 465 | return NULL; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 469 | fileio_fileno(fileio *self) |
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 | if (self->fd < 0) |
| 472 | return err_closed(); |
| 473 | return PyLong_FromLong((long) self->fd); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 477 | fileio_readable(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 478 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 479 | if (self->fd < 0) |
| 480 | return err_closed(); |
| 481 | return PyBool_FromLong((long) self->readable); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 485 | fileio_writable(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 486 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 487 | if (self->fd < 0) |
| 488 | return err_closed(); |
| 489 | return PyBool_FromLong((long) self->writable); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 493 | fileio_seekable(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 494 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 495 | if (self->fd < 0) |
| 496 | return err_closed(); |
| 497 | if (self->seekable < 0) { |
| 498 | PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR); |
| 499 | if (pos == NULL) { |
| 500 | PyErr_Clear(); |
| 501 | self->seekable = 0; |
| 502 | } else { |
| 503 | Py_DECREF(pos); |
| 504 | self->seekable = 1; |
| 505 | } |
| 506 | } |
| 507 | return PyBool_FromLong((long) self->seekable); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 511 | fileio_readinto(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 512 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 513 | Py_buffer pbuf; |
Victor Stinner | e6edec2 | 2011-01-04 00:29:35 +0000 | [diff] [blame] | 514 | Py_ssize_t n, len; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 515 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 516 | if (self->fd < 0) |
| 517 | return err_closed(); |
| 518 | if (!self->readable) |
| 519 | return err_mode("reading"); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 520 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 521 | if (!PyArg_ParseTuple(args, "w*", &pbuf)) |
| 522 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 523 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 524 | if (_PyVerify_fd(self->fd)) { |
Victor Stinner | e6edec2 | 2011-01-04 00:29:35 +0000 | [diff] [blame] | 525 | len = pbuf.len; |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 526 | Py_BEGIN_ALLOW_THREADS |
| 527 | errno = 0; |
Victor Stinner | e6edec2 | 2011-01-04 00:29:35 +0000 | [diff] [blame] | 528 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 529 | if (len > INT_MAX) |
| 530 | len = INT_MAX; |
| 531 | n = read(self->fd, pbuf.buf, (int)len); |
| 532 | #else |
Victor Stinner | 7234479 | 2011-01-11 00:04:12 +0000 | [diff] [blame] | 533 | n = read(self->fd, pbuf.buf, len); |
Victor Stinner | e6edec2 | 2011-01-04 00:29:35 +0000 | [diff] [blame] | 534 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 535 | Py_END_ALLOW_THREADS |
| 536 | } else |
| 537 | n = -1; |
| 538 | PyBuffer_Release(&pbuf); |
| 539 | if (n < 0) { |
| 540 | if (errno == EAGAIN) |
| 541 | Py_RETURN_NONE; |
| 542 | PyErr_SetFromErrno(PyExc_IOError); |
| 543 | return NULL; |
| 544 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 545 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 546 | return PyLong_FromSsize_t(n); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 549 | static size_t |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 550 | new_buffersize(fileio *self, size_t currentsize) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 551 | { |
| 552 | #ifdef HAVE_FSTAT |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 553 | off_t pos, end; |
| 554 | struct stat st; |
| 555 | if (fstat(self->fd, &st) == 0) { |
| 556 | end = st.st_size; |
| 557 | pos = lseek(self->fd, 0L, SEEK_CUR); |
| 558 | /* Files claiming a size smaller than SMALLCHUNK may |
| 559 | actually be streaming pseudo-files. In this case, we |
| 560 | apply the more aggressive algorithm below. |
| 561 | */ |
| 562 | if (end >= SMALLCHUNK && end >= pos && pos >= 0) { |
| 563 | /* Add 1 so if the file were to grow we'd notice. */ |
| 564 | return currentsize + end - pos + 1; |
| 565 | } |
| 566 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 567 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 568 | if (currentsize > SMALLCHUNK) { |
| 569 | /* Keep doubling until we reach BIGCHUNK; |
| 570 | then keep adding BIGCHUNK. */ |
| 571 | if (currentsize <= BIGCHUNK) |
| 572 | return currentsize + currentsize; |
| 573 | else |
| 574 | return currentsize + BIGCHUNK; |
| 575 | } |
| 576 | return currentsize + SMALLCHUNK; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 579 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 580 | fileio_readall(fileio *self) |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 581 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 582 | PyObject *result; |
| 583 | Py_ssize_t total = 0; |
| 584 | int n; |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 585 | |
Victor Stinner | b79f28c | 2011-05-25 22:09:03 +0200 | [diff] [blame] | 586 | if (self->fd < 0) |
| 587 | return err_closed(); |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 588 | if (!_PyVerify_fd(self->fd)) |
| 589 | return PyErr_SetFromErrno(PyExc_IOError); |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 590 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 591 | result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK); |
| 592 | if (result == NULL) |
| 593 | return NULL; |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 594 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 595 | while (1) { |
| 596 | size_t newsize = new_buffersize(self, total); |
| 597 | if (newsize > PY_SSIZE_T_MAX || newsize <= 0) { |
| 598 | PyErr_SetString(PyExc_OverflowError, |
| 599 | "unbounded read returned more bytes " |
| 600 | "than a Python string can hold "); |
| 601 | Py_DECREF(result); |
| 602 | return NULL; |
| 603 | } |
Christian Heimes | a872de5 | 2008-12-05 08:26:55 +0000 | [diff] [blame] | 604 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 605 | if (PyBytes_GET_SIZE(result) < (Py_ssize_t)newsize) { |
| 606 | if (_PyBytes_Resize(&result, newsize) < 0) { |
| 607 | if (total == 0) { |
| 608 | Py_DECREF(result); |
| 609 | return NULL; |
| 610 | } |
| 611 | PyErr_Clear(); |
| 612 | break; |
| 613 | } |
| 614 | } |
| 615 | Py_BEGIN_ALLOW_THREADS |
| 616 | errno = 0; |
| 617 | n = read(self->fd, |
| 618 | PyBytes_AS_STRING(result) + total, |
| 619 | newsize - total); |
| 620 | Py_END_ALLOW_THREADS |
| 621 | if (n == 0) |
| 622 | break; |
| 623 | if (n < 0) { |
| 624 | if (total > 0) |
| 625 | break; |
| 626 | if (errno == EAGAIN) { |
| 627 | Py_DECREF(result); |
| 628 | Py_RETURN_NONE; |
| 629 | } |
| 630 | Py_DECREF(result); |
| 631 | PyErr_SetFromErrno(PyExc_IOError); |
| 632 | return NULL; |
| 633 | } |
| 634 | total += n; |
| 635 | } |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 636 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 637 | if (PyBytes_GET_SIZE(result) > total) { |
| 638 | if (_PyBytes_Resize(&result, total) < 0) { |
| 639 | /* This should never happen, but just in case */ |
| 640 | Py_DECREF(result); |
| 641 | return NULL; |
| 642 | } |
| 643 | } |
| 644 | return result; |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 647 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 648 | fileio_read(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 | char *ptr; |
| 651 | Py_ssize_t n; |
| 652 | Py_ssize_t size = -1; |
| 653 | PyObject *bytes; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 654 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 655 | if (self->fd < 0) |
| 656 | return err_closed(); |
| 657 | if (!self->readable) |
| 658 | return err_mode("reading"); |
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 (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size)) |
| 661 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 662 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 663 | if (size < 0) { |
| 664 | return fileio_readall(self); |
| 665 | } |
Guido van Rossum | c2f93dc | 2007-05-24 00:50:02 +0000 | [diff] [blame] | 666 | |
Victor Stinner | c655a72 | 2011-07-05 11:31:49 +0200 | [diff] [blame^] | 667 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 668 | if (size > INT_MAX) |
| 669 | size = INT_MAX; |
| 670 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 671 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 672 | if (bytes == NULL) |
| 673 | return NULL; |
| 674 | ptr = PyBytes_AS_STRING(bytes); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 675 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 676 | if (_PyVerify_fd(self->fd)) { |
| 677 | Py_BEGIN_ALLOW_THREADS |
| 678 | errno = 0; |
Victor Stinner | c655a72 | 2011-07-05 11:31:49 +0200 | [diff] [blame^] | 679 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 680 | n = read(self->fd, ptr, (int)size); |
| 681 | #else |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 682 | n = read(self->fd, ptr, size); |
Victor Stinner | c655a72 | 2011-07-05 11:31:49 +0200 | [diff] [blame^] | 683 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 684 | Py_END_ALLOW_THREADS |
| 685 | } else |
| 686 | n = -1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 687 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 688 | if (n < 0) { |
| 689 | Py_DECREF(bytes); |
| 690 | if (errno == EAGAIN) |
| 691 | Py_RETURN_NONE; |
| 692 | PyErr_SetFromErrno(PyExc_IOError); |
| 693 | return NULL; |
| 694 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 695 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 696 | if (n != size) { |
| 697 | if (_PyBytes_Resize(&bytes, n) < 0) { |
| 698 | Py_DECREF(bytes); |
| 699 | return NULL; |
| 700 | } |
| 701 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 702 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 703 | return (PyObject *) bytes; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 707 | fileio_write(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 708 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 709 | Py_buffer pbuf; |
Victor Stinner | e6edec2 | 2011-01-04 00:29:35 +0000 | [diff] [blame] | 710 | Py_ssize_t n, len; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 711 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 712 | if (self->fd < 0) |
| 713 | return err_closed(); |
| 714 | if (!self->writable) |
| 715 | return err_mode("writing"); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 716 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 717 | if (!PyArg_ParseTuple(args, "y*", &pbuf)) |
| 718 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 719 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 720 | if (_PyVerify_fd(self->fd)) { |
| 721 | Py_BEGIN_ALLOW_THREADS |
| 722 | errno = 0; |
Victor Stinner | e6edec2 | 2011-01-04 00:29:35 +0000 | [diff] [blame] | 723 | len = pbuf.len; |
| 724 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | e0daff1 | 2011-03-20 23:36:35 +0100 | [diff] [blame] | 725 | if (len > 32767 && isatty(self->fd)) { |
| 726 | /* Issue #11395: the Windows console returns an error (12: not |
| 727 | enough space error) on writing into stdout if stdout mode is |
| 728 | binary and the length is greater than 66,000 bytes (or less, |
| 729 | depending on heap usage). */ |
| 730 | len = 32767; |
| 731 | } |
| 732 | else if (len > INT_MAX) |
Victor Stinner | e6edec2 | 2011-01-04 00:29:35 +0000 | [diff] [blame] | 733 | len = INT_MAX; |
| 734 | n = write(self->fd, pbuf.buf, (int)len); |
| 735 | #else |
Victor Stinner | 7234479 | 2011-01-11 00:04:12 +0000 | [diff] [blame] | 736 | n = write(self->fd, pbuf.buf, len); |
Victor Stinner | e6edec2 | 2011-01-04 00:29:35 +0000 | [diff] [blame] | 737 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 738 | Py_END_ALLOW_THREADS |
| 739 | } else |
| 740 | n = -1; |
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 | PyBuffer_Release(&pbuf); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 743 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 744 | if (n < 0) { |
| 745 | if (errno == EAGAIN) |
| 746 | Py_RETURN_NONE; |
| 747 | PyErr_SetFromErrno(PyExc_IOError); |
| 748 | return NULL; |
| 749 | } |
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 PyLong_FromSsize_t(n); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 754 | /* XXX Windows support below is likely incomplete */ |
| 755 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 756 | /* Cribbed from posix_lseek() */ |
| 757 | static PyObject * |
| 758 | portable_lseek(int fd, PyObject *posobj, int whence) |
| 759 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 760 | Py_off_t pos, res; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 761 | |
| 762 | #ifdef SEEK_SET |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 763 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 764 | switch (whence) { |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 765 | #if SEEK_SET != 0 |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 766 | case 0: whence = SEEK_SET; break; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 767 | #endif |
| 768 | #if SEEK_CUR != 1 |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 769 | case 1: whence = SEEK_CUR; break; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 770 | #endif |
Antoine Pitrou | 4f7945f | 2009-01-20 11:42:11 +0000 | [diff] [blame] | 771 | #if SEEK_END != 2 |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 772 | case 2: whence = SEEK_END; break; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 773 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 774 | } |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 775 | #endif /* SEEK_SET */ |
| 776 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 777 | if (posobj == NULL) |
| 778 | pos = 0; |
| 779 | else { |
| 780 | if(PyFloat_Check(posobj)) { |
| 781 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 782 | return NULL; |
| 783 | } |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 784 | #if defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 785 | pos = PyLong_AsLongLong(posobj); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 786 | #else |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 787 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 788 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 789 | if (PyErr_Occurred()) |
| 790 | return NULL; |
| 791 | } |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 792 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 793 | if (_PyVerify_fd(fd)) { |
| 794 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 795 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 796 | res = _lseeki64(fd, pos, whence); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 797 | #else |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 798 | res = lseek(fd, pos, whence); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 799 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 800 | Py_END_ALLOW_THREADS |
| 801 | } else |
| 802 | res = -1; |
| 803 | if (res < 0) |
| 804 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 805 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 806 | #if defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 807 | return PyLong_FromLongLong(res); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 808 | #else |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 809 | return PyLong_FromLong(res); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 810 | #endif |
| 811 | } |
| 812 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 813 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 814 | fileio_seek(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 815 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 816 | PyObject *posobj; |
| 817 | int whence = 0; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 818 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 819 | if (self->fd < 0) |
| 820 | return err_closed(); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 821 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 822 | if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence)) |
| 823 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 824 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 825 | return portable_lseek(self->fd, posobj, whence); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 829 | fileio_tell(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 830 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 831 | if (self->fd < 0) |
| 832 | return err_closed(); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 833 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 834 | return portable_lseek(self->fd, NULL, 1); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 837 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 838 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 839 | fileio_truncate(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 840 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 841 | PyObject *posobj = NULL; /* the new size wanted by the user */ |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 842 | #ifndef MS_WINDOWS |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 843 | Py_off_t pos; |
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 | int ret; |
| 846 | int fd; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 847 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 848 | fd = self->fd; |
| 849 | if (fd < 0) |
| 850 | return err_closed(); |
| 851 | if (!self->writable) |
| 852 | return err_mode("writing"); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 853 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 854 | if (!PyArg_ParseTuple(args, "|O", &posobj)) |
| 855 | return NULL; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 856 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 857 | if (posobj == Py_None || posobj == NULL) { |
| 858 | /* Get the current position. */ |
| 859 | posobj = portable_lseek(fd, NULL, 1); |
| 860 | if (posobj == NULL) |
| 861 | return NULL; |
| 862 | } |
| 863 | else { |
| 864 | Py_INCREF(posobj); |
| 865 | } |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 866 | |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 867 | #ifdef MS_WINDOWS |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 868 | /* MS _chsize doesn't work if newsize doesn't fit in 32 bits, |
| 869 | so don't even try using it. */ |
| 870 | { |
| 871 | PyObject *oldposobj, *tempposobj; |
| 872 | HANDLE hFile; |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 873 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 874 | /* we save the file pointer position */ |
| 875 | oldposobj = portable_lseek(fd, NULL, 1); |
| 876 | if (oldposobj == NULL) { |
| 877 | Py_DECREF(posobj); |
| 878 | return NULL; |
| 879 | } |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 880 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 881 | /* we then move to the truncation position */ |
| 882 | tempposobj = portable_lseek(fd, posobj, 0); |
| 883 | if (tempposobj == NULL) { |
| 884 | Py_DECREF(oldposobj); |
| 885 | Py_DECREF(posobj); |
| 886 | return NULL; |
| 887 | } |
| 888 | Py_DECREF(tempposobj); |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 889 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 890 | /* Truncate. Note that this may grow the file! */ |
| 891 | Py_BEGIN_ALLOW_THREADS |
| 892 | errno = 0; |
| 893 | hFile = (HANDLE)_get_osfhandle(fd); |
| 894 | ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */ |
| 895 | if (ret == 0) { |
| 896 | ret = SetEndOfFile(hFile) == 0; |
| 897 | if (ret) |
| 898 | errno = EACCES; |
| 899 | } |
| 900 | Py_END_ALLOW_THREADS |
| 901 | |
| 902 | /* we restore the file pointer position in any case */ |
| 903 | tempposobj = portable_lseek(fd, oldposobj, 0); |
| 904 | Py_DECREF(oldposobj); |
| 905 | if (tempposobj == NULL) { |
| 906 | Py_DECREF(posobj); |
| 907 | return NULL; |
| 908 | } |
| 909 | Py_DECREF(tempposobj); |
| 910 | } |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 911 | #else |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 912 | |
| 913 | #if defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 914 | pos = PyLong_AsLongLong(posobj); |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 915 | #else |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 916 | pos = PyLong_AsLong(posobj); |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 917 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 918 | if (PyErr_Occurred()){ |
| 919 | Py_DECREF(posobj); |
| 920 | return NULL; |
| 921 | } |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 922 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 923 | Py_BEGIN_ALLOW_THREADS |
| 924 | errno = 0; |
| 925 | ret = ftruncate(fd, pos); |
| 926 | Py_END_ALLOW_THREADS |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 927 | |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 928 | #endif /* !MS_WINDOWS */ |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 929 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 930 | if (ret != 0) { |
| 931 | Py_DECREF(posobj); |
| 932 | PyErr_SetFromErrno(PyExc_IOError); |
| 933 | return NULL; |
| 934 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 935 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 936 | return posobj; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 937 | } |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 938 | #endif /* HAVE_FTRUNCATE */ |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 939 | |
| 940 | static char * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 941 | mode_string(fileio *self) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 942 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 943 | if (self->readable) { |
| 944 | if (self->writable) |
| 945 | return "rb+"; |
| 946 | else |
| 947 | return "rb"; |
| 948 | } |
| 949 | else |
| 950 | return "wb"; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 951 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 952 | |
| 953 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 954 | fileio_repr(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 955 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 956 | PyObject *nameobj, *res; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 957 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 958 | if (self->fd < 0) |
| 959 | return PyUnicode_FromFormat("<_io.FileIO [closed]>"); |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 960 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 961 | nameobj = PyObject_GetAttrString((PyObject *) self, "name"); |
| 962 | if (nameobj == NULL) { |
| 963 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 964 | PyErr_Clear(); |
| 965 | else |
| 966 | return NULL; |
| 967 | res = PyUnicode_FromFormat("<_io.FileIO fd=%d mode='%s'>", |
| 968 | self->fd, mode_string(self)); |
| 969 | } |
| 970 | else { |
| 971 | res = PyUnicode_FromFormat("<_io.FileIO name=%R mode='%s'>", |
| 972 | nameobj, mode_string(self)); |
| 973 | Py_DECREF(nameobj); |
| 974 | } |
| 975 | return res; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 979 | fileio_isatty(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 980 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 981 | long res; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 982 | |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 983 | if (self->fd < 0) |
| 984 | return err_closed(); |
| 985 | Py_BEGIN_ALLOW_THREADS |
| 986 | res = isatty(self->fd); |
| 987 | Py_END_ALLOW_THREADS |
| 988 | return PyBool_FromLong(res); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Antoine Pitrou | 243757e | 2010-11-05 21:15:39 +0000 | [diff] [blame] | 991 | static PyObject * |
| 992 | fileio_getstate(fileio *self) |
| 993 | { |
| 994 | PyErr_Format(PyExc_TypeError, |
| 995 | "cannot serialize '%s' object", Py_TYPE(self)->tp_name); |
| 996 | return NULL; |
| 997 | } |
| 998 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 999 | |
| 1000 | PyDoc_STRVAR(fileio_doc, |
| 1001 | "file(name: str[, mode: str]) -> file IO object\n" |
| 1002 | "\n" |
| 1003 | "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] | 1004 | "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] | 1005 | "when opened for writing or appending; it will be truncated when\n" |
| 1006 | "opened for writing. Add a '+' to the mode to allow simultaneous\n" |
| 1007 | "reading and writing."); |
| 1008 | |
| 1009 | PyDoc_STRVAR(read_doc, |
| 1010 | "read(size: int) -> bytes. read at most size bytes, returned as bytes.\n" |
| 1011 | "\n" |
| 1012 | "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] | 1013 | "In non-blocking mode, returns None if no data is available.\n" |
| 1014 | "On end-of-file, returns ''."); |
| 1015 | |
| 1016 | PyDoc_STRVAR(readall_doc, |
| 1017 | "readall() -> bytes. read all data from the file, returned as bytes.\n" |
| 1018 | "\n" |
| 1019 | "In non-blocking mode, returns as much as is immediately available,\n" |
| 1020 | "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] | 1021 | |
| 1022 | PyDoc_STRVAR(write_doc, |
| 1023 | "write(b: bytes) -> int. Write bytes b to file, return number written.\n" |
| 1024 | "\n" |
| 1025 | "Only makes one system call, so not all of the data may be written.\n" |
| 1026 | "The number of bytes actually written is returned."); |
| 1027 | |
| 1028 | PyDoc_STRVAR(fileno_doc, |
| 1029 | "fileno() -> int. \"file descriptor\".\n" |
| 1030 | "\n" |
| 1031 | "This is needed for lower-level file interfaces, such the fcntl module."); |
| 1032 | |
| 1033 | PyDoc_STRVAR(seek_doc, |
| 1034 | "seek(offset: int[, whence: int]) -> None. Move to new file position.\n" |
| 1035 | "\n" |
| 1036 | "Argument offset is a byte count. Optional argument whence defaults to\n" |
| 1037 | "0 (offset from start of file, offset should be >= 0); other values are 1\n" |
| 1038 | "(move relative to current position, positive or negative), and 2 (move\n" |
| 1039 | "relative to end of file, usually negative, although many platforms allow\n" |
| 1040 | "seeking beyond the end of a file)." |
| 1041 | "\n" |
| 1042 | "Note that not all file objects are seekable."); |
| 1043 | |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 1044 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1045 | PyDoc_STRVAR(truncate_doc, |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1046 | "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] | 1047 | "\n" |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 1048 | "Size defaults to the current file position, as returned by tell()." |
| 1049 | "The current file position is changed to the value of size."); |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 1050 | #endif |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1051 | |
| 1052 | PyDoc_STRVAR(tell_doc, |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1053 | "tell() -> int. Current file position"); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1054 | |
| 1055 | PyDoc_STRVAR(readinto_doc, |
Benjamin Peterson | 9a8082f | 2009-03-05 00:55:56 +0000 | [diff] [blame] | 1056 | "readinto() -> Same as RawIOBase.readinto()."); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1057 | |
| 1058 | PyDoc_STRVAR(close_doc, |
| 1059 | "close() -> None. Close the file.\n" |
| 1060 | "\n" |
| 1061 | "A closed file cannot be used for further I/O operations. close() may be\n" |
| 1062 | "called more than once without error. Changes the fileno to -1."); |
| 1063 | |
| 1064 | PyDoc_STRVAR(isatty_doc, |
| 1065 | "isatty() -> bool. True if the file is connected to a tty device."); |
| 1066 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1067 | PyDoc_STRVAR(seekable_doc, |
| 1068 | "seekable() -> bool. True if file supports random-access."); |
| 1069 | |
| 1070 | PyDoc_STRVAR(readable_doc, |
| 1071 | "readable() -> bool. True if file was opened in a read mode."); |
| 1072 | |
| 1073 | PyDoc_STRVAR(writable_doc, |
| 1074 | "writable() -> bool. True if file was opened in a write mode."); |
| 1075 | |
| 1076 | static PyMethodDef fileio_methods[] = { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1077 | {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc}, |
| 1078 | {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc}, |
| 1079 | {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc}, |
| 1080 | {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc}, |
| 1081 | {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc}, |
| 1082 | {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc}, |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 1083 | #ifdef HAVE_FTRUNCATE |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1084 | {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc}, |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 1085 | #endif |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1086 | {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc}, |
| 1087 | {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc}, |
| 1088 | {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc}, |
| 1089 | {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc}, |
| 1090 | {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc}, |
| 1091 | {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc}, |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 1092 | {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL}, |
Antoine Pitrou | 243757e | 2010-11-05 21:15:39 +0000 | [diff] [blame] | 1093 | {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL}, |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1094 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1095 | }; |
| 1096 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 1097 | /* 'closed' and 'mode' are attributes for backwards compatibility reasons. */ |
| 1098 | |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 1099 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1100 | get_closed(fileio *self, void *closure) |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 1101 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1102 | return PyBool_FromLong((long)(self->fd < 0)); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1106 | get_closefd(fileio *self, void *closure) |
Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 1107 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1108 | return PyBool_FromLong((long)(self->closefd)); |
Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1112 | get_mode(fileio *self, void *closure) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 1113 | { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1114 | return PyUnicode_FromString(mode_string(self)); |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | static PyGetSetDef fileio_getsetlist[] = { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1118 | {"closed", (getter)get_closed, NULL, "True if the file is closed"}, |
| 1119 | {"closefd", (getter)get_closefd, NULL, |
| 1120 | "True if the file descriptor will be closed"}, |
| 1121 | {"mode", (getter)get_mode, NULL, "String giving the file mode"}, |
| 1122 | {NULL}, |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 1123 | }; |
| 1124 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1125 | PyTypeObject PyFileIO_Type = { |
Antoine Pitrou | ae4b472 | 2010-05-05 16:31:07 +0000 | [diff] [blame] | 1126 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1127 | "_io.FileIO", |
| 1128 | sizeof(fileio), |
| 1129 | 0, |
| 1130 | (destructor)fileio_dealloc, /* tp_dealloc */ |
| 1131 | 0, /* tp_print */ |
| 1132 | 0, /* tp_getattr */ |
| 1133 | 0, /* tp_setattr */ |
| 1134 | 0, /* tp_reserved */ |
| 1135 | (reprfunc)fileio_repr, /* tp_repr */ |
| 1136 | 0, /* tp_as_number */ |
| 1137 | 0, /* tp_as_sequence */ |
| 1138 | 0, /* tp_as_mapping */ |
| 1139 | 0, /* tp_hash */ |
| 1140 | 0, /* tp_call */ |
| 1141 | 0, /* tp_str */ |
| 1142 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1143 | 0, /* tp_setattro */ |
| 1144 | 0, /* tp_as_buffer */ |
| 1145 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
| 1146 | | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 1147 | fileio_doc, /* tp_doc */ |
| 1148 | (traverseproc)fileio_traverse, /* tp_traverse */ |
| 1149 | (inquiry)fileio_clear, /* tp_clear */ |
| 1150 | 0, /* tp_richcompare */ |
| 1151 | offsetof(fileio, weakreflist), /* tp_weaklistoffset */ |
| 1152 | 0, /* tp_iter */ |
| 1153 | 0, /* tp_iternext */ |
| 1154 | fileio_methods, /* tp_methods */ |
| 1155 | 0, /* tp_members */ |
| 1156 | fileio_getsetlist, /* tp_getset */ |
| 1157 | 0, /* tp_base */ |
| 1158 | 0, /* tp_dict */ |
| 1159 | 0, /* tp_descr_get */ |
| 1160 | 0, /* tp_descr_set */ |
| 1161 | offsetof(fileio, dict), /* tp_dictoffset */ |
| 1162 | fileio_init, /* tp_init */ |
| 1163 | PyType_GenericAlloc, /* tp_alloc */ |
| 1164 | fileio_new, /* tp_new */ |
| 1165 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1166 | }; |