ohmu
ohmu

Reputation: 19760

PyArg_ParseTuple default argument

If I have the following function and the optional argument myobj is not passed, does myobj remain NULL or is it set to Py_None?

static PyObject * myfunc(PyObject * self, PyObject * args) {
    PyObject * myobj = NULL;
    if (!PyArg_ParseTuple(args, "|O", &myobj)) {
        return NULL;
    }
    // ...
}

According Parsing arguments and building values,

| Indicates that the remaining arguments in the Python argument list are optional. The C variables corresponding to optional arguments should be initialized to their default value — when an optional argument is not specified, PyArg_ParseTuple() does not touch the contents of the corresponding C variable(s).

Does this apply to PyObject *s? It's obviously a pointer that exists in C so one could say it's a C variable, but it's a pointer to a python object so one could also say it does not count as a C variable.

Upvotes: 6

Views: 3600

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318518

It will remain NULL. And of course a pointer to a struct is a C object.

Upvotes: 6

Related Questions