diff --git a/Doc/reference/ldap.rst b/Doc/reference/ldap.rst index 3254375..1f093ab 100644 --- a/Doc/reference/ldap.rst +++ b/Doc/reference/ldap.rst @@ -321,6 +321,9 @@ The module defines the following exceptions: is set to a truncated form of the name provided or alias dereferenced for the lowest entry (object or alias) that was matched. + Most exceptions from protocol results also carry the :py:attr:`errnum` + attribute. + .. py:exception:: ADMINLIMIT_EXCEEDED diff --git a/Modules/constants.c b/Modules/constants.c index bc7c2dc..a1c79d1 100644 --- a/Modules/constants.c +++ b/Modules/constants.c @@ -198,7 +198,7 @@ LDAPerror(LDAP *l, char *msg) int LDAPinit_constants(PyObject *m) { - PyObject *exc; + PyObject *exc, *nobj; /* simple constants */ @@ -228,6 +228,10 @@ LDAPinit_constants(PyObject *m) #define add_err(n) do { \ exc = PyErr_NewException("ldap." #n, LDAPexception_class, NULL); \ if (exc == NULL) return -1; \ + nobj = PyLong_FromLong(LDAP_##n); \ + if (nobj == NULL) return -1; \ + if (PyObject_SetAttrString(exc, "errnum", nobj) != 0) return -1; \ + Py_DECREF(nobj); \ errobjects[LDAP_##n+LDAP_ERROR_OFFSET] = exc; \ if (PyModule_AddObject(m, #n, exc) != 0) return -1; \ Py_INCREF(exc); \