[pypy-svn] r20895 - in pypy/dist/pypy: annotation translator/c/test

arigo at codespeak.net arigo at codespeak.net
Thu Dec 8 14:21:33 CET 2005


Author: arigo
Date: Thu Dec  8 14:21:32 2005
New Revision: 20895

Modified:
   pypy/dist/pypy/annotation/binaryop.py
   pypy/dist/pypy/annotation/builtin.py
   pypy/dist/pypy/annotation/model.py
   pypy/dist/pypy/annotation/unaryop.py
   pypy/dist/pypy/translator/c/test/test_tasklets.py
   pypy/dist/pypy/translator/c/test/test_typed.py
Log:
(pedronis, arigo)

Fixed misusages of s.is_constant() for non-immutable objects --
in this case, s.const is valid as far as its identity is concerned,
but s.const can still get mutated.


Modified: pypy/dist/pypy/annotation/binaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/binaryop.py	(original)
+++ pypy/dist/pypy/annotation/binaryop.py	Thu Dec  8 14:21:32 2005
@@ -101,42 +101,42 @@
     inplace_mod.can_only_throw = [ZeroDivisionError]
 
     def lt((obj1, obj2)):
-        if obj1.is_constant() and obj2.is_constant():
+        if obj1.is_immutable_constant() and obj2.is_immutable_constant():
             return immutablevalue(obj1.const < obj2.const)
         else:
             getbookkeeper().count("non_int_comp", obj1, obj2)
             return SomeBool()
 
     def le((obj1, obj2)):
-        if obj1.is_constant() and obj2.is_constant():
+        if obj1.is_immutable_constant() and obj2.is_immutable_constant():
             return immutablevalue(obj1.const <= obj2.const)
         else:
             getbookkeeper().count("non_int_comp", obj1, obj2)
             return SomeBool()
 
     def eq((obj1, obj2)):
-        if obj1.is_constant() and obj2.is_constant():
+        if obj1.is_immutable_constant() and obj2.is_immutable_constant():
             return immutablevalue(obj1.const == obj2.const)
         else:
             getbookkeeper().count("non_int_eq", obj1, obj2)
             return SomeBool()
 
     def ne((obj1, obj2)):
-        if obj1.is_constant() and obj2.is_constant():
+        if obj1.is_immutable_constant() and obj2.is_immutable_constant():
             return immutablevalue(obj1.const != obj2.const)
         else:
             getbookkeeper().count("non_int_eq", obj1, obj2)
             return SomeBool()
 
     def gt((obj1, obj2)):
-        if obj1.is_constant() and obj2.is_constant():
+        if obj1.is_immutable_constant() and obj2.is_immutable_constant():
             return immutablevalue(obj1.const > obj2.const)
         else:
             getbookkeeper().count("non_int_comp", obj1, obj2)
             return SomeBool()
 
     def ge((obj1, obj2)):
-        if obj1.is_constant() and obj2.is_constant():
+        if obj1.is_immutable_constant() and obj2.is_immutable_constant():
             return immutablevalue(obj1.const >= obj2.const)
         else:
             getbookkeeper().count("non_int_comp", obj1, obj2)
@@ -144,7 +144,7 @@
 
     def cmp((obj1, obj2)):
         getbookkeeper().count("cmp", obj1, obj2)
-        if obj1.is_constant() and obj2.is_constant():
+        if obj1.is_immutable_constant() and obj2.is_immutable_constant():
             return immutablevalue(cmp(obj1.const, obj2.const))
         else:
             return SomeInteger()

Modified: pypy/dist/pypy/annotation/builtin.py
==============================================================================
--- pypy/dist/pypy/annotation/builtin.py	(original)
+++ pypy/dist/pypy/annotation/builtin.py	Thu Dec  8 14:21:32 2005
@@ -30,7 +30,7 @@
     """
     args = []
     for s in args_s:
-        if not s.is_constant():
+        if not s.is_immutable_constant():
             return s_result
         args.append(s.const)
     realresult = func(*args)
@@ -70,7 +70,7 @@
 builtin_xrange = builtin_range # xxx for now allow it
 
 def builtin_bool(s_obj):
-    return constpropagate(bool, [s_obj], SomeBool())
+    return s_obj.is_true()
 
 def builtin_int(s_obj, s_base=None):
     assert (s_base is None or isinstance(s_base, SomeInteger)
@@ -172,7 +172,7 @@
         getbookkeeper().warning('hasattr(%r, %r) is not RPythonic enough' %
                                 (s_obj, s_attr))
     r = SomeBool()
-    if s_obj.is_constant():
+    if s_obj.is_immutable_constant():
         r.const = hasattr(s_obj.const, s_attr.const)
     elif (isinstance(s_obj, SomePBC)
           and s_obj.getKind() is description.FrozenDesc):

Modified: pypy/dist/pypy/annotation/model.py
==============================================================================
--- pypy/dist/pypy/annotation/model.py	(original)
+++ pypy/dist/pypy/annotation/model.py	Thu Dec  8 14:21:32 2005
@@ -63,6 +63,7 @@
     for an arbitrary object about which nothing is known."""
     __metaclass__ = extendabletype
     knowntype = object
