#include "Python.h" #include "pycore_object.h" #include "pycore_sysmodule.h" // _PySys_GetSizeOf() #include <stddef.h> // offsetof() #include "_iomodule.h" /*[clinic input] module _io class _io.BytesIO "bytesio *" "clinic_state()->PyBytesIO_Type" [clinic start generated code]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=48ede2f330f847c3]*/ bytesio; bytesiobuf; /* The bytesio object can be in three states: * Py_REFCNT(buf) == 1, exports == 0. * Py_REFCNT(buf) > 1. exports == 0, first modification or export causes the internal buffer copying. * exports > 0. Py_REFCNT(buf) == 1, any modifications are forbidden. */ static int check_closed(bytesio *self) { … } static int check_exports(bytesio *self) { … } #define CHECK_CLOSED(self) … #define CHECK_EXPORTS(self) … #define SHARED_BUF(self) … /* Internal routine to get a line from the buffer of a BytesIO object. Returns the length between the current position to the next newline character. */ static Py_ssize_t scan_eol(bytesio *self, Py_ssize_t len) { … } /* Internal routine for detaching the shared buffer of BytesIO objects. The caller should ensure that the 'size' argument is non-negative and not lesser than self->string_size. Returns 0 on success, -1 otherwise. */ static int unshare_buffer(bytesio *self, size_t size) { … } /* Internal routine for changing the size of the buffer of BytesIO objects. The caller should ensure that the 'size' argument is non-negative. Returns 0 on success, -1 otherwise. */ static int resize_buffer(bytesio *self, size_t size) { … } /* Internal routine for writing a string of bytes to the buffer of a BytesIO object. Returns the number of bytes written, or -1 on error. Inlining is disabled because it's significantly decreases performance of writelines() in PGO build. */ Py_NO_INLINE static Py_ssize_t write_bytes(bytesio *self, PyObject *b) { … } static PyObject * bytesio_get_closed(bytesio *self, void *Py_UNUSED(ignored)) { … } /*[clinic input] _io.BytesIO.readable Returns True if the IO object can be read. [clinic start generated code]*/ static PyObject * _io_BytesIO_readable_impl(bytesio *self) /*[clinic end generated code: output=4e93822ad5b62263 input=96c5d0cccfb29f5c]*/ { … } /*[clinic input] _io.BytesIO.writable Returns True if the IO object can be written. [clinic start generated code]*/ static PyObject * _io_BytesIO_writable_impl(bytesio *self) /*[clinic end generated code: output=64ff6a254b1150b8 input=700eed808277560a]*/ { … } /*[clinic input] _io.BytesIO.seekable Returns True if the IO object can be seeked. [clinic start generated code]*/ static PyObject * _io_BytesIO_seekable_impl(bytesio *self) /*[clinic end generated code: output=6b417f46dcc09b56 input=9421f65627a344dd]*/ { … } /*[clinic input] _io.BytesIO.flush Does nothing. [clinic start generated code]*/ static PyObject * _io_BytesIO_flush_impl(bytesio *self) /*[clinic end generated code: output=187e3d781ca134a0 input=561ea490be4581a7]*/ { … } /*[clinic input] _io.BytesIO.getbuffer cls: defining_class / Get a read-write view over the contents of the BytesIO object. [clinic start generated code]*/ static PyObject * _io_BytesIO_getbuffer_impl(bytesio *self, PyTypeObject *cls) /*[clinic end generated code: output=045091d7ce87fe4e input=0668fbb48f95dffa]*/ { … } /*[clinic input] _io.BytesIO.getvalue Retrieve the entire contents of the BytesIO object. [clinic start generated code]*/ static PyObject * _io_BytesIO_getvalue_impl(bytesio *self) /*[clinic end generated code: output=b3f6a3233c8fd628 input=4b403ac0af3973ed]*/ { … } /*[clinic input] _io.BytesIO.isatty Always returns False. BytesIO objects are not connected to a TTY-like device. [clinic start generated code]*/ static PyObject * _io_BytesIO_isatty_impl(bytesio *self) /*[clinic end generated code: output=df67712e669f6c8f input=6f97f0985d13f827]*/ { … } /*[clinic input] _io.BytesIO.tell Current file position, an integer. [clinic start generated code]*/ static PyObject * _io_BytesIO_tell_impl(bytesio *self) /*[clinic end generated code: output=b54b0f93cd0e5e1d input=b106adf099cb3657]*/ { … } static PyObject * read_bytes(bytesio *self, Py_ssize_t size) { … } /*[clinic input] _io.BytesIO.read size: Py_ssize_t(accept={int, NoneType}) = -1 / Read at most size bytes, returned as a bytes object. If the size argument is negative, read until EOF is reached. Return an empty bytes object at EOF. [clinic start generated code]*/ static PyObject * _io_BytesIO_read_impl(bytesio *self, Py_ssize_t size) /*[clinic end generated code: output=9cc025f21c75bdd2 input=74344a39f431c3d7]*/ { … } /*[clinic input] _io.BytesIO.read1 size: Py_ssize_t(accept={int, NoneType}) = -1 / Read at most size bytes, returned as a bytes object. If the size argument is negative or omitted, read until EOF is reached. Return an empty bytes object at EOF. [clinic start generated code]*/ static PyObject * _io_BytesIO_read1_impl(bytesio *self, Py_ssize_t size) /*[clinic end generated code: output=d0f843285aa95f1c input=440a395bf9129ef5]*/ { … } /*[clinic input] _io.BytesIO.readline size: Py_ssize_t(accept={int, NoneType}) = -1 / Next line from the file, as a bytes object. Retain newline. A non-negative size argument limits the maximum number of bytes to return (an incomplete line may be returned then). Return an empty bytes object at EOF. [clinic start generated code]*/ static PyObject * _io_BytesIO_readline_impl(bytesio *self, Py_ssize_t size) /*[clinic end generated code: output=4bff3c251df8ffcd input=e7c3fbd1744e2783]*/ { … } /*[clinic input] _io.BytesIO.readlines size as arg: object = None / List of bytes objects, each a line from the file. Call readline() repeatedly and return a list of the lines so read. The optional size argument, if given, is an approximate bound on the total number of bytes in the lines returned. [clinic start generated code]*/ static PyObject * _io_BytesIO_readlines_impl(bytesio *self, PyObject *arg) /*[clinic end generated code: output=09b8e34c880808ff input=691aa1314f2c2a87]*/ { … } /*[clinic input] _io.BytesIO.readinto buffer: Py_buffer(accept={rwbuffer}) / Read bytes into buffer. Returns number of bytes read (0 for EOF), or None if the object is set not to block and has no data to read. [clinic start generated code]*/ static PyObject * _io_BytesIO_readinto_impl(bytesio *self, Py_buffer *buffer) /*[clinic end generated code: output=a5d407217dcf0639 input=1424d0fdce857919]*/ { … } /*[clinic input] _io.BytesIO.truncate size: Py_ssize_t(accept={int, NoneType}, c_default="self->pos") = None / Truncate the file to at most size bytes. Size defaults to the current file position, as returned by tell(). The current file position is unchanged. Returns the new size. [clinic start generated code]*/ static PyObject * _io_BytesIO_truncate_impl(bytesio *self, Py_ssize_t size) /*[clinic end generated code: output=9ad17650c15fa09b input=423759dd42d2f7c1]*/ { … } static PyObject * bytesio_iternext(bytesio *self) { … } /*[clinic input] _io.BytesIO.seek pos: Py_ssize_t whence: int = 0 / Change stream position. Seek to byte offset pos relative to position indicated by whence: 0 Start of stream (the default). pos should be >= 0; 1 Current position - pos may be negative; 2 End of stream - pos usually negative. Returns the new absolute position. [clinic start generated code]*/ static PyObject * _io_BytesIO_seek_impl(bytesio *self, Py_ssize_t pos, int whence) /*[clinic end generated code: output=c26204a68e9190e4 input=1e875e6ebc652948]*/ { … } /*[clinic input] _io.BytesIO.write b: object / Write bytes to file. Return the number of bytes written. [clinic start generated code]*/ static PyObject * _io_BytesIO_write(bytesio *self, PyObject *b) /*[clinic end generated code: output=53316d99800a0b95 input=f5ec7c8c64ed720a]*/ { … } /*[clinic input] _io.BytesIO.writelines lines: object / Write lines to the file. Note that newlines are not added. lines can be any iterable object producing bytes-like objects. This is equivalent to calling write() for each element. [clinic start generated code]*/ static PyObject * _io_BytesIO_writelines(bytesio *self, PyObject *lines) /*[clinic end generated code: output=7f33aa3271c91752 input=e972539176fc8fc1]*/ { … } /*[clinic input] _io.BytesIO.close Disable all I/O operations. [clinic start generated code]*/ static PyObject * _io_BytesIO_close_impl(bytesio *self) /*[clinic end generated code: output=1471bb9411af84a0 input=37e1f55556e61f60]*/ { … } /* Pickling support. Note that only pickle protocol 2 and onward are supported since we use extended __reduce__ API of PEP 307 to make BytesIO instances picklable. Providing support for protocol < 2 would require the __reduce_ex__ method which is notably long-winded when defined properly. For BytesIO, the implementation would similar to one coded for object.__reduce_ex__, but slightly less general. To be more specific, we could call bytesio_getstate directly and avoid checking for the presence of a fallback __reduce__ method. However, we would still need a __newobj__ function to use the efficient instance representation of PEP 307. */ static PyObject * bytesio_getstate(bytesio *self, PyObject *Py_UNUSED(ignored)) { … } static PyObject * bytesio_setstate(bytesio *self, PyObject *state) { … } static void bytesio_dealloc(bytesio *self) { … } static PyObject * bytesio_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { … } /*[clinic input] _io.BytesIO.__init__ initial_bytes as initvalue: object(c_default="NULL") = b'' Buffered I/O implementation using an in-memory bytes buffer. [clinic start generated code]*/ static int _io_BytesIO___init___impl(bytesio *self, PyObject *initvalue) /*[clinic end generated code: output=65c0c51e24c5b621 input=aac7f31b67bf0fb6]*/ { … } static PyObject * bytesio_sizeof(bytesio *self, void *unused) { … } static int bytesio_traverse(bytesio *self, visitproc visit, void *arg) { … } static int bytesio_clear(bytesio *self) { … } #define clinic_state … #include "clinic/bytesio.c.h" #undef clinic_state static PyGetSetDef bytesio_getsetlist[] = …; static struct PyMethodDef bytesio_methods[] = …; static PyMemberDef bytesio_members[] = …; static PyType_Slot bytesio_slots[] = …; PyType_Spec bytesio_spec = …; /* * Implementation of the small intermediate object used by getbuffer(). * getbuffer() returns a memoryview over this object, which should make it * invisible from Python code. */ static int bytesiobuf_getbuffer(bytesiobuf *obj, Py_buffer *view, int flags) { … } static void bytesiobuf_releasebuffer(bytesiobuf *obj, Py_buffer *view) { … } static int bytesiobuf_traverse(bytesiobuf *self, visitproc visit, void *arg) { … } static void bytesiobuf_dealloc(bytesiobuf *self) { … } static PyType_Slot bytesiobuf_slots[] = …; PyType_Spec bytesiobuf_spec = …;