def __execute_code(self, compiled_code, namespace): _ast, expr = compiled_code # Two-step eval: eval() the body of the exec call eval(ast_compile(_ast, "<eval_body>", "exec"), namespace) # Then eval the expression context and return that return eval(ast_compile(expr, "<eval>", "eval"), namespace)
def runsource(self, source, filename='<input>', symbol='single'): global SIMPLE_TRACEBACKS try: tokens = tokenize(source) except PrematureEndOfInput: return True except LexException as e: if e.source is None: e.source = source e.filename = filename sys.stderr.write(str(e)) return False try: _ast = hy_compile(tokens, "__console__", root=ast.Interactive) if self.spy: print_python_code(_ast) code = ast_compile(_ast, filename, symbol) except HyTypeError as e: if e.source is None: e.source = source e.filename = filename if SIMPLE_TRACEBACKS: sys.stderr.write(str(e)) else: self.showtraceback() return False except Exception: self.showtraceback() return False self.runcode(code) return False
def runsource(self, source, filename="<input>", symbol="single"): global _machine try: _machine.process(source + "\n") except LexException: _machine = Machine(Idle, 1, 0) self.showsyntaxerror(filename) return False if type(_machine.state) != Idle: _machine = Machine(Idle, 1, 0) return True try: tokens = process(_machine.nodes, "__console__") except Exception: _machine = Machine(Idle, 1, 0) self.showtraceback() return False _machine = Machine(Idle, 1, 0) try: _ast = hy_compile(tokens, "__console__", root=ast.Interactive) code = ast_compile(_ast, filename, symbol) except Exception: self.showtraceback() return False self.runcode(code) return False
def runsource(self, source, filename="<input>", symbol="single"): global SIMPLE_TRACEBACKS try: tokens = tokenize(source) except PrematureEndOfInput: return True except LexException as e: if e.source is None: e.source = source e.filename = filename sys.stderr.write(str(e)) return False try: _ast = hy_compile(tokens, "__console__", root=ast.Interactive) if self.spy: print_python_code(_ast) code = ast_compile(_ast, filename, symbol) except HyTypeError as e: if e.source is None: e.source = source e.filename = filename if SIMPLE_TRACEBACKS: sys.stderr.write(str(e)) else: self.showtraceback() return False except Exception: self.showtraceback() return False self.runcode(code) return False
def runsource(self, source, filename='<input>', symbol='single'): global _machine try: _machine.process(source + "\n") except LexException: _machine = Machine(Idle, 1, 0) self.showsyntaxerror(filename) return False if type(_machine.state) != Idle: _machine = Machine(Idle, 1, 0) return True try: tokens = process(_machine.nodes, "__console__") except Exception: _machine = Machine(Idle, 1, 0) self.showtraceback() return False _machine = Machine(Idle, 1, 0) try: _ast = hy_compile(tokens, "__console__", root=ast.Interactive) code = ast_compile(_ast, filename, symbol) except Exception: self.showtraceback() return False self.runcode(code) return False
def import_file_to_module(module_name, fpath, lang): """Import content from fpath and put it into a Python module. If module_name already exists, the preexisting module is updated instead of a new module being created. If a new module is created, it is installed in sys.modules. Returns the module.""" preexisting = module_name in sys.modules try: if lang == "hy": _ast = import_file_to_ast(fpath, module_name) mod = (sys.modules[module_name] if preexisting else imp.new_module(module_name)) mod.__file__ = fpath if lang == "hy": eval(ast_compile(_ast, fpath, "exec"), mod.__dict__) elif lang == "python": exec(compile(open(fpath).read(), fpath, 'exec'), mod.__dict__) else: raise ValueError("Unknown language: {}".format(lang)) sys.modules[module_name] = mod except (HyTypeError, LexException) as e: if e.source is None: with open(fpath, 'rt') as fp: e.source = fp.read() e.filename = fpath raise except Exception: if not preexisting: sys.modules.pop(module_name, None) raise return mod
def runsource(self, source, filename='<input>', symbol='single'): try: tokens = tokenize(source) except PrematureEndOfInput: return True except LexException: self.showsyntaxerror(filename) return False try: _ast = hy_compile(tokens, "__console__", root=ast.Interactive) code = ast_compile(_ast, filename, symbol) except Exception: self.showtraceback() return False self.runcode(code) return False
def load_recipe(root: Path) -> None: """Import content from fpath and puts it into a Python module. Returns the module.""" module_name = 'dotops.recipes.' + root.name fpath = find_recipe(root) try: _ast = import_file_to_ast(fpath, module_name) mod = imp.new_module(module_name) mod.__file__ = fpath eval(ast_compile(_ast, fpath, "exec"), _globals(root, mod)) except (HyTypeError, LexException) as e: if e.source is None: with open(fpath, 'rt') as fp: e.source = fp.read() e.filename = fpath raise except Exception: sys.modules.pop(module_name, None) raise
def repl_import(filename, code, globals_d, context_name, lang = "hy"): """'code' should be a Hy sexp or None. If it's None, the file 'filename' is opened to get the code to evaluate, and None is returned. If it's a Hy sexp, it's evaluated and the result is returned. 'lang' can be "hy" or "python" when 'code' is None, but must be 'hy' when 'code' is not None. We use different semantics from import *. In particular, __all__ is ignored. The goal of this somewhat involved method (including the module daylight-hy.load) is to ensure that: - (def x 15) (defn foo [] x) followed by (foo) at the command line works - After loading two files, you can access variables defined in file 1 that are shadowed by variables defined in file 2.""" mname = 'daylight-' + os.path.dirname(os.path.abspath(filename)) m = None try: if code is None: preexisting = mname in sys.modules try: with open(filename) as o: text = o.read() if lang == "hy": model = hy_parse(text) m = (sys.modules[mname] if preexisting else imp.new_module(mname)) m.__file__ = filename if lang == "hy": _ast = hy_compile(model, mname) eval(ast_compile(_ast, filename, "exec"), m.__dict__) elif lang == "python": exec(compile(text, filename, 'exec'), m.__dict__) else: raise ValueError("Unknown language: {}".format(lang)) sys.modules[mname] = m except (HyTypeError, LexException) as e: if e.source is None: with open(filename, 'rt') as fp: e.source = fp.read() e.filename = filename raise except Exception: if not preexisting: sys.modules.pop(mname, None) raise returnval = None else: if lang != 'hy': raise NotImplementedError m = sys.modules.get(mname) if not m: m = imp.new_module(mname) sys.modules[mname] = m returnval = hy_eval(code, m.__dict__, mname) finally: if m is not None: # Copy the module's names (except for names starting with "_") # into our globals. for k in dir(m): if not k.startswith('_'): globals_d[k] = getattr(m, k) # Import macros. hy.macros.require(mname, context_name, "ALL") return returnval