PyObject_CallMethod with keyword arguments

后端 未结 2 1431
小鲜肉
小鲜肉 2021-02-15 12:47

I\'m trying to embed a Python (2.7) library in my C application and I\'m using the Python/C API to call Python code from C. I need to call a Python method that takes keyword arg

2条回答
  •  春和景丽
    2021-02-15 13:16

    In the end I used PyObject_Call to do this (thanks Bakuriu for the hint!). Just in case anyone wonders how to do that, here's my code:

    PyObject *args = Py_BuildValue("(s)", "blahdy blah");    
    PyObject *keywords = PyDict_New();
    PyDict_SetItemString(keywords, "somearg", Py_True);
    
    PyObject_Call(PyObject_GetAttrString(myobject, "do something"), args, keywords);
    Py_DECREF(args);
    Py_DECREF(keywords);
    

提交回复
热议问题