获取 C++ 中的 Python 函数参数名称

Getting Python function argument names in C++

本文关键字:参数 函数 Python C++ 中的 获取      更新时间:2023-10-16

我正在尝试获取从C++调用的Python函数的参数名称。一点背景:

我有一个C++应用程序需要调用 Python 函数,按名称传递参数。到目前为止,我已经能够通过解析 Python 模块的.py文件来做到这一点;但是我也希望能够处理 .pyc 文件。

理想情况下,这将通过Python C API完成(而不是尝试反编译字节码(,但我看不到任何明显的方法。在 Python 中有一些方法可以做到这一点,例如使用 inspect 但不在 C++ 中。

有人知道可能的解决方案吗?蒂亚。

你说如果你想从python中做到这一点,你会使用inspect模块。为什么不从 C API 调用inspect模块?

以下 C 代码打印 python 函数的所有参数。它也应该是有效的C++代码。我不经常使用Python C API,所以请告诉我是否有一些可以改进的地方。

#include <Python.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
wchar_t* const program = Py_DecodeLocale(argv[0], nullptr);
Py_SetProgramName(program);
Py_Initialize();
// Define a python function f with argument names x and y.
// >>> def f(x, y): return x*y
PyRun_SimpleString("def f(x, y): return x*yn");
// Get the function as python object.
// >>> import sys
// >>> f_function = sys.modules["__main__"].f
PyObject* const sys_module_name = PyUnicode_DecodeFSDefault("sys");
PyObject* const sys_module = PyImport_Import(sys_module_name);
PyObject* const modules_dict = PyObject_GetAttrString(sys_module, "modules");
PyObject* const main_name = PyUnicode_DecodeFSDefault("__main__");
PyObject* const main_module = PyDict_GetItem(modules_dict, main_name);
PyObject* const f_function = PyObject_GetAttrString(main_module, "f");
// Get the inspect.getargspec function.
// >>> import inspect
// >>> getargspec_function = inspect.getargspec
PyObject* const inspect_module_name = PyUnicode_DecodeFSDefault("inspect");
PyObject* const inspect_module = PyImport_Import(inspect_module_name);
PyObject* const getargspec_function = PyObject_GetAttrString(inspect_module, "getargspec");
// Call the inspect.getargspec function.
// >>> argspec = getargspec_function(f_function)
PyObject* const argspec_call_args = PyTuple_New(1);
PyTuple_SetItem(argspec_call_args, 0, f_function);
PyObject* const argspec = PyObject_CallObject(getargspec_function, argspec_call_args);
// Get args from argspec.
// >>> f_args = argspec.args
PyObject* const f_args = PyObject_GetAttrString(argspec, "args");
// f_args now holds a python list with all arguments of f.
// As example usage, you can print the arguments:
// >>> for i, a in enumerate(f_args):
// ...     print("Repr of arg", i, "is", repr(a))
Py_ssize_t const num_args = PyList_Size(f_args);
for (Py_ssize_t i = 0; i < num_args; ++i)
{
PyObject* const arg = PyList_GetItem(f_args, i);
PyObject* const arg_repr = PyObject_Repr(arg);
PyObject* const arg_str = PyUnicode_AsASCIIString(arg_repr);
char const* const arg_c_str = PyBytes_AS_STRING(arg_str);
printf("Repr of arg %ld is %sn", i, arg_c_str);
}
Py_Finalize();
PyMem_RawFree(program);
return 0;
}