Skip to content

Commit

Permalink
fixed pickling and restoring of ReconnectLDAPObject, avoid .has_key()…
Browse files Browse the repository at this point in the history
… in ldap.ldapobject
  • Loading branch information
stroeder authored and Petr Viktorin committed Nov 22, 2017
1 parent be191b1 commit cdd3a41
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ Released 2.5.2 2017-11-xx
Changes since 2.5.1:

Modules/
* PyBytes_ instead of PyString_ and added PyInt_FromLong compat macro
* moved code from version.c to ldapmodule.c
* removed obsolete back-ward compability constants from common.h
* build checks whether LDAP_API_VERSION is OpenLDAP 2.4.x
* _ldap.__author__ and _ldap.__license__ also set from ldap.pkginfo

Lib/
* removed all dependencies on modules string and types
* removed use of .has_key()
* new global constant ldap.LIBLDAP_API_INFO
* right after importing _ldap there is a call into libldap to initialize it
* method .decodeControlValue() of SSSResponseControl and VLVResponseControl
does not set class attribute result_code anymore
* always use bytes() for UUID() constructor in ldap.syncrepl
* module ldif now uses functions b64encode() and b64decode()
* fixed pickling and restoring of ReconnectLDAPObject

Tests/
* scripts do not directly call SlapdTestCase.setUpClass() anymore
Expand Down
32 changes: 18 additions & 14 deletions Lib/ldap/ldapobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def _ldap_call(self,func,*args,**kwargs):
except LDAPError, e:
exc_type,exc_value,exc_traceback = sys.exc_info()
try:
if not e.args[0].has_key('info') and e.args[0].has_key('errno'):
if 'info' not in e.args[0] and 'errno' in e.args[0]:
e.args[0]['info'] = strerror(e.args[0]['errno'])
except IndexError:
pass
Expand All @@ -123,7 +123,7 @@ def __setattr__(self,name,value):
def __getattr__(self,name):
if name in self.CLASSATTR_OPTION_MAPPING:
return self.get_option(self.CLASSATTR_OPTION_MAPPING[name])
elif self.__dict__.has_key(name):
elif name in self.__dict__:
return self.__dict__[name]
else:
raise AttributeError,'%s has no attribute %s' % (
Expand Down Expand Up @@ -807,12 +807,13 @@ class ReconnectLDAPObject(SimpleLDAPObject):
application.
"""

__transient_attrs__ = {
'_l':None,
'_ldap_object_lock':None,
'_trace_file':None,
'_reconnect_lock':None,
}
__transient_attrs__ = set([
'_l',
'_ldap_object_lock',
'_trace_file',
'_reconnect_lock',
'_last_bind',
])

def __init__(
self,uri,
Expand Down Expand Up @@ -840,15 +841,18 @@ def __init__(

def __getstate__(self):
"""return data representation for pickled object"""
d = {}
for k,v in self.__dict__.items():
if not self.__transient_attrs__.has_key(k):
d[k] = v
return d
state = dict([
(k,v)
for k,v in self.__dict__.items()
if k not in self.__transient_attrs__
])
state['_last_bind'] = self._last_bind[0].__name__, self._last_bind[1], self._last_bind[2]
return state

def __setstate__(self,d):
"""set up the object from pickled data"""
self.__dict__.update(d)
self._last_bind = getattr(SimpleLDAPObject, self._last_bind[0]), self._last_bind[1], self._last_bind[2]
self._ldap_object_lock = self._ldap_lock()
self._reconnect_lock = ldap.LDAPLock(desc='reconnect lock within %s' % (repr(self)))
self._trace_file = sys.stdout
Expand Down Expand Up @@ -918,7 +922,7 @@ def reconnect(self,uri,retry_max=1,retry_delay=60.0):
return # reconnect()

def _apply_method_s(self,func,*args,**kwargs):
if not self.__dict__.has_key('_l'):
if not hasattr(self,'_l'):
self.reconnect(self._uri,retry_max=self._retry_max,retry_delay=self._retry_delay)
try:
return func(self,*args,**kwargs)
Expand Down

0 comments on commit cdd3a41

Please sign in to comment.