python - 在 C++ 中获取 Python 函数参数名称

标签 python c++ embedding

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

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

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

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

最佳答案

您说过如果您想从 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*y\n");

    // 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 %s\n", i, arg_c_str);
    }

    Py_Finalize();
    PyMem_RawFree(program);

    return 0;
}

关于python - 在 C++ 中获取 Python 函数参数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50617465/

相关文章:

python - 如何从 pandas 数据框中的文本字符串中提取所有形容词?

python - Tensorflow:对 tf.estimator.inputs.numpy_input_fn 函数进行故障排除

c++ - 引入新变量是否会破坏返回值优化?

c++ - 清除谷歌测试错误中的标准输出错误

c++ - Case 语句中的数组

c++ - 适合在线嵌入的SVM库

在嵌入式单声道中捕获异常

html - 将非方形 Youtube 视频嵌入为方形

python - 在 Linux 上的 python 中完成文件传输后执行代码

python - 删除字符串/单词的方法