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