[cig-commits] r15397 - in cs/spatialdata-0.1/trunk: libsrc/units tests/libtests/units
brad at geodynamics.org
brad at geodynamics.org
Mon Jun 29 13:24:30 PDT 2009
Author: brad
Date: 2009-06-29 13:24:30 -0700 (Mon, 29 Jun 2009)
New Revision: 15397
Modified:
cs/spatialdata-0.1/trunk/libsrc/units/Parser.cc
cs/spatialdata-0.1/trunk/tests/libtests/units/TestParser.cc
cs/spatialdata-0.1/trunk/tests/libtests/units/TestParser.hh
Log:
Fixed trapping of Python errors in parsing of units.
Modified: cs/spatialdata-0.1/trunk/libsrc/units/Parser.cc
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/units/Parser.cc 2009-06-29 20:18:25 UTC (rev 15396)
+++ cs/spatialdata-0.1/trunk/libsrc/units/Parser.cc 2009-06-29 20:24:30 UTC (rev 15397)
@@ -62,27 +62,37 @@
* p = pyre.units.parser()
* x = p.parse(units) [units is a string]
* scale = x.value
- *
- * Any nontrivial setup/teardown should be moved to
- * constructor/destructor.
*/
PyObject *pyUnit = PyObject_CallMethod(_parser, "parse", "s", units);
- if (pyUnit == NULL) {
- if (PyErr_Occurred()) {PyErr_Print();}
- return 0.0;
- }
+ if (pyUnit == 0) {
+ if (PyErr_Occurred()) {
+ PyErr_Clear();
+ std::ostringstream msg;
+ msg << "Could not parse units string '" << units << "'.";
+ throw std::runtime_error(msg.str());
+ } // if
+ } // if
PyObject *pyScale = PyObject_GetAttrString(pyUnit, "value");
- if (pyScale == NULL) {
+ if (pyScale == 0) {
Py_DECREF(pyUnit);
- if (PyErr_Occurred()) {PyErr_Print();}
- return 0.0;
- }
+ if (PyErr_Occurred()) {
+ PyErr_Clear();
+ std::ostringstream msg;
+ msg << "Could not get floating point value when parsing units string '"
+ << units << "'.";
+ throw std::runtime_error(msg.str());
+ } // if
+ } // if
if (!PyFloat_Check(pyScale)) {
Py_DECREF(pyScale);
Py_DECREF(pyUnit);
- // Should throw a C++ exception. Which one?
- return 0.0;
- }
+ PyErr_Clear();
+ std::ostringstream msg;
+ msg << "Could not get floating point value when parsing units string '"
+ << units << "'.";
+ throw std::runtime_error(msg.str());
+ } // if
+
scale = PyFloat_AsDouble(pyScale);
Py_DECREF(pyScale);
Py_DECREF(pyUnit);
Modified: cs/spatialdata-0.1/trunk/tests/libtests/units/TestParser.cc
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/units/TestParser.cc 2009-06-29 20:18:25 UTC (rev 15396)
+++ cs/spatialdata-0.1/trunk/tests/libtests/units/TestParser.cc 2009-06-29 20:24:30 UTC (rev 15397)
@@ -96,4 +96,22 @@
} // testPressure
+// ----------------------------------------------------------------------
+// Test trapping errors with parse().
+void
+spatialdata::units::TestParser::testError(void)
+{ // testError
+ Parser parser;
+
+ bool caught = false;
+ try {
+ parser.parse("abc");
+ } catch (const std::exception& err) {
+ caught = true; // assume caught correct error
+ } // try/catch
+
+ CPPUNIT_ASSERT(caught);
+} // testError
+
+
// End of file
Modified: cs/spatialdata-0.1/trunk/tests/libtests/units/TestParser.hh
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/units/TestParser.hh 2009-06-29 20:18:25 UTC (rev 15396)
+++ cs/spatialdata-0.1/trunk/tests/libtests/units/TestParser.hh 2009-06-29 20:24:30 UTC (rev 15397)
@@ -42,6 +42,7 @@
CPPUNIT_TEST( testVelocity );
CPPUNIT_TEST( testDensity );
CPPUNIT_TEST( testPressure );
+ CPPUNIT_TEST( testError );
CPPUNIT_TEST_SUITE_END();
@@ -66,6 +67,9 @@
/// Test parse() with pressure scale.
void testPressure(void);
+ /// Test trapping errors with parse().
+ void testError(void);
+
}; // class TestParser
#endif // spatialdata_units_testparser_hh
More information about the CIG-COMMITS
mailing list