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