Skip to content

Commit

Permalink
better safe than sorry with errno
Browse files Browse the repository at this point in the history
  • Loading branch information
stroeder committed Apr 25, 2017
1 parent 5e86ba0 commit c4d9ec3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
8 changes: 7 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 $
6 changes: 5 additions & 1 deletion Lib/ldap/ldapobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,6 +18,8 @@
lock self._ldap_object_lock.
"""

from os import strerror

from ldap import __version__

__all__ = [
Expand Down Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions Modules/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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') {
Expand All @@ -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);
}
Expand Down

0 comments on commit c4d9ec3

Please sign in to comment.