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