summaryrefslogtreecommitdiff
path: root/src/block_template.c
diff options
context:
space:
mode:
authorDwayne Litzenberger <dlitz@dlitz.net>2013-07-14 19:06:58 -0700
committerDwayne Litzenberger <dlitz@dlitz.net>2013-07-14 19:20:43 -0700
commit6fbddf912294b96a66f0e18b32c9312c67455ad5 (patch)
treeff318d3c01a831344c7ba16f14d5d7f2632622f5 /src/block_template.c
parent385830424043c81945a21ca14e051e3b4c282829 (diff)
downloadpycrypto-6fbddf912294b96a66f0e18b32c9312c67455ad5.tar.gz
Py3k cleanup: Module initialization
Diffstat (limited to 'src/block_template.c')
-rw-r--r--src/block_template.c66
1 files changed, 40 insertions, 26 deletions
diff --git a/src/block_template.c b/src/block_template.c
index e5e9d16..94ad224 100644
--- a/src/block_template.c
+++ b/src/block_template.c
@@ -725,23 +725,20 @@ static struct PyModuleDef moduledef = {
PyMODINIT_FUNC
_MODULE_NAME (void)
{
- PyObject *m;
+ PyObject *m = NULL;
+ PyObject *abiver = NULL;
-#ifdef IS_PY3K
if (PyType_Ready(&ALGtype) < 0)
- return NULL;
+ goto errout;
/* Create the module and add the functions */
+#ifdef IS_PY3K
m = PyModule_Create(&moduledef);
- if (m == NULL)
- return NULL;
#else
- if (PyType_Ready(&ALGtype) < 0)
- return;
-
- /* Create the module and add the functions */
m = Py_InitModule("Crypto.Cipher." _MODULE_STRING, modulemethods);
#endif
+ if (m == NULL)
+ goto errout;
PyModule_AddIntConstant(m, "MODE_ECB", MODE_ECB);
PyModule_AddIntConstant(m, "MODE_CBC", MODE_CBC);
@@ -755,28 +752,45 @@ _MODULE_NAME (void)
/* Import CounterBE and CounterLE from the _counter module */
Py_CLEAR(_counter_module);
_counter_module = PyImport_ImportModule("Crypto.Util._counter");
- if (_counter_module) {
- PCT_CounterBEType = (PyTypeObject *)PyObject_GetAttrString(_counter_module, "CounterBE");
- PCT_CounterLEType = (PyTypeObject *)PyObject_GetAttrString(_counter_module, "CounterLE");
-
- /* Simple ABI version check in case the user doesn't re-compile all of
- * the modules during an upgrade. */
- PyObject *abiver = PyObject_GetAttrString(_counter_module, "_PCT_CTR_ABI_VERSION");
- if (PCT_CounterBEType == NULL || PyType_Check((PyObject *)PCT_CounterBEType) < 0 ||
- PCT_CounterLEType == NULL || PyType_Check((PyObject *)PCT_CounterLEType) < 0 ||
- abiver == NULL || PyInt_CheckExact(abiver) < 0 || PyInt_AS_LONG(abiver) != PCT_CTR_ABI_VERSION)
- {
- PyErr_SetString(PyExc_ImportError, "Crypto.Util._counter ABI mismatch. Was PyCrypto incorrectly compiled?");
- }
- Py_CLEAR(abiver);
+ if (_counter_module == NULL)
+ goto errout;
+ PCT_CounterBEType = (PyTypeObject *)PyObject_GetAttrString(_counter_module, "CounterBE");
+ PCT_CounterLEType = (PyTypeObject *)PyObject_GetAttrString(_counter_module, "CounterLE");
+
+ /* Simple ABI version check in case the user doesn't re-compile all of
+ * the modules during an upgrade. */
+ abiver = PyObject_GetAttrString(_counter_module, "_PCT_CTR_ABI_VERSION");
+ if (PCT_CounterBEType == NULL || PyType_Check((PyObject *)PCT_CounterBEType) < 0 ||
+ PCT_CounterLEType == NULL || PyType_Check((PyObject *)PCT_CounterLEType) < 0 ||
+ abiver == NULL || PyInt_CheckExact(abiver) < 0 || PyInt_AS_LONG(abiver) != PCT_CTR_ABI_VERSION)
+ {
+ PyErr_SetString(PyExc_ImportError, "Crypto.Util._counter ABI mismatch. Was PyCrypto incorrectly compiled?");
+ goto errout;
}
- /* Check for errors */
- if (PyErr_Occurred())
- Py_FatalError("can't initialize module " _MODULE_STRING);
+out:
+ /* Final error check */
+ if (m == NULL && !PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ImportError, "can't initialize module");
+ goto errout;
+ }
+ /* Free local objects here */
+ Py_CLEAR(abiver);
+
+ /* Return */
#ifdef IS_PY3K
return m;
+#else
+ return;
#endif
+
+errout:
+ /* Free the module and other global objects here */
+ Py_CLEAR(m);
+ Py_CLEAR(_counter_module);
+ Py_CLEAR(PCT_CounterBEType);
+ Py_CLEAR(PCT_CounterLEType);
+ goto out;
}
/* vim:set ts=4 sw=4 sts=0 noexpandtab: */