Questions about the IO modules and C-api
On 03/06/2019 04:02, Windson Yang wrote:
> I have some questions about the IO modules.
> 1. My script:
> f = open('myfile, 'a+b')
> I added a printf statement at the beginning of _io_open_impl
> the output is:
Is this the output of running `python your_script.py`? If it is, it may
include calls to _io.open made during Python startup. Maybe you can add
a line to print out which file is being opened each time as well (this
is a bit tricky). Or you could add print() calls at the start and end of
your script to see what's happening during, what before, and what after
> 2. I'm not familiar with the C, How the c-api like
> _PyIO_str_write, memobj, NULL)
If you haven't yet, I suggest you read the Python C API tutorial  and
consult the C API reference .
FWIW, the write call is ultimately here  via here .
> I guess this function will finally call the `write()` system call but I
> don't know why it would work. I found `self->raw` is an empty PyObject and
> `_PyIO_str_write` is a global variable which is NULL. Why an empty
> PyObject have a write method? Why we didn't just use `write()` system call
Oh, but it's not NULL, is it?
I don't believe self->raw is "empty" either. It's initialized by
_io.open, isn't it?
As for why go through the python method rather than calling something in
C directly, that would be in order to allow BufferedWriter to be used
with different types of IO classes, not just files.