C Python 实现中函数 `_PyIO_str_readline` 的实现在哪里?
Where is the implementation of the function `_PyIO_str_readline` on the C Python implementation?
关于这个问题,我了解了open()
方法是如何实现的,但是我找不到在其实现上使用的_PyIO_str_readline
函数是在哪里定义的。
你的问题是你认为,_PyIO_str_readline
是一个函数,但实际上它只是一个全局变量(PyObject *
类型),声明为here:
extern PyObject *_PyIO_str_readline;
并定义了here:
PyObject *_PyIO_str_readline = NULL;
是 NULL
,但顾名思义,可以是任何 string-object(即 Python3 中的 unicode 或 Python2 中的字节)。
_PyIO_str_readline
是一种缓存(在 CPython 中通常被称为 "interned string" - 参见 PyUnicode_InternFromString
),所以每次 PyObject_CallMethodObjArgs
都用 [=43 调用=] as method-name, 对应的对象一定不能重新构造.
_PyIO_str_readline
在 PyInit__io
中初始化为其实际值,使用宏 ADD_INTERNED
:
/* Interned strings */
#define ADD_INTERNED(name) \
if (!_PyIO_str_ ## name && \
!(_PyIO_str_ ## name = PyUnicode_InternFromString(# name))) \
goto fail;
...
ADD_INTERNED(readline)
..
即_PyIO_str_readline
是一个 unicode-object,值为 readline
。实际使用哪种 readline
方法,在 运行 时间内解决并取决于 self
实际上是什么。
关于这个问题,open()
方法是如何实现的,但是我找不到在其实现上使用的_PyIO_str_readline
函数是在哪里定义的。
你的问题是你认为,_PyIO_str_readline
是一个函数,但实际上它只是一个全局变量(PyObject *
类型),声明为here:
extern PyObject *_PyIO_str_readline;
并定义了here:
PyObject *_PyIO_str_readline = NULL;
是 NULL
,但顾名思义,可以是任何 string-object(即 Python3 中的 unicode 或 Python2 中的字节)。
_PyIO_str_readline
是一种缓存(在 CPython 中通常被称为 "interned string" - 参见 PyUnicode_InternFromString
),所以每次 PyObject_CallMethodObjArgs
都用 [=43 调用=] as method-name, 对应的对象一定不能重新构造.
_PyIO_str_readline
在 PyInit__io
中初始化为其实际值,使用宏 ADD_INTERNED
:
/* Interned strings */
#define ADD_INTERNED(name) \
if (!_PyIO_str_ ## name && \
!(_PyIO_str_ ## name = PyUnicode_InternFromString(# name))) \
goto fail;
...
ADD_INTERNED(readline)
..
即_PyIO_str_readline
是一个 unicode-object,值为 readline
。实际使用哪种 readline
方法,在 运行 时间内解决并取决于 self
实际上是什么。