[pypy-commit] pypy __debug__-optimize: add space and compile_info parameters to as_constant()

rlamy pypy.commits at gmail.com
Tue Aug 6 11:05:12 EDT 2019


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: __debug__-optimize
Changeset: r97076:25c246923b60
Date: 2019-08-06 16:02 +0100
http://bitbucket.org/pypy/pypy/changeset/25c246923b60/

Log:	add space and compile_info parameters to as_constant()

diff --git a/pypy/interpreter/astcompiler/codegen.py b/pypy/interpreter/astcompiler/codegen.py
--- a/pypy/interpreter/astcompiler/codegen.py
+++ b/pypy/interpreter/astcompiler/codegen.py
@@ -542,7 +542,8 @@
     def visit_If(self, if_):
         self.update_position(if_.lineno, True)
         end = self.new_block()
-        test_constant = if_.test.as_constant_truth(self.space)
+        test_constant = if_.test.as_constant_truth(
+            self.space, self.compile_info)
         if test_constant == optimize.CONST_FALSE:
             self.visit_sequence(if_.orelse)
         elif test_constant == optimize.CONST_TRUE:
@@ -686,7 +687,7 @@
 
     def visit_While(self, wh):
         self.update_position(wh.lineno, True)
-        test_constant = wh.test.as_constant_truth(self.space)
+        test_constant = wh.test.as_constant_truth(self.space, self.compile_info)
         if test_constant == optimize.CONST_FALSE:
             self.visit_sequence(wh.orelse)
         else:
@@ -1207,7 +1208,7 @@
         count = len(elts) if elts is not None else 0
         consts_w = [None] * count
         for i in range(count):
-            w_value = elts[i].as_constant()
+            w_value = elts[i].as_constant(self.space, self.compile_info)
             if w_value is None:
                 # Not all constants
                 return None
@@ -1342,11 +1343,16 @@
             if len(d.keys) < 0xffff:
                 all_constant_keys_w = []
                 for key in d.keys:
-                    if key is None or key.as_constant() is None:
+                    if key is None:
+                        constant_key = None
+                    else:
+                        constant_key = key.as_constant(
+                            self.space, self.compile_info)
+                    if constant_key is None:
                         all_constant_keys_w = None
                         break
                     else:
-                        all_constant_keys_w.append(key.as_constant())
+                        all_constant_keys_w.append(constant_key)
             for i in range(len(d.values)):
                 key = d.keys[i]
                 is_unpacking = key is None
diff --git a/pypy/interpreter/astcompiler/optimize.py b/pypy/interpreter/astcompiler/optimize.py
--- a/pypy/interpreter/astcompiler/optimize.py
+++ b/pypy/interpreter/astcompiler/optimize.py
@@ -20,14 +20,14 @@
 
 class __extend__(ast.AST):
 
-    def as_constant_truth(self, space):
+    def as_constant_truth(self, space, compile_info):
         """Return the truth of this node if known."""
-        const = self.as_constant()
+        const = self.as_constant(space, compile_info)
         if const is None:
             return CONST_NOT_CONST
         return int(space.is_true(const))
 
-    def as_constant(self):
+    def as_constant(self, space, compile_info):
         """Return the value of this node as a wrapped constant if possible."""
         return None
 
@@ -47,46 +47,46 @@
 
 class __extend__(ast.Num):
 
-    def as_constant(self):
+    def as_constant(self, space, compile_info):
         return self.n
 
 
 class __extend__(ast.Str):
 
-    def as_constant(self):
+    def as_constant(self, space, compile_info):
         return self.s
 
 
 class __extend__(ast.Bytes):
 
-    def as_constant(self):
+    def as_constant(self, space, compile_info):
         return self.s
 
 
 class __extend__(ast.Ellipsis):
 
-    def as_constant_truth(self, space):
+    def as_constant_truth(self, space, compile_info):
         return True
 
 
 class __extend__(ast.Constant):
 
-    def as_constant(self):
+    def as_constant(self, space, compile_info):
         return self.value
 
 class __extend__(ast.NameConstant):
 
-    def as_constant(self):
+    def as_constant(self, space, compile_info):
         return self.value
 
 class __extend__(ast.Index):
-    def as_constant(self):
-        return self.value.as_constant()
+    def as_constant(self, space, compile_info):
+        return self.value.as_constant(space, compile_info)
 
 class __extend__(ast.Slice):
-    def as_constant(self):
+    def as_constant(self, space, compile_info):
         # XXX: this ought to return a slice object if all the indices are
-        # constants, but we don't have a space here.
+        # constants
         return None
 
 class __extend__(ast.UnaryOp):
@@ -189,9 +189,9 @@
         return node
 
     def visit_BinOp(self, binop):
-        left = binop.left.as_constant()
+        left = binop.left.as_constant(self.space, self.compile_info)
         if left is not None:
-            right = binop.right.as_constant()
+            right = binop.right.as_constant(self.space, self.compile_info)
             if right is not None:
                 op = binop.op
                 try:
@@ -218,7 +218,7 @@
         return binop
 
     def visit_UnaryOp(self, unary):
-        w_operand = unary.operand.as_constant()
+        w_operand = unary.operand.as_constant(self.space, self.compile_info)
         op = unary.op
         if w_operand is not None:
             try:
@@ -254,7 +254,7 @@
         we_are_and = bop.op == ast.And
         i = 0
         while i < len(values) - 1:
-            truth = values[i].as_constant_truth(self.space)
+            truth = values[i].as_constant_truth(self.space, self.compile_info)
             if truth != CONST_NOT_CONST:
                 if (truth != CONST_TRUE) == we_are_and:
                     del values[i + 1:]
@@ -268,7 +268,7 @@
         return bop
 
     def visit_Repr(self, rep):
-        w_const = rep.value.as_constant()
+        w_const = rep.value.as_constant(self.space, self.compile_info)
         if w_const is not None:
             w_repr = self.space.repr(w_const)
             return ast.Constant(w_repr, rep.lineno, rep.col_offset)
@@ -300,7 +300,7 @@
             consts_w = [None]*len(tup.elts)
             for i in range(len(tup.elts)):
                 node = tup.elts[i]
-                w_const = node.as_constant()
+                w_const = node.as_constant(self.space, self.compile_info)
                 if w_const is None:
                     new_elts = self._optimize_constant_star_unpacks(tup.elts)
                     if new_elts is not None:
@@ -350,7 +350,7 @@
                 after_last_star_index = i + 1
                 new_elts.append(elt)
             elif const_since_last_star_w is not None:
-                w_const = elt.as_constant()
+                w_const = elt.as_constant(self.space, self.compile_info)
                 if w_const is None:
                     new_elts.extend(elts[after_last_star_index:i + 1])
                     const_since_last_star_w = None
@@ -375,9 +375,9 @@
 
     def visit_Subscript(self, subs):
         if subs.ctx == ast.Load:
-            w_obj = subs.value.as_constant()
+            w_obj = subs.value.as_constant(self.space, self.compile_info)
             if w_obj is not None:
-                w_idx = subs.slice.as_constant()
+                w_idx = subs.slice.as_constant(self.space, self.compile_info)
                 if w_idx is not None:
                     try:
                         w_const = self.space.getitem(w_obj, w_idx)


More information about the pypy-commit mailing list