Code # 1:
A file object such as a StringIO instance is prepared for code testing and then passed to:
Code # 2:
Unlike a regular system file, a file-like object is not necessarily built on a low-level file descriptor. Thus, normal C library functions cannot be used to access it. Instead, the Python C API is used to manipulate an object-like object in the same way as in Python.
read () method is retrieved from the passed object. The argument list is
PyObject_Call () and then re-passed to
PyObject_Call () to call the method. To detect end of file (EOF),
PySequence_Length () is used to see if the returned result is of zero length.
For all I / O operations, the problem is encoding and distinguishing between bytes and Unicode. This recipe shows how to read a file in text mode and decode the resulting text into a byte encoding that can be used by C. If the file is read in binary mode, only minor changes will be made, as shown in the code below.
Code # 3: