获取 C++ 中的 Python 函数参数名称
Getting Python function argument names in C++
我正在尝试获取从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;
}
相关文章:
- 将可变参数函数的参数封装在类实例中
- QML 使用带有参数C++函数
- 使用可变参数函数作为模板参数
- 如何在C++中伪造虚拟可变参数函数模板?
- 为什么可变参数函数不适用于模板
- C++ std::functional 中的可变参数函数模板
- 可变参数函数指针的定义对于VxWorks spyLib来说不清楚
- 使用可变参数函数覆盖具有不同函数签名的虚函数
- 考虑引用和常量的可变参数函数包装器
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 在可变参数函数中转发特定范围的参数
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 使用带有一个参数函数的递归找到数字的平方
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 多个可变参数函数的单个模板参数包?
- 参数数据类型未知的可变参数函数
- 可变参数函数参数包扩展
- 使用模板可变参数函数将多个参数传递给另一个函数
- 对可变参数函数的递归调用的链接器错误
- 通过像printf这样的可变参数函数传递一个带有常量字符*转换函数的类