[CalendarServer-changes] [1964] PyOpenDirectory/trunk/src

source_changes at macosforge.org source_changes at macosforge.org
Tue Oct 16 13:09:04 PDT 2007


Revision: 1964
          http://trac.macosforge.org/projects/calendarserver/changeset/1964
Author:   cdaboo at apple.com
Date:     2007-10-16 13:09:04 -0700 (Tue, 16 Oct 2007)

Log Message:
-----------
Cannot set Python exceptions whilst threads are enabled (bus error if you do), so had to refactor the thread save/restore behavior.

Modified Paths:
--------------
    PyOpenDirectory/trunk/src/CDirectoryService.cpp
    PyOpenDirectory/trunk/src/CDirectoryService.h
    PyOpenDirectory/trunk/src/CDirectoryServiceException.cpp
    PyOpenDirectory/trunk/src/CDirectoryServiceException.h
    PyOpenDirectory/trunk/src/PythonWrapper.cpp

Modified: PyOpenDirectory/trunk/src/CDirectoryService.cpp
===================================================================
--- PyOpenDirectory/trunk/src/CDirectoryService.cpp	2007-10-16 19:54:36 UTC (rev 1963)
+++ PyOpenDirectory/trunk/src/CDirectoryService.cpp	2007-10-16 20:09:04 UTC (rev 1964)
@@ -93,6 +93,8 @@
 {
 	try
 	{
+		StPythonThreadState threading;
+
 		// Get attribute map
 		return _ListAllRecordsWithAttributes(recordType, NULL, attributes);
 	}
@@ -103,7 +105,8 @@
 	}
 	catch(...)
 	{
-		PyErr_SetObject(ODException_class, Py_BuildValue("((s:i))", "Unknown Error", -1));		
+		CDirectoryServiceException dserror;
+		dserror.SetPythonException();
 		return NULL;
 	}
 }
@@ -127,6 +130,8 @@
 {
 	try
 	{
+		StPythonThreadState threading;
+
 		// Get attribute map
 		return _QueryRecordsWithAttributes(attr, value, matchType, NULL, casei, recordType, attributes);
 	}
@@ -137,7 +142,8 @@
 	}
 	catch(...)
 	{
-		PyErr_SetObject(ODException_class, Py_BuildValue("((s:i))", "Unknown Error", -1));		
+		CDirectoryServiceException dserror;
+		dserror.SetPythonException();
 		return NULL;
 	}
 }
@@ -159,6 +165,8 @@
 {
 	try
 	{
+		StPythonThreadState threading;
+
 		// Get attribute map
 		return _QueryRecordsWithAttributes(NULL, NULL, 0, query, casei, recordType, attributes);
 	}
@@ -169,7 +177,8 @@
 	}
 	catch(...)
 	{
-		PyErr_SetObject(ODException_class, Py_BuildValue("((s:i))", "Unknown Error", -1));		
+		CDirectoryServiceException dserror;
+		dserror.SetPythonException();
 		return NULL;
 	}
 }
@@ -187,6 +196,8 @@
 {
 	try
 	{
+		StPythonThreadState threading;
+
 		result = NativeAuthenticationBasicToNode(nodename, user, pswd);
 		return true;
 	}
@@ -197,7 +208,8 @@
 	}
 	catch(...)
 	{
-		PyErr_SetObject(ODException_class, Py_BuildValue("((s:i))", "Unknown Error", -1));		
+		CDirectoryServiceException dserror;
+		dserror.SetPythonException();
 		return false;
 	}
 }
@@ -215,6 +227,8 @@
 {
 	try
 	{
+		StPythonThreadState threading;
+
 		result = NativeAuthenticationDigestToNode(nodename, user, challenge, response, method);
 		return true;
 	}
@@ -225,7 +239,8 @@
 	}
 	catch(...)
 	{
-		PyErr_SetObject(ODException_class, Py_BuildValue("((s:i))", "Unknown Error", -1));		
+		CDirectoryServiceException dserror;
+		dserror.SetPythonException();
 		return false;
 	}
 }

Modified: PyOpenDirectory/trunk/src/CDirectoryService.h
===================================================================
--- PyOpenDirectory/trunk/src/CDirectoryService.h	2007-10-16 19:54:36 UTC (rev 1963)
+++ PyOpenDirectory/trunk/src/CDirectoryService.h	2007-10-16 20:09:04 UTC (rev 1964)
@@ -23,6 +23,7 @@
 
 #include <CoreFoundation/CoreFoundation.h>
 #include <DirectoryService/DirectoryService.h>
+#include <Python.h>
 
 class CFStringUtil;
 
@@ -41,6 +42,23 @@
 	
 private:
 
+	class StPythonThreadState
+	{
+	public:
+		StPythonThreadState()
+		{
+			mSavedState = PyEval_SaveThread();
+ 		}
+		
+		~StPythonThreadState()
+		{
+			PyEval_RestoreThread(mSavedState);
+		}
+	
+	private:
+		PyThreadState* mSavedState;
+	};
+
 	const char*			mNodeName;
 	tDirReference		mDir;
 	tDirNodeReference	mNode;

Modified: PyOpenDirectory/trunk/src/CDirectoryServiceException.cpp
===================================================================
--- PyOpenDirectory/trunk/src/CDirectoryServiceException.cpp	2007-10-16 19:54:36 UTC (rev 1963)
+++ PyOpenDirectory/trunk/src/CDirectoryServiceException.cpp	2007-10-16 20:09:04 UTC (rev 1964)
@@ -30,6 +30,12 @@
 
 #pragma mark -----Public API
 
