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