tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ajbor...@apache.org
Subject svn commit: r510974 - in /incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python: PythonServiceWrapper.cpp sca_module.cpp
Date Fri, 23 Feb 2007 15:10:19 GMT
Author: ajborley
Date: Fri Feb 23 07:10:18 2007
New Revision: 510974

URL: http://svn.apache.org/viewvc?view=rev&rev=510974
Log:
Add more checking and support for ElementTree in pre-Python2.5

Modified:
    incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/PythonServiceWrapper.cpp
    incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/sca_module.cpp

Modified: incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/PythonServiceWrapper.cpp
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/PythonServiceWrapper.cpp?view=diff&rev=510974&r1=510973&r2=510974
==============================================================================
--- incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/PythonServiceWrapper.cpp
(original)
+++ incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/PythonServiceWrapper.cpp
Fri Feb 23 07:10:18 2007
@@ -293,7 +293,19 @@
 
                         // Load up the xml.etree.ElementTree module for dealing with SDO
params and return values
                         PyObject* elementTreeModuleName = PyString_FromString("xml.etree.ElementTree");

-                        PyObject* elementTreeModule = PyImport_Import(elementTreeModuleName);
                       
+                        PyObject* elementTreeModule = PyImport_Import(elementTreeModuleName);

