PyArg_ParseTuple default argument
Asked Answered
B

1

6

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.

Binal answered 11/4, 2012 at 19:33 Comment(0)
V
6

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

Verlie answered 11/4, 2012 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.