【问题标题】:Calling python hello world function from C, parsing string arguments从C调用python hello world函数,解析字符串参数
【发布时间】:2018-10-30 16:02:49
【问题描述】:

我使用this answer中的代码创建了以下文件

调用python.c

#include </usr/include/python2.7/Python.h>

int
main(int argc, char *argv[])
{
    PyObject *pName, *pModule, *pDict, *pFunc;
    PyObject *pArgs, *pValue;
    int i;

    if (argc < 3) {
        fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
        return 1;
    }

    Py_Initialize();
    pName = PyString_FromString(argv[1]);
    /* Error checking of pName left out */
    //fprintf(stderr,"pName is %s\n", pName);
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append(\".\")");
    //PySys_SetArgv(argc, argv);

    pModule = PyImport_Import(pName);
    Py_DECREF(pName);

    if (pModule != NULL) {
        pFunc = PyObject_GetAttrString(pModule, argv[2]);
        /* pFunc is a new reference */

        if (pFunc && PyCallable_Check(pFunc)) {
            pArgs = PyTuple_New(argc - 3);
            for (i = 0; i < argc - 3; ++i) {
                pValue = PyInt_FromLong(atoi(argv[i + 3]));
                if (!pValue) {
                    Py_DECREF(pArgs);
                    Py_DECREF(pModule);
                    fprintf(stderr, "Cannot convert argument\n");
                    return 1;
                } 
                /* iValue reference stolen here: */
                PyTuple_SetItem(pArgs, i, pValue);
                //PyTuple_SetItem(pArgs, i, argv[i + 3]);
            }
            pValue = PyObject_CallObject(pFunc, pArgs);
            Py_DECREF(pArgs);
            if (pValue != NULL) {
                printf("Result of call: %ld\n", PyInt_AsLong(pValue));
                Py_DECREF(pValue);
            }
            else {
                Py_DECREF(pFunc);
                Py_DECREF(pModule);
                PyErr_Print();
                fprintf(stderr,"Call failed\n");
                return 1;
            }
        }
        else {
            if (PyErr_Occurred())
                PyErr_Print();
            fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
        }
        Py_XDECREF(pFunc);
        Py_DECREF(pModule);
    }
    else {
        PyErr_Print();
        fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
        return 1;
    }
    Py_Finalize();
    return 0;
}

我在与 helloWorld.py 相同的目录中创建了另一个文件。这个python脚本的内容是

def helloworldFunc(a):
    print 'Hello '+str(a)

我编译并运行 callpython.c 如下

 g++ -o callpython callpython.c -lpython2.7 -lm -L/usr/lib/python2.7/config && ./callpython helloworld helloworldFunc world

它不是打印“Hello world”,而是打印“Hello 0”

为什么不将python函数参数解析为字符串?

【问题讨论】:

  • 你试过使用调试器吗?您是否尝试过使用更多参数运行程序?
  • @n.m.请看我的回答。

标签: python c python-2.7


【解决方案1】:

示例代码将参数解析为整数,购买你已经传递了一个字符串。 atoi("world") 返回 0,这就是你得到的整数:

/* Create tuple of the correct length for the arguments. */
pArgs = PyTuple_New(argc - 3);
for (i = 0; i < argc - 3; ++i) {
   /* Convert each C argv to a C integer, then to a Python integer. */
    pValue = PyInt_FromLong(atoi(argv[i + 3]));
    if (!pValue) {
        Py_DECREF(pArgs);
        Py_DECREF(pModule);
        fprintf(stderr, "Cannot convert argument\n");
        return 1;
    } 
    /* iValue reference stolen here: */
    /* Store the Python integer in the tuple at the correct offset (i) */
    PyTuple_SetItem(pArgs, i, pValue);
}

将转换行更改为以下以处理任何字符串:

pValue = PyString_FromString(argv[i + 3]);

【讨论】:

    【解决方案2】:

    解决了这个问题。罪魁祸首是这条线

    pValue = PyInt_FromLong(atoi(argv[i + 3]));
    

    它将python脚本的每个参数解析为整数。

    当替换为以下行时,它将每个参数解析为字符串

    pValue = PyString_FromString(argv[i+3]);
    

    我还没有真正理解 pValue 是如何工作的,但这暂时解决了问题。

    【讨论】:

    • 哈,你在我准备我的时候回答了。
    • @MarkTolonen 谢谢,如果在您的回答中,您可以添加对PyTuple_SetItem(pArgs, i, pValue); 的解释,那就太好了!
    • PyTuple_New() 创建一个大小等于函数参数数量的元组。 PyTuple_SetItem() 将元组的相应元素设置为为每个参数生成的 pValue 对象。有关详细信息,请参阅文档中的 PyTuple_SetItem
    • 也更新了我的答案。
    猜你喜欢
    • 2020-05-02
    • 1970-01-01
    • 2022-01-22
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    相关资源
    最近更新 更多