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