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