While rewriting a nasty perl LDAP sync program in python I encountered one
quirk with python-ldap that made my life a bit difficult.  When doing
asynchronous searches an ldap exception in result3() doesn't include 'msgid'
in the info dict.  Since I'm tracking pending work by the msgid of the
search, having the msgid in the exception is very useful for removing work
items.

Here is a patch to the 'C' code of python-ldap to provide msgid.

Thanks,
Chris Dukes
Only in python-ldap-2.3.9: build
Only in python-ldap-2.3.9: dist
Only in python-ldap-2.3.9/Lib/python_ldap.egg-info: native_libs.txt
diff -urwp python-ldap-2.3.9.orig/Lib/python_ldap.egg-info/SOURCES.txt python-ldap-2.3.9/Lib/python_ldap.egg-info/SOURCES.txt
--- python-ldap-2.3.9.orig/Lib/python_ldap.egg-info/SOURCES.txt	2009-07-26 10:51:44.000000000 -0400
+++ python-ldap-2.3.9/Lib/python_ldap.egg-info/SOURCES.txt	2009-10-22 09:56:54.000000000 -0400
@@ -84,6 +84,7 @@ Lib/ldap/schema/tokenizer.py
 Lib/python_ldap.egg-info/PKG-INFO
 Lib/python_ldap.egg-info/SOURCES.txt
 Lib/python_ldap.egg-info/dependency_links.txt
+Lib/python_ldap.egg-info/native_libs.txt
 Lib/python_ldap.egg-info/not-zip-safe
 Lib/python_ldap.egg-info/requires.txt
 Lib/python_ldap.egg-info/top_level.txt
diff -urwp python-ldap-2.3.9.orig/Modules/errors.c python-ldap-2.3.9/Modules/errors.c
--- python-ldap-2.3.9.orig/Modules/errors.c	2009-04-17 08:19:09.000000000 -0400
+++ python-ldap-2.3.9/Modules/errors.c	2009-10-22 09:56:50.000000000 -0400
@@ -45,7 +45,7 @@ LDAPerr(int errnum)
 
 /* Convert an LDAP error into an informative python exception */
 PyObject*
