Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add PyArg_ParseArray() function #97

@vstinner

Description

@vstinner

Hi,

I propose adding the following functions to parse arguments of functions using the METH_FASTCALL calling convention:

int PyArg_ParseArray(
    PyObject *const *args,
    Py_ssize_t nargs,
    const char *format,
    ...);

int PyArg_ParseArrayAndKeywords(
    PyObject *const *args,
    Py_ssize_t nargs,
    PyObject *kwnames,
    const char *format,
    char * const *kwlist,
    ...);

So what do you think of these functions?

Example with positional only arguments (METH_FASTCALL):

static PyObject*
func(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
{   
    int arg1, arg2;
    if (!PyArg_ParseArray(args, nargs, "ii",
                           &arg1, &arg2)) {
        return NULL;
    }
    ...
}

Example with keywords (METH_FASTCALL | METH_KEYWORDS):

static PyObject*
func(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
    static char *kwlist[] = {"arg1", "arg2", NULL};
    int arg1, arg2;
    if (!PyArg_ParseArrayAndKeywords(args, nargs, kwnames,
                                     "ii", kwlist,
                                     &arg1, &arg2)) {
        return NULL;
    }
    ...
}

These functions are similar to PyArg_ParseTuple() and PyArg_ParseTupleAndKeywords() which are used for the METH_VARARGS calling convention, but expect arguments in the METH_FASTCALL format.

Python already has internal functions _PyArg_ParseStack() and _PyArg_ParseStackAndKeywords(). _PyArg_ParseStack() is the same as PyArg_ParseArray(). _PyArg_ParseStackAndKeywords() has the same purpose than PyArg_ParseArrayAndKeywords(), but requires a _PyArg_Parser argument which is non-trivial to initialize and declared as a static function variable.

See also:

UPDATE: Function PyArg_ParseVector() renamed to PyArg_ParseArray().

Victor

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions