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