I'm working on making my first Python C extension, which defines a few functions and custom types. The strange thing is that the custom types are working, but not the regular functions. The top-level MyModule.c file looks like this:
static PyMethodDef MyModule_methods[] = {
{"doStuff", MyModule_doStuff, METH_VARARGS, ""},
{NULL, NULL, 0, NULL} /* Sentinel */
};
static struct PyModuleDef MyModule_module = {
PyModuleDef_HEAD_INIT,
"mymodule",
"Documentation",
-1,
MyModule_methods
};
PyMODINIT_FUNC PyInit_audioDevice(void) {
PyObject *object = PyModule_Create(&MyModule_module);
if(object == NULL) {
return NULL;
}
if(PyType_Ready(&MyCustomType_type) < 0) {
return NULL;
}
Py_INCREF(&MyCustomType_type);
PyModule_AddObject(object, "MyCustomType", (PyObject*)&MyCustomType_type);
return object;
}
I'm building the extension with this setup.py file:
from distutils.core import setup, Extension
setup(name = "mymodule",
version = "1.0",
ext_modules = [Extension("mymodule", ["MyModule.c", "MyCustomType.c", "DoStuff.c"])])
The "DoStuff" file defines its function as such:
static PyObject*
AudioOutputOSX_doStuff(PyObject *self, PyObject *args) {
printf("Hello from doStuff\n");
return Py_None;
}
The funny thing is that the MyCustomType type works fine, as I can instantiate it with:
from mymodule.MyCustomType import MyCustomType
foo = MyCustomType()
And I see my printf() statements from the custom type's new and init methods printed out. However, this code fails:
import mymodule
mymodule.doStuff()
I get the following error: Traceback (most recent call last): File "MyModuleTest.py", line 9, in mymodule.doStuff(buffer) AttributeError: 'module' object has no attribute 'doStuff'
What is going on here? Do I have some error in my module's method declarations somehow?