def codetest(self, source, functionname, args): """Compile and run the given code string, and then call its function named by 'functionname' with arguments 'args'.""" space = self.space source = str(py.code.Source(source).strip()) + '\n' w = space.wrap w_code = space.builtin.call('compile', w(source), w('<string>'), w('exec'), w(0), w(0)) tempmodule = module.Module(space, w("__temp__")) w_glob = tempmodule.w_dict space.setitem(w_glob, w("__builtins__"), space.builtin) code = space.unwrap(w_code) code.exec_code(space, w_glob, w_glob) wrappedargs = [w(a) for a in args] wrappedfunc = space.getitem(w_glob, w(functionname)) try: w_output = space.call_function(wrappedfunc, *wrappedargs) except error.OperationError, e: #e.print_detailed_traceback(space) return '<<<%s>>>' % e.errorstr(space)
def ensure__main__(space): w_main = space.newtext('__main__') w_modules = space.sys.get('modules') try: return space.getitem(w_modules, w_main) except OperationError as e: if not e.match(space, space.w_KeyError): raise mainmodule = module.Module(space, w_main) space.setitem(w_modules, w_main, mainmodule) return mainmodule
def pick_builtin(self, w_globals): "Look up the builtin module to use from the __builtins__ global" # pick the __builtins__ roughly in the same way CPython does it # this is obscure and slow space = self.space try: w_builtin = space.getitem(w_globals, space.newtext('__builtins__')) except OperationError as e: if not e.match(space, space.w_KeyError): raise else: if w_builtin is space.builtin: # common case return space.builtin if space.isinstance_w(w_builtin, space.w_dict): return module.Module(space, None, w_builtin) if isinstance(w_builtin, module.Module): return w_builtin # no builtin! make a default one. Give them None, at least. builtin = module.Module(space, None) space.setitem(builtin.w_dict, space.newtext('None'), space.w_None) return builtin
import sys from pypy.interpreter import eval, module from pypy.interpreter.error import OperationError def ensure__main__(space): w_main = space.wrap('__main__') w_modules = space.sys.get('modules') try: return space.getitem(w_modules, w_main) except OperationError, e: if not e.match(space, space.w_KeyError): raise mainmodule = module.Module(space, w_main) space.setitem(w_modules, w_main, mainmodule) return mainmodule def compilecode(space, source, filename, cmd='exec'): w = space.wrap w_code = space.builtin.call( 'compile', space.wrapbytes(source), space.wrap_fsdecoded(filename), w(cmd), w(0), w(0)) pycode = space.interp_w(eval.Code, w_code) return pycode def _run_eval_string(source, filename, space, eval): if eval: cmd = 'eval' else:
def pick_builtin(self, w_globals): "Look up the builtin module to use from the __builtins__ global" # pick the __builtins__ roughly in the same way CPython does it # this is obscure and slow space = self.space try: w_builtin = space.getitem(w_globals, space.wrap('__builtins__')) except OperationError, e: if not e.match(space, space.w_KeyError): raise else: if w_builtin is space.builtin: # common case return space.builtin if space.is_true(space.isinstance(w_builtin, space.w_dict)): return module.Module(space, None, w_builtin) builtin = space.interpclass_w(w_builtin) if isinstance(builtin, module.Module): return builtin # no builtin! make a default one. Given them None, at least. builtin = module.Module(space, None) space.setitem(builtin.w_dict, space.wrap('None'), space.w_None) return builtin def setup_after_space_initialization(self): """NOT_RPYTHON""" space = self.space self.builtins_by_index = [None] * len(OPTIMIZED_BUILTINS) for i, name in enumerate(OPTIMIZED_BUILTINS): self.builtins_by_index[i] = space.getattr(self, space.wrap(name)) # install the more general version of isinstance() & co. in the space