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