Commits

Carl Friedrich Bolz  committed d23ba11

(arigo, cfbolz): a branch where we want "not x" to no longer split paths in the
flow object space. So far, flow space and annotation are fixed.

  • Participants
  • Parent commits 40d6226
  • Branches refactor-not-in-translator

Comments (0)

Files changed (4)

File pypy/annotation/test/test_annrpython.py

         s = a.build_types(snippet.prime, [int])
         assert s.knowntype == bool
 
+    def test_not_bool(self):
+        def f(x):
+            return not x
+        a = self.RPythonAnnotator()
+        s = a.build_types(f, [int])
+        assert s == annmodel.SomeBool()
+
+        def g():
+            return f(True)
+        a = self.RPythonAnnotator()
+        s = a.build_types(g, [])
+        assert isinstance(s, annmodel.SomeBool)
+        assert s.const is False
+
     def test_and_is_true_coalesce(self):
         def f(a,b,c,d,e):
             x = a and b
         assert isinstance(s, annmodel.SomeInstance)
         assert s.classdef == a.bookkeeper.getuniqueclassdef(B)
 
+
     def test_type_is_no_improvement(self):
         class B(object):
             pass
         s = a.build_types(f, [D])
         assert s == annmodel.SomeImpossibleValue()
 
+    def test_type_not_isinstance(self):
+        class B(object):
+            pass
+        class C(B):
+            pass
+        def f(x):
+            c = not isinstance(x, C)
+            if c:
+                return None
+            return x
+        a = self.RPythonAnnotator()
+        s = a.build_types(f, [B])
+        assert isinstance(s, annmodel.SomeInstance)
+        assert s.classdef == a.bookkeeper.getuniqueclassdef(C)
+
     def test_is_constant_instance(self):
         class A(object):
             pass

File pypy/annotation/unaryop.py

 def immutablevalue(x):
     return getbookkeeper().immutablevalue(x)
 
-UNARY_OPERATIONS = set(['len', 'is_true', 'getattr', 'setattr', 'delattr',
+UNARY_OPERATIONS = set(['len', 'is_true', 'is_false',
+                        'getattr', 'setattr', 'delattr',
                         'simple_call', 'call_args', 'str', 'repr',
                         'iter', 'next', 'invert', 'type', 'issubtype',
                         'pos', 'neg', 'nonzero', 'abs', 'hex', 'oct',
             s_nonnone_obj = s_obj.nonnoneify()
         add_knowntypedata(knowntypedata, True, [arg], s_nonnone_obj)
         return r
-        
+
+    def is_false(s_obj):
+        s_bool = s_obj.is_true()
+        s_res = SomeBool()
+        if s_bool.is_constant():
+            s_res.const = not s_bool.const
+        if hasattr(s_bool, "knowntypedata"):
+            ktd = {}
+            for (cond, var), s_stuff in s_bool.knowntypedata.iteritems():
+                ktd[not cond, var] = s_stuff
+            s_res.knowntypedata = ktd
+        return s_res
 
     def nonzero(obj):
         return obj.is_true()

File pypy/objspace/flow/objspace.py

         context = self.getexecutioncontext()
         return context.guessbool(w_truthvalue)
 
+    def not_(self, w_obj):
+        try:
+            obj = self.unwrap_for_computation(w_obj)
+        except UnwrapException:
+            pass
+        else:
+            return not obj
+        w_falthvalue = self.do_operation('is_false', w_obj)
+        return w_falthvalue
+
+
     def iter(self, w_iterable):
         try:
             iterable = self.unwrap(w_iterable)

File pypy/objspace/flow/test/test_objspace.py

         simplify_graph(graph)
         assert self.all_operations(graph) == {'getitem': 1}
 
+    def test_not_does_not_split(self):
+        def f(x):
+            return not x
+        graph = self.codetest(f)
+        assert self.all_operations(graph) == {"is_false": 1}
+
+
     def test_context_manager(self):
         def f(c, x):
             with x: