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