From c4d9ec3801ec6c83f3f017e904c370c0d3994e36 Mon Sep 17 00:00:00 2001 From: stroeder Date: Tue, 25 Apr 2017 13:40:52 +0000 Subject: [PATCH] better safe than sorry with errno --- CHANGES | 8 +++++++- Lib/ldap/ldapobject.py | 6 +++++- Modules/errors.c | 15 +++++++++------ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/CHANGES b/CHANGES index 6b35c4c..90d9ec7 100644 --- a/CHANGES +++ b/CHANGES @@ -3,6 +3,12 @@ Released 2.4.34 2017-04-xx Modules/ * use errno in a safer way +* set errno as LDAPError class item +* do not use strerror() which is not thread-safe and platform-specific + +Lib/ +* LDAPObject._ldap_call() sets LDAPError info to value returned + by platform-neutral os.stderror() ---------------------------------------------------------------- Released 2.4.33 2017-04-25 @@ -1361,4 +1367,4 @@ Released 2.0.0pre02 2002-02-01 ---------------------------------------------------------------- Released 1.10alpha3 2000-09-19 -$Id: CHANGES,v 1.419 2017/04/25 12:45:27 stroeder Exp $ +$Id: CHANGES,v 1.420 2017/04/25 13:40:52 stroeder Exp $ diff --git a/Lib/ldap/ldapobject.py b/Lib/ldap/ldapobject.py index f0e05b9..a87538a 100644 --- a/Lib/ldap/ldapobject.py +++ b/Lib/ldap/ldapobject.py @@ -3,7 +3,7 @@ See http://www.python-ldap.org/ for details. -\$Id: ldapobject.py,v 1.159 2017/04/24 08:25:16 stroeder Exp $ +\$Id: ldapobject.py,v 1.160 2017/04/25 13:40:52 stroeder Exp $ Compability: - Tested with Python 2.0+ but should work with Python 1.5.x @@ -18,6 +18,8 @@ lock self._ldap_object_lock. """ +from os import strerror + from ldap import __version__ __all__ = [ @@ -110,6 +112,8 @@ def _ldap_call(self,func,*args,**kwargs): finally: self._ldap_object_lock.release() except LDAPError,e: + if 'info' not in e.args[0]: + e.args[0]['info'] = strerror(e.args[0]['errno']) if __debug__ and self._trace_level>=2: self._trace_file.write('=> LDAPError - %s: %s\n' % (e.__class__.__name__,str(e))) raise diff --git a/Modules/errors.c b/Modules/errors.c index 6a8d14b..9dd78b6 100644 --- a/Modules/errors.c +++ b/Modules/errors.c @@ -2,7 +2,7 @@ * errors that arise from ldap use * Most errors become their own exception * See http://www.python-ldap.org/ for details. - * $Id: errors.c,v 1.29 2017/04/25 12:45:27 stroeder Exp $ */ + * $Id: errors.c,v 1.30 2017/04/25 13:40:52 stroeder Exp $ */ #include "common.h" #include "errors.h" @@ -58,6 +58,7 @@ LDAPerror( LDAP *l, char *msg ) PyObject *errobj; PyObject *info; PyObject *str; + PyObject *pyerrno; /* at first save errno for later use before it gets overwritten by another call */ myerrno = errno; @@ -85,6 +86,13 @@ LDAPerror( LDAP *l, char *msg ) PyDict_SetItemString( info, "desc", str ); Py_XDECREF(str); + if (myerrno != 0) { + pyerrno = PyInt_FromLong(myerrno); + if (pyerrno) + PyDict_SetItemString( info, "errno", pyerrno ); + Py_XDECREF(pyerrno); + } + if (ldap_get_option(l, LDAP_OPT_MATCHED_DN, &matched) >= 0 && matched != NULL) { if (*matched != '\0') { @@ -107,11 +115,6 @@ LDAPerror( LDAP *l, char *msg ) if (str) PyDict_SetItemString( info, "info", str ); Py_XDECREF(str); - } else if (myerrno != 0) { - str = PyString_FromString(strerror(myerrno)); - if(str) - PyDict_SetItemString( info, "info", str); - Py_XDECREF(str); } ldap_memfree(error); }