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