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