+CDirectoryServiceException::CDirectoryServiceException()
+{
+	mDSError = -1;
+	::snprintf(mDescription, 1024, "Unknown Error");
+}
+
 CDirectoryServiceException::CDirectoryServiceException(long error, const char* file, long line)
 {
 	mDSError = error;

Modified: PyOpenDirectory/trunk/src/CDirectoryServiceException.h
===================================================================
--- PyOpenDirectory/trunk/src/CDirectoryServiceException.h	2007-10-16 19:54:36 UTC (rev 1963)
+++ PyOpenDirectory/trunk/src/CDirectoryServiceException.h	2007-10-16 20:09:04 UTC (rev 1964)
@@ -24,6 +24,7 @@
 class CDirectoryServiceException
 {
 public:
+	CDirectoryServiceException();
 	CDirectoryServiceException(long error, const char* file, long line);
 	~CDirectoryServiceException();
 	

Modified: PyOpenDirectory/trunk/src/PythonWrapper.cpp
===================================================================
--- PyOpenDirectory/trunk/src/PythonWrapper.cpp	2007-10-16 19:54:36 UTC (rev 1963)
+++ PyOpenDirectory/trunk/src/PythonWrapper.cpp	2007-10-16 20:09:04 UTC (rev 1964)
@@ -295,9 +295,7 @@
 	{
 		std::auto_ptr<CDirectoryService> ds(dsmgr->GetService());
 		CFMutableArrayRef list = NULL;
-		Py_BEGIN_ALLOW_THREADS
 		list = ds->ListAllRecordsWithAttributes(recordType, cfattributes);
-		Py_END_ALLOW_THREADS
 		if (list != NULL)
 		{
 			PyObject* result = CFArrayArrayDictionaryToPyDict(list);
@@ -362,9 +360,7 @@
 	{
 		std::auto_ptr<CDirectoryService> ds(dsmgr->GetService());
 		CFMutableArrayRef list = NULL;
-		Py_BEGIN_ALLOW_THREADS
 		list = ds->QueryRecordsWithAttribute(attr, value, matchType, casei, recordType, cfattributes);
-		Py_END_ALLOW_THREADS
 		if (list != NULL)
 		{
 			PyObject* result = CFArrayArrayDictionaryToPyDict(list);
@@ -425,9 +421,7 @@
 	{
 		std::auto_ptr<CDirectoryService> ds(dsmgr->GetService());
 		CFMutableArrayRef list = NULL;
-		Py_BEGIN_ALLOW_THREADS
 		list = ds->QueryRecordsWithAttributes(query, casei, recordType, cfattributes);
-		Py_END_ALLOW_THREADS
 		if (list != NULL)
 		{
 			PyObject* result = CFArrayArrayDictionaryToPyDict(list);
@@ -480,9 +474,7 @@
 	{
 		std::auto_ptr<CDirectoryService> ds(dsmgr->GetService());
 		CFMutableArrayRef list = NULL;
-		Py_BEGIN_ALLOW_THREADS
 		list = ds->ListAllRecordsWithAttributes(recordType, cfattributes);
-		Py_END_ALLOW_THREADS
 		if (list != NULL)
 		{
 			PyObject* result = CFArrayArrayDictionaryToPyList(list);
@@ -547,9 +539,7 @@
 	{
 		std::auto_ptr<CDirectoryService> ds(dsmgr->GetService());
 		CFMutableArrayRef list = NULL;
-		Py_BEGIN_ALLOW_THREADS
 		list = ds->QueryRecordsWithAttribute(attr, value, matchType, casei, recordType, cfattributes);
-		Py_END_ALLOW_THREADS
 		if (list != NULL)
 		{
 			PyObject* result = CFArrayArrayDictionaryToPyList(list);
@@ -610,9 +600,7 @@
 	{
 		std::auto_ptr<CDirectoryService> ds(dsmgr->GetService());
 		CFMutableArrayRef list = NULL;
-		Py_BEGIN_ALLOW_THREADS
 		list = ds->QueryRecordsWithAttributes(query, casei, recordType, cfattributes);
-		Py_END_ALLOW_THREADS
 		if (list != NULL)
 		{
 			PyObject* result = CFArrayArrayDictionaryToPyList(list);
@@ -659,9 +647,7 @@
 		std::auto_ptr<CDirectoryService> ds(dsmgr->GetService());
 		bool result = false;
 		bool authresult = false;
-		Py_BEGIN_ALLOW_THREADS
 		result = ds->AuthenticateUserBasic(nodename, user, pswd, authresult);
-		Py_END_ALLOW_THREADS
 		if (result)
 		{
 			if (authresult)
@@ -710,9 +696,7 @@
 		std::auto_ptr<CDirectoryService> ds(dsmgr->GetService());
 		bool result = false;
 		bool authresult = false;
-		Py_BEGIN_ALLOW_THREADS
 		result = ds->AuthenticateUserDigest(nodename, user, challenge, response, method, authresult);
-		Py_END_ALLOW_THREADS
 		if (result)
 		{
 			if (authresult)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20071016/624cfc58/attachment.html


More information about the calendarserver-changes mailing list