def visit_Tuple(self, tup): """Try to turn tuple building into a constant.""" if tup.ctx != ast.Load: return tup # Don't do the rest for assignment or delete targets. # It would replace Tuple([]) with Constant('()')! if tup.elts: consts_w = [None] * len(tup.elts) for i in range(len(tup.elts)): node = tup.elts[i] 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: return ast.Tuple(new_elts, ast.Load, tup.lineno, tup.col_offset) return tup consts_w[i] = w_const # intern the string constants packed into the tuple here, # because assemble.py will see the result as just a tuple constant for i in range(len(consts_w)): consts_w[i] = misc.intern_if_common_string( self.space, consts_w[i]) else: consts_w = [] w_consts = self.space.newtuple(consts_w) return ast.Constant(w_consts, tup.lineno, tup.col_offset)
def visit_Tuple(self, tup): """Try to turn tuple building into a constant.""" if tup.elts: consts_w = [None]*len(tup.elts) for i in range(len(tup.elts)): node = tup.elts[i] w_const = node.as_constant() if w_const is None: return tup consts_w[i] = w_const # intern the string constants packed into the tuple here, # because assemble.py will see the result as just a tuple constant for i in range(len(consts_w)): consts_w[i] = misc.intern_if_common_string( self.space, consts_w[i]) else: consts_w = [] w_consts = self.space.newtuple(consts_w) return ast.Const(w_consts, tup.lineno, tup.col_offset)
def _build_consts_array(self): """Turn the applevel constants dictionary into a list.""" w_consts = self.w_consts space = self.space consts_w = [space.w_None] * space.len_w(w_consts) w_iter = space.iter(w_consts) first = space.wrap(0) while True: try: w_key = space.next(w_iter) except OperationError as e: if not e.match(space, space.w_StopIteration): raise break w_index = space.getitem(w_consts, w_key) w_constant = space.getitem(w_key, first) w_constant = misc.intern_if_common_string(space, w_constant) consts_w[space.int_w(w_index)] = w_constant return consts_w
def add_const(self, w_obj): """Add a W_Root to the constant array and return its location.""" space = self.space if isinstance(w_obj, PyCode): # unlike CPython, never share code objects, it's pointless w_key = space.id(w_obj) else: w_key = PyCode.const_comparison_key(self.space, w_obj) w_len = space.finditem(self.w_consts, w_key) if w_len is not None: length = space.int_w(w_len) else: length = len(self.consts_w) w_obj = misc.intern_if_common_string(space, w_obj) self.consts_w.append(w_obj) space.setitem(self.w_consts, w_key, space.newint(length)) if length == 0: self.scope.doc_removable = False return length