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