[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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')
>     f.close()
> I added a printf statement at the beginning of _io_open_impl
> <>,
> the output is:

Is this the output of running `python`? If it is, it may
include calls to 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
your script.

> 2. I'm not familiar with the C, How the c-api like
> PyObject_CallMethodObjArgs(self->raw,
> _PyIO_str_write, memobj, NULL)
> <>
>  works?

If you haven't yet, I suggest you read the Python C API tutorial [1] and
consult the C API reference [2].

FWIW, the write call is ultimately here [3] via here [4].


> 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
> directly?

Oh, but it's not NULL, is it?

I don't believe self->raw is "empty" either. It's initialized by, 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.

-- Thomas