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