From ec3e8e45195084fecf50b7c1d878fe430f53c420 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Sun, 14 Apr 2013 12:57:35 -0400 Subject: [PATCH] proper module init --- devsupApp/src/dbfield.c | 18 +++++++++--------- devsupApp/src/dbrec.c | 18 ++++++++++-------- devsupApp/src/pydevsup.h | 6 ++---- devsupApp/src/setup.c | 35 ++++++++++++++++++++--------------- 4 files changed, 41 insertions(+), 36 deletions(-) diff --git a/devsupApp/src/dbfield.c b/devsupApp/src/dbfield.c index facc9be..8450432 100644 --- a/devsupApp/src/dbfield.c +++ b/devsupApp/src/dbfield.c @@ -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; diff --git a/devsupApp/src/dbrec.c b/devsupApp/src/dbrec.c index 2145a08..acdf066 100644 --- a/devsupApp/src/dbrec.c +++ b/devsupApp/src/dbrec.c @@ -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); -} diff --git a/devsupApp/src/pydevsup.h b/devsupApp/src/pydevsup.h index b106e28..67ad62d 100644 --- a/devsupApp/src/pydevsup.h +++ b/devsupApp/src/pydevsup.h @@ -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 *); diff --git a/devsupApp/src/setup.c b/devsupApp/src/setup.c index c93f7d5..1fa04d9 100644 --- a/devsupApp/src/setup.c +++ b/devsupApp/src/setup.c @@ -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)