-LDAPerror( LDAP*l, char*msg ) 
+LDAPerror( LDAP*l, char*msg, int msgid ) 
 {
 	if (l == NULL) {
 		PyErr_SetFromErrno( LDAPexception_class );
@@ -91,6 +91,11 @@ LDAPerror( LDAP*l, char*msg ) 
 		    ldap_memfree(matched);
 		}
 		
+                if (msgid >= 0) {
+                    str = PyInt_FromLong((long)msgid);
+                    PyDict_SetItemString( info, "msgid", str );
+                }
+		
 		if (errnum == LDAP_REFERRAL) {
 		    str = PyString_FromString(msg);
 		    if (str)
diff -urwp python-ldap-2.3.9.orig/Modules/errors.h python-ldap-2.3.9/Modules/errors.h
--- python-ldap-2.3.9.orig/Modules/errors.h	2009-04-17 08:19:09.000000000 -0400
+++ python-ldap-2.3.9/Modules/errors.h	2009-10-22 09:40:18.000000000 -0400
@@ -9,7 +9,7 @@
 #include "ldap.h"
 
 extern PyObject* LDAPexception_class;
-extern PyObject* LDAPerror( LDAP*, char*msg );
+extern PyObject* LDAPerror( LDAP*, char*msg, int msgid );
 extern void LDAPinit_errors( PyObject* );
 PyObject* LDAPerr(int errnum);
 
diff -urwp python-ldap-2.3.9.orig/Modules/functions.c python-ldap-2.3.9/Modules/functions.c
--- python-ldap-2.3.9.orig/Modules/functions.c	2009-04-17 08:19:09.000000000 -0400
+++ python-ldap-2.3.9/Modules/functions.c	2009-10-22 09:46:58.000000000 -0400
@@ -23,7 +23,7 @@ l_ldap_initialize(PyObject* unused, PyOb
     ret = ldap_initialize(&ld, uri);
     Py_END_ALLOW_THREADS
     if (ret != LDAP_SUCCESS)
-    	return LDAPerror(ld, "ldap_initialize");
+    	return LDAPerror(ld, "ldap_initialize", -1);
     return (PyObject*)newLDAPObject(ld);
 }
 
diff -urwp python-ldap-2.3.9.orig/Modules/LDAPObject.c python-ldap-2.3.9/Modules/LDAPObject.c
--- python-ldap-2.3.9.orig/Modules/LDAPObject.c	2009-04-17 08:19:09.000000000 -0400
+++ python-ldap-2.3.9/Modules/LDAPObject.c	2009-10-22 09:39:56.000000000 -0400
@@ -356,7 +356,7 @@ l_ldap_unbind_ext( LDAPObject* self, PyO
     LDAPControl_List_DEL( client_ldcs );
 
     if ( ldaperror!=LDAP_SUCCESS )
-    	return LDAPerror( self->ldap, "ldap_unbind_ext" );
+    	return LDAPerror( self->ldap, "ldap_unbind_ext", -1 );
 
     self->valid = 0;
     Py_INCREF(Py_None);
@@ -399,7 +399,7 @@ l_ldap_abandon_ext( LDAPObject* self, Py
     LDAPControl_List_DEL( client_ldcs );
 
     if ( ldaperror!=LDAP_SUCCESS )
-    	return LDAPerror( self->ldap, "ldap_abandon_ext" );
+    	return LDAPerror( self->ldap, "ldap_abandon_ext", -1 );
 
     Py_INCREF(Py_None);
     return Py_None;
@@ -448,7 +448,7 @@ l_ldap_add_ext( LDAPObject* self, PyObje
     LDAPControl_List_DEL( client_ldcs );
 
     if ( ldaperror!=LDAP_SUCCESS )
-    	return LDAPerror( self->ldap, "ldap_add_ext" );
+    	return LDAPerror( self->ldap, "ldap_add_ext", -1 );
 
     return PyInt_FromLong(msgid);
 }
@@ -493,7 +493,7 @@ l_ldap_simple_bind( LDAPObject* self, Py
     LDAPControl_List_DEL( client_ldcs );
 
     if ( ldaperror!=LDAP_SUCCESS )
-    	return LDAPerror( self->ldap, "ldap_simple_bind" );
+    	return LDAPerror( self->ldap, "ldap_simple_bind", -1 );
 
     return PyInt_FromLong( msgid );
 }
@@ -693,7 +693,7 @@ l_ldap_sasl_interactive_bind_s( LDAPObje
     LDAPControl_List_DEL( client_ldcs );
 
     if (msgid != LDAP_SUCCESS)
-    	return LDAPerror( self->ldap, "ldap_sasl_interactive_bind_s" );
+    	return LDAPerror( self->ldap, "ldap_sasl_interactive_bind_s", -1 );
     return PyInt_FromLong( msgid );
 }
 #endif
@@ -738,7 +738,7 @@ l_ldap_cancel( LDAPObject* self, PyObjec
     LDAPControl_List_DEL( client_ldcs );
 
     if ( ldaperror!=LDAP_SUCCESS )
-    	return LDAPerror( self->ldap, "ldap_cancel" );
+    	return LDAPerror( self->ldap, "ldap_cancel", -1 );
 
     return PyInt_FromLong( msgid );
 }
@@ -786,7 +786,7 @@ l_ldap_compare_ext( LDAPObject* self, Py
     LDAPControl_List_DEL( client_ldcs );
 
     if ( ldaperror!=LDAP_SUCCESS )
-    	return LDAPerror( self->ldap, "ldap_compare_ext" );
+    	return LDAPerror( self->ldap, "ldap_compare_ext", -1 );
 
     return PyInt_FromLong( msgid );
 }
@@ -829,7 +829,7 @@ l_ldap_delete_ext( LDAPObject* self, PyO
     LDAPControl_List_DEL( client_ldcs );
 
     if ( ldaperror!=LDAP_SUCCESS )
-    	return LDAPerror( self->ldap, "ldap_delete_ext" );
+    	return LDAPerror( self->ldap, "ldap_delete_ext", -1 );
 
     return PyInt_FromLong(msgid);
 }
@@ -879,7 +879,7 @@ l_ldap_modify_ext( LDAPObject* self, PyO
     LDAPControl_List_DEL( client_ldcs );
 
     if ( ldaperror!=LDAP_SUCCESS )
-    	return LDAPerror( self->ldap, "ldap_modify_ext" );
+    	return LDAPerror( self->ldap, "ldap_modify_ext", -1 );
 
     return PyInt_FromLong( msgid );
 }
@@ -925,7 +925,7 @@ l_ldap_rename( LDAPObject* self, PyObjec
     LDAPControl_List_DEL( client_ldcs );
 
     if ( ldaperror!=LDAP_SUCCESS )
-    	return LDAPerror( self->ldap, "ldap_rename" );
+    	return LDAPerror( self->ldap, "ldap_rename", -1 );
 
     return PyInt_FromLong( msgid );
 }
@@ -944,7 +944,7 @@ l_ldap_result3( LDAPObject* self, PyObje
     int res_type;
     LDAPMessage *msg = NULL;
     PyObject *result_str, *retval, *pmsg, *pyctrls = 0;
-    int res_msgid = 0;
+    int res_msgid = -1;
 
     if (!PyArg_ParseTuple( args, "|iid", &msgid, &all, &timeout ))
     	return NULL;
@@ -961,8 +961,11 @@ l_ldap_result3( LDAPObject* self, PyObje
     res_type = ldap_result( self->ldap, msgid, all, tvp, &msg );
     LDAP_END_ALLOW_THREADS( self );
 
+    if (msg)
+	    res_msgid = ldap_msgid(msg);
+
     if (res_type < 0)	/* LDAP or system error */
-    	return LDAPerror( self->ldap, "ldap_result3" );
+    	return LDAPerror( self->ldap, "ldap_result3", res_msgid );
 
     if (res_type == 0) {
 	/* Polls return (None, None, None, None); timeouts raise an exception */
@@ -972,9 +975,6 @@ l_ldap_result3( LDAPObject* self, PyObje
 		return LDAPerr(LDAP_TIMEOUT);
     }
 
-    if (msg)
-	    res_msgid = ldap_msgid(msg);
-
     if (res_type == LDAP_RES_SEARCH_ENTRY
 	    || res_type == LDAP_RES_SEARCH_REFERENCE
 	)
@@ -996,14 +996,14 @@ l_ldap_result3( LDAPObject* self, PyObje
 	    } else
 		e = "ldap_parse_result";
 	    ldap_msgfree(msg);
-	    return LDAPerror( self->ldap, e );
+	    return LDAPerror( self->ldap, e, res_msgid );
 	}
 
         if (!(pyctrls = LDAPControls_to_List(serverctrls))) {
             int err = LDAP_NO_MEMORY;
             ldap_set_option(self->ldap, LDAP_OPT_ERROR_NUMBER, &err);
 	    ldap_msgfree(msg);
-            return LDAPerror(self->ldap, "LDAPControls_to_List");
+            return LDAPerror(self->ldap, "LDAPControls_to_List", res_msgid);
         }
         ldap_controls_free(serverctrls);
 
@@ -1098,7 +1098,7 @@ l_ldap_search_ext( LDAPObject* self, PyO
     LDAPControl_List_DEL( client_ldcs );
 
     if ( ldaperror!=LDAP_SUCCESS )
-    	return LDAPerror( self->ldap, "ldap_search_ext" );
+    	return LDAPerror( self->ldap, "ldap_search_ext", -1 );
 
     return PyInt_FromLong( msgid );
 }	
@@ -1142,7 +1142,7 @@ l_ldap_whoami_s( LDAPObject* self, PyObj
     LDAPControl_List_DEL( client_ldcs );
 
     if ( ldaperror!=LDAP_SUCCESS )
-    	return LDAPerror( self->ldap, "ldap_whoami_s" );
+    	return LDAPerror( self->ldap, "ldap_whoami_s", -1 );
 
     result = PyString_FromStringAndSize(bvalue->bv_val, bvalue->bv_len);
 
@@ -1163,7 +1163,7 @@ l_ldap_start_tls_s( LDAPObject* self, Py
     result = ldap_start_tls_s( self->ldap, NULL, NULL );
     if ( result != LDAP_SUCCESS ){
 	ldap_set_option(self->ldap, LDAP_OPT_ERROR_NUMBER, &result);
-	return LDAPerror( self->ldap, "ldap_start_tls_s" );
+	return LDAPerror( self->ldap, "ldap_start_tls_s", -1 );
     }
 
     Py_INCREF(Py_None);
@@ -1256,7 +1256,7 @@ l_ldap_passwd( LDAPObject* self, PyObjec
     LDAPControl_List_DEL( client_ldcs );
 
     if ( ldaperror!=LDAP_SUCCESS )
-    	return LDAPerror( self->ldap, "ldap_passwd" );
+    	return LDAPerror( self->ldap, "ldap_passwd", -1 );
 
     return PyInt_FromLong( msgid );
 }
diff -urwp python-ldap-2.3.9.orig/Modules/message.c python-ldap-2.3.9/Modules/message.c
--- python-ldap-2.3.9.orig/Modules/message.c	2009-04-17 08:19:09.000000000 -0400
+++ python-ldap-2.3.9/Modules/message.c	2009-10-22 09:50:15.000000000 -0400
@@ -15,12 +15,14 @@ LDAPmessage_to_python( LDAP*ld, LDAPMess
 
      PyObject* result;
      LDAPMessage* entry;
+     int msgid;
 
      result = PyList_New(0);
      if (result == NULL) {
         ldap_msgfree( m );
 	return NULL;
      }
+     msgid = ldap_msgid(m);
 
      for(entry = ldap_first_entry(ld,m);
          entry != NULL;
@@ -36,7 +38,7 @@ LDAPmessage_to_python( LDAP*ld, LDAPMess
 	 if (dn == NULL)  {
 	     Py_DECREF(result);
              ldap_msgfree( m );
-	     return LDAPerror( ld, "ldap_get_dn" );
+	     return LDAPerror( ld, "ldap_get_dn", msgid );
 	 }
 
 	 attrdict = PyDict_New();
@@ -131,7 +133,7 @@ LDAPmessage_to_python( LDAP*ld, LDAPMess
 	 if (ldap_parse_reference(ld, entry, &refs, NULL, 0) != LDAP_SUCCESS) {
 	     Py_DECREF(result);
 	     ldap_msgfree( m );
-	     return LDAPerror( ld, "ldap_parse_reference" );
+	     return LDAPerror( ld, "ldap_parse_reference", msgid );
 	 }
 	 if (refs) {
 	     Py_ssize_t i;
------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Python-LDAP-dev mailing list
Python-LDAP-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/python-ldap-dev

Reply via email to