Skip to content

Commit

Permalink
Modules: Write out error checking in constants.c
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Viktorin committed Nov 27, 2017
1 parent b096d31 commit a367596
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 34 deletions.
5 changes: 2 additions & 3 deletions Lib/ldap/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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();',
'',
])

Expand Down
30 changes: 8 additions & 22 deletions Modules/constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand All @@ -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"
Expand Down
15 changes: 6 additions & 9 deletions Modules/constants_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a367596

Please sign in to comment.