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