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