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

arigo at codespeak.net arigo at codespeak.net
Tue Aug 23 16:23:28 CEST 2005


Author: arigo
Date: Tue Aug 23 16:23:26 2005
New Revision: 16273

Added:
   pypy/dist/pypy/rpython/test/test_rexternalobj.py   (contents, props changed)
Modified:
   pypy/dist/pypy/rpython/lltype.py
   pypy/dist/pypy/rpython/rexternalobj.py
   pypy/dist/pypy/rpython/test/test_lltype.py
Log:
* Support for prebuilt constants of external type in the RTyper.
  Useful for prebuilt thread.LockType objects.

* Completed the move of lltype.OpaqueType towards inlineability inside
  structs, with a test.  Also tests parentlink().


Modified: pypy/dist/pypy/rpython/lltype.py
==============================================================================
--- pypy/dist/pypy/rpython/lltype.py	(original)
+++ pypy/dist/pypy/rpython/lltype.py	Tue Aug 23 16:23:26 2005
@@ -282,7 +282,7 @@
         return _opaque(self)
 
     def _defl(self, parent=None, parentindex=None):
-        return self._container_example()
+        return _opaque(self, parent=parent, parentindex=parentindex)
 
 RuntimeTypeInfo = OpaqueType("RuntimeTypeInfo")
 
@@ -690,12 +690,10 @@
         if n is None and TYPE._arrayfld is not None:
             raise TypeError("%r is variable-sized" % (TYPE,))
         for fld, typ in TYPE._flds.items():
-            if isinstance(typ, Struct):
-                value = _struct(typ, parent=self, parentindex=fld)
-            elif fld == TYPE._arrayfld:
+            if fld == TYPE._arrayfld:
                 value = _array(typ, n, parent=self, parentindex=fld)
             else:
-                value = typ._defl()
+                value = typ._defl(parent=self, parentindex=fld)
             setattr(self, fld, value)
         if parent is not None:
             self._setparentstructure(parent, parentindex)
@@ -783,17 +781,13 @@
     def __hash__(self):
         return hash(frozendict(self.__dict__))
 
-class _opaque(object):
-    def __init__(self, TYPE, **attrs):
-        self._TYPE = TYPE
+class _opaque(_parentable):
+    def __init__(self, TYPE, parent=None, parentindex=None, **attrs):
+        _parentable.__init__(self, TYPE)
         self._name = "?"
         self.__dict__.update(attrs)
-
-    def _parentstructure(self):
-        return None
-
-    def _check(self):
-        pass
+        if parent is not None:
+            self._setparentstructure(parent, parentindex)
 
     def __repr__(self):
         return '<%s>' % (self,)

Modified: pypy/dist/pypy/rpython/rexternalobj.py
==============================================================================
--- pypy/dist/pypy/rpython/rexternalobj.py	(original)
+++ pypy/dist/pypy/rpython/rexternalobj.py	Tue Aug 23 16:23:26 2005
@@ -4,6 +4,8 @@
 from pypy.rpython.rmodel import Repr
 from pypy.rpython.extfunctable import typetable
 from pypy.rpython import rbuiltin
+from pypy.rpython.module.support import init_opaque_object
+from pypy.objspace.flow.model import Constant
 from pypy.tool import sourcetools
 
 
@@ -20,6 +22,7 @@
         self.exttypeinfo = typetable[knowntype]
         TYPE = self.exttypeinfo.get_lltype()
         self.lowleveltype = lltype.Ptr(TYPE)
+        self.instance_cache = {}
         # The set of methods supported depends on 'knowntype', so we
         # cannot have rtype_method_xxx() methods directly on the
         # ExternalObjRepr class.  But we can store them in 'self' now.
@@ -27,3 +30,19 @@
             methodname = 'rtype_method_' + name
             bltintyper = rbuiltin.make_rtype_extfunc(extfuncinfo)
             setattr(self, methodname, bltintyper)
+
+    def convert_const(self, value):
+        T = self.exttypeinfo.get_lltype()
+        if value is None:
+            return nullptr(T)
+        if not isinstance(value, self.exttypeinfo.typ):
+            raise TyperError("expected a %r: %r" % (self.exttypeinfo.typ,
+                                                    value))
+        key = Constant(value)
+        try:
+            p = self.instance_cache[key]
+        except KeyError:
+            p = lltype.malloc(T)
+            init_opaque_object(p.obj, value)
+            self.instance_cache[key] = p
+        return p

Modified: pypy/dist/pypy/rpython/test/test_lltype.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_lltype.py	(original)
+++ pypy/dist/pypy/rpython/test/test_lltype.py	Tue Aug 23 16:23:26 2005
@@ -396,4 +396,14 @@
     assert typeOf(p).TO == S
     assert not isweak(p, S)
     
-    
+def test_opaque():
+    O = OpaqueType('O')
+    p1 = opaqueptr(O, 'p1', hello="world")
+    assert typeOf(p1) == Ptr(O)
+    assert p1._obj.hello == "world"
+    assert parentlink(p1._obj) == (None, None)
+    S = GcStruct('S', ('stuff', O))
+    p2 = malloc(S)
+    assert typeOf(p2) == Ptr(S)
+    assert typeOf(p2.stuff) == Ptr(O)
+    assert parentlink(p2.stuff._obj) == (p2._obj, 'stuff')

Added: pypy/dist/pypy/rpython/test/test_rexternalobj.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/test/test_rexternalobj.py	Tue Aug 23 16:23:26 2005
@@ -0,0 +1,21 @@
+from pypy.rpython.lltype import *
+from pypy.rpython.test.test_llinterp import interpret, gengraph
+from pypy.annotation.policy import AnnotatorPolicy
+
+def test_prebuilt_lock():
+    import thread
+    import pypy.module.thread.rpython.exttable   # for declare()/declaretype()
+    lock0 = thread.allocate_lock()
+    lock1 = thread.allocate_lock()
+    lock1.acquire()
+    def fn(i):
+        lock = [lock0, lock1][i]
+        ok = lock.acquire(False)
+        if ok: lock.release()
+        return ok
+    policy = AnnotatorPolicy()
+    policy.allow_someobjects = False
+    res = interpret(fn, [0], policy=policy)
+    assert res is True
+    res = interpret(fn, [1], policy=policy)
+    assert res is False



More information about the Pypy-commit mailing list