简体   繁体   中英

Python sentinel in C++ extension

I'm working on a Python extension module written in C++.

According to the Python documentation the module method table should be written like this:

static PyMethodDef SpamMethods[] = {
    ...
    {"system",  spam_system, METH_VARARGS,
    "Execute a shell command."},
    ...
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

But I see that some developers write the sentinel like this:

static PyMethodDef SpamMethods[] = {
    ...
    {"system",  spam_system, METH_VARARGS,
    "Execute a shell command."},
    ...
    {}                          /* Sentinel */
};

My question is the following is there a risk to use the shortened version?

A pod class initialized with {} has fields not explicitly set zeroed.

If PyMethodDef is pod (plain old data), as I suspect, {NULL, NULL, 0, NULL} will generate the same data as {} .

This is true in both C and C++.

If the class PyMethodDef was a non-pod class in C++11, {NULL, NULL, 0, NULL} could do something different than {} , but I strongly doubt that is the case here.

The only concern I would have is if the library changed on me to make PyMethodDef a non-pod, and chose to make {} not zero the data at the same time. I would find that unlikely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM