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