From 6fbddf912294b96a66f0e18b32c9312c67455ad5 Mon Sep 17 00:00:00 2001 From: Dwayne Litzenberger Date: Sun, 14 Jul 2013 19:06:58 -0700 Subject: Py3k cleanup: Module initialization --- src/_counter.c | 39 ++++++++++++++++++------------ src/_fastmath.c | 43 ++++++++++++++++++++++----------- src/block_template.c | 66 +++++++++++++++++++++++++++++++-------------------- src/hash_template.c | 34 ++++++++++++++++---------- src/stream_template.c | 42 +++++++++++++++++++------------- src/strxor.c | 29 ++++++++++++++++------ src/winrand.c | 36 +++++++++++++++++++--------- 7 files changed, 188 insertions(+), 101 deletions(-) (limited to 'src') diff --git a/src/_counter.c b/src/_counter.c index d8805db..59ceb54 100644 --- a/src/_counter.c +++ b/src/_counter.c @@ -495,30 +495,21 @@ PyInit__counter(void) init_counter(void) #endif { - PyObject *m; + PyObject *m = NULL; - /* TODO - Is the error handling here correct? */ -#ifdef IS_PY3K if (PyType_Ready(&PCT_CounterLEType) < 0) - return NULL; + goto errout; if (PyType_Ready(&PCT_CounterBEType) < 0) - return NULL; + goto errout; /* Initialize the module */ +#ifdef IS_PY3K m = PyModule_Create(&moduledef); - if (m == NULL) - return NULL; - #else - if (PyType_Ready(&PCT_CounterLEType) < 0) - return; - if (PyType_Ready(&PCT_CounterBEType) < 0) - return; - m = Py_InitModule("_counter", module_methods); - if (m == NULL) - return; #endif + if (m == NULL) + goto errout; /* Add the counter types to the module so that epydoc can see them, and so * that we can access them in the block cipher modules. */ @@ -528,9 +519,27 @@ init_counter(void) /* Allow block_template.c to do an ABI check */ PyModule_AddIntConstant(m, "_PCT_CTR_ABI_VERSION", PCT_CTR_ABI_VERSION); + +out: + /* Final error check */ + if (m == NULL && !PyErr_Occurred()) { + PyErr_SetString(PyExc_ImportError, "can't initialize module"); + goto errout; + } + + /* Free local objects here */ + + /* Return */ #ifdef IS_PY3K return m; +#else + return; #endif + +errout: + /* Free the module and other global objects here */ + Py_CLEAR(m); + goto out; } /* vim:set ts=4 sw=4 sts=4 expandtab: */ diff --git a/src/_fastmath.c b/src/_fastmath.c index 063ad70..7d486c2 100644 --- a/src/_fastmath.c +++ b/src/_fastmath.c @@ -1598,35 +1598,50 @@ PyInit__fastmath (void) init_fastmath (void) #endif { - PyObject *m; + PyObject *m = NULL; -#ifdef IS_PY3K if (PyType_Ready(&rsaKeyType) < 0) - return NULL; + goto errout; if (PyType_Ready(&dsaKeyType) < 0) - return NULL; - + goto errout; + + /* Initialize the module */ +#ifdef IS_PY3K m = PyModule_Create(&moduledef); - if (m == NULL) - return NULL; #else - if (PyType_Ready(&rsaKeyType) < 0) - return; - if (PyType_Ready(&dsaKeyType) < 0) - return; m = Py_InitModule ("_fastmath", _fastmath__methods__); #endif + if (m == NULL) + goto errout; + fastmathError = PyErr_NewException ("_fastmath.error", NULL, NULL); -#ifdef IS_PY3K - if (fastmathError == NULL) return NULL; -#endif + if (fastmathError == NULL) + goto errout; PyObject_SetAttrString(m, "error", fastmathError); PyModule_AddIntConstant(m, "HAVE_DECL_MPZ_POWM_SEC", HAVE_DECL_MPZ_POWM_SEC); +out: + /* Final error check */ + if (m == NULL && !PyErr_Occurred()) { + PyErr_SetString(PyExc_ImportError, "can't initialize module"); + goto errout; + } + + /* Free local objects here */ + + /* Return */ #ifdef IS_PY3K return m; +#else + return; #endif + +errout: + /* Free the module and other global objects here */ + Py_CLEAR(m); + Py_CLEAR(fastmathError); + goto out; } /* The first 10000 primes to be used as a base for sieving */ 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: */ diff --git a/src/hash_template.c b/src/hash_template.c index f3f116d..106e70b 100644 --- a/src/hash_template.c +++ b/src/hash_template.c @@ -300,32 +300,42 @@ static struct PyModuleDef moduledef = { PyMODINIT_FUNC _MODULE_NAME (void) { - PyObject *m; + PyObject *m = 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; - m = Py_InitModule3("Crypto.Hash." _MODULE_STRING, ALG_functions, MODULE__doc__); #endif + if (m == NULL) + goto errout; /* Add some symbolic constants to the module */ PyModule_AddIntConstant(m, "digest_size", DIGEST_SIZE); PyModule_AddIntConstant(m, "block_size", BLOCK_SIZE); - /* Check for errors */ - if (PyErr_Occurred()) - Py_FatalError("can't initialize module " - _MODULE_STRING); +out: + /* Final error check, then return */ + if (m == NULL && !PyErr_Occurred()) { + PyErr_SetString(PyExc_ImportError, "can't initialize module"); + goto errout; + } + + /* Free local objects here */ + + /* Return */ #ifdef IS_PY3K return m; +#else + return; #endif + +errout: + /* Free the module and other global objects here */ + Py_CLEAR(m); + goto out; } diff --git a/src/stream_template.c b/src/stream_template.c index cb11a0c..1770a3c 100644 --- a/src/stream_template.c +++ b/src/stream_template.c @@ -277,34 +277,44 @@ static PyTypeObject ALGtype = PyMODINIT_FUNC _MODULE_NAME (void) { - PyObject *m; - -#ifdef IS_PY3K + PyObject *m = NULL; + if (PyType_Ready(&ALGtype) < 0) - return NULL; + goto errout; +#ifdef IS_PY3K /* Create the module and add the functions */ 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 - - PyModule_AddIntConstant(m, "block_size", BLOCK_SIZE); + if (m == NULL) + goto errout; + + PyModule_AddIntConstant(m, "block_size", BLOCK_SIZE); PyModule_AddIntConstant(m, "key_size", KEY_SIZE); - /* 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 */ + /* Return */ #ifdef IS_PY3K return m; +#else + return; #endif - } - + +errout: + /* Free the module and other global objects here */ + Py_CLEAR(m); + goto out; +} + /* vim:set ts=4 sw=4 sts=0 noexpandtab: */ diff --git a/src/strxor.c b/src/strxor.c index 575d3f8..55d8067 100644 --- a/src/strxor.c +++ b/src/strxor.c @@ -232,25 +232,40 @@ PyInit_strxor(void) initstrxor(void) #endif { - PyObject *m; + PyObject *m = NULL; /* Initialize the module */ #ifdef IS_PY3K m = PyModule_Create(&moduledef); - if (m == NULL) - return NULL; #else m = Py_InitModule("strxor", strxor_methods); - if (m == NULL) - return; #endif - + if (m == NULL) + goto errout; + /* Perform runtime tests */ runtime_test(); +out: + /* Final error check */ + if (m == NULL && !PyErr_Occurred()) { + PyErr_SetString(PyExc_ImportError, "can't initialize module"); + goto errout; + } + + /* Free local objects here */ + + /* Return */ #ifdef IS_PY3K - return m; + return m; +#else + return; #endif + +errout: + /* Free the module and other global objects here */ + Py_CLEAR(m); + goto out; } /* vim:set ts=4 sw=4 sts=4 expandtab: */ diff --git a/src/winrand.c b/src/winrand.c index d06b242..4200fbc 100644 --- a/src/winrand.c +++ b/src/winrand.c @@ -271,19 +271,19 @@ PyInit_winrandom() initwinrandom() #endif { - PyObject *m; -#ifdef IS_PY3K + PyObject *m = NULL; + if (PyType_Ready(&WRtype) < 0) - return NULL; - /* Initialize the module */ - m = PyModule_Create(&moduledef); - if (m == NULL) - return NULL; + goto errout; + + /* Initialize the module */ +#ifdef IS_PY3K + m = PyModule_Create(&moduledef); #else - if (PyType_Ready(&WRtype) < 0) - return NULL; m = Py_InitModule("winrandom", WR_mod_methods); #endif + if (m == NULL) + goto errout; /* define Windows CSP Provider Types */ #ifdef PROV_RSA_FULL @@ -359,12 +359,26 @@ initwinrandom() PyModule_AddStringConstant(m, "INTEL_DEF_PROV", INTEL_DEF_PROV); #endif - if (PyErr_Occurred()) - Py_FatalError("can't initialize module winrandom"); +out: + /* Final error check */ + if (m == NULL && !PyErr_Occurred()) { + PyErr_SetString(PyExc_ImportError, "can't initialize module"); + goto errout; + } + + /* Free local objects here */ + /* Return */ #ifdef IS_PY3K return m; +#else + return; #endif + +errout: + /* Free the module and other global objects here */ + Py_CLEAR(m); + goto out; } /* -- cgit v1.2.1