From 785729194f133fd7947c2366a941363a57380429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Kuzn=C3=ADk?= Date: Thu, 30 Apr 2020 12:11:16 +0100 Subject: [PATCH] Add .errnum to LDAPExceptions --- Doc/reference/ldap.rst | 3 +++ Modules/constants.c | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) 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); \