瀏覽代碼

Possibly fix problem with cast_v2s and difficult strings

Yentl Van Tendeloo 8 年之前
父節點
當前提交
2dbce333b5
共有 2 個文件被更改,包括 7 次插入7 次删除
  1. 6 6
      kernel/modelverse_kernel/primitives.py
  2. 1 1
      kernel/test/primitives/test_cast.py

+ 6 - 6
kernel/modelverse_kernel/primitives.py

@@ -1,4 +1,5 @@
 import time as python_time
+import json
 
 class PrimitiveFinished(Exception):
     """Exception to indicate the result value of a primitive, as a return cannot be used."""
@@ -209,13 +210,12 @@ def cast_e2s(a, **remainder):
 
 def cast_v2s(a, **remainder):
     a_value, = yield [("RV", [a])]
-    if isinstance(a_value, (str, unicode)):
-        # String should be encoded to distinguish between 3 and "3"
-        a_value = '"%s"' % a_value
-    elif isinstance(a_value, dict):
+    if isinstance(a_value, dict):
         # Action or type
-        a_value = a_value["value"]
-    result, = yield [("CNV", ["%s" % (a_value)])]
+        value = a_value["value"]
+    else:
+        value = json.dumps(a_value)
+    result, = yield [("CNV", [value])]
     raise PrimitiveFinished(result)
 
 def cast_id2s(a, **remainder):

+ 1 - 1
kernel/test/primitives/test_cast.py

@@ -107,7 +107,7 @@ class TestCast(unittest.TestCase):
         self.helper_primitives_1_params("cast_v2s", "3", "\"3\"")
 
     def test_cast_v2s_bool(self):
-        self.helper_primitives_1_params("cast_v2s", True, "True")
+        self.helper_primitives_1_params("cast_v2s", True, "true")
 
     def test_cast_v2s_action(self):
         self.helper_primitives_1_params("cast_v2s", {"value": "call"}, "call")