[cig-commits] r14685 - cs/spatialdata-0.1/trunk/libsrc/units

knepley at geodynamics.org knepley at geodynamics.org
Mon Apr 13 16:36:29 PDT 2009


Author: knepley
Date: 2009-04-13 16:36:29 -0700 (Mon, 13 Apr 2009)
New Revision: 14685

Modified:
   cs/spatialdata-0.1/trunk/libsrc/units/Parser.cc
   cs/spatialdata-0.1/trunk/libsrc/units/Parser.hh
Log:
Added Python code for unit parsing


Modified: cs/spatialdata-0.1/trunk/libsrc/units/Parser.cc
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/units/Parser.cc	2009-04-13 23:26:21 UTC (rev 14684)
+++ cs/spatialdata-0.1/trunk/libsrc/units/Parser.cc	2009-04-13 23:36:29 UTC (rev 14685)
@@ -22,12 +22,19 @@
 // Default constructor
 spatialdata::units::Parser::Parser(void)
 { // constructor
+  // Should check for NULL, decode the exception, and throw a C++ equivalent
+  PyObject *mod = PyImport_ImportModule("pyre.units");
+  PyObject *cls = PyObject_GetAttrString(mod, "parser");
+  this->_p      = PyObject_CallFunctionObjArgs(cls, NULL);
+  Py_DECREF(cls);
+  Py_DECREF(mod);
 } // constructor
 
 // ----------------------------------------------------------------------
 // Default destructor
 spatialdata::units::Parser::~Parser(void)
 { // destructor
+  Py_DECREF(this->_p);
 } // destructor
 
 // ----------------------------------------------------------------------
@@ -48,7 +55,11 @@
    * Any nontrivial setup/teardown should be moved to
    * constructor/destructor.
    */
-
+  PyObject *pyUnit  = PyObject_CallFunction(this->_p, "s", units);
+  PyObject *pyScale = PyObject_GetAttrString(pyUnit, "value");
+  scale             = PyFloat_AsDouble(pyScale);
+  Py_DECREF(pyScale);
+  Py_DECREF(pyUnit);
   return scale;
 } // parser
 

Modified: cs/spatialdata-0.1/trunk/libsrc/units/Parser.hh
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/units/Parser.hh	2009-04-13 23:26:21 UTC (rev 14684)
+++ cs/spatialdata-0.1/trunk/libsrc/units/Parser.hh	2009-04-13 23:36:29 UTC (rev 14685)
@@ -20,6 +20,8 @@
 
 #include "unitsfwd.hh"
 
+#include "Python.h"
+
 /// C++ interface to Pyre units parser.
 class spatialdata::units::Parser
 { // class Parser
@@ -50,7 +52,7 @@
 private :
   // PRIVATE MEMBERS ////////////////////////////////////////////////////
 
-
+  PyObject *_p;
 }; // class Parser
 
 #endif // spatialdata_units_parser_hh



More information about the CIG-COMMITS mailing list