Skip to content

Allow to pickle a unbound connection #588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Lib/ldap/ldapobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,10 @@ def __getstate__(self):
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]
if self._last_bind is None:
state['_last_bind'] = None
else:
state['_last_bind'] = self._last_bind[0].__name__, self._last_bind[1], self._last_bind[2]
return state

def __setstate__(self,d):
Expand All @@ -888,7 +891,8 @@ def __setstate__(self,d):
else:
d.setdefault('bytes_strictness', 'warn')
self.__dict__.update(d)
self._last_bind = getattr(SimpleLDAPObject, self._last_bind[0]), self._last_bind[1], self._last_bind[2]
if self._last_bind is not None:
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)))
# XXX cannot pickle file, use default trace file
Expand Down
10 changes: 9 additions & 1 deletion Tests/t_ldapobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,12 +617,20 @@ def test103_reconnect_get_state(self):
)

def test104_reconnect_restore(self):
l1 = self.ldap_object_class(self.server.ldap_uri)
l0 = self.ldap_object_class(self.server.ldap_uri)

l0_state = pickle.dumps(l0)
del l0
l1 = pickle.loads(l0_state)
self.assertEqual(l1.whoami_s(), '')

bind_dn = 'cn=user1,'+self.server.suffix
l1.simple_bind_s(bind_dn, 'user1_pw')

self.assertEqual(l1.whoami_s(), 'dn:'+bind_dn)
l1_state = pickle.dumps(l1)
del l1

l2 = pickle.loads(l1_state)
self.assertEqual(l2.whoami_s(), 'dn:'+bind_dn)

Expand Down