+
+                        if(elementTreeModule == NULL)
+                        {
+                            // pre-Python2.5? - try to get an installed elementtree package
+                            elementTreeModuleName = PyString_FromString("elementtree.ElementTree");

+                            elementTreeModule = PyImport_Import(elementTreeModuleName); 
+                        }
+                        if(elementTreeModule == NULL)
+                        {
+                            // Still null - throw a warning but carry on - user may not need
XML
+                            logwarning("Could not load Python ElementTree module - is it
installed? SDO and XML will not be supported");
+                        }
 
                         for(unsigned int i = 0; i < operation.getNParms(); i++) 
                         {
@@ -370,25 +382,32 @@
     			                }
                                 case Operation::DATAOBJECT: 
                                 {
-                                    DataObjectPtr dob = *(DataObjectPtr*)parm.getValue();
+                                    if(elementTreeModule != NULL)
+                                    {
+                                        DataObjectPtr dob = *(DataObjectPtr*)parm.getValue();
 
-                                    // Convert a DataObject to a xml.etree.ElementTree Element
object
-                                    Composite* composite = component->getComposite();
                                   
-                                    XMLHelper* xmlHelper = composite->getXMLHelper();
-                                    char* str = xmlHelper->save(
-                                        dob,
-                                        dob->getType().getURI(),
-                                        dob->getType().getName());
+                                        // Convert a DataObject to a xml.etree.ElementTree
Element object
+                                        Composite* composite = component->getComposite();
                                   
+                                        XMLHelper* xmlHelper = composite->getXMLHelper();
+                                        char* str = xmlHelper->save(
+                                            dob,
+                                            dob->getType().getURI(),
+                                            dob->getType().getName());
 
-                                    loginfo("Converting SDO DataObject to Python ElementTree:
%s", str);
+                                        loginfo("Converting SDO DataObject to Python ElementTree:
%s", str);
 
-                                    // Get the xml.etree.ElementTree.XML function
-                                    PyObject* elementTreeXMLFunc = PyObject_GetAttrString(elementTreeModule,
"XML");
+                                        // Get the xml.etree.ElementTree.XML function
+                                        PyObject* elementTreeXMLFunc = PyObject_GetAttrString(elementTreeModule,
"XML");
 
-                                    // Call the XML() function with the XML string 
-                                    pValue = PyObject_CallFunction(elementTreeXMLFunc, "s",
str);
+                                        // Call the XML() function with the XML string 
+                                        pValue = PyObject_CallFunction(elementTreeXMLFunc,
"s", str);
 
-                                    Py_DECREF(elementTreeXMLFunc);
+                                        Py_DECREF(elementTreeXMLFunc);
+                                    }
+                                    else
+                                    {
+                                        throwException(ServiceDataException, "Could not convert
SDO DataObject to Python ElementTree as ElementTree module could not be loaded");
+                                    }
                                     break;
                                 }
                                 default:
@@ -778,11 +797,17 @@
                             }
                             else
                             {
-                                // Get the xml.etree.ElementTree.iselement function
-                                PyObject* elementTreeIsElementFunc = PyObject_GetAttrString(elementTreeModule,
"iselement");
+                                PyObject* pIsElement = Py_False;
 
-                                // Call the iselement() function with pValue to check it
-                                PyObject* pIsElement = PyObject_CallFunction(elementTreeIsElementFunc,
"O", pValue);
+                                if(elementTreeModule != NULL)
+                                {
+                                    // Get the xml.etree.ElementTree.iselement function
+                                    PyObject* elementTreeIsElementFunc = PyObject_GetAttrString(elementTreeModule,
"iselement");
+
+                                    // Call the iselement() function with pValue to check
it
+                                    PyObject* pIsElement = PyObject_CallFunction(elementTreeIsElementFunc,
"O", pValue);
+                                    Py_DECREF(elementTreeIsElementFunc);
+                                }
 
                                 if(PyObject_IsTrue(pIsElement) == 1)
                                 {
@@ -829,8 +854,7 @@
                                     Py_DECREF(valueRepr);
                                 }
 
-                                Py_DECREF(pIsElement);
-                                Py_DECREF(elementTreeIsElementFunc);
+                                Py_DECREF(pIsElement);                                
                             }
 
                             Py_DECREF(elementTreeModule);

Modified: incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/sca_module.cpp
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/sca_module.cpp?view=diff&rev=510974&r1=510973&r2=510974
==============================================================================
--- incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/sca_module.cpp
(original)
+++ incubator/tuscany/cpp/sca/runtime/extensions/python/src/tuscany/sca/python/sca_module.cpp
Fri Feb 23 07:10:18 2007
@@ -234,6 +234,18 @@
     PyObject* elementTreeModuleName = PyString_FromString("xml.etree.ElementTree"); 
     PyObject* elementTreeModule = PyImport_Import(elementTreeModuleName);               
        
 
+    if(elementTreeModule == NULL)
+    {
+        // pre-Python2.5? - try to get an installed elementtree package
+        elementTreeModuleName = PyString_FromString("elementtree.ElementTree"); 
+        elementTreeModule = PyImport_Import(elementTreeModuleName); 
+    }
+    if(elementTreeModule == NULL)
+    {
+        // Still null - throw a warning but carry on - user may not need XML
+        logwarning("Could not load Python ElementTree module - is it installed? SDO and XML
will not be supported");
+    }
+
     // Parameters are the fourth argument
     PyObject* paramTuple = PyTuple_GetItem(args, 3);
     unsigned int numberOfArgs = (unsigned int) PyTuple_Size(paramTuple);
@@ -300,11 +312,15 @@
         }
         else
         {
-            // Get the xml.etree.ElementTree.iselement function
-            PyObject* elementTreeIsElementFunc = PyObject_GetAttrString(elementTreeModule,
"iselement");
+            PyObject* pIsElement = Py_False;
+            if(elementTreeModule != NULL)
+            {
+                // Get the xml.etree.ElementTree.iselement function
+                PyObject* elementTreeIsElementFunc = PyObject_GetAttrString(elementTreeModule,
"iselement");
 
-            // Call the iselement() function with pValue to check it
-            PyObject* pIsElement = PyObject_CallFunction(elementTreeIsElementFunc, "O", param);
+                // Call the iselement() function with pValue to check it
+                PyObject* pIsElement = PyObject_CallFunction(elementTreeIsElementFunc, "O",
param);
+            }
 
             if(PyObject_IsTrue(pIsElement) == 1)
             {
@@ -342,7 +358,7 @@
                     logerror(msg.c_str());
                     PyErr_SetString(scaError, msg.c_str());
                     return NULL;
-                }                                    
+                }
             }
             else
             {
@@ -462,25 +478,34 @@
             }
         case Operation::DATAOBJECT: 
             {
-                DataObjectPtr dob = *(DataObjectPtr*)operation.getReturnValue();
+                if(elementTreeModule != NULL)
+                {
+                    DataObjectPtr dob = *(DataObjectPtr*)operation.getReturnValue();
 
-                // Convert a DataObject to a xml.etree.ElementTree Element object
-                Composite* composite = component->getComposite();                    
               
-                XMLHelper* xmlHelper = composite->getXMLHelper();
-                char* str = xmlHelper->save(
-                    dob,
-                    dob->getType().getURI(),
-                    dob->getType().getName());                                    
+                    // Convert a DataObject to a xml.etree.ElementTree Element object
+                    Composite* composite = component->getComposite();                
                   
+                    XMLHelper* xmlHelper = composite->getXMLHelper();
+                    char* str = xmlHelper->save(
+                        dob,
+                        dob->getType().getURI(),
+                        dob->getType().getName());                                   

 
-                loginfo("Converting SDO DataObject to Python ElementTree: %s", str);
+                    loginfo("Converting SDO DataObject to Python ElementTree: %s", str);
 
-                // Get the xml.etree.ElementTree.XML function
-                PyObject* elementTreeXMLFunc = PyObject_GetAttrString(elementTreeModule,
"XML");
+                    // Get the xml.etree.ElementTree.XML function
+                    PyObject* elementTreeXMLFunc = PyObject_GetAttrString(elementTreeModule,
"XML");
 
-                // Call the XML() function with the XML string 
-                returnValue = PyObject_CallFunction(elementTreeXMLFunc, "s", str);
+                    // Call the XML() function with the XML string 
+                    returnValue = PyObject_CallFunction(elementTreeXMLFunc, "s", str);
 
-                Py_DECREF(elementTreeXMLFunc);
+                    Py_DECREF(elementTreeXMLFunc);
+                }
+                else
+                {
+                    logwarning("Could not convert SDO DataObject to Python ElementTree as
ElementTree module could not be loaded. Returning NONE");
+                    Py_INCREF(Py_None);
+                    returnValue = Py_None;
+                }
                 break;
             }
         default:



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org


Mime
View raw message