Using set_rebind_proc(func) with a bound method

f.glasner at f.glasner at
Fri Jun 1 20:53:23 CEST 2001


currently I'm playing with python-ldap-1.10a3 on WinNT with
ActivePython 2.1.0-210b.

When dealing with LDAP referrals I tried to pass a
bound method object to set_rebind_proc, but it failed with the
exception "expected function or None".

But there is no reason why this "callback" should only work
with "normal" functions in Python.

So I patched the sources in file LDAPObject.c and changed the
function "l_ldap_set_rebind_proc":
Now the very specific check "PyFunction_Check(func)" is replaced
by "PyCallable_Check(func)". Passing bound methods now work.

I dont know whether this also works for older versions of Python
(and there is no check whether func is a unbound method object, but
here is the diff:

@@ -595,15 +595,18 @@
          ldap_set_rebind_proc( self->ldap, NULL, 0);
          ldap_set_rebind_proc( self->ldap, NULL );
+        Py_XDECREF(rebind_callback_func);
     rebind_callback_func = NULL;
     rebind_callback_ld = NULL;
     return Py_None;

-    if ( PyFunction_Check(func) ) {
+    if ( PyCallable_Check(func) ) {
+        Py_XDECREF(rebind_callback_func);
+        Py_INCREF(func);
     rebind_callback_func = func;
     rebind_callback_ld = self;
         ldap_set_rebind_proc( self->ldap, rebind_callback, 0);
@@ -613,9 +616,9 @@
     return Py_None;

-    PyErr_SetString( PyExc_TypeError, "expected function or None" );
+    PyErr_SetString( PyExc_TypeError, "expected function, bound method or None" );
     return NULL;

 static char doc_set_rebind_proc[] =


More information about the python-ldap mailing list