Commits

Carl Friedrich Bolz committed a65817c Draft

add some kind of rpython tuple support

  • Participants
  • Parent commits 7b39d35

Comments (0)

Files changed (2)

     __metaclass__ = RPythonClass
 
 
+class RPyTuple(RPyType):
+    _cache = {}
+
+    def type_check(self, value):
+        if not isinstance(value, tuple) or len(value) != len(self.element_types):
+            raise RPythonTypeError
+        for val, typ in zip(value, self.element_types):
+            typ.type_check(val)
+
+    @staticmethod
+    def make_tuple_type(types):
+        key = tuple(types)
+        res = RPyTuple._cache.get(key)
+        if not res:
+            res = RPyTuple._cache[key] = RPyTuple(str(key), (tuple, ), {"element_types": types})
+        return res
+
+
 def get_rpy_type(obj):
     if isinstance(obj, RPyType):
         return obj
         return RPyInt
     if obj is float:
         return RPyFloat
+    if isinstance(obj, tuple):
+        return RPyTuple.make_tuple_type([get_rpy_type(t) for t in obj])
     assert 0, "unknown type %s" % obj
     a.x = 31
     assert a.x == 31
     py.test.raises(r.RPythonTypeError, "a.x = 1")
+
+def test_rpy_tuple():
+    ttyp = r.get_rpy_type((int, int))
+    assert r.get_rpy_type((int, int)) is ttyp
+    ttyp.type_check((1, 2))
+    py.test.raises(r.RPythonTypeError, ttyp.type_check, (1, "abc"))