git.net

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

Questions about the IO modules and C-api


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
<https://github.com/python/cpython/blob/master/Modules/_io/_iomodule.c#L233>,
the output is:

    _io_open
    _io_open
    _io_open
    _io_open
    _io_open

Why this function has been called more than one time? I expect this
function only let us call the `open()` system call once.

2. I'm not familiar with the C, How the c-api like
PyObject_CallMethodObjArgs(self->raw,
_PyIO_str_write, memobj, NULL)
<https://github.com/python/cpython/blob/master/Modules/_io/bufferedio.c#L1818>
 works?

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?

Thank you so much :D

Best,

Windson