def prepare(self): SLBlock.prepare(self) # Prepare the positional arguments. exprs = [ ] values = [ ] has_exprs = False has_values = False for a in self.positional: node = py_compile(a, 'eval', ast_node=True) if is_constant(node): values.append(py_eval_bytecode(compile_expr(node))) exprs.append(ast.Num(n=0)) has_values = True else: values.append(use_expression) exprs.append(node) has_exprs = True if has_values: self.positional_values = values else: self.positional_values = None if has_exprs: t = ast.Tuple(elts=exprs, ctx=ast.Load()) ast.copy_location(t, exprs[0]) self.positional_exprs = compile_expr(t) else: self.positional_exprs = None
def prepare(self): for i in self.children: i.prepare() # Compile the keywords. keyword_values = { } keyword_keys = [ ] keyword_exprs = [ ] for k, expr in self.keyword: node = py_compile(expr, 'eval', ast_node=True) if is_constant(node): keyword_values[k] = py_eval_bytecode(compile_expr(node)) else: keyword_keys.append(ast.Str(s=k)) keyword_exprs.append(node) if keyword_values: self.keyword_values = keyword_values else: self.keyword_values = None if keyword_keys: node = ast.Dict(keys=keyword_keys, values=keyword_exprs) ast.copy_location(node, keyword_exprs[0]) self.keyword_exprs = compile_expr(node) else: self.keyword_exprs = None
def ast_eval_literal(self, expr): """ Compiles an expression into an AST. """ if isinstance(expr, renpy.ast.PyExpr): filename = expr.filename linenumber = expr.linenumber else: filename = None linenumber = None key = (expr, filename, linenumber) rv = self.ast_eval_cache.get(key, None) if rv is None: expr = py_compile(expr, 'eval', ast_node=True) try: ast.literal_eval(expr) literal = True except: literal = False rv = (expr, literal) self.ast_eval_cache[key] = rv self.updated = True return rv
def prepare(self): SLBlock.prepare(self) # Prepare the positional arguments. exprs = [] values = [] has_exprs = False has_values = False for a in self.positional: node = py_compile(a, 'eval', ast_node=True) if is_constant(node): values.append(py_eval_bytecode(compile_expr(node))) exprs.append(ast.Num(n=0)) has_values = True else: values.append(use_expression) exprs.append(node) has_exprs = True if has_values: self.positional_values = values else: self.positional_values = None if has_exprs: t = ast.Tuple(elts=exprs, ctx=ast.Load()) ast.copy_location(t, exprs[0]) self.positional_exprs = compile_expr(t) else: self.positional_exprs = None
def prepare(self): for i in self.children: i.prepare() # Compile the keywords. keyword_values = {} keyword_keys = [] keyword_exprs = [] for k, expr in self.keyword: node = py_compile(expr, 'eval', ast_node=True) if is_constant(node): keyword_values[k] = py_eval_bytecode(compile_expr(node)) else: keyword_keys.append(ast.Str(s=k)) keyword_exprs.append(node) if keyword_values: self.keyword_values = keyword_values else: self.keyword_values = None if keyword_keys: node = ast.Dict(keys=keyword_keys, values=keyword_exprs) ast.copy_location(node, keyword_exprs[0]) self.keyword_exprs = compile_expr(node) else: self.keyword_exprs = None
def is_constant_expr(self, expr): """ Compiles `expr` into an AST node, then returns the result of self.is_constant called on that node. """ node = py_compile(expr, 'eval', ast_node=True) return self.is_constant(node)
def prepare(self): node = py_compile(self.expression, 'eval', ast_node=True) if is_constant(node): self.expression_value = py_eval_bytecode(compile_expr(node)) self.expression_expr = None else: self.expression_value = None self.expression_expr = compile_expr(node) SLBlock.prepare(self)
def python(self, code): """ Performs analysis on a block of python code. """ nodes = py_compile(code, 'exec', ast_node=True) a = PyAnalysis(self) for i in nodes: a.visit(i)
def prepare(self, analysis): node = py_compile(self.expression, 'eval', ast_node=True) if analysis.is_constant(node): self.expression_value = py_eval_bytecode(compile_expr(node)) self.expression_expr = None else: self.expression_value = None self.expression_expr = compile_expr(node) self.constant = False SLBlock.prepare(self, analysis)
def prepare(self, analysis): keywords = dict(self.keyword) event_node = py_compile(self.event, 'eval', ast_node=True) action_node = py_compile(keywords.get('action', None), 'eval', ast_node=True) self.event_expr = compile_expr(event_node) self.action_expr = compile_expr(action_node) if analysis.is_constant(event_node): self.event_value = py_eval_bytecode(self.event_expr) else: self.event_value = None if analysis.is_constant(action_node): self.action_value = py_eval_bytecode(self.action_expr) else: self.action_value = None self.constant = False
def prepare(self): # A list of prepared entries, with each consisting of expression # bytecode and a SLBlock. self.prepared_entries = [] for cond, block in self.entries: if cond is not None: cond = py_compile(cond, 'eval') block.prepare() self.prepared_entries.append((cond, block))
def prepare(self, analysis): keywords = dict(self.keyword) event_node = py_compile(self.event, 'eval', ast_node=True) action_node = py_compile(keywords.get('action', None), 'eval', ast_node=True) self.event_expr = compile_expr(event_node) self.action_expr = compile_expr(action_node) if analysis.is_constant(event_node): self.event_value = py_eval_bytecode(self.event_expr) else: self.event_value = None if analysis.is_constant(action_node): self.action_value = py_eval_bytecode(self.action_expr) else: self.action_value = None self.constant = NOT_CONST self.last_keyword = True
def prepare(self): # A list of prepared entries, with each consisting of expression # bytecode and a SLBlock. self.prepared_entries = [ ] for cond, block in self.entries: if cond is not None: cond = py_compile(cond, 'eval') block.prepare() self.prepared_entries.append((cond, block))
def prepare(self, analysis): SLBlock.prepare(self, analysis) # Prepare the positional arguments. exprs = [ ] values = [ ] has_exprs = False has_values = False for a in self.positional: node = py_compile(a, 'eval', ast_node=True) const = analysis.is_constant(node) if const == GLOBAL_CONST: values.append(py_eval_bytecode(compile_expr(node))) exprs.append(ast.Num(n=0)) has_values = True else: values.append(use_expression) exprs.append(node) # Will be compiled as part of the tuple. has_exprs = True self.constant = min(self.constant, const) if has_values: self.positional_values = values else: self.positional_values = None if has_exprs: t = ast.Tuple(elts=exprs, ctx=ast.Load()) ast.copy_location(t, exprs[0]) self.positional_exprs = compile_expr(t) else: self.positional_exprs = None # We do not pass keywords to our parents. self.has_keyword = False # We want to preserve last_keyword, however, in case we run a # python block. # If we have the id property, we're not constant - since we may get # additional keywords via id. (It's unlikely, but id should be pretty # rare.) for k, _expr in self.keyword: if k == "id": self.constant = NOT_CONST
def prepare(self, analysis): SLBlock.prepare(self, analysis) # Prepare the positional arguments. exprs = [] values = [] has_exprs = False has_values = False for a in self.positional: node = py_compile(a, 'eval', ast_node=True) const = analysis.is_constant(node) if const == GLOBAL_CONST: values.append(py_eval_bytecode(compile_expr(node))) exprs.append(ast.Num(n=0)) has_values = True else: values.append(use_expression) exprs.append(node) # Will be compiled as part of the tuple. has_exprs = True self.constant = min(self.constant, const) if has_values: self.positional_values = values else: self.positional_values = None if has_exprs: t = ast.Tuple(elts=exprs, ctx=ast.Load()) ast.copy_location(t, exprs[0]) self.positional_exprs = compile_expr(t) else: self.positional_exprs = None # We do not pass keywords to our parents. self.has_keyword = False # We want to preserve last_keyword, however, in case we run a # python block. # If we have the id property, we're not constant - since we may get # additional keywords via id. (It's unlikely, but id should be pretty # rare.) for k, _expr in self.keyword: if k == "id": self.constant = NOT_CONST
def prepare(self, analysis): for i in self.children: i.prepare(analysis) self.constant = min(self.constant, i.constant) # Compile the keywords. keyword_values = {} keyword_keys = [] keyword_exprs = [] for k, expr in self.keyword: node = py_compile(expr, 'eval', ast_node=True) const = analysis.is_constant(node) if const == GLOBAL_CONST: keyword_values[k] = py_eval_bytecode(compile_expr(node)) else: keyword_keys.append(ast.Str(s=k)) keyword_exprs.append( node) # Will be compiled as part of ast.Dict below. self.constant = min(self.constant, const) if keyword_values: self.keyword_values = keyword_values else: self.keyword_values = None if keyword_keys: node = ast.Dict(keys=keyword_keys, values=keyword_exprs) ast.copy_location(node, keyword_exprs[0]) self.keyword_exprs = compile_expr(node) else: self.keyword_exprs = None self.has_keyword = bool(self.keyword) self.keyword_children = [] for i in self.children: if i.has_keyword: self.keyword_children.append(i) self.has_keyword = True if i.last_keyword: self.last_keyword = True break
def prepare(self, analysis): for i in self.children: i.prepare(analysis) self.constant = min(self.constant, i.constant) # Compile the keywords. keyword_values = { } keyword_keys = [ ] keyword_exprs = [ ] for k, expr in self.keyword: node = py_compile(expr, 'eval', ast_node=True) const = analysis.is_constant(node) if const == GLOBAL_CONST: keyword_values[k] = py_eval_bytecode(compile_expr(node)) else: keyword_keys.append(ast.Str(s=k)) keyword_exprs.append(node) # Will be compiled as part of ast.Dict below. self.constant = min(self.constant, const) if keyword_values: self.keyword_values = keyword_values else: self.keyword_values = None if keyword_keys: node = ast.Dict(keys=keyword_keys, values=keyword_exprs) ast.copy_location(node, keyword_exprs[0]) self.keyword_exprs = compile_expr(node) else: self.keyword_exprs = None self.has_keyword = bool(self.keyword) self.keyword_children = [ ] for i in self.children: if i.has_keyword: self.keyword_children.append(i) self.has_keyword = True if i.last_keyword: self.last_keyword = True break
def prepare(self, analysis): node = py_compile(self.expression, 'eval', ast_node=True) const = analysis.is_constant(node) if const == GLOBAL_CONST: self.expression_value = py_eval_bytecode(compile_expr(node)) self.expression_expr = None else: self.expression_value = None self.expression_expr = compile_expr(node) self.constant = min(self.constant, const) SLBlock.prepare(self, analysis) self.last_keyword = True
def ast_exec(self, code): """ Compiles a block into an AST. """ if isinstance(code, renpy.ast.PyExpr): key = (code, code.filename, code.linenumber) else: key = (code, None, None) rv = self.ast_exec_cache.get(key, None) if rv is None: rv = py_compile(code, 'exec', ast_node=True) self.ast_exec_cache[key] = rv self.updated = True return rv
def prepare(self, analysis): # A list of prepared entries, with each consisting of expression # bytecode and a SLBlock. self.prepared_entries = [] for cond, block in self.entries: if cond is not None: node = py_compile(cond, 'eval', ast_node=True) self.constant = min(self.constant, analysis.is_constant(node)) cond = compile_expr(node) block.prepare(analysis) self.constant = min(self.constant, block.constant) self.prepared_entries.append((cond, block)) self.last_keyword = True
def prepare(self, analysis): # A list of prepared entries, with each consisting of expression # bytecode and a SLBlock. self.prepared_entries = [ ] for cond, block in self.entries: if cond is not None: node = py_compile(cond, 'eval', ast_node=True) self.constant = min(self.constant, analysis.is_constant(node)) cond = compile_expr(node) block.prepare(analysis) self.constant = min(self.constant, block.constant) self.prepared_entries.append((cond, block)) self.last_keyword = True
def prepare(self, analysis): SLBlock.prepare(self, analysis) # Prepare the positional arguments. exprs = [ ] values = [ ] has_exprs = False has_values = False for a in self.positional: node = py_compile(a, 'eval', ast_node=True) const = analysis.is_constant(node) if const == GLOBAL_CONST: values.append(py_eval_bytecode(compile_expr(node))) exprs.append(ast.Num(n=0)) has_values = True else: values.append(use_expression) exprs.append(node) # Will be compiled as part of the tuple. has_exprs = True self.constant = min(self.constant, const) if has_values: self.positional_values = values else: self.positional_values = None if has_exprs: t = ast.Tuple(elts=exprs, ctx=ast.Load()) ast.copy_location(t, exprs[0]) self.positional_exprs = compile_expr(t) else: self.positional_exprs = None # We do not pass keywords to our parents. self.has_keyword = False
def prepare(self): # A list of prepared entries, with each consisting of expression # bytecode and a SLBlock. self.prepared_entries = [ ] for cond, block in self.entries: if cond is not None: node = py_compile(cond, 'eval', ast_node=True) if not is_constant(node): self.constant = False cond = compile_expr(node) block.prepare() if not block.constant: self.constant = False self.prepared_entries.append((cond, block))
def prepare(self, analysis): for i in self.children: i.prepare(analysis) if not i.constant: self.constant = False # Compile the keywords. keyword_values = { } keyword_keys = [ ] keyword_exprs = [ ] for k, expr in self.keyword: node = py_compile(expr, 'eval', ast_node=True) if analysis.is_constant(node): keyword_values[k] = py_eval_bytecode(compile_expr(node)) else: keyword_keys.append(ast.Str(s=k)) keyword_exprs.append(node) # Will be compiled as part of ast.Dict below. if keyword_values: self.keyword_values = keyword_values else: self.keyword_values = None if keyword_keys: node = ast.Dict(keys=keyword_keys, values=keyword_exprs) ast.copy_location(node, keyword_exprs[0]) self.keyword_exprs = compile_expr(node) self.constant = False else: self.keyword_exprs = None
def prepare(self): self.expr = py_compile(self.expression, 'eval')
def prepare(self, analysis): self.expr = py_compile(self.expression, 'eval') self.constant = NOT_CONST self.last_keyword = True
def prepare(self, analysis): self.expr = py_compile(self.expression, 'eval') self.constant = False