CPython 源代码中 math_sin 函数的定义?
Definition of math_sin function in the CPython source code?
我正在研究 CPython 的代码库。
我想知道在哪里可以找到mathmodule.c
中mathmethods
table中出现的math_sin
函数的定义:
{"sin", math_sin, METH_O, math_sin_doc}
仅在 cpython
主文件夹中执行 grep "math_sin" -wr
returns:
Modules/mathmodule.c: {"sin", math_sin, METH_O, math_sin_doc},
在哪里可以找到这个函数的定义?
math_sin
通过 FUNC1
macro:
定义
FUNC1(sin, sin, 0,
"sin($module, x, /)\n--\n\n"
"Return the sine of x (measured in radians).")
#define FUNC1(funcname, func, can_overflow, docstring) \
static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
return math_1(args, func, can_overflow); \
}\
PyDoc_STRVAR(math_##funcname##_doc, docstring);
因此预处理器将其扩展为:
static PyObject * math_sin(PyObject *self, PyObject *args) {
return math_1(args, sin, 0);
}
PyDoc_STRVAR(math_sin_doc, "sin($module, x, /)\n--\n\n"
"Return the sine of x (measured in radians).");
(然后全部在一行中,并且 PyDoc_STRVAR
宏也已扩展)
所以 math_sin(module, args)
基本上是对 math_1(args, sin, 0)
的调用,而 math_1(args, sin, 0)
calls math_1_to_whatever(args, sin, PyFloat_FromDouble, 0)
负责验证是否传入了 Python 浮点数,并将其转换为 C double,调用 sin(arg_as_double)
,根据需要引发异常或使用 return 之前 math_1()
传入的 PyFloat_FromDouble
函数包装来自 sin()
的双 return 值]将该结果发送给调用者。
sin()
这里是the double sin(double x)
function defined in POSIX math.h
.
原则上,您可以预处理整个 Python 源代码树并将输出转储到新目录中;以下确实假定您已经成功构建了 python
二进制文件,因为它用于提取 gcc
:
的必要包含标志
find . -type d -exec mkdir -p /tmp/processed/{} \;
(export FLAGS=$(./python.exe -m sysconfig | grep PY_CORE_CFLAGS | cut -d\" -f2) && \
find . -type f \( -name '*.c' -o -name '*.h' \) -exec gcc -E $FLAGS {} -o /tmp/processed/{} \;)
然后 math_sin
将出现在 /tmp/preprocessed/Modules/mathmodule.c
。
或者您可以使用 -save-temps
标志告诉编译器将预处理器输出保存到 .i
文件:
make clean && make CC="gcc -save-temps"
你会在 Modules/mathmodule.i
中找到 make_sin
。
我正在研究 CPython 的代码库。
我想知道在哪里可以找到mathmodule.c
中mathmethods
table中出现的math_sin
函数的定义:
{"sin", math_sin, METH_O, math_sin_doc}
仅在 cpython
主文件夹中执行 grep "math_sin" -wr
returns:
Modules/mathmodule.c: {"sin", math_sin, METH_O, math_sin_doc},
在哪里可以找到这个函数的定义?
math_sin
通过 FUNC1
macro:
FUNC1(sin, sin, 0,
"sin($module, x, /)\n--\n\n"
"Return the sine of x (measured in radians).")
#define FUNC1(funcname, func, can_overflow, docstring) \
static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
return math_1(args, func, can_overflow); \
}\
PyDoc_STRVAR(math_##funcname##_doc, docstring);
因此预处理器将其扩展为:
static PyObject * math_sin(PyObject *self, PyObject *args) {
return math_1(args, sin, 0);
}
PyDoc_STRVAR(math_sin_doc, "sin($module, x, /)\n--\n\n"
"Return the sine of x (measured in radians).");
(然后全部在一行中,并且 PyDoc_STRVAR
宏也已扩展)
所以 math_sin(module, args)
基本上是对 math_1(args, sin, 0)
的调用,而 math_1(args, sin, 0)
calls math_1_to_whatever(args, sin, PyFloat_FromDouble, 0)
负责验证是否传入了 Python 浮点数,并将其转换为 C double,调用 sin(arg_as_double)
,根据需要引发异常或使用 return 之前 math_1()
传入的 PyFloat_FromDouble
函数包装来自 sin()
的双 return 值]将该结果发送给调用者。
sin()
这里是the double sin(double x)
function defined in POSIX math.h
.
原则上,您可以预处理整个 Python 源代码树并将输出转储到新目录中;以下确实假定您已经成功构建了 python
二进制文件,因为它用于提取 gcc
:
find . -type d -exec mkdir -p /tmp/processed/{} \;
(export FLAGS=$(./python.exe -m sysconfig | grep PY_CORE_CFLAGS | cut -d\" -f2) && \
find . -type f \( -name '*.c' -o -name '*.h' \) -exec gcc -E $FLAGS {} -o /tmp/processed/{} \;)
然后 math_sin
将出现在 /tmp/preprocessed/Modules/mathmodule.c
。
或者您可以使用 -save-temps
标志告诉编译器将预处理器输出保存到 .i
文件:
make clean && make CC="gcc -save-temps"
你会在 Modules/mathmodule.i
中找到 make_sin
。