def try_import_mod(space, w_modulename, filepart, w_parent, w_name, w_import_nametoken, pkgdir=None): # decide what type we want (pyc/py) modtype = find_modtype(space, filepart) if modtype == NOFILE: return None w = space.wrap w_mod = w(Module(space, w_modulename)) # tag module with import_token if not w_import_nametoken is None: space.namespace_table[id(w_mod)] = w_import_nametoken #else: TODO: add objects with (w_import_nametoken == None) to a separate table try: if modtype == PYFILE: filename = filepart + ".py" stream = streamio.open_file_as_stream(filename, "rU") else: assert modtype == PYCFILE filename = filepart + ".pyc" stream = streamio.open_file_as_stream(filename, "rb") try: _prepare_module(space, w_mod, filename, pkgdir) try: if modtype == PYFILE: load_source_module(space, w_modulename, w_mod, filename, stream.readall(), w_import_nametoken) else: magic = _r_long(stream) timestamp = _r_long(stream) load_compiled_module(space, w_modulename, w_mod, filename, magic, timestamp, stream.readall(), w_import_nametoken) except OperationError, e: w_mods = space.sys.get('modules') space.call_method(w_mods,'pop', w_modulename, space.w_None) raise finally: stream.close() except StreamErrors: return None w_mod = check_sys_modules(space, w_modulename) if w_mod is not None and w_parent is not None: space.setattr(w_parent, w_name, w_mod) return w_mod
def test_write_compiled_module(self): space = self.space pathname = _testfilesource() os.chmod(pathname, 0777) stream = streamio.open_file_as_stream(pathname, "r") try: w_ret = importing.parse_source_module(space, pathname, stream.readall()) finally: stream.close() pycode = space.interpclass_w(w_ret) assert type(pycode) is pypy.interpreter.pycode.PyCode cpathname = str(udir.join('cpathname.pyc')) mode = 0777 mtime = 12345 importing.write_compiled_module(space, pycode, cpathname, mode, mtime) # check ret = importing.check_compiled_module(space, cpathname, mtime) assert ret is not None ret.close() # Check that the executable bit was removed assert os.stat(cpathname).st_mode & 0111 == 0 # read compiled module stream = streamio.open_file_as_stream(cpathname, "rb") try: stream.seek(8, 0) w_code = importing.read_compiled_module(space, cpathname, stream.readall()) pycode = space.interpclass_w(w_code) finally: stream.close() # check value of load w_dic = space.newdict() pycode.exec_code(space, w_dic, w_dic) w_ret = space.getitem(w_dic, space.wrap('x')) ret = space.int_w(w_ret) assert ret == 42
def load(self, cartridge_path): cartridge_path = str(cartridge_path) self.cartridge_file_path = cartridge_path self.cartridge_stream = open_file_as_stream(cartridge_path) self.cartridge_file_contents = map_to_byte( self.cartridge_stream.readall()) self.load_battery(cartridge_path)
def run_file(filename): fp = open_file_as_stream(filename) source = fp.readall() toplevel_env = get_report_env() try: exprs_w = parse_string(source) except BacktrackException as e: print e.error.nice_error_message('<file %s>' % filename, source) return 1 try: nodelist = Builder(exprs_w).getast() except OperationError as e: print e.unwrap().to_string() return 1 cpsform = Rewriter(nodelist, toplevel=True).run() try: w_maincont, proto_w = compile_all(cpsform, toplevel_env) except OperationError as e: print e.unwrap().to_string() return 1 #print 'ast:', map(lambda o: o.to_string(), nodelist) #print cpsform.to_string() #print w_maincont, 'dis:' #print dis_proto(w_maincont.w_proto) #for w_proto in proto_w: # print w_proto, 'dis:' # print dis_proto(w_proto) #return 0 frame = Frame(w_maincont, proto_w) frame.run() return 0
def test_load_compiled_module(self): space = self.space pathname = "whatever" mtime = 12345 co = compile('x = 42', '?', 'exec') cpathname = _testfile(importing.get_pyc_magic(space), mtime, co) w_modulename = space.wrap('somemodule') stream = streamio.open_file_as_stream(cpathname, "r") try: w_mod = space.wrap(Module(space, w_modulename)) magic = importing._r_long(stream) timestamp = importing._r_long(stream) w_ret = importing.load_compiled_module(space, w_modulename, w_mod, cpathname, magic, timestamp, stream.readall()) finally: stream.close() assert w_mod is w_ret w_ret = space.getattr(w_mod, space.wrap('x')) ret = space.int_w(w_ret) assert ret == 42
def direct___init__(self, w_name, mode='r', buffering=-1): name = self.space.str_w(w_name) self.direct_close() self.check_mode_ok(mode) stream = streamio.open_file_as_stream(name, mode, buffering) fd = stream.try_to_find_file_descriptor() self.fdopenstream(stream, fd, mode, w_name)
def entry_point(argv): if len(argv) == 2: code = open_file_as_stream(argv[1]).readall() try: t = parse(code) except BacktrackException: #(line, col) = e.error.get_line_column(code) #expected = " ".join(e.error.expected) print "parse error" return 1 #this should not be necessary here assert isinstance(t, list) ctx = ExecutionContext() try: for sexpr in t: try: w_retval = sexpr.eval(ctx) print w_retval.to_string() except ContinuationReturn, e: print e.result.to_string() except SchemeQuit, e: return 0 return 0
def check_compiled_module(space, pathname, mtime, cpathname): """ Given a pathname for a Python source file, its time of last modification, and a pathname for a compiled file, check whether the compiled file represents the same version of the source. If so, return a FILE pointer for the compiled file, positioned just after the header; if not, return NULL. Doesn't set an exception. """ w_marshal = space.getbuiltinmodule('marshal') stream = streamio.open_file_as_stream(cpathname, "rb") magic = _r_long(stream) try: if magic != get_pyc_magic(space): # XXX what to do about Py_VerboseFlag ? # PySys_WriteStderr("# %s has bad magic\n", cpathname); return -1 pyc_mtime = _r_long(stream) if pyc_mtime != mtime: # PySys_WriteStderr("# %s has bad mtime\n", cpathname); return 0 # if (Py_VerboseFlag) # PySys_WriteStderr("# %s matches %s\n", cpathname, pathname); finally: stream.close() return 1
def test_pyc_magic_changes(self): # test that the pyc files produced by a space are not reimportable # from another, if they differ in what opcodes they support allspaces = [self.space] for opcodename in self.space.config.objspace.opcodes.getpaths(): key = 'objspace.opcodes.' + opcodename space2 = gettestobjspace(**{key: True}) allspaces.append(space2) for space1 in allspaces: for space2 in allspaces: if space1 is space2: continue pathname = "whatever" mtime = 12345 co = compile('x = 42', '?', 'exec') cpathname = _testfile(importing.get_pyc_magic(space1), mtime, co) w_modulename = space2.wrap('somemodule') stream = streamio.open_file_as_stream(cpathname, "rb") try: w_mod = space2.wrap(Module(space2, w_modulename)) magic = importing._r_long(stream) timestamp = importing._r_long(stream) space2.raises_w(space2.w_ImportError, importing.load_compiled_module, space2, w_modulename, w_mod, cpathname, magic, timestamp, stream.readall()) finally: stream.close()
def load_bytecode(filename): from pypy.rlib.streamio import open_file_as_stream f = open_file_as_stream(filename) bytecode = f.readall() f.close() return bytecode
def compile_file(filename, outfname): """maybe rpython...""" w_skel = compile_list_of_expr(filename_to_expr_list(filename)) outf = open_file_as_stream(outfname, 'w') chunkio.dump(w_skel, outf) outf.close()
def find_module(space, modulename, w_modulename, partname, w_path, use_loader=True): # Examin importhooks (PEP302) before doing the import if use_loader: w_loader = find_in_meta_path(space, w_modulename, w_path) if w_loader: return FindInfo.fromLoader(w_loader) # XXX Check for frozen modules? # when w_path is a string if w_path is None: # check the builtin modules if modulename in space.builtin_modules: return FindInfo(C_BUILTIN, modulename, None) w_path = space.sys.get('path') # XXX check frozen modules? # when w_path is null if w_path is not None: for w_pathitem in space.unpackiterable(w_path): # sys.path_hooks import hook if use_loader: w_loader = find_in_path_hooks(space, w_modulename, w_pathitem) if w_loader: return FindInfo.fromLoader(w_loader) path = space.str_w(w_pathitem) filepart = os.path.join(path, partname) if os.path.isdir(filepart) and case_ok(filepart): initfile = os.path.join(filepart, '__init__') modtype, _, _ = find_modtype(space, initfile) if modtype in (PY_SOURCE, PY_COMPILED): return FindInfo(PKG_DIRECTORY, filepart, None) else: msg = "Not importing directory " +\ "'%s' missing __init__.py" % (filepart,) space.warn(msg, space.w_ImportWarning) modtype, suffix, filemode = find_modtype(space, filepart) try: if modtype in (PY_SOURCE, PY_COMPILED): assert suffix is not None filename = filepart + suffix stream = streamio.open_file_as_stream(filename, filemode) try: return FindInfo(modtype, filename, stream, suffix, filemode) except: stream.close() raise if modtype == C_EXTENSION: filename = filepart + suffix return FindInfo(modtype, filename, None, suffix, filemode) except StreamErrors: pass # XXX! must not eat all exceptions, e.g. # Out of file descriptors. # not found return None
def load_compiled_chunk(filename): vm = VM() open_lib(vm) stream = open_file_as_stream(filename, 'r') w_skel = chunkio.load(stream) stream.close() vm.bootstrap(w_skel) vm.run()
def load(self, cartridge_path): if cartridge_path is None: raise Exception("cartridge_path cannot be None!") cartridge_path = str(cartridge_path) self.cartridge_file_path = cartridge_path self.cartridge_stream = open_file_as_stream(cartridge_path) self.cartridge_file_contents = map_to_byte( \ self.cartridge_stream.readall()) self.load_battery(cartridge_path)
def get_file(space, w_file, filename, filemode): if w_file is None or space.is_w(w_file, space.w_None): try: return streamio.open_file_as_stream(filename, filemode) except StreamErrors, e: # XXX this is not quite the correct place, but it will do for now. # XXX see the issue which I'm sure exists already but whose number # XXX I cannot find any more... raise wrap_streamerror(space, e)
def main(argv): if not len(argv) == 2: print __doc__ return 1 f = open_file_as_stream(argv[1]) data = f.readall() f.close() interpret(data) return 0
def test_write_compiled_module(self): space = self.space pathname = _testfilesource() stream = streamio.open_file_as_stream(pathname, "r") try: w_ret = importing.parse_source_module(space, pathname, stream.readall()) finally: stream.close() pycode = space.interpclass_w(w_ret) assert type(pycode) is pypy.interpreter.pycode.PyCode cpathname = str(udir.join('cpathname.pyc')) mtime = 12345 importing.write_compiled_module(space, pycode, cpathname, mtime) # check pathname = str(udir.join('cpathname.py')) ret = importing.check_compiled_module(space, pathname, mtime, cpathname) assert ret == 1 # read compile module stream = streamio.open_file_as_stream(cpathname, "r") try: stream.seek(8, 0) w_code = importing.read_compiled_module(space, cpathname, stream.readall()) pycode = space.interpclass_w(w_code) finally: stream.close() # check value of load w_dic = space.newdict() pycode.exec_code(space, w_dic, w_dic) w_ret = space.getitem(w_dic, space.wrap('x')) ret = space.int_w(w_ret) assert ret == 42
def entry_point(argv): if len(argv) < 2: print __doc__ os._exit(1) args = argv[2:] stream = open_file_as_stream(argv[1]) co = serializer.deserialize(stream.readall()) w_args = [unwrap_arg(args[i]) for i in range(len(args))] execution.run(co, w_args) return 0
def main(fname, argv): f = open_file_as_stream(fname, "r") input = f.readall() f.close() code = compile(input) mainframe = Frame(code) for i in range(len(argv)): mainframe.registers[i] = Int(int(argv[i])) res = mainframe.interpret() print "Result:", res.repr()
def test_long_writes(self): pathname = str(udir.join('test.dat')) stream = streamio.open_file_as_stream(pathname, "wb") try: importing._w_long(stream, 42) importing._w_long(stream, 12312) importing._w_long(stream, 128397198) finally: stream.close() stream = streamio.open_file_as_stream(pathname, "rb") try: res = importing._r_long(stream) assert res == 42 res = importing._r_long(stream) assert res == 12312 res = importing._r_long(stream) assert res == 128397198 finally: stream.close()
def __init__( self, program=None, filename=None, argv=[] ): self.program = program if filename: f = open_file_as_stream(filename) self.program = f.readall() f.close() self.space = Space2D( self.program ) self.stacks = StackStack() self.argv = argv
def entry_point(argv): if len(argv) == 2: f = open_file_as_stream(argv[1]) interp.run(load_source(f.readall())) return 0 elif argv[0] == 'foo': raise ExecutionReturned(None) else: print "Usage: %s jsourcefile" % argv[0] return 1
def dump_heap_stats(space, filename): tb = rgc._heap_stats() if not tb: raise OperationError(space.w_RuntimeError, space.wrap("Wrong GC")) f = open_file_as_stream(filename, mode="w") for i in range(len(tb)): f.write("%d %d " % (tb[i].count, tb[i].size)) f.write(",".join([str(tb[i].links[j]) for j in range(len(tb))]) + "\n") f.close()
def parse(code): GFILE = open_file_as_stream(abspath(join(dirname(__file__), "grammar.txt"))) t = None try: t = GFILE.read() regexs, rules, ToAST = parse_ebnf(t) except ParseError,e: print e.nice_error_message(filename=str(GFILE),source=t) raise
def main(args): search = args[1] fname = args[2] s = open_file_as_stream(fname, 'r', 1024) while True: next_line = s.readline() if not next_line: break if search in next_line: print next_line return 0
def load_feature(space, path, orig_path): if not os.path.exists(assert_str0(path)): raise space.error(space.w_LoadError, orig_path) f = open_file_as_stream(path) try: contents = f.readall() finally: f.close() space.execute(contents, filepath=path)
def parse_file(self, f): fd = open_file_as_stream(f, 'r') while 1: line = fd.readline() if line == '': break self.parse_line(line) assert None not in self.clauses assert None not in self.vars assert self.prepared return self.build_formula()
def _entry_point(space, argv): verbose = False path = None argv_w = [] idx = 1 while idx < len(argv): arg = argv[idx] idx += 1 if arg == "-v": verbose = True else: path = arg while idx < len(argv): arg = argv[idx] idx += 1 argv_w.append(space.newstr_fromstr(arg)) space.set_const(space.w_object, "ARGV", space.newarray(argv_w)) system, _, _, _, cpu = os.uname() platform = "%s-%s" % (cpu, system.lower()) engine = "topaz" version = "1.9.3" patchlevel = 125 description = "%s (ruby-%sp%d) [%s]" % (engine, version, patchlevel, platform) space.set_const(space.w_object, "RUBY_ENGINE", space.newstr_fromstr(engine)) space.set_const(space.w_object, "RUBY_VERSION", space.newstr_fromstr(version)) space.set_const(space.w_object, "RUBY_PATCHLEVEL", space.newint(patchlevel)) space.set_const(space.w_object, "RUBY_PLATFORM", space.newstr_fromstr(platform)) space.set_const(space.w_object, "RUBY_DESCRIPTION", space.newstr_fromstr(platform)) if verbose: os.write(1, "%s\n" % description) status = 0 w_exit_error = None if path is not None: f = open_file_as_stream(path) try: source = f.readall() finally: f.close() try: space.execute(source, filepath=path) except RubyError as e: w_exc = e.w_value if isinstance(w_exc, W_SystemExit): return w_exc.status else: w_exit_error = w_exc status = 1 space.run_exit_handlers() if w_exit_error is not None: print_traceback(space, w_exit_error) return status
def entry_point(args): f = open_file_as_stream(args[1]) source = f.readall() f.close() relax = '--relaxed' in args book = '--by-the-book' in args try: Vm(source,relax=relax,book=book) except MBException, e: return 1
def create_log(self, extension='.ops'): if self.log_stream is not None: return self.log_stream s = os.environ.get('PYPYJITLOG') if not s: return None s += extension try: self.log_stream = open_file_as_stream(s, 'w') except OSError: os.write(2, "could not create log file\n") return None return self.log_stream
def test_load_source_module(self): space = self.space w_modulename = space.wrap('somemodule') w_mod = space.wrap(Module(space, w_modulename)) pathname = _testfilesource() stream = streamio.open_file_as_stream(pathname, "r") try: w_ret = importing.load_source_module(space, w_modulename, w_mod, pathname, stream.readall()) finally: stream.close() assert w_mod is w_ret w_ret = space.getattr(w_mod, space.wrap('x')) ret = space.int_w(w_ret) assert ret == 42 cpathname = udir.join('test.pyc') assert cpathname.check() cpathname.remove()
def check_compiled_module(space, pycfilename, expected_mtime=0): """ Check if a pyc file's magic number and (optionally) mtime match. """ try: stream = streamio.open_file_as_stream(pycfilename, "rb") try: magic = _r_long(stream) if magic != get_pyc_magic(space): return False if expected_mtime != 0: pyc_mtime = _r_long(stream) if pyc_mtime != expected_mtime: return False finally: stream.close() except StreamErrors: return False return True
def test_read_compiled_module(self): space = self.space mtime = 12345 co = compile('x = 42', '?', 'exec') cpathname = _testfile(importing.get_pyc_magic(space), mtime, co) stream = streamio.open_file_as_stream(cpathname, "rb") try: stream.seek(8, 0) w_code = importing.read_compiled_module(space, cpathname, stream.readall()) pycode = space.interpclass_w(w_code) finally: stream.close() assert type(pycode) is pypy.interpreter.pycode.PyCode w_dic = space.newdict() pycode.exec_code(space, w_dic, w_dic) w_ret = space.getitem(w_dic, space.wrap('x')) ret = space.int_w(w_ret) assert ret == 42
def test_load_compiled_module(self): space = self.space mtime = 12345 co = compile('x = 42', '?', 'exec') cpathname = _testfile(importing.get_pyc_magic(space), mtime, co) w_modulename = space.wrap('somemodule') stream = streamio.open_file_as_stream(cpathname, "rb") try: w_mod = space.wrap(Module(space, w_modulename)) magic = importing._r_long(stream) timestamp = importing._r_long(stream) w_ret = importing.load_compiled_module(space, w_modulename, w_mod, cpathname, magic, timestamp, stream.readall()) finally: stream.close() assert w_mod is w_ret w_ret = space.getattr(w_mod, space.wrap('x')) ret = space.int_w(w_ret) assert ret == 42
def test_load_source_module_syntaxerror(self): # No .pyc file on SyntaxError space = self.space w_modulename = space.wrap('somemodule') w_mod = space.wrap(Module(space, w_modulename)) pathname = _testfilesource(source="<Syntax Error>") stream = streamio.open_file_as_stream(pathname, "r") try: w_ret = importing.load_source_module(space, w_modulename, w_mod, pathname, stream.readall()) except OperationError: # OperationError("Syntax Error") pass stream.close() cpathname = udir.join('test.pyc') assert not cpathname.check()
def check_compiled_module(space, pycfilename, expected_mtime): """ Check if a pyc file's magic number and mtime match. """ stream = None try: stream = streamio.open_file_as_stream(pycfilename, "rb") magic = _r_long(stream) if magic != get_pyc_magic(space): stream.close() return None pyc_mtime = _r_long(stream) if pyc_mtime != expected_mtime: stream.close() return None return stream except StreamErrors: if stream: stream.close() return None # XXX! must not eat all exceptions, e.g.
def test_load_source_module_importerror(self): # the .pyc file is created before executing the module space = self.space w_modulename = space.wrap('somemodule') w_mod = space.wrap(Module(space, w_modulename)) pathname = _testfilesource(source="a = unknown_name") stream = streamio.open_file_as_stream(pathname, "r") try: w_ret = importing.load_source_module(space, w_modulename, w_mod, pathname, stream.readall()) except OperationError: # OperationError("NameError", "global name 'unknown_name' is not defined") pass stream.close() # And the .pyc has been generated cpathname = udir.join('test.pyc') assert cpathname.check()
def check_compiled_module(space, pathname, mtime, cpathname): """ Given a pathname for a Python source file, its time of last modification, and a pathname for a compiled file, check whether the compiled file represents the same version of the source. If so, return a FILE pointer for the compiled file, positioned just after the header; if not, return NULL. Doesn't set an exception. """ w_marshal = space.getbuiltinmodule('marshal') stream = streamio.open_file_as_stream(cpathname, "rb") magic = _r_long(stream) try: if magic != get_pyc_magic(space): return -1 pyc_mtime = _r_long(stream) if pyc_mtime != mtime: return 0 finally: stream.close() return 1
def compile(filename): f = open_file_as_stream(filename) source = f.readall() f.close() result = statements.match(source) # Remove trailing whitespaces match = whitespaces.match(result.rest) if match: result.rest = result.rest[match.end():] if result.rest: raise BarlaSyntaxError(result.rest) print 'Parse tree:' result.tree.dump() bytecode = [] consts = [] for stmt in result.tree.children: stmt.compile(bytecode, consts) return Code(''.join(bytecode), consts)
def load_file(filename, env): f = open_file_as_stream(filename) t = parse(f.readall(), filename) f.close() t.evaluate(env) return t
def call(self, args_w): from pypy.rlib.streamio import open_file_as_stream assert len(args_w) == 1 w_name, = args_w assert isinstance(w_name, W_String) return W_File(open_file_as_stream(w_name.content(), 'r'))
def write_battery(self, ram): output_stream = open_file_as_stream(self.battery_file_path, "w") output_stream.write(map_to_string(ram)) output_stream.flush() self.battery_file_contents = ram
def load_file(filename): from pypy.rlib.streamio import open_file_as_stream stream = open_file_as_stream(filename, 'r') content = stream.readall() stream.close() return read_string(content)
def load_file(filename): f = open_file_as_stream(filename) t = load_source(f.readall(), filename) f.close() return t
w_marshal = space.getbuiltinmodule('marshal') try: w_str = space.call_method(w_marshal, 'dumps', space.wrap(co), space.wrap(MARSHAL_VERSION_FOR_PYC)) strbuf = space.str_w(w_str) except OperationError, e: if e. async (space): raise #print "Problem while marshalling %s, skipping" % cpathname return # # Careful here: we must not crash nor leave behind something that looks # too much like a valid pyc file but really isn't one. # try: stream = streamio.open_file_as_stream(cpathname, "wb") except StreamErrors: return # cannot create file try: try: # will patch the header later; write zeroes until we are sure that # the rest of the file is valid _w_long(stream, 0) # pyc_magic _w_long(stream, 0) # mtime stream.write(strbuf) # should be ok (XXX or should call os.fsync() to be sure?) stream.seek(0, 0) _w_long(stream, get_pyc_magic(space)) _w_long(stream, mtime) finally:
def runcodefromfile(self, filename): f = open_file_as_stream(filename) self.runsource(f.readall(), filename) f.close()
def direct___init__(self, name, mode='r', buffering=-1): self.direct_close() self.check_mode_ok(mode) stream = streamio.open_file_as_stream(name, mode, buffering) fd = stream.try_to_find_file_descriptor() self.fdopenstream(stream, fd, mode, name)
def setup_directory_structure(space): root = setuppkg( "", a="imamodule = 1\ninpackage = 0", b="imamodule = 1\ninpackage = 0", ambig="imamodule = 1", test_reload="def test():\n raise ValueError\n", infinite_reload="import infinite_reload; reload(infinite_reload)", del_sys_module="import sys\ndel sys.modules['del_sys_module']\n", itertools="hello_world = 42\n", gc="should_never_be_seen = 42\n", ) root.ensure("notapackage", dir=1) # empty, no __init__.py setuppkg( "pkg", a="imamodule = 1\ninpackage = 1", relative_a="import a", abs_b="import b", abs_x_y="import x.y", abs_sys="import sys", string="inpackage = 1", errno="", absolute="from __future__ import absolute_import\nimport string", relative_b= "from __future__ import absolute_import\nfrom . import string", relative_c= "from __future__ import absolute_import\nfrom .string import inpackage", relative_f="from .imp import get_magic", relative_g="import imp; from .imp import get_magic", ) setuppkg( "pkg.pkg1", __init__='from . import a', a='', relative_d= "from __future__ import absolute_import\nfrom ..string import inpackage", relative_e= "from __future__ import absolute_import\nfrom .. import string", relative_g="from .. import pkg1\nfrom ..pkg1 import b", b="insubpackage = 1", ) setuppkg("pkg.pkg2", a='', b='') setuppkg("pkg_r", inpkg="import x.y") setuppkg("pkg_r.x") setuppkg("x", y='') setuppkg("ambig", __init__="imapackage = 1") setuppkg( "pkg_relative_a", __init__="import a", a="imamodule = 1\ninpackage = 1", ) setuppkg("pkg_substituting", __init__="import sys, pkg_substituted\n" "print 'TOTO', __name__\n" "sys.modules[__name__] = pkg_substituted") setuppkg("pkg_substituted", mod='') setuppkg("evil_pkg", evil="import sys\n" "from evil_pkg import good\n" "sys.modules['evil_pkg.evil'] = good", good="a = 42") p = setuppkg("readonly", x='') p = setuppkg("pkg_univnewlines") p.join('__init__.py').write('a=5\nb=6\rc="""hello\r\nworld"""\r', mode='wb') p.join('mod.py').write('a=15\nb=16\rc="""foo\r\nbar"""\r', mode='wb') # create compiled/x.py and a corresponding pyc file p = setuppkg("compiled", x="x = 84") if conftest.option.runappdirect: import marshal, stat, struct, os, imp code = py.code.Source(p.join("x.py").read()).compile() s3 = marshal.dumps(code) s2 = struct.pack("i", os.stat(str(p.join("x.py")))[stat.ST_MTIME]) p.join("x.pyc").write(imp.get_magic() + s2 + s3, mode='wb') else: w = space.wrap w_modname = w("compiled.x") filename = str(p.join("x.py")) stream = streamio.open_file_as_stream(filename, "r") try: importing.load_source_module(space, w_modname, w(importing.Module(space, w_modname)), filename, stream.readall()) finally: stream.close() if space.config.objspace.usepycfiles: # also create a lone .pyc file p.join('lone.pyc').write(p.join('x.pyc').read(mode='rb'), mode='wb') # create a .pyw file p = setuppkg("windows", x="x = 78") try: p.join('x.pyw').remove() except py.error.ENOENT: pass p.join('x.py').rename(p.join('x.pyw')) return str(root)
def dotmesh( path, facesAddr, facesSmoothAddr, facesMatAddr, vertsPosAddr, vertsNorAddr, numFaces, numVerts, materials ): for matname in materials: print( 'Material Name: %s' %matname ) file = streamio.open_file_as_stream( path, 'w') faces = rffi.cast( rffi.UINTP, facesAddr ) # face vertex indices facesSmooth = rffi.cast( rffi.CCHARP, facesSmoothAddr ) facesMat = rffi.cast( rffi.USHORTP, facesMatAddr ) vertsPos = rffi.cast( rffi.FLOATP, vertsPosAddr ) vertsNor = rffi.cast( rffi.FLOATP, vertsNorAddr ) VB = [ '<sharedgeometry>', '<vertexbuffer positions="true" normals="true">' ] fastlookup = {} ogre_vert_index = 0 triangles = [] for fidx in range( numFaces ): smooth = ord( facesSmooth[ fidx ] ) # ctypes.c_bool > char > int matidx = facesMat[ fidx ] i = fidx*4 ai = faces[ i ]; bi = faces[ i+1 ] ci = faces[ i+2 ]; di = faces[ i+3 ] triangle = [] for J in [ai, bi, ci]: i = J*3 x = rffi.cast( rffi.DOUBLE, vertsPos[ i ] ) y = rffi.cast( rffi.DOUBLE, vertsPos[ i+1 ] ) z = rffi.cast( rffi.DOUBLE, vertsPos[ i+2 ] ) pos = (x,y,z) #if smooth: x = rffi.cast( rffi.DOUBLE, vertsNor[ i ] ) y = rffi.cast( rffi.DOUBLE, vertsNor[ i+1 ] ) z = rffi.cast( rffi.DOUBLE, vertsNor[ i+2 ] ) nor = (x,y,z) SIG = (pos,nor, matidx) skip = False if J in fastlookup: for otherSIG in fastlookup[ J ]: if SIG == otherSIG: triangle.append( fastlookup[J][otherSIG] ) skip = True break if not skip: triangle.append( ogre_vert_index ) fastlookup[ J ][ SIG ] = ogre_vert_index else: triangle.append( ogre_vert_index ) fastlookup[ J ] = { SIG : ogre_vert_index } if skip: continue xml = [ '<vertex>', '<position x="%s" y="%s" z="%s" />' %pos, # funny that tuple is valid here '<normal x="%s" y="%s" z="%s" />' %nor, '</vertex>' ] VB.append( '\n'.join(xml) ) ogre_vert_index += 1 triangles.append( triangle ) VB.append( '</vertexbuffer>' ) VB.append( '</sharedgeometry>' ) file.write( '\n'.join(VB) ) del VB # free memory SMS = ['<submeshes>'] #for matidx, matname in ...: SM = [ '<submesh usesharedvertices="true" use32bitindexes="true" material="%s">' %'somemat', '<faces count="%s">' %'100', ] for tri in triangles: #x,y,z = tri # rpython bug, when in a new 'block' need to unpack/repack tuple #s = '<face v1="%s" v2="%s" v3="%s" />' %(x,y,z) assert isinstance(tri,tuple) #and len(tri)==3 # this also works s = '<face v1="%s" v2="%s" v3="%s" />' %tri # but tuple is not valid here SM.append( s ) SM.append( '</faces>' ) SM.append( '</submesh>' ) file.write( '\n'.join(SM) ) file.close()
def get_fp(self): return open_file_as_stream(self.filename, self.mode, 1024)
def load_battery(self, cartridge_file_path): self.battery_file_path = self.create_battery_file_path(cartridge_file_path) if self.has_battery(): self.battery_stream = open_file_as_stream(self.battery_file_path) self.battery_file_contents = map_to_byte( \ self.battery_stream.readall())
def get_file(space, w_file, filename, filemode): if w_file is None or space.is_w(w_file, space.w_None): return streamio.open_file_as_stream(filename, filemode) else: return space.interp_w(W_File, w_file).stream
def load_bytecode(filename): from pypy.rlib.streamio import open_file_as_stream from pypy.jit.tl.tlopcode import decode_program f = open_file_as_stream(filename) return decode_program(f.readall())
def setup_directory_structure(space): root = setuppkg("", a = "imamodule = 1\ninpackage = 0", b = "imamodule = 1\ninpackage = 0", ambig = "imamodule = 1", ) root.ensure("notapackage", dir=1) # empty, no __init__.py setuppkg("pkg", a = "imamodule = 1\ninpackage = 1", relative_a = "import a", abs_b = "import b", abs_x_y = "import x.y", string = "inpackage = 1", absolute = "from __future__ import absolute_import\nimport string", relative_b = "from __future__ import absolute_import\nfrom . import string", relative_c = "from __future__ import absolute_import\nfrom .string import inpackage", ) setuppkg("pkg.pkg1", a = '', relative_d = "from __future__ import absolute_import\nfrom ..string import inpackage", relative_e = "from __future__ import absolute_import\nfrom .. import string", ) setuppkg("pkg.pkg2", a='', b='') setuppkg("pkg_r", inpkg = "import x.y") setuppkg("pkg_r.x") setuppkg("x", y='') setuppkg("ambig", __init__ = "imapackage = 1") setuppkg("pkg_relative_a", __init__ = "import a", a = "imamodule = 1\ninpackage = 1", ) setuppkg("pkg_substituting", __init__ = "import sys, pkg_substituted\n" "sys.modules[__name__] = pkg_substituted") setuppkg("pkg_substituted", mod='') p = setuppkg("readonly", x='') p = setuppkg("pkg_univnewlines") p.join('__init__.py').write('a=5\nb=6\rc="""hello\r\nworld"""\r') p.join('mod.py').write('a=15\nb=16\rc="""foo\r\nbar"""\r') # create compiled/x.py and a corresponding pyc file p = setuppkg("compiled", x = "x = 84") if conftest.option.runappdirect: import marshal, stat, struct, os, imp code = py.code.Source(p.join("x.py").read()).compile() s3 = marshal.dumps(code) s2 = struct.pack("i", os.stat(str(p.join("x.py")))[stat.ST_MTIME]) p.join("x.pyc").write(imp.get_magic() + s2 + s3) else: w = space.wrap w_modname = w("compiled.x") filename = str(p.join("x.py")) stream = streamio.open_file_as_stream(filename, "r") try: importing.load_source_module(space, w_modname, w(importing.Module(space, w_modname)), filename, stream.readall()) finally: stream.close() if space.config.objspace.usepycfiles: # also create a lone .pyc file p.join('lone.pyc').write(p.join('x.pyc').read()) return str(root)
def find_module(space, modulename, w_modulename, partname, w_path, use_loader=True): # Examin importhooks (PEP302) before doing the import if use_loader: w_loader = find_in_meta_path(space, w_modulename, w_path) if w_loader: return FindInfo.fromLoader(w_loader) # XXX Check for frozen modules? # when w_path is a string delayed_builtin = None w_lib_extensions = None if w_path is None: # check the builtin modules if modulename in space.builtin_modules: delayed_builtin = FindInfo(C_BUILTIN, modulename, None) # a "real builtin module xx" shadows every file "xx.py" there # could possibly be; a "pseudo-extension module" does not, and # is only loaded at the point in sys.path where we find # '.../lib_pypy/__extensions__'. if modulename in space.MODULES_THAT_ALWAYS_SHADOW: return delayed_builtin w_lib_extensions = space.sys.get_state(space).w_lib_extensions w_path = space.sys.get('path') # XXX check frozen modules? # when w_path is null if w_path is not None: for w_pathitem in space.unpackiterable(w_path): # sys.path_hooks import hook if (w_lib_extensions is not None and space.eq_w(w_pathitem, w_lib_extensions)): return delayed_builtin if use_loader: w_loader = find_in_path_hooks(space, w_modulename, w_pathitem) if w_loader: return FindInfo.fromLoader(w_loader) path = space.str_w(w_pathitem) filepart = os.path.join(path, partname) if os.path.isdir(filepart) and case_ok(filepart): initfile = os.path.join(filepart, '__init__') modtype, _, _ = find_modtype(space, initfile) if modtype in (PY_SOURCE, PY_COMPILED): return FindInfo(PKG_DIRECTORY, filepart, None) else: msg = "Not importing directory " +\ "'%s' missing __init__.py" % (filepart,) space.warn(msg, space.w_ImportWarning) modtype, suffix, filemode = find_modtype(space, filepart) try: if modtype in (PY_SOURCE, PY_COMPILED, C_EXTENSION): assert suffix is not None filename = filepart + suffix stream = streamio.open_file_as_stream(filename, filemode) try: return FindInfo(modtype, filename, stream, suffix, filemode) except: stream.close() raise except StreamErrors: pass # XXX! must not eat all exceptions, e.g. # Out of file descriptors. # not found return delayed_builtin
def read_battery(self): self.battery_stream = open_file_as_stream(self.battery_file_path) self.battery_file_contents = map_to_byte(self.battery_stream.readall())