Skip to content

Commit 1a21801

Browse files
committed
Add wrapper for ldap_connect
1 parent 72c1b5e commit 1a21801

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

Lib/ldap/ldapobject.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ def fileno(self):
171171
"""
172172
return self.get_option(ldap.OPT_DESC)
173173

174+
def connect(self):
175+
"""
176+
connect() -> None
177+
Establishes LDAP connection if needed.
178+
"""
179+
if _ldap.VENDOR_VERSION >= 20500:
180+
return self._ldap_call(self._l.connect)
181+
raise NotImplementedError
182+
174183
def abandon_ext(self,msgid,serverctrls=None,clientctrls=None):
175184
"""
176185
abandon_ext(msgid[,serverctrls=None[,clientctrls=None]]) -> None

Modules/LDAPObject.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,29 @@ l_ldap_extended_operation(LDAPObject *self, PyObject *args)
15051505
return PyInt_FromLong(msgid);
15061506
}
15071507

1508+
/* ldap_connect */
1509+
1510+
#if LDAP_VENDOR_VERSION >= 20500
1511+
static PyObject *
1512+
l_ldap_connect(LDAPObject *self, PyObject Py_UNUSED(args))
1513+
{
1514+
int ldaperror;
1515+
1516+
if (not_valid(self))
1517+
return NULL;
1518+
1519+
LDAP_BEGIN_ALLOW_THREADS(self);
1520+
ldaperror = ldap_connect(self->ldap);
1521+
LDAP_END_ALLOW_THREADS(self);
1522+
1523+
if ( ldaperror != LDAP_SUCCESS )
1524+
return LDAPerror(self->ldap);
1525+
1526+
Py_INCREF(Py_None);
1527+
return Py_None;
1528+
}
1529+
#endif
1530+
15081531
/* methods */
15091532

15101533
static PyMethodDef methods[] = {
@@ -1534,6 +1557,9 @@ static PyMethodDef methods[] = {
15341557
{"cancel", (PyCFunction)l_ldap_cancel, METH_VARARGS},
15351558
#endif
15361559
{"extop", (PyCFunction)l_ldap_extended_operation, METH_VARARGS},
1560+
#if LDAP_VENDOR_VERSION >= 20500
1561+
{"connect", (PyCFunction)l_ldap_connect, METH_NOARGS},
1562+
#endif
15371563
{NULL, NULL}
15381564
};
15391565

Tests/t_cext.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,19 @@ def test_simple_anonymous_bind(self):
280280
self.assertEqual(pmsg, [])
281281
self.assertEqual(ctrls, [])
282282

283+
@unittest.skipUnless(
284+
_ldap.VENDOR_VERSION >= 20500,
285+
reason="Test requires libldap 2.5+"
286+
)
287+
def test_connect(self):
288+
l = self._open_conn(bind=False)
289+
invalid_fileno = l.get_option(_ldap.OPT_DESC)
290+
l.connect()
291+
fileno = l.get_option(_ldap.OPT_DESC)
292+
assertNotEqual(invalid_fileno, fileno)
293+
294+
self._bind_conn(l)
295+
283296
def test_anon_rootdse_search(self):
284297
l = self._open_conn(bind=False)
285298
# see if we can get the rootdse with anon search (without prior bind)

0 commit comments

Comments
 (0)