Accessing Python C-API String Constants

I wanted to implement a library I wrote for python in C using the C-API for python. In python, I can declare "constants" in my module by simply specifying:

RED = "red"   # Not really a constant, I know
BLUE = "blue" # but suitable, nevertheless

def solve(img_h):
    # Awesome computations
    return (RED, BLUE)[some_flag]

      

These constants are then returned by functions provided by the module. I have some problems with the same case in C. Here is what I got so far:

PyMODINIT_FUNC
PyInit_puzzler(void)
{
    PyObject* module = PyModule_Create(&Module);
    (void) PyModule_AddStringConstant(module, "BLUE",   "blue");
    (void) PyModule_AddStringConstant(module, "RED",    "red");
    return module;
}

PyObject* solve(PyObject* module, PyObject* file_handle)
{
    // Do some awesome computations based on the file
    // Involves HUGE amounts of memory management, thus efficient in C
    // PROBLEM: How do I return the StringConstants from here?
    return some_flag ? BLUE : RED;
}

      

I have already noted the problematic part. After I add the string constants to the module using PyModule_AddStringConstant(module, "FOO", "foo");

, how can I actually return them PyObject*

from both my methods? Should I increase the ref-counter when I return them?

+3


source to share


1 answer


Since PyModule_AddStringConstant (module, name, value) adds a constant to the module, it must be available from the module dictionary, which can be purchased from PyModule_GetDict (module) . Then you can access any attribute from the module through its dictionary using PyDict_GetItemString (dict, key) This is how you can access the constants from your module (after their definitions):

// Get module dict. This is a borrowed reference.
PyObject* module_dict = PyModule_GetDict(module);

// Get BLUE constant. This is a borrowed reference.
PyObject* BLUE = PyDict_GetItemString(module_dict, "BLUE");

// Get RED constant. This is a borrowed reference.
PyObject* RED = PyDict_GetItemString(module_dict, "RED");

      



To put this into context with your function solve()

, you need something similar to:

PyObject* solve(PyObject* module, PyObject* file_handle)
{
    // Do some awesome computations based on the file
    // Involves HUGE amounts of memory management, thus efficient in C

    // Return string constant at the end.
    PyObject* module_dict = PyModule_GetDict(module);
    PyObject* constant = NULL;
    if (some_flag) {
        // Return BLUE constant. Since BLUE is a borrowed 
        // reference, increment its reference count before 
        // returning it.
        constant = PyDict_GetItemString(module_dict, "BLUE");
        Py_INCREF(constant);
    } else {
        // Return RED constant. Since RED is a borrowed 
        // reference, increment its reference count before 
        // returning it.
        constant = PyDict_GetItemString(module_dict, "RED");
        Py_INCREF(constant);
    }

    // NOTE: Before you return, make sure to release any owned
    // references that this function acquired. `module_dict` does
    // not need to be released because it is merely "borrowed".

    // Return the constant (either BLUE or RED) as an owned
    // reference. Whatever calls `solve()` must make sure to
    // release the returned reference with `Py_DECREF()`.
    return constant;
}

      

+3


source







All Articles