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