Commit 979c472a authored by Gemma Guest's avatar Gemma Guest
Browse files

Explicitly check python types for setting properties

Previously we were allowing the type checking to be done implicitly
which would mark int as bool etc.
parent fee52bfc
......@@ -97,15 +97,6 @@ template <typename T> void extractKwargs(const dict &kwargs, const std::string &
}
}
template <typename T> boost::optional<T> extractObject(const object &obj) {
boost::python::extract<T> extractor(obj);
if (extractor.check())
return boost::optional<T>(extractor);
else
return boost::none;
}
// Signature createChildWithProps(self, name, startProgress, endProgress, enableLogging, version, **kwargs)
object createChildWithProps(tuple args, dict kwargs) {
std::shared_ptr<Algorithm> parentAlg = extract<std::shared_ptr<Algorithm>>(args[0]);
......@@ -141,16 +132,21 @@ object createChildWithProps(tuple args, dict kwargs) {
if (!curArg)
continue;
if (auto val = extractObject<bool>(curArg))
const PyObject *rawptr = curArg.ptr();
// This currently doesn't handle lists, but this could be retrofitted in future work
if (PyBool_Check(rawptr) == 1) {
bool val = extract<bool>(curArg);
childAlg->setProperty(propName, val);
if (auto val = extractObject<float>(curArg))
} else if (PyFloat_Check(rawptr) == 1) {
double val = extract<double>(curArg);
childAlg->setProperty(propName, val);
if (auto val = extractObject<int>(curArg))
} else if (PyLong_Check(rawptr) == 1) {
int val = extract<int>(curArg);
childAlg->setProperty(propName, val);
// This currently doesn't handle lists, but this could be retrofitted in future work
std::string propValue = extract<std::string>(curArg);
childAlg->setPropertyValue(propName, propValue);
} else {
childAlg->setPropertyValue(propName, extract<std::string>(curArg));
}
}
return object(childAlg);
}
......
......@@ -203,15 +203,28 @@ class AlgorithmTest(unittest.TestCase):
self.assertIsNotNone(ws)
def xtest_createChildAlgorithm_with_int_arg(self):
def test_createChildAlgorithm_with_int_arg(self):
parent_alg = AlgorithmManager.createUnmanaged('Load')
num_banks = 4
child_alg = parent_alg.createChildAlgorithm("CreateSampleWorkspace", 0.0, 1.0, version=1,
enableLogging=False, NumEvents=1000)
enableLogging=False, NumBanks=num_banks)
self.assertTrue(child_alg.isChild())
child_alg.execute()
ws = child_alg.getProperty("OutputWorkspace").value
self.assertIsNotNone(ws)
default_num_histos = 100 # From createSampleWorkspace
self.assertEquals((default_num_histos * num_banks), ws.getNumberHistograms())
def test_createChildAlgorithm_with_float_arg(self):
parent_alg = AlgorithmManager.createUnmanaged('Load')
child_alg = parent_alg.createChildAlgorithm("CreateSampleWorkspace", 0.0, 1.0, version=1,
enableLogging=False, XMin=2.0)
self.assertTrue(child_alg.isChild())
child_alg.execute()
ws = child_alg.getProperty("OutputWorkspace").value
self.assertEquals(2.0, ws.readX(0)[0])
if __name__ == '__main__':
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment