From a3675965499f3a939f7fd6848b2506d2e4ffdd46 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Mon, 27 Nov 2017 15:33:45 +0100 Subject: [PATCH] Modules: Write out error checking in constants.c --- Lib/ldap/constants.py | 5 ++--- Modules/constants.c | 30 ++++++++---------------------- Modules/constants_generated.h | 15 ++++++--------- 3 files changed, 16 insertions(+), 34 deletions(-) diff --git a/Lib/ldap/constants.py b/Lib/ldap/constants.py index aafa265..c5ac6c1 100644 --- a/Lib/ldap/constants.py +++ b/Lib/ldap/constants.py @@ -61,11 +61,10 @@ class Feature(Constant): c_template = '\n'.join([ '', '#ifdef {self.c_feature}', - 'result = PyModule_AddIntConstant(m, "{self.name}", 1);', + 'if (PyModule_AddIntConstant(m, "{self.name}", 1) != 0) return -1;', '#else', - 'result = PyModule_AddIntConstant(m, "{self.name}", 0);', + 'if (PyModule_AddIntConstant(m, "{self.name}", 0) != 0) return -1;', '#endif', - 'check_result();', '', ]) diff --git a/Modules/constants.c b/Modules/constants.c index 4fd09b8..8906581 100644 --- a/Modules/constants.c +++ b/Modules/constants.c @@ -131,18 +131,11 @@ int LDAPinit_constants( PyObject* m ) { PyObject *exc; - int result; - -#define check_result() { \ - if (result != 0) return -1; \ -} /* simple constants */ - result = PyModule_AddIntConstant(m, "OPT_ON", 1); - check_result(); - result = PyModule_AddIntConstant(m, "OPT_OFF", 0); - check_result(); + if (PyModule_AddIntConstant(m, "OPT_ON", 1) != 0) return -1; + if (PyModule_AddIntConstant(m, "OPT_OFF", 0) != 0) return -1; /* exceptions */ @@ -151,36 +144,29 @@ LDAPinit_constants( PyObject* m ) return -1; } - result = PyModule_AddObject(m, "LDAPError", LDAPexception_class); - check_result(); + if (PyModule_AddObject(m, "LDAPError", LDAPexception_class) != 0) return -1; Py_INCREF(LDAPexception_class); /* XXX - backward compatibility with pre-1.8 */ - result = PyModule_AddObject(m, "error", LDAPexception_class); - check_result(); + if (PyModule_AddObject(m, "error", LDAPexception_class) != 0) return -1; Py_INCREF(LDAPexception_class); /* Generated constants -- see Lib/ldap/constants.py */ -#define seterrobj2(n, o) \ - PyModule_AddObject(m, #n, (errobjects[LDAP_##n+LDAP_ERROR_OFFSET] = o)) - #define add_err(n) { \ exc = PyErr_NewException("ldap." #n, LDAPexception_class, NULL); \ if (exc == NULL) return -1; \ - result = seterrobj2(n, exc); \ - check_result(); \ + errobjects[LDAP_##n+LDAP_ERROR_OFFSET] = exc; \ + if (PyModule_AddObject(m, #n, exc) != 0) return -1; \ Py_INCREF(exc); \ } #define add_int(n) { \ - result = PyModule_AddIntConstant(m, #n, LDAP_##n); \ - check_result(); \ + if (PyModule_AddIntConstant(m, #n, LDAP_##n) != 0) return -1; \ } #define add_string(n) { \ - result = PyModule_AddStringConstant(m, #n, LDAP_##n); \ - check_result(); \ + if (PyModule_AddStringConstant(m, #n, LDAP_##n) != 0) return -1; \ } #include "constants_generated.h" diff --git a/Modules/constants_generated.h b/Modules/constants_generated.h index 27addc9..083ba16 100644 --- a/Modules/constants_generated.h +++ b/Modules/constants_generated.h @@ -325,27 +325,24 @@ add_int(URL_ERR_BADSCOPE); add_int(URL_ERR_MEM); #ifdef HAVE_LIBLDAP_R -result = PyModule_AddIntConstant(m, "LIBLDAP_R", 1); +if (PyModule_AddIntConstant(m, "LIBLDAP_R", 1) != 0) return -1; #else -result = PyModule_AddIntConstant(m, "LIBLDAP_R", 0); +if (PyModule_AddIntConstant(m, "LIBLDAP_R", 0) != 0) return -1; #endif -check_result(); #ifdef HAVE_SASL -result = PyModule_AddIntConstant(m, "SASL_AVAIL", 1); +if (PyModule_AddIntConstant(m, "SASL_AVAIL", 1) != 0) return -1; #else -result = PyModule_AddIntConstant(m, "SASL_AVAIL", 0); +if (PyModule_AddIntConstant(m, "SASL_AVAIL", 0) != 0) return -1; #endif -check_result(); #ifdef HAVE_TLS -result = PyModule_AddIntConstant(m, "TLS_AVAIL", 1); +if (PyModule_AddIntConstant(m, "TLS_AVAIL", 1) != 0) return -1; #else -result = PyModule_AddIntConstant(m, "TLS_AVAIL", 0); +if (PyModule_AddIntConstant(m, "TLS_AVAIL", 0) != 0) return -1; #endif -check_result(); add_string(CONTROL_MANAGEDSAIT); add_string(CONTROL_PROXY_AUTHZ);