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