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 = 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 = 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 main(args): prog = [] try: program_file = open_file_as_stream(args[1]) lines = program_file.readall().split("\n") program_file.close() for line in lines: line = line.strip() # some lines are 'commented' with '#' if not line.startswith("#") and line: command = line.split(" ") if command[0] == "LOAD" or command[0] == "STORE": prog.append(StringOp(opcodes[command[0]], command[1])) elif command[0] == "PUSH" or command[0] == "JMP" or \ command[0] == "CJMP": prog.append(IntOp(opcodes[command[0]], command[1])) else: prog.append(Op(opcodes[command[0]])) except IndexError as e: print "[ERROR] Need an argument" except IOError as e: print "[ERROR] File not found: %s" % args[1] else: interp = Interpreter(prog) interp.run() return 0
def test_pyc_magic_changes(self): # skipped: for now, PyPy generates only one kind of .pyc file # per version. Different versions should differ in # sys.implementation.cache_tag, which means that they'll look up # different .pyc files anyway. See test_get_tag() in test_app.py. py.test.skip("For now, PyPy generates only one kind of .pyc files") # 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 = maketestobjspace(make_config(None, **{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(space1, 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 = _r_long(stream) timestamp = _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 init_from_ini(self): exedir = self.get_exedir() if not exedir: return inifile = rpath.rjoin(exedir, "rsqueak.ini") if os.path.exists(inifile): f = streamio.open_file_as_stream(inifile, mode="r", buffering=0) try: argini = [""] line = f.readline().strip() while len(line) > 0: if line.startswith("[") or line.startswith(";"): pass elif "=" in line: option, param = line.split("=", 1) option = option.strip() param = param.strip() param = param.strip("\"'") if param == "1": argini += ["--%s" % option] elif param == "0": pass else: argini += ["--%s" % option, param] else: raise error.Exit("Invalid line in INI file: %s" % line) line = f.readline().strip() self.parse_args(argini) finally: f.close()
def come_up(basename): """ Bring up previously marshalled Tags, shapes and transformations from '.docked' file un-marshalling, slurping and replacement of current Tags. """ from theseus.shape import CompoundShape # later # from os import stat # statres = stat(path) debug_start("theseus-come-up") path = basename + '.docked' if not os.path.exists(path): return try: f = open_file_as_stream(path, buffering=0) except OSError as e: os.write(2, "Error(come_up)%s -- %s\n" % (os.strerror(e.errno), path)) return try: res = unmarshaller(f.readall()) finally: f.close() del CompoundShape._shapes[:] W_Tag.tags.clear() new_tags = slurp_tags(res) for key, value in new_tags.items(): W_Tag.tags[key] = value debug_stop("theseus-come-up")
def entry_point(argv): path = argv[0] if len(argv) == 1 else argv[-1] try: f = open_file_as_stream(path, buffering=0) except OSError as exc: os.write(2, "%s -- %s (LoadError)\n" % (os.strerror(exc.errno), path)) return 1 try: code = f.readall() finally: f.close() try: t = parse(code) except BacktrackException as exc: (line, col) = exc.error.get_line_column(code) # expected = ' '.join(exc.error.expected) os.write(2, "parse error in line %d, column %d" % (line, col)) return 1 ctx = ExecutionContext() try: for sexpr in t: try: print(sexpr.eval(ctx).to_string()) except ContinuationReturn as exc: print(exc.result.to_string()) except SchemeQuit: return 0 else: return 0
def compile(self, path, filename, system_class, universe): fname = path + os.sep + filename + ".som" try: input_file = open_file_as_stream(fname, "r") try: self._parser = Parser(input_file, fname, universe) result = self._compile(system_class, universe) finally: input_file.close() except OSError: raise IOError() except ParseError as e: from som.vm.universe import error_println error_println(str(e)) universe.exit(1) return None cname = result.get_name() cnameC = cname.get_string() if filename != cnameC: from som.vm.universe import error_println error_println("File name %s does not match class name %s." % (filename, cnameC)) universe.exit(1) return result
def test_pyc_magic_changes(self): py.test.skip("For now, PyPy generates only one kind of .pyc files") # 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 = maketestobjspace(make_config(None, **{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 main(argv): filename = argv[1] f = open_file_as_stream(filename) data = f.readall() f.close() interpret(data) return 0
def main(argv): if len(argv) < 2: print __doc__ return 1 f = None bconly = argv[1] == "--print-bc-only" if bconly: f = open_file_as_stream(argv[2]) else: f = open_file_as_stream(argv[1]) data = f.readall() f.close() run(data, bconly) return 0
def _main(argv): """Program entry point. :param argv: command-line arguments :type argv: :class:`list` :return: exit code :rtype: :class:`int` """ author_strings = [] for i in xrange(len(metadata.authors)): author_strings.append( 'Author: %s <%s>' % (metadata.authors[i], metadata.emails[i])) # We can't use argparse, so we do some old-school argument parsing. if '-h' in argv or '--help' in argv: print usage(argv) # RPython doesn't support named specifiers. print ''' %s %s %s URL: <%s> ''' % (metadata.project, metadata.version, '\n'.join(author_strings), metadata.url) return 0 if '-V' in argv or '--version' in argv: print '%s %s' % (metadata.project, metadata.version) return 0 num_argv = len(argv) if num_argv > 2: print usage(argv) return 1 if num_argv == 1 or (num_argv == 2 and argv[1] == '-'): input_stream = fdopen_as_stream(0, 'r') else: input_stream = open_file_as_stream(argv[1]) try: source_code = input_stream.readall() finally: input_stream.close() try: run(source_code) except StencilLanguageError as error: # The purpose of this except block is two-fold: # # * Don't print tracebacks for interpreter errors. Tracebacks shouldn't # be shown to the user. # * RPython doesn't honor most magic methods including __str__ and # __repr__, so error messages aren't shown to the user when using the # translated executable. Only the exception name is shown. # TODO: This should print to stderr. print '%s: %s' % (error.name, error.__str__()) return 0
def read_source(filename): f = open_file_as_stream(filename) source = f.readall() f.close() sources = source.splitlines() sources = [line for line in sources if not line.strip().startswith('#')] source = '\n'.join(sources) return source
def input(prompt=None): search = "\n" stdin = open_file_as_stream("/dev/stdin", "r", 1024) stdout = open_file_as_stream("/dev/stdout", "w", 1024) if prompt is not None: stdout.write(prompt) stdout.flush() while True: try: line = stdin.readline() except (KeyboardInterrupt, EOFError): return if not line: return if search in line: return line
def fetch(self): data = "" for package in self.packages: fetchfound = False packagefound = False filepath = self.pathtopackage[package] + package + ".na" try: sourcefile = open_file_as_stream(filepath) data = sourcefile.readall() sourcefile.close() except OSError: errtok = self.tokentopackage.get(package, None) errfile = self.packages[errtok.getsourcepos().idx] sodaError(errfile, str(errtok.getsourcepos().lineno), str(errtok.getsourcepos().colno), "package \"%s\" not found" % package) i = 0 tokenlist = [] if not data == "": for token in lexer.lex(data, self.idx): if not fetchfound: if token.name == "FETCH": if not i == 0: sodaError( package, str(token.getsourcepos().lineno), str(token.getsourcepos().colno), "fetch statement must precede main program" ) else: i += 1 fetchfound = True else: i += 1 tokenlist.append(token) else: if token.name == "STRING": self.tokentopackage[token.value] = token if not packagefound: fullpath = self.pathtopackage[self.packages[ self.idx]] + token.value self.addpackage(fullpath) packagefound = True continue else: sodaError( package, str(token.getsourcepos().lineno), str(token.getsourcepos().colno), "package names must be " "separated by newlines") elif token.name == "END": packagefound = False continue else: tokenlist.append(token) fetchfound = False data = "" self.idx += 1 self.tokgeneratorlist.append(tokenlist)
def main(argv): if not len(argv) == 2: return 1 f = open_file_as_stream(argv[1]) data = f.readall() f.close() interpret(data) return 0
def entry_point(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 get_file(space, w_file, filename, filemode): if space.is_none(w_file): 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 get_file(space, w_file, filename, filemode): if space.is_none(w_file): try: return streamio.open_file_as_stream(filename, filemode) except streamio.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 _read_file(self, filename): try: f = open_file_as_stream(filename) except OSError: print 'File not found %s' % filename return None data = f.readall() f.close() return data
def expand_file(path): if we_are_translated(): assert isinstance(_reader.reader, ReaderRPython) if not os.access(path, os.R_OK): raise Exception("Can not read file") f = streamio.open_file_as_stream(path) s = f.readall() f.close() return _reader.reader.expand("(%s)" % s)
def test_long_writes(self): pathname = str(udir.join('test.dat')) stream = streamio.open_file_as_stream(pathname, "wb") try: _w_long(stream, 42) _w_long(stream, 12312) _w_long(stream, 128397198) finally: stream.close() stream = streamio.open_file_as_stream(pathname, "rb") try: res = _r_long(stream) assert res == 42 res = _r_long(stream) assert res == 12312 res = _r_long(stream) assert res == 128397198 finally: stream.close()
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 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 read_file(f): err = 0 code = '' try: file = open_file_as_stream(f) code = file.readall() file.close() except Exception: print 'Exception: Could not open file "%s"' % f err = 1 return code,err
def readall(filename): if not file_exists(filename): raise IOError(filename) # must catch WindowsError here or it won't compile for some reason try: fp = open_file_as_stream(filename) data = fp.readall() fp.close() return data except WindowsError as e: return ""
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 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 main(argv): if not len(argv) == 2: print __doc__ return 1 filename = argv[1] f = open_file_as_stream(filename) source = f.readall() f.close() return run(source, filename=filename)
def impl_open_options(engine, heap, srcpath, mode, stream, options): if not isinstance(stream, term.Var): error.throw_type_error("variable", stream) opts = make_option_dict(options) mode = rwa.get(mode, None) if mode is None: error.throw_domain_error("io_mode", term.Callable.build("mode not supported")) else: buffering = opts.get("buffer", "full") if buffering == "full": bufmode = -1 elif buffering == "line": bufmode = 1 elif buffering == "false": bufmode = 0 else: error.throw_domain_error("buffering", term.Callable.build(buffering)) assert 0, "unreachable" try: if mode == "r": prolog_stream = PrologInputStream( open_file_as_stream(srcpath, mode, bufmode)) else: prolog_stream = PrologOutputStream( open_file_as_stream(srcpath, mode, bufmode)) except OSError: error.throw_existence_error("source_sink", term.Callable.build(srcpath)) assert 0, "unreachable" engine.streamwrapper.streams[prolog_stream.fd()] = prolog_stream try: alias = opts["alias"] prolog_stream.alias = alias except KeyError: alias = "$stream_%d" % prolog_stream.fd() engine.streamwrapper.aliases[alias] = prolog_stream stream.unify(term.Callable.build(alias), heap)
def _parse_json_file(self, path): f = streamio.open_file_as_stream(path) s = f.readall() f.close() os.remove(path) json_data = loads(s) assert json_data.is_object json_data = json_data.object_value() if json_data['success']: return json_data['content'] else: raise Exception("Read error")
def read_file(filename): path = rpath.rabspath(filename) try: file = open_file_as_stream(path, mode="rb", buffering=0) try: data = file.readall() finally: file.close() except OSError as e: os.write(2, "%s -- %s (LoadError)\n" % (os.strerror(e.errno), path)) return ("", False) return (data, True)
def main(argv): if len(argv) < 2: bail("bad usage") hndl = open_file_as_stream(argv[1]) prog = parse.parse(hndl.readall()) hndl.close() args = [ int(x) for x in argv[2:] ] prog.run(args) return 0
def impl_open_options(engine, heap, srcpath, mode, stream, options): if not isinstance(stream, term.Var): error.throw_type_error("variable", stream) opts = make_option_dict(options) mode = rwa.get(mode, None) if mode is None: error.throw_domain_error("io_mode", term.Callable.build( "mode not supported")) else: buffering = opts.get("buffer", "full") if buffering == "full": bufmode = -1 elif buffering == "line": bufmode = 1 elif buffering == "false": bufmode = 0 else: error.throw_domain_error("buffering", term.Callable.build(buffering)) assert 0, "unreachable" try: if mode == "r": prolog_stream = PrologInputStream(open_file_as_stream( srcpath, mode, bufmode)) else: prolog_stream = PrologOutputStream(open_file_as_stream( srcpath, mode, bufmode)) except OSError: error.throw_existence_error("source_sink", term.Callable.build(srcpath)) assert 0, "unreachable" engine.streamwrapper.streams[prolog_stream.fd()] = prolog_stream try: alias = opts["alias"] prolog_stream.alias = alias except KeyError: alias = "$stream_%d" % prolog_stream.fd() engine.streamwrapper.aliases[alias] = prolog_stream stream.unify(term.Callable.build(alias), heap)
def entry_point(argv): if len(argv) != 3: print 'Usage: %s FILE n' % argv[0] return 1 filename = argv[1] N = int(argv[2]) f = open_file_as_stream(filename) msg = f.readall() try: bench('loads ', N, myloads, msg) except OperationError, e: print 'Error', e._compute_value(fakespace)
def load_feature(space, path, orig_path): if not os.path.exists(path): raise space.error(space.w_LoadError, orig_path) try: f = open_file_as_stream(path, buffering=0) try: contents = f.readall() finally: f.close() except OSError as e: raise error_for_oserror(space, e) space.execute(contents, filepath=path)
def _parse_file(filename, error_displayer): path = os.getcwd() fullname = path + os.sep + filename try: input_file = open_file_as_stream(fullname, "r") source = input_file.readall() error_displayer.source = source try: ast = parse(source) except ParsingError, e: error_displayer.handle_error(e.message, e.getsourcepos(), "Syntax Error") raise e finally: input_file.close()
def from_file(filename): """Parse a matrix from a file. :param filename: file name from which to read the matrix :type filename: :class:`str` :return: the created matrix :rtype: :class:`Matrix` """ stream = open_file_as_stream(filename) try: matrix = from_string(stream.readall()) finally: stream.close() return matrix
def impl_see(engine, heap, obj): w = engine.streamwrapper try: stream = w.aliases[obj] impl_set_input(engine, heap, stream) except KeyError: try: stream = PrologInputStream( open_file_as_stream(obj, rwa["read"], -1)) w.streams[stream.fd()] = stream w.aliases["$stream_%d" % stream.fd()] = stream impl_set_input(engine, heap, stream) except OSError: error.throw_existence_error("source_sink", term.Callable.build(obj))
def get_file(space, w_file, filename, filemode): if space.is_none(w_file): try: return streamio.open_file_as_stream(filename, filemode) except streamio.StreamErrors as 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) else: w_iobase = space.interp_w(W_IOBase, w_file) # XXX: not all W_IOBase have a fileno method: in that case, we should # probably raise a TypeError? fd = space.int_w(space.call_method(w_iobase, 'fileno')) return streamio.fdopen_as_stream(fd, filemode)
def test_parse_source_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 = w_ret 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 impl_see(engine, heap, obj): w = engine.streamwrapper try: stream = w.aliases[obj] impl_set_input(engine, heap, stream) except KeyError: try: stream = PrologInputStream(open_file_as_stream(obj, rwa["read"], -1)) w.streams[stream.fd()] = stream w.aliases["$stream_%d" % stream.fd()] = stream impl_set_input(engine, heap, stream) except OSError: error.throw_existence_error("source_sink", term.Callable.build(obj))
def test_parse_source_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 = w_ret assert type(pycode) is 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_source_module_nowrite(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(), stream.try_to_find_file_descriptor(), write_pyc=False) finally: stream.close() cpathname = udir.join('test.pyc') assert not cpathname.check()
def run_source(command_line): cycy = command_line.cycy sources = [] source_string = command_line.source_string if source_string: sources.append(source_string) else: for source_path in command_line.source_files: source_file = open_file_as_stream(source_path) sources.append(source_file.readall()) source_file.close() w_exit_status = cycy.interpret(sources) if w_exit_status is None: # internal error during interpret return 1 return w_exit_status.rint()
def entry_point(argv): bytecode_str = streamio.open_file_as_stream(argv[1]).readall() bytecode_list = [strip(w) for w in bytecode_str.split(',')] bytecode = [] for i, w in enumerate(bytecode_list): if w == '': continue if w == 'JUMP_IF_A': bytecode.append(JUMP_IF_A) elif w == 'MOV_A_R': bytecode.append(MOV_A_R) elif w == 'MOV_R_A': bytecode.append(MOV_R_A) elif w == 'ADD_R_TO_A': bytecode.append(ADD_R_TO_A) elif w == 'DECR_A': bytecode.append(DECR_A) elif w == 'RETURN_A': bytecode.append(RETURN_A) else: bytecode.append(int(w)) result = interpret(bytecode, int(argv[2])) print result return 0
def load_feature(space, path, orig_path, wrap=False): if not os.path.exists(path): raise space.error(space.w_LoadError, orig_path) try: f = open_file_as_stream(path, buffering=0) try: contents = f.readall() finally: f.close() except OSError as e: raise error_for_oserror(space, e) if wrap: lexical_scope = StaticScope(space.newmodule("Anonymous"), None) else: lexical_scope = None space.execute(contents, filepath=path, lexical_scope=lexical_scope)
def __init__(self, filename=None, inputfile=None, data=None): if filename: f = streamio.open_file_as_stream(filename, mode="rb", buffering=0) try: self.data = f.readall() finally: f.close() elif inputfile: try: self.data = inputfile.read() finally: inputfile.close() elif data: self.data = data else: raise RuntimeError("need to supply either inputfile or data") self.reset()