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