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.

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144

1 Answers1

6

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

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636