[pypy-svn] r21169 - in pypy/dist/pypy: annotation rpython rpython/test

arigo at codespeak.net arigo at codespeak.net
Thu Dec 15 12:54:02 CET 2005


Author: arigo
Date: Thu Dec 15 12:53:59 2005
New Revision: 21169

Modified:
   pypy/dist/pypy/annotation/binaryop.py
   pypy/dist/pypy/rpython/rexternalobj.py
   pypy/dist/pypy/rpython/test/test_rexternalobj.py
Log:
Added support for merging SomeExternalObjects and None (thanks Christian).
Cleaned up a bit the corresponding code in binaryop.py.


Modified: pypy/dist/pypy/annotation/binaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/binaryop.py	(original)
+++ pypy/dist/pypy/annotation/binaryop.py	Thu Dec 15 12:53:59 2005
@@ -2,6 +2,7 @@
 Binary operations between SomeValues.
 """
 
+import py
 import operator
 from pypy.annotation.pairtype import pair, pairtype
 from pypy.annotation.model import SomeObject, SomeInteger, SomeBool
@@ -572,57 +573,32 @@
     def union((obj1, imp2)):
         return obj1
 
-class __extend__(pairtype(SomeInstance, SomePBC)):
-    def union((ins, pbc)):
-        if pbc.isNone():
-            return SomeInstance(classdef=ins.classdef, can_be_None = True)
-        raise UnionError("mixing pbc and instance not supported anymore:  %s %s" % (pbc, ins))
-        # XXX is the following still useful?
-        #classdef = ins.classdef.superdef_containing(pbc.knowntype)
-        #if classdef is None:
-        #    # print warning?
-        #    return SomeObject()
-        #if not getattr(TLS, 'no_side_effects_in_union', 0):
-        #    raise UnionError("mixing pbc and instance not supported anymore:  %s %s" % (pbc, ins))
-        #return SomeInstance(classdef)
-
-class __extend__(pairtype(SomePBC, SomeInstance)):
-    def union((pbc, ins)):
-        return pair(ins, pbc).union()
-
-# let mix lists and None for now
-class __extend__(pairtype(SomeList, SomePBC)):
-    def union((lst, pbc)):
-        if pbc.isNone():
-            return SomeList(lst.listdef)
-        return SomeObject()
-
-class __extend__(pairtype(SomePBC, SomeList    )):
-    def union((pbc, lst)):
-        return pair(lst, pbc).union()
-
-# let mix dicts and None
-class __extend__(pairtype(SomeDict, SomePBC)):
-    def union((dct, pbc)):
-        if pbc.isNone():
-            return SomeDict(dct.dictdef)
-        return SomeObject()
+# mixing Nones with other objects
 
-class __extend__(pairtype(SomePBC, SomeDict    )):
-    def union((pbc, dct)):
-        return pair(dct, pbc).union()
-
-# mixing strings and None
-
-class __extend__(pairtype(SomeString, SomePBC)):
-    def union((s, pbc)):
-        if pbc.isNone():
-            return SomeString(can_be_None=True)
-        return SomeObject()
+def _make_none_union(classname, constructor_args=''):
+    loc = locals()
+    source = py.code.Source("""
+        class __extend__(pairtype(%(classname)s, SomePBC)):
+            def union((obj, pbc)):
+                if pbc.isNone():
+                    return %(classname)s(%(constructor_args)s)
+                else:
+                    return SomeObject()
 
-class __extend__(pairtype(SomePBC, SomeString    )):
-    def union((pbc, s)):
-        return pair(s, pbc).union()
+        class __extend__(pairtype(SomePBC, %(classname)s)):
+            def union((pbc, obj)):
+                if pbc.isNone():
+                    return %(classname)s(%(constructor_args)s)
+                else:
+                    return SomeObject()
+    """ % loc)
+    exec source.compile() in globals()
+
+_make_none_union('SomeInstance',   'classdef=obj.classdef, can_be_None=True')
+_make_none_union('SomeString',      'can_be_None=True')
+_make_none_union('SomeList',         'obj.listdef')
+_make_none_union('SomeDict',          'obj.dictdef')
+_make_none_union('SomeExternalObject', 'obj.knowntype')
 
 # getitem on SomePBCs, in particular None fails
 

Modified: pypy/dist/pypy/rpython/rexternalobj.py
==============================================================================
--- pypy/dist/pypy/rpython/rexternalobj.py	(original)
+++ pypy/dist/pypy/rpython/rexternalobj.py	Thu Dec 15 12:53:59 2005
@@ -32,7 +32,7 @@
     def convert_const(self, value):
         T = self.exttypeinfo.get_lltype()
         if value is None:
-            return nullptr(T)
+            return lltype.nullptr(T)
         if not isinstance(value, self.exttypeinfo.typ):
             raise TyperError("expected a %r: %r" % (self.exttypeinfo.typ,
                                                     value))

Modified: pypy/dist/pypy/rpython/test/test_rexternalobj.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rexternalobj.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rexternalobj.py	Thu Dec 15 12:53:59 2005
@@ -19,3 +19,21 @@
     assert res is True
     res = interpret(fn, [1], policy=policy)
     assert res is False
+
+def test_lock_or_None():
+    import thread
+    import pypy.module.thread.rpython.exttable   # for declare()/declaretype()
+    def makelock(i):
+        if i > 0:
+            return thread.allocate_lock()
+        else:
+            return None
+    def fn(i):
+        lock = makelock(i)
+        return lock is not None and lock.acquire(False)
+    policy = AnnotatorPolicy()
+    policy.allow_someobjects = False
+    res = interpret(fn, [0], policy=policy)
+    assert res is False
+    res = interpret(fn, [1], policy=policy)
+    assert res is True



More information about the Pypy-commit mailing list