Parsing User Defined Types Using PyArg_ParseTuple

旧巷老猫 提交于 2019-12-04 13:46:42

Instead of using the plain O format, as Martijn suggested, I normally prefer using the O& format. It allows you to pass a function that will be called to convert any PyObject* to an arbitrary C (double) pointer. Here is some example usage, in which I'm converting a passed value to a pointer to my own object type:

/** 
 * This method should return 0 if it does not work or 1 in case it does
 * PyArg_*() functions will handle the rest from there or let your program
 * continue in case things are fine.
 */
int MyConverter(PyObject* o, MyOwnType** mine) {
  //write the converter here.
}

Then, at the point you need to parse your object:

/**
 * Simple example
 */
static PyObject* py_do_something(PyObject*, PyObject* args, PyObject* kwds) {

    /* Parses input arguments in a single shot */
    static char* kwlist[] = {"o", 0};

    MyOwnType* obj = 0; ///< if things go OK, obj will be there after the next call

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&", kwlist, &MyConverter, &obj))
      return 0; ///< we have failed, let Python check exceptions.

    /* if you get to this point, your converter worked, just use */
    /* your newly allocated object and free it, when done. */

}

The advantage of this approach is that you can encapsulate your MyConverter on a C-API and then re-use it in other functions for the the same job.

Custom python classes can be parsed using the O format:

O (object) [PyObject *]
Store a Python object (without any conversion) in a C object pointer. The C program thus receives the actual object that was passed. The object’s reference count is not increased. The pointer stored is not NULL.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!