[pypy-svn] r18347 - in pypy/dist/pypy/rpython/ootype: . test

boria at codespeak.net boria at codespeak.net
Mon Oct 10 19:41:57 CEST 2005


Author: boria
Date: Mon Oct 10 19:41:56 2005
New Revision: 18347

Modified:
   pypy/dist/pypy/rpython/ootype/ootype.py
   pypy/dist/pypy/rpython/ootype/test/test_ootype.py
Log:
(Boris, Samuele, Arre, Bert)
* Initial tests and class implementation for high-level backend.


Modified: pypy/dist/pypy/rpython/ootype/ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootype/ootype.py	(original)
+++ pypy/dist/pypy/rpython/ootype/ootype.py	Mon Oct 10 19:41:56 2005
@@ -129,8 +129,43 @@
 Void     = Primitive("Void", None)
 UniChar  = Primitive("UniChar", u'\x00')
 
+class Class(OOType):
+
+    def __init__(self, name, superclass, fields):
+        self._fields = fields
+
+        for name, defn in fields.iteritems():
+            if type(defn) is not tuple:
+                fields[name] = (defn, defn._defl())
+
 # ____________________________________________________________
 
+class _instance(object):
+    
+    def __init__(self, CLASS):
+        self.__dict__["_TYPE"] = CLASS
+
+        for name, (ootype, default) in self._TYPE._fields.iteritems():
+            self.__dict__[name] = default
+
+    def __getattr__(self, name):
+        try:
+            self._TYPE._fields[name]
+        except KeyError:
+            raise TypeError("No field named %r" % name)
+
+        return self.__dict__[name]
+
+    def __setattr__(self, name, value):
+        self.__getattr__(name)
+            
+        if self._TYPE._fields[name][0] != typeOf(value):
+            raise TypeError("Expected type %r" % self._TYPE._fields[name][0])
+
+        self.__dict__[name] = value
+
+def new(CLASS):
+    return _instance(CLASS)
 
 def typeOf(val):
     try:

Modified: pypy/dist/pypy/rpython/ootype/test/test_ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootype/test/test_ootype.py	(original)
+++ pypy/dist/pypy/rpython/ootype/test/test_ootype.py	Mon Oct 10 19:41:56 2005
@@ -1,4 +1,23 @@
 from pypy.rpython.ootype.ootype import *
+import py
 
 def test_simple():
     assert typeOf(1) is Signed
+
+def test_simple_class():
+    C = Class("test", None, {"a": Signed})
+
+    c = new(C)
+    assert typeOf(c) == C
+    
+    py.test.raises(TypeError, "c.z")
+    py.test.raises(TypeError, "c.a = 3.0")
+
+    c.a = 3
+    assert c.a == 3
+
+def test_simple_default_class():
+    C = Class("test", None, {"a": (Signed, 3)})
+
+    c = new(C)
+    assert c.a == 3



More information about the Pypy-commit mailing list