Bladeren bron

pygrass: modify the parameter check function to be less stringent with strings parameters

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@61182 15284696-431f-4ddb-bdfa-cd5b030d7da7
Pietro Zambelli 11 jaren geleden
bovenliggende
commit
9931034a89

+ 8 - 5
lib/python/pygrass/modules/interface/parameter.py

@@ -18,7 +18,6 @@ def _check_value(param, value):
     """
     must_val = 'The Parameter <%s>, must be one of the following values: %r'
     req = 'The Parameter <%s>, require: %s, get: %s instead: %r\n%s'
-    string = (str, unicode)
 
     def raiseexcpet(exc, param, ptype, value):
         """Function to modifa the error message"""
@@ -33,10 +32,14 @@ def _check_value(param, value):
 
     def check_string(value):
         """Function to check that a string parameter is already a string"""
-        if param.type in string and type(value) not in string:
-            msg = ("The Parameter <%s> require a string,"
-                   " %s instead is provided: %r")
-            raise ValueError(msg % (param.name, type(value), value))
+        string = (str, unicode)
+        if param.type in string:
+            if type(value) in (int, float):
+                value = str(value)
+            if type(value) not in string:
+                msg = ("The Parameter <%s> require a string,"
+                       " %s instead is provided: %r")
+                raise ValueError(msg % (param.name, type(value), value))
         return value
 
     # return None if None

+ 13 - 6
lib/python/pygrass/modules/interface/testsuite/test_parameter.py

@@ -189,7 +189,6 @@ class TestCheckValueFunction(unittest.TestCase):
             _check_value(param, "elev")
         with self.assertRaises(TypeError):
             _check_value(param, (1, 2))
-        #import ipdb; ipdb.set_trace()
         with self.assertRaises(ValueError):
             _check_value(param, 3)
 
@@ -199,12 +198,14 @@ class TestCheckValueFunction(unittest.TestCase):
                                        multiple='no', type=ptype))
             value = u'elev'
             self.assertTupleEqual((value, value), _check_value(param, value))
+            value = 10
+            self.assertTupleEqual((str(value), value),
+                                  _check_value(param, value))
+            value = 12.5
+            self.assertTupleEqual((str(value), value),
+                                  _check_value(param, value))
 
             # test errors
-            with self.assertRaises(ValueError):
-                _check_value(param, 1)
-            with self.assertRaises(ValueError):
-                _check_value(param, 1.0)
             with self.assertRaises(TypeError):
                 _check_value(param, ('abc', 'def'))
 
@@ -217,10 +218,16 @@ class TestCheckValueFunction(unittest.TestCase):
         self.assertTupleEqual((list(value), value), _check_value(param, value))
         value = ['1.3', '2.3', '4.5']
         self.assertTupleEqual((value, value), _check_value(param, value))
+        value = [1.3, 2.3, 4.5]
+        self.assertTupleEqual(([str(v) for v in value], value),
+                              _check_value(param, value))
+        value = (1, 2, 3)
+        self.assertTupleEqual(([str(v) for v in value], value),
+                              _check_value(param, value))
 
         # test errors
         with self.assertRaises(ValueError):
-            _check_value(param, (1, 2, 3))
+            _check_value(param, ({}, {}, {}))
 
     def test_choice_string(self):
         values = ["elev", "asp", "slp"]