proper module init

This commit is contained in:
Michael Davidsaver
2013-04-14 12:57:35 -04:00
parent 47c47dfa6e
commit ec3e8e4519
4 changed files with 41 additions and 36 deletions

View File

@ -292,10 +292,12 @@ static PyTypeObject pyField_type = {
sizeof(pyField),
};
int pyField_prepare(void)
int pyField_prepare(PyObject *module)
{
size_t i;
import_array1(-1);
pyField_type.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
#if PY_MAJOR_VERSION < 3
pyField_type.tp_flags |= Py_TPFLAGS_HAVE_GETCHARBUFFER|Py_TPFLAGS_HAVE_NEWBUFFER;
@ -308,7 +310,12 @@ int pyField_prepare(void)
if(PyType_Ready(&pyField_type)<0)
return -1;
import_array1(-1);
PyObject *typeobj=(PyObject*)&pyField_type;
Py_INCREF(typeobj);
if(PyModule_AddObject(module, "_Field", typeobj)) {
Py_DECREF(typeobj);
return -1;
}
#ifdef HAVE_NUMPY
for(i=0; i<=DBF_MENU; i++) {
@ -320,13 +327,6 @@ int pyField_prepare(void)
return 0;
}
void pyField_setup(PyObject *module)
{
PyObject *typeobj=(PyObject*)&pyField_type;
Py_INCREF(typeobj);
PyModule_AddObject(module, "_Field", (PyObject*)&pyField_type);
}
void pyField_cleanup(void)
{
size_t i;

View File

@ -324,8 +324,10 @@ static PyTypeObject pyRecord_type = {
};
int pyRecord_prepare(void)
int pyRecord_prepare(PyObject *module)
{
PyObject *typeobj=(PyObject*)&pyRecord_type;
pyRecord_type.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
pyRecord_type.tp_methods = pyRecord_methods;
@ -338,12 +340,12 @@ int pyRecord_prepare(void)
if(PyType_Ready(&pyRecord_type)<0)
return -1;
Py_INCREF(typeobj);
if(PyModule_AddObject(module, "_Record", typeobj)) {
Py_DECREF(typeobj);
return -1;
}
return 0;
}
void pyRecord_setup(PyObject *module)
{
PyObject *typeobj=(PyObject*)&pyRecord_type;
Py_INCREF(typeobj);
PyModule_AddObject(module, "_Record", (PyObject*)&pyRecord_type);
}

View File

@ -13,12 +13,10 @@
#endif
void pyDBD_cleanup(void);
int pyField_prepare(void);
void pyField_setup(PyObject *module);
int pyField_prepare(PyObject *module);
void pyField_cleanup(void);
int pyRecord_prepare(void);
void pyRecord_setup(PyObject *module);
int pyRecord_prepare(PyObject *module);
int isPyRecord(dbCommon *);
int canIOScanRecord(dbCommon *);

View File

@ -150,40 +150,45 @@ static struct PyModuleDef dbapimodule = {
/* initialize "magic" builtin module */
PyMODINIT_FUNC init_dbapi(void)
{
PyObject *mod, *hookdict, *pysuptable;
PyObject *mod = NULL, *hookdict;
pystate *st;
import_array();
if(pyField_prepare())
MODINIT_RET(NULL);
if(pyRecord_prepare())
MODINIT_RET(NULL);
#if PY_MAJOR_VERSION >= 3
mod = PyModule_Create(&dbapimodule);
#else
mod = Py_InitModule("_dbapi", devsup_methods);
#endif
pysuptable = PySet_New(NULL);
if(!pysuptable)
MODINIT_RET(NULL);
PyModule_AddObject(mod, "_supports", pysuptable);
if(!mod)
goto fail;
hookdict = PyDict_New();
if(!hookdict)
MODINIT_RET(NULL);
goto fail;
PyModule_AddObject(mod, "_hooks", hookdict);
for(st = statenames; st->name; st++) {
PyDict_SetItemString(hookdict, st->name, PyInt_FromLong((long)st->state));
PyObject *ent = PyInt_FromLong((long)st->state);
if(!ent) goto fail;
if(PyDict_SetItemString(hookdict, st->name, ent)) {
Py_DECREF(ent);
goto fail;
}
}
pyField_setup(mod);
pyRecord_setup(mod);
if(pyField_prepare(mod))
goto fail;
if(pyRecord_prepare(mod))
goto fail;
MODINIT_RET(mod);
fail:
fprintf(stderr, "Failed to initialize builtin _dbapi module!\n");
Py_XDECREF(mod);
MODINIT_RET(NULL);
}
static void cleanupPy(void *junk)