+    immutable = False
 
     def __eq__(self, other):
         return (self.__class__ is other.__class__ and
@@ -113,6 +114,9 @@
     def is_constant(self):
         return hasattr(self, 'const')
 
+    def is_immutable_constant(self):
+        return self.immutable and hasattr(self, 'const')
+
     # for debugging, record where each instance comes from
     # this is disabled if DEBUG is set to False
     _coming_from = {}
@@ -152,6 +156,7 @@
     "Stands for a float or an integer."
     knowntype = float   # if we don't know if it's a float or an int,
                         # pretend it's a float.
+    immutable = True
 
     def can_be_none(self):
         return False
@@ -192,6 +197,7 @@
 class SomeString(SomeObject):
     "Stands for an object which is known to be a string."
     knowntype = str
+    immutable = True
     def __init__(self, can_be_None=False):
         self.can_be_None = can_be_None
 
@@ -205,8 +211,9 @@
     "Stands for an object known to be a string of length 1."
 
 class SomeUnicodeCodePoint(SomeObject):
-    knowntype = unicode
     "Stands for an object known to be a unicode codepoint."
+    knowntype = unicode
+    immutable = True
 
     def can_be_none(self):
         return False
@@ -232,6 +239,7 @@
 
 class SomeSlice(SomeObject):
     knowntype = slice
+    immutable = True
     def __init__(self, start, stop, step):
         self.start = start
         self.stop = stop
@@ -243,6 +251,7 @@
 class SomeTuple(SomeObject):
     "Stands for a tuple of known length."
     knowntype = tuple
+    immutable = True
     def __init__(self, items):
         self.items = tuple(items)   # tuple of s_xxx elements
         for i in items:
@@ -316,6 +325,8 @@
 class SomePBC(SomeObject):
     """Stands for a global user instance, built prior to the analysis,
     or a set of such instances."""
+    immutable = True
+
     def __init__(self, descriptions, can_be_None=False):
         # descriptions is a set of Desc instances.
         descriptions = dict.fromkeys(descriptions)
@@ -390,6 +401,8 @@
 class SomeBuiltin(SomeObject):
     "Stands for a built-in function or method with special-cased analysis."
     knowntype = BuiltinFunctionType  # == BuiltinMethodType
+    immutable = True
+
     def __init__(self, analyser, s_self=None, methodname=None):
         self.analyser = analyser
         self.s_self = s_self
@@ -414,6 +427,7 @@
 class SomeImpossibleValue(SomeObject):
     """The empty set.  Instances are placeholders for objects that
     will never show up at run-time, e.g. elements of an empty list."""
+    immutable = True
 
     def can_be_none(self):
         return False
@@ -428,6 +442,7 @@
 from pypy.rpython.memory import lladdress
 
 class SomeAddress(SomeObject):
+    immutable = True
     def __init__(self, is_null=False):
         self.is_null = is_null
 
@@ -450,6 +465,7 @@
 # annotation of low-level types
 
 class SomePtr(SomeObject):
+    immutable = True
     def __init__(self, ll_ptrtype):
         self.ll_ptrtype = ll_ptrtype
 
@@ -457,6 +473,7 @@
         return False
 
 class SomeLLADTMeth(SomeObject):
+    immutable = True
     def __init__(self, ll_ptrtype, func):
         self.ll_ptrtype = ll_ptrtype
         self.func = func 
@@ -473,11 +490,13 @@
         self.ootype = ootype
 
 class SomeOOBoundMeth(SomeObject):
+    immutable = True
     def __init__(self, ootype, name):
         self.ootype = ootype
         self.name = name
 
 class SomeOOStaticMeth(SomeObject):
+    immutable = True
     def __init__(self, method):
         self.method = method
         

Modified: pypy/dist/pypy/annotation/unaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/unaryop.py	(original)
+++ pypy/dist/pypy/annotation/unaryop.py	Thu Dec  8 14:21:32 2005
@@ -66,7 +66,7 @@
         return SomeInteger(nonneg=True)
 
     def is_true_behavior(obj):
-        if obj.is_constant():
+        if obj.is_immutable_constant():
             return immutablevalue(bool(obj.const))
         else:
             s_len = obj.len()
@@ -151,7 +151,7 @@
             if s_method is not None:
                 return s_method
             # if the SomeObject is itself a constant, allow reading its attrs
-            if obj.is_constant() and hasattr(obj.const, attr):
+            if obj.is_immutable_constant() and hasattr(obj.const, attr):
                 return immutablevalue(getattr(obj.const, attr))
         else:
             getbookkeeper().warning('getattr(%r, %r) is not RPythonic enough' %
@@ -479,6 +479,12 @@
         getbookkeeper().needs_hash_support[ins.classdef] = True
         return SomeInteger()
 
+    def is_true_behavior(ins):
+        if ins.can_be_None:
+            return SomeBool()
+        else:
+            return immutablevalue(True)
+
 
 class __extend__(SomeBuiltin):
     def simple_call(bltn, *args):

Modified: pypy/dist/pypy/translator/c/test/test_tasklets.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_tasklets.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_tasklets.py	Thu Dec  8 14:21:32 2005
@@ -68,7 +68,7 @@
 
     def run(self):            
         debug("running: length of runnables %s" % len(self.runnables))
-        while len(self.runnables):
+        while self.runnables:
             t = self.runnables.pop(0)
             debug("resuming %s(%s)" % (t.name, t.alive))
             self.current_tasklet = t
@@ -117,4 +117,5 @@
         run()
         return c.get_count() == 25
     
-    assert wrap_stackless_function(f) == '1'
+    res = wrap_stackless_function(f)
+    assert res == '1'

Modified: pypy/dist/pypy/translator/c/test/test_typed.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_typed.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_typed.py	Thu Dec  8 14:21:32 2005
@@ -419,13 +419,8 @@
             
         a1 = A()
         def f():
-            a2 = A()
             for ii in range(1):
                 a1.append_to_list(X())
-                a2.append_to_list(X())
-            return a1.check_list_is_true() + 2 * a2.check_list_is_true()
+            return a1.check_list_is_true()
         fn = self.getcompiled(f)
-        assert fn() == 3        
-        
-
-            
+        assert fn() == 1



More information about the Pypy-commit mailing list