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