1 /* 2 An implementation of the I/O abstract base classes hierarchy 3 as defined by PEP 3116 - "New I/O" 4 5 Classes defined here: IOBase, RawIOBase. 6 7 Written by Amaury Forgeot d'Arc and Antoine Pitrou 8 */ 9 10 11 #define PY_SSIZE_T_CLEAN 12 #include "Python.h" 13 #include "structmember.h" 14 #include "_iomodule.h" 15 16 /*[clinic input] 17 module _io 18 class _io._IOBase "PyObject *" "&PyIOBase_Type" 19 class _io._RawIOBase "PyObject *" "&PyRawIOBase_Type" 20 [clinic start generated code]*/ 21 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=d29a4d076c2b211c]*/ 22 23 /*[python input] 24 class io_ssize_t_converter(CConverter): 25 type = 'Py_ssize_t' 26 converter = '_PyIO_ConvertSsize_t' 27 [python start generated code]*/ 28 /*[python end generated code: output=da39a3ee5e6b4b0d input=d0a811d3cbfd1b33]*/ 29 30 /* 31 * IOBase class, an abstract class 32 */ 33 34 typedef struct { 35 PyObject_HEAD 36 37 PyObject *dict; 38 PyObject *weakreflist; 39 } iobase; 40 41 PyDoc_STRVAR(iobase_doc, 42 "The abstract base class for all I/O classes, acting on streams of\n" 43 "bytes. There is no public constructor.\n" 44 "\n" 45 "This class provides dummy implementations for many methods that\n" 46 "derived classes can override selectively; the default implementations\n" 47 "represent a file that cannot be read, written or seeked.\n" 48 "\n" 49 "Even though IOBase does not declare read, readinto, or write because\n" 50 "their signatures will vary, implementations and clients should\n" 51 "consider those methods part of the interface. Also, implementations\n" 52 "may raise UnsupportedOperation when operations they do not support are\n" 53 "called.\n" 54 "\n" 55 "The basic type used for binary data read from or written to a file is\n" 56 "bytes. Other bytes-like objects are accepted as method arguments too.\n" 57 "In some cases (such as readinto), a writable object is required. Text\n" 58 "I/O classes work with str data.\n" 59 "\n" 60 "Note that calling any method (except additional calls to close(),\n" 61 "which are ignored) on a closed stream should raise a ValueError.\n" 62 "\n" 63 "IOBase (and its subclasses) support the iterator protocol, meaning\n" 64 "that an IOBase object can be iterated over yielding the lines in a\n" 65 "stream.\n" 66 "\n" 67 "IOBase also supports the :keyword:`with` statement. In this example,\n" 68 "fp is closed after the suite of the with statement is complete:\n" 69 "\n" 70 "with open('spam.txt', 'r') as fp:\n" 71 " fp.write('Spam and eggs!')\n"); 72 73 /* Use this macro whenever you want to check the internal `closed` status 74 of the IOBase object rather than the virtual `closed` attribute as returned 75 by whatever subclass. */ 76 77 _Py_IDENTIFIER(__IOBase_closed); 78 #define IS_CLOSED(self) \ 79 _PyObject_HasAttrId(self, &PyId___IOBase_closed) 80 81 _Py_IDENTIFIER(read); 82 83 /* Internal methods */ 84 static PyObject * 85 iobase_unsupported(const char *message) 86 { 87 _PyIO_State *state = IO_STATE(); 88 if (state != NULL) 89 PyErr_SetString(state->unsupported_operation, message); 90 return NULL; 91 } 92 93 /* Positioning */ 94 95 PyDoc_STRVAR(iobase_seek_doc, 96 "Change stream position.\n" 97 "\n" 98 "Change the stream position to the given byte offset. The offset is\n" 99 "interpreted relative to the position indicated by whence. Values\n" 100 "for whence are:\n" 101 "\n" 102 "* 0 -- start of stream (the default); offset should be zero or positive\n" 103 "* 1 -- current stream position; offset may be negative\n" 104 "* 2 -- end of stream; offset is usually negative\n" 105 "\n" 106 "Return the new absolute position."); 107 108 static PyObject * 109 iobase_seek(PyObject *self, PyObject *args) 110 { 111 return iobase_unsupported("seek"); 112 } 113 114 /*[clinic input] 115 _io._IOBase.tell 116 117 Return current stream position. 118 [clinic start generated code]*/ 119 120 static PyObject * 121 _io__IOBase_tell_impl(PyObject *self) 122 /*[clinic end generated code: output=89a1c0807935abe2 input=04e615fec128801f]*/ 123 { 124 _Py_IDENTIFIER(seek); 125 126 return _PyObject_CallMethodId(self, &PyId_seek, "ii", 0, 1); 127 } 128 129 PyDoc_STRVAR(iobase_truncate_doc, 130 "Truncate file to size bytes.\n" 131 "\n" 132 "File pointer is left unchanged. Size defaults to the current IO\n" 133 "position as reported by tell(). Returns the new size."); 134 135 static PyObject * 136 iobase_truncate(PyObject *self, PyObject *args) 137 { 138 return iobase_unsupported("truncate"); 139 } 140 141 /* Flush and close methods */ 142 143 /*[clinic input] 144 _io._IOBase.flush 145 146 Flush write buffers, if applicable. 147 148 This is not implemented for read-only and non-blocking streams. 149 [clinic start generated code]*/ 150 151 static PyObject * 152 _io__IOBase_flush_impl(PyObject *self) 153 /*[clinic end generated code: output=7cef4b4d54656a3b input=773be121abe270aa]*/ 154 { 155 /* XXX Should this return the number of bytes written??? */ 156 if (IS_CLOSED(self)) { 157 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file."); 158 return NULL; 159 } 160 Py_RETURN_NONE; 161 } 162 163 static int 164 iobase_closed(PyObject *self) 165 { 166 PyObject *res; 167 int closed; 168 /* This gets the derived attribute, which is *not* __IOBase_closed 169 in most cases! */ 170 res = PyObject_GetAttr(self, _PyIO_str_closed); 171 if (res == NULL) 172 return 0; 173 closed = PyObject_IsTrue(res); 174 Py_DECREF(res); 175 return closed; 176 } 177 178 static PyObject * 179 iobase_closed_get(PyObject *self, void *context) 180 { 181 return PyBool_FromLong(IS_CLOSED(self)); 182 } 183 184 PyObject * 185 _PyIOBase_check_closed(PyObject *self, PyObject *args) 186 { 187 if (iobase_closed(self)) { 188 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file."); 189 return NULL; 190 } 191 if (args == Py_True) 192 return Py_None; 193 else 194 Py_RETURN_NONE; 195 } 196 197 /* XXX: IOBase thinks it has to maintain its own internal state in 198 `__IOBase_closed` and call flush() by itself, but it is redundant with 199 whatever behaviour a non-trivial derived class will implement. */ 200 201 /*[clinic input] 202 _io._IOBase.close 203 204 Flush and close the IO object. 205 206 This method has no effect if the file is already closed. 207 [clinic start generated code]*/ 208 209 static PyObject * 210 _io__IOBase_close_impl(PyObject *self) 211 /*[clinic end generated code: output=63c6a6f57d783d6d input=f4494d5c31dbc6b7]*/ 212 { 213 PyObject *res; 214 215 if (IS_CLOSED(self)) 216 Py_RETURN_NONE; 217 218 res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL); 219 220 if (_PyObject_SetAttrId(self, &PyId___IOBase_closed, Py_True) < 0) { 221 Py_XDECREF(res); 222 return NULL; 223 } 224 225 if (res == NULL) 226 return NULL; 227 228 Py_DECREF(res); 229 Py_RETURN_NONE; 230 } 231 232 /* Finalization and garbage collection support */ 233 234 static void 235 iobase_finalize(PyObject *self) 236 { 237 PyObject *res; 238 PyObject *error_type, *error_value, *error_traceback; 239 int closed; 240 _Py_IDENTIFIER(_finalizing); 241 242 /* Save the current exception, if any. */ 243 PyErr_Fetch(&error_type, &error_value, &error_traceback); 244 245 /* If `closed` doesn't exist or can't be evaluated as bool, then the 246 object is probably in an unusable state, so ignore. */ 247 res = PyObject_GetAttr(self, _PyIO_str_closed); 248 if (res == NULL) { 249 PyErr_Clear(); 250 closed = -1; 251 } 252 else { 253 closed = PyObject_IsTrue(res); 254 Py_DECREF(res); 255 if (closed == -1) 256 PyErr_Clear(); 257 } 258 if (closed == 0) { 259 /* Signal close() that it was called as part of the object 260 finalization process. */ 261 if (_PyObject_SetAttrId(self, &PyId__finalizing, Py_True)) 262 PyErr_Clear(); 263 res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_close, 264 NULL); 265 /* Silencing I/O errors is bad, but printing spurious tracebacks is 266 equally as bad, and potentially more frequent (because of 267 shutdown issues). */ 268 if (res == NULL) 269 PyErr_Clear(); 270 else 271 Py_DECREF(res); 272 } 273 274 /* Restore the saved exception. */ 275 PyErr_Restore(error_type, error_value, error_traceback); 276 } 277 278 int 279 _PyIOBase_finalize(PyObject *self) 280 { 281 int is_zombie; 282 283 /* If _PyIOBase_finalize() is called from a destructor, we need to 284 resurrect the object as calling close() can invoke arbitrary code. */ 285 is_zombie = (Py_REFCNT(self) == 0); 286 if (is_zombie) 287 return PyObject_CallFinalizerFromDealloc(self); 288 else { 289 PyObject_CallFinalizer(self); 290 return 0; 291 } 292 } 293 294 static int 295 iobase_traverse(iobase *self, visitproc visit, void *arg) 296 { 297 Py_VISIT(self->dict); 298 return 0; 299 } 300 301 static int 302 iobase_clear(iobase *self) 303 { 304 Py_CLEAR(self->dict); 305 return 0; 306 } 307 308 /* Destructor */ 309 310 static void 311 iobase_dealloc(iobase *self) 312 { 313 /* NOTE: since IOBaseObject has its own dict, Python-defined attributes 314 are still available here for close() to use. 315 However, if the derived class declares a __slots__, those slots are 316 already gone. 317 */ 318 if (_PyIOBase_finalize((PyObject *) self) < 0) { 319 /* When called from a heap type's dealloc, the type will be 320 decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */ 321 if (PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE)) 322 Py_INCREF(Py_TYPE(self)); 323 return; 324 } 325 _PyObject_GC_UNTRACK(self); 326 if (self->weakreflist != NULL) 327 PyObject_ClearWeakRefs((PyObject *) self); 328 Py_CLEAR(self->dict); 329 Py_TYPE(self)->tp_free((PyObject *) self); 330 } 331 332 /* Inquiry methods */ 333 334 /*[clinic input] 335 _io._IOBase.seekable 336 337 Return whether object supports random access. 338 339 If False, seek(), tell() and truncate() will raise OSError. 340 This method may need to do a test seek(). 341 [clinic start generated code]*/ 342 343 static PyObject * 344 _io__IOBase_seekable_impl(PyObject *self) 345 /*[clinic end generated code: output=4c24c67f5f32a43d input=b976622f7fdf3063]*/ 346 { 347 Py_RETURN_FALSE; 348 } 349 350 PyObject * 351 _PyIOBase_check_seekable(PyObject *self, PyObject *args) 352 { 353 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_seekable, NULL); 354 if (res == NULL) 355 return NULL; 356 if (res != Py_True) { 357 Py_CLEAR(res); 358 iobase_unsupported("File or stream is not seekable."); 359 return NULL; 360 } 361 if (args == Py_True) { 362 Py_DECREF(res); 363 } 364 return res; 365 } 366 367 /*[clinic input] 368 _io._IOBase.readable 369 370 Return whether object was opened for reading. 371 372 If False, read() will raise OSError. 373 [clinic start generated code]*/ 374 375 static PyObject * 376 _io__IOBase_readable_impl(PyObject *self) 377 /*[clinic end generated code: output=e48089250686388b input=285b3b866a0ec35f]*/ 378 { 379 Py_RETURN_FALSE; 380 } 381 382 /* May be called with any object */ 383 PyObject * 384 _PyIOBase_check_readable(PyObject *self, PyObject *args) 385 { 386 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_readable, NULL); 387 if (res == NULL) 388 return NULL; 389 if (res != Py_True) { 390 Py_CLEAR(res); 391 iobase_unsupported("File or stream is not readable."); 392 return NULL; 393 } 394 if (args == Py_True) { 395 Py_DECREF(res); 396 } 397 return res; 398 } 399 400 /*[clinic input] 401 _io._IOBase.writable 402 403 Return whether object was opened for writing. 404 405 If False, write() will raise OSError. 406 [clinic start generated code]*/ 407 408 static PyObject * 409 _io__IOBase_writable_impl(PyObject *self) 410 /*[clinic end generated code: output=406001d0985be14f input=9dcac18a013a05b5]*/ 411 { 412 Py_RETURN_FALSE; 413 } 414 415 /* May be called with any object */ 416 PyObject * 417 _PyIOBase_check_writable(PyObject *self, PyObject *args) 418 { 419 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_writable, NULL); 420 if (res == NULL) 421 return NULL; 422 if (res != Py_True) { 423 Py_CLEAR(res); 424 iobase_unsupported("File or stream is not writable."); 425 return NULL; 426 } 427 if (args == Py_True) { 428 Py_DECREF(res); 429 } 430 return res; 431 } 432 433 /* Context manager */ 434 435 static PyObject * 436 iobase_enter(PyObject *self, PyObject *args) 437 { 438 if (_PyIOBase_check_closed(self, Py_True) == NULL) 439 return NULL; 440 441 Py_INCREF(self); 442 return self; 443 } 444 445 static PyObject * 446 iobase_exit(PyObject *self, PyObject *args) 447 { 448 return PyObject_CallMethodObjArgs(self, _PyIO_str_close, NULL); 449 } 450 451 /* Lower-level APIs */ 452 453 /* XXX Should these be present even if unimplemented? */ 454 455 /*[clinic input] 456 _io._IOBase.fileno 457 458 Returns underlying file descriptor if one exists. 459 460 OSError is raised if the IO object does not use a file descriptor. 461 [clinic start generated code]*/ 462 463 static PyObject * 464 _io__IOBase_fileno_impl(PyObject *self) 465 /*[clinic end generated code: output=7cc0973f0f5f3b73 input=4e37028947dc1cc8]*/ 466 { 467 return iobase_unsupported("fileno"); 468 } 469 470 /*[clinic input] 471 _io._IOBase.isatty 472 473 Return whether this is an 'interactive' stream. 474 475 Return False if it can't be determined. 476 [clinic start generated code]*/ 477 478 static PyObject * 479 _io__IOBase_isatty_impl(PyObject *self) 480 /*[clinic end generated code: output=60cab77cede41cdd input=9ef76530d368458b]*/ 481 { 482 if (_PyIOBase_check_closed(self, Py_True) == NULL) 483 return NULL; 484 Py_RETURN_FALSE; 485 } 486 487 /* Readline(s) and writelines */ 488 489 /*[clinic input] 490 _io._IOBase.readline 491 size as limit: io_ssize_t = -1 492 / 493 494 Read and return a line from the stream. 495 496 If size is specified, at most size bytes will be read. 497 498 The line terminator is always b'\n' for binary files; for text 499 files, the newlines argument to open can be used to select the line 500 terminator(s) recognized. 501 [clinic start generated code]*/ 502 503 static PyObject * 504 _io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit) 505 /*[clinic end generated code: output=4479f79b58187840 input=df4cc8884f553cab]*/ 506 { 507 /* For backwards compatibility, a (slowish) readline(). */ 508 509 int has_peek = 0; 510 PyObject *buffer, *result; 511 Py_ssize_t old_size = -1; 512 _Py_IDENTIFIER(peek); 513 514 if (_PyObject_HasAttrId(self, &PyId_peek)) 515 has_peek = 1; 516 517 buffer = PyByteArray_FromStringAndSize(NULL, 0); 518 if (buffer == NULL) 519 return NULL; 520 521 while (limit < 0 || Py_SIZE(buffer) < limit) { 522 Py_ssize_t nreadahead = 1; 523 PyObject *b; 524 525 if (has_peek) { 526 PyObject *readahead = _PyObject_CallMethodId(self, &PyId_peek, "i", 1); 527 if (readahead == NULL) { 528 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() 529 when EINTR occurs so we needn't do it ourselves. */ 530 if (_PyIO_trap_eintr()) { 531 continue; 532 } 533 goto fail; 534 } 535 if (!PyBytes_Check(readahead)) { 536 PyErr_Format(PyExc_IOError, 537 "peek() should have returned a bytes object, " 538 "not '%.200s'", Py_TYPE(readahead)->tp_name); 539 Py_DECREF(readahead); 540 goto fail; 541 } 542 if (PyBytes_GET_SIZE(readahead) > 0) { 543 Py_ssize_t n = 0; 544 const char *buf = PyBytes_AS_STRING(readahead); 545 if (limit >= 0) { 546 do { 547 if (n >= PyBytes_GET_SIZE(readahead) || n >= limit) 548 break; 549 if (buf[n++] == '\n') 550 break; 551 } while (1); 552 } 553 else { 554 do { 555 if (n >= PyBytes_GET_SIZE(readahead)) 556 break; 557 if (buf[n++] == '\n') 558 break; 559 } while (1); 560 } 561 nreadahead = n; 562 } 563 Py_DECREF(readahead); 564 } 565 566 b = _PyObject_CallMethodId(self, &PyId_read, "n", nreadahead); 567 if (b == NULL) { 568 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() 569 when EINTR occurs so we needn't do it ourselves. */ 570 if (_PyIO_trap_eintr()) { 571 continue; 572 } 573 goto fail; 574 } 575 if (!PyBytes_Check(b)) { 576 PyErr_Format(PyExc_IOError, 577 "read() should have returned a bytes object, " 578 "not '%.200s'", Py_TYPE(b)->tp_name); 579 Py_DECREF(b); 580 goto fail; 581 } 582 if (PyBytes_GET_SIZE(b) == 0) { 583 Py_DECREF(b); 584 break; 585 } 586 587 old_size = PyByteArray_GET_SIZE(buffer); 588 if (PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b)) < 0) { 589 Py_DECREF(b); 590 goto fail; 591 } 592 memcpy(PyByteArray_AS_STRING(buffer) + old_size, 593 PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b)); 594 595 Py_DECREF(b); 596 597 if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n') 598 break; 599 } 600 601 result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer), 602 PyByteArray_GET_SIZE(buffer)); 603 Py_DECREF(buffer); 604 return result; 605 fail: 606 Py_DECREF(buffer); 607 return NULL; 608 } 609 610 static PyObject * 611 iobase_iter(PyObject *self) 612 { 613 if (_PyIOBase_check_closed(self, Py_True) == NULL) 614 return NULL; 615 616 Py_INCREF(self); 617 return self; 618 } 619 620 static PyObject * 621 iobase_iternext(PyObject *self) 622 { 623 PyObject *line = PyObject_CallMethodObjArgs(self, _PyIO_str_readline, NULL); 624 625 if (line == NULL) 626 return NULL; 627 628 if (PyObject_Size(line) == 0) { 629 Py_DECREF(line); 630 return NULL; 631 } 632 633 return line; 634 } 635 636 /*[clinic input] 637 _io._IOBase.readlines 638 hint: io_ssize_t = -1 639 / 640 641 Return a list of lines from the stream. 642 643 hint can be specified to control the number of lines read: no more 644 lines will be read if the total size (in bytes/characters) of all 645 lines so far exceeds hint. 646 [clinic start generated code]*/ 647 648 static PyObject * 649 _io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint) 650 /*[clinic end generated code: output=2f50421677fa3dea input=1961c4a95e96e661]*/ 651 { 652 Py_ssize_t length = 0; 653 PyObject *result; 654 655 result = PyList_New(0); 656 if (result == NULL) 657 return NULL; 658 659 if (hint <= 0) { 660 /* XXX special-casing this made sense in the Python version in order 661 to remove the bytecode interpretation overhead, but it could 662 probably be removed here. */ 663 _Py_IDENTIFIER(extend); 664 PyObject *ret = _PyObject_CallMethodId(result, &PyId_extend, "O", self); 665 666 if (ret == NULL) { 667 Py_DECREF(result); 668 return NULL; 669 } 670 Py_DECREF(ret); 671 return result; 672 } 673 674 while (1) { 675 PyObject *line = PyIter_Next(self); 676 if (line == NULL) { 677 if (PyErr_Occurred()) { 678 Py_DECREF(result); 679 return NULL; 680 } 681 else 682 break; /* StopIteration raised */ 683 } 684 685 if (PyList_Append(result, line) < 0) { 686 Py_DECREF(line); 687 Py_DECREF(result); 688 return NULL; 689 } 690 length += PyObject_Size(line); 691 Py_DECREF(line); 692 693 if (length > hint) 694 break; 695 } 696 return result; 697 } 698 699 /*[clinic input] 700 _io._IOBase.writelines 701 lines: object 702 / 703 [clinic start generated code]*/ 704 705 static PyObject * 706 _io__IOBase_writelines(PyObject *self, PyObject *lines) 707 /*[clinic end generated code: output=976eb0a9b60a6628 input=432e729a8450b3cb]*/ 708 { 709 PyObject *iter, *res; 710 711 if (_PyIOBase_check_closed(self, Py_True) == NULL) 712 return NULL; 713 714 iter = PyObject_GetIter(lines); 715 if (iter == NULL) 716 return NULL; 717 718 while (1) { 719 PyObject *line = PyIter_Next(iter); 720 if (line == NULL) { 721 if (PyErr_Occurred()) { 722 Py_DECREF(iter); 723 return NULL; 724 } 725 else 726 break; /* Stop Iteration */ 727 } 728 729 res = NULL; 730 do { 731 res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL); 732 } while (res == NULL && _PyIO_trap_eintr()); 733 Py_DECREF(line); 734 if (res == NULL) { 735 Py_DECREF(iter); 736 return NULL; 737 } 738 Py_DECREF(res); 739 } 740 Py_DECREF(iter); 741 Py_RETURN_NONE; 742 } 743 744 #include "clinic/iobase.c.h" 745 746 static PyMethodDef iobase_methods[] = { 747 {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc}, 748 _IO__IOBASE_TELL_METHODDEF 749 {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc}, 750 _IO__IOBASE_FLUSH_METHODDEF 751 _IO__IOBASE_CLOSE_METHODDEF 752 753 _IO__IOBASE_SEEKABLE_METHODDEF 754 _IO__IOBASE_READABLE_METHODDEF 755 _IO__IOBASE_WRITABLE_METHODDEF 756 757 {"_checkClosed", _PyIOBase_check_closed, METH_NOARGS}, 758 {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS}, 759 {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS}, 760 {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS}, 761 762 _IO__IOBASE_FILENO_METHODDEF 763 _IO__IOBASE_ISATTY_METHODDEF 764 765 {"__enter__", iobase_enter, METH_NOARGS}, 766 {"__exit__", iobase_exit, METH_VARARGS}, 767 768 _IO__IOBASE_READLINE_METHODDEF 769 _IO__IOBASE_READLINES_METHODDEF 770 _IO__IOBASE_WRITELINES_METHODDEF 771 772 {NULL, NULL} 773 }; 774 775 static PyGetSetDef iobase_getset[] = { 776 {"__dict__", PyObject_GenericGetDict, NULL, NULL}, 777 {"closed", (getter)iobase_closed_get, NULL, NULL}, 778 {NULL} 779 }; 780 781 782 PyTypeObject PyIOBase_Type = { 783 PyVarObject_HEAD_INIT(NULL, 0) 784 "_io._IOBase", /*tp_name*/ 785 sizeof(iobase), /*tp_basicsize*/ 786 0, /*tp_itemsize*/ 787 (destructor)iobase_dealloc, /*tp_dealloc*/ 788 0, /*tp_print*/ 789 0, /*tp_getattr*/ 790 0, /*tp_setattr*/ 791 0, /*tp_compare */ 792 0, /*tp_repr*/ 793 0, /*tp_as_number*/ 794 0, /*tp_as_sequence*/ 795 0, /*tp_as_mapping*/ 796 0, /*tp_hash */ 797 0, /*tp_call*/ 798 0, /*tp_str*/ 799 0, /*tp_getattro*/ 800 0, /*tp_setattro*/ 801 0, /*tp_as_buffer*/ 802 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE 803 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/ 804 iobase_doc, /* tp_doc */ 805 (traverseproc)iobase_traverse, /* tp_traverse */ 806 (inquiry)iobase_clear, /* tp_clear */ 807 0, /* tp_richcompare */ 808 offsetof(iobase, weakreflist), /* tp_weaklistoffset */ 809 iobase_iter, /* tp_iter */ 810 iobase_iternext, /* tp_iternext */ 811 iobase_methods, /* tp_methods */ 812 0, /* tp_members */ 813 iobase_getset, /* tp_getset */ 814 0, /* tp_base */ 815 0, /* tp_dict */ 816 0, /* tp_descr_get */ 817 0, /* tp_descr_set */ 818 offsetof(iobase, dict), /* tp_dictoffset */ 819 0, /* tp_init */ 820 0, /* tp_alloc */ 821 PyType_GenericNew, /* tp_new */ 822 0, /* tp_free */ 823 0, /* tp_is_gc */ 824 0, /* tp_bases */ 825 0, /* tp_mro */ 826 0, /* tp_cache */ 827 0, /* tp_subclasses */ 828 0, /* tp_weaklist */ 829 0, /* tp_del */ 830 0, /* tp_version_tag */ 831 iobase_finalize, /* tp_finalize */ 832 }; 833 834 835 /* 836 * RawIOBase class, Inherits from IOBase. 837 */ 838 PyDoc_STRVAR(rawiobase_doc, 839 "Base class for raw binary I/O."); 840 841 /* 842 * The read() method is implemented by calling readinto(); derived classes 843 * that want to support read() only need to implement readinto() as a 844 * primitive operation. In general, readinto() can be more efficient than 845 * read(). 846 * 847 * (It would be tempting to also provide an implementation of readinto() in 848 * terms of read(), in case the latter is a more suitable primitive operation, 849 * but that would lead to nasty recursion in case a subclass doesn't implement 850 * either.) 851 */ 852 853 /*[clinic input] 854 _io._RawIOBase.read 855 size as n: Py_ssize_t = -1 856 / 857 [clinic start generated code]*/ 858 859 static PyObject * 860 _io__RawIOBase_read_impl(PyObject *self, Py_ssize_t n) 861 /*[clinic end generated code: output=6cdeb731e3c9f13c input=b6d0dcf6417d1374]*/ 862 { 863 PyObject *b, *res; 864 865 if (n < 0) { 866 _Py_IDENTIFIER(readall); 867 868 return _PyObject_CallMethodId(self, &PyId_readall, NULL); 869 } 870 871 /* TODO: allocate a bytes object directly instead and manually construct 872 a writable memoryview pointing to it. */ 873 b = PyByteArray_FromStringAndSize(NULL, n); 874 if (b == NULL) 875 return NULL; 876 877 res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL); 878 if (res == NULL || res == Py_None) { 879 Py_DECREF(b); 880 return res; 881 } 882 883 n = PyNumber_AsSsize_t(res, PyExc_ValueError); 884 Py_DECREF(res); 885 if (n == -1 && PyErr_Occurred()) { 886 Py_DECREF(b); 887 return NULL; 888 } 889 890 res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n); 891 Py_DECREF(b); 892 return res; 893 } 894 895 896 /*[clinic input] 897 _io._RawIOBase.readall 898 899 Read until EOF, using multiple read() call. 900 [clinic start generated code]*/ 901 902 static PyObject * 903 _io__RawIOBase_readall_impl(PyObject *self) 904 /*[clinic end generated code: output=1987b9ce929425a0 input=688874141213622a]*/ 905 { 906 int r; 907 PyObject *chunks = PyList_New(0); 908 PyObject *result; 909 910 if (chunks == NULL) 911 return NULL; 912 913 while (1) { 914 PyObject *data = _PyObject_CallMethodId(self, &PyId_read, 915 "i", DEFAULT_BUFFER_SIZE); 916 if (!data) { 917 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() 918 when EINTR occurs so we needn't do it ourselves. */ 919 if (_PyIO_trap_eintr()) { 920 continue; 921 } 922 Py_DECREF(chunks); 923 return NULL; 924 } 925 if (data == Py_None) { 926 if (PyList_GET_SIZE(chunks) == 0) { 927 Py_DECREF(chunks); 928 return data; 929 } 930 Py_DECREF(data); 931 break; 932 } 933 if (!PyBytes_Check(data)) { 934 Py_DECREF(chunks); 935 Py_DECREF(data); 936 PyErr_SetString(PyExc_TypeError, "read() should return bytes"); 937 return NULL; 938 } 939 if (PyBytes_GET_SIZE(data) == 0) { 940 /* EOF */ 941 Py_DECREF(data); 942 break; 943 } 944 r = PyList_Append(chunks, data); 945 Py_DECREF(data); 946 if (r < 0) { 947 Py_DECREF(chunks); 948 return NULL; 949 } 950 } 951 result = _PyBytes_Join(_PyIO_empty_bytes, chunks); 952 Py_DECREF(chunks); 953 return result; 954 } 955 956 static PyObject * 957 rawiobase_readinto(PyObject *self, PyObject *args) 958 { 959 PyErr_SetNone(PyExc_NotImplementedError); 960 return NULL; 961 } 962 963 static PyObject * 964 rawiobase_write(PyObject *self, PyObject *args) 965 { 966 PyErr_SetNone(PyExc_NotImplementedError); 967 return NULL; 968 } 969 970 static PyMethodDef rawiobase_methods[] = { 971 _IO__RAWIOBASE_READ_METHODDEF 972 _IO__RAWIOBASE_READALL_METHODDEF 973 {"readinto", rawiobase_readinto, METH_VARARGS}, 974 {"write", rawiobase_write, METH_VARARGS}, 975 {NULL, NULL} 976 }; 977 978 PyTypeObject PyRawIOBase_Type = { 979 PyVarObject_HEAD_INIT(NULL, 0) 980 "_io._RawIOBase", /*tp_name*/ 981 0, /*tp_basicsize*/ 982 0, /*tp_itemsize*/ 983 0, /*tp_dealloc*/ 984 0, /*tp_print*/ 985 0, /*tp_getattr*/ 986 0, /*tp_setattr*/ 987 0, /*tp_compare */ 988 0, /*tp_repr*/ 989 0, /*tp_as_number*/ 990 0, /*tp_as_sequence*/ 991 0, /*tp_as_mapping*/ 992 0, /*tp_hash */ 993 0, /*tp_call*/ 994 0, /*tp_str*/ 995 0, /*tp_getattro*/ 996 0, /*tp_setattro*/ 997 0, /*tp_as_buffer*/ 998 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/ 999 rawiobase_doc, /* tp_doc */ 1000 0, /* tp_traverse */ 1001 0, /* tp_clear */ 1002 0, /* tp_richcompare */ 1003 0, /* tp_weaklistoffset */ 1004 0, /* tp_iter */ 1005 0, /* tp_iternext */ 1006 rawiobase_methods, /* tp_methods */ 1007 0, /* tp_members */ 1008 0, /* tp_getset */ 1009 &PyIOBase_Type, /* tp_base */ 1010 0, /* tp_dict */ 1011 0, /* tp_descr_get */ 1012 0, /* tp_descr_set */ 1013 0, /* tp_dictoffset */ 1014 0, /* tp_init */ 1015 0, /* tp_alloc */ 1016 0, /* tp_new */ 1017 0, /* tp_free */ 1018 0, /* tp_is_gc */ 1019 0, /* tp_bases */ 1020 0, /* tp_mro */ 1021 0, /* tp_cache */ 1022 0, /* tp_subclasses */ 1023 0, /* tp_weaklist */ 1024 0, /* tp_del */ 1025 0, /* tp_version_tag */ 1026 0, /* tp_finalize */ 1027 }; 1028