Creating a C extension for Python that requires a different extension

I have a couple of Python functions that I use to simplify game development with Pygame. I have them in the helper.py file in my Python path, so I can import them from any game I make. I was thinking of an exercise to learn about Python extensions in order to convert this module to C. My first problem is that I need to use Pygame functions, and I'm not sure if this is possible. Pygame installs some header files, but they don't seem to have C versions of the Python functions. Maybe I missed something.

How can i solve this? As a workaround, the function currently takes a function parameter and calls it, but this is not an ideal solution.

Using Windows XP, Python 2.6 and Pygame 1.9.1, by the way.

+5
source share
3 answers
/* get the sys.modules dictionary */
PyObject* sysmodules PyImport_GetModuleDict();
PyObject* pygame_module;
if(PyMapping_HasKeyString(sysmodules, "pygame")) {
    pygame_module = PyMapping_GetItemString(sysmodules, "pygame");
} else {
    PyObject* initresult;
    pygame_module = PyImport_ImportModule("pygame");
    if(!pygame_module) {
      /* insert error handling here! and exit this function */
    }
    initresult = PyObject_CallMethod(pygame_module, "init", NULL);
    if(!initresult) {
      /* more error handling &c */
    }
    Py_DECREF(initresult);
}
/* use PyObject_CallMethod(pygame_module, ...) to your heart contents */
/* and lastly, when done, don't forget, before you exit, to: */
Py_DECREF(pygame_module);
+6
source

You can import python modules from C code and call things defined just like you can in python code. It is a little long, but quite possible.

When I want to figure out how to do this, I look at the C API documentation . The module import section will help. You will also need to read how to read the attributes, call functions, etc. that are in the documents.

However, I suspect what you really want to do is call the sdl base library from C. It is a C library and very simple to use with C.

python C,

PyObject *module = 0;
PyObject *result = 0;
PyObject *module_dict = 0;
PyObject *func = 0;

module = PyImport_ImportModule((char *)"pygame"); /* new ref */
if (module == 0)
{
    PyErr_Print();
    log("Couldn't find python module pygame");
    goto out;
}
module_dict = PyModule_GetDict(module); /* borrowed */
if (module_dict == 0)
{
    PyErr_Print();
    log("Couldn't find read python module pygame");
    goto out;
}
func = PyDict_GetItemString(module_dict, "pygame_function"); /* borrowed */
if (func == 0)
{
    PyErr_Print();
    log("Couldn't find pygame.pygame_function");
    goto out;
}
result = PyEval_CallObject(func, NULL); /* new ref */
if (result == 0)
{
    PyErr_Print();
    log("Couldn't run pygame.pygame_function");
    goto out;
}
/* do stuff with result */
out:;
Py_XDECREF(result);
Py_XDECREF(module);
+3

Most of the functions in the module pygameare just wrappers around the SDL functions, this is where you should look for the C version of your functions. pygame.hdefines a number of functions import_pygame_*(). Call import_pygame_base()others one time when initializing the extension module to access the required part of the C API pygame modules (it is defined in the header file for each). A Google code search leads you to a few examples .

0
source

All Articles