def test_redirect_assembler(self, tmpdir, metainterp_sd): looptoken = FakeCallAssemblerLoopToken(0x0) newlooptoken = FakeCallAssemblerLoopToken(0x1234) # logger = jl.JitLogger() file = tmpdir.join('binary_file') file.ensure() # use rfile instead of file.open since the host python and compiled # code may use different runtime libraries (win32 visual2008 vs. # visual2019 for instance rfile = create_file(str(file), 'wb') with SuppressIPH(): jl.jitlog_init(rfile.fileno()) logger.start_new_trace(metainterp_sd, jd_name='jdname') log_trace = logger.log_trace(jl.MARK_TRACE, None, None) op = ResOperation(rop.CALL_ASSEMBLER_I, [], descr=looptoken) log_trace.write([], [op]) jl.redirect_assembler(looptoken, newlooptoken, 0x1234) #the next line will close the 'fd', instead of logger.finish() rfile.close() binary = file.read() opnum = jl.encode_le_16bit(rop.CALL_ASSEMBLER_I) id_looptoken = compute_unique_id(looptoken) new_id_looptoken = compute_unique_id(newlooptoken) end = jl.MARK_RESOP_DESCR + opnum + jl.encode_str('i0,looptoken') + \ jl.encode_le_addr(id_looptoken) + jl.encode_str('') + \ jl.MARK_REDIRECT_ASSEMBLER + \ jl.encode_le_addr(id_looptoken) + \ jl.encode_le_addr(new_id_looptoken) + \ jl.encode_le_addr(newlooptoken._ll_function_addr) assert binary.endswith(end)
def test_debug_merge_point(self, tmpdir, metainterp_sd): logger = jl.JitLogger() file = tmpdir.join('binary_file') file.ensure() # use rfile instead of file.open since the host python and compiled # code may use different runtime libraries (win32 visual2008 vs. # visual2019 for instance rfile = create_file(str(file), 'wb') with SuppressIPH(): jl.jitlog_init(rfile.fileno()) logger.start_new_trace(metainterp_sd, jd_name='jdname') log_trace = logger.log_trace(jl.MARK_TRACE, None, None) op = ResOperation( rop.DEBUG_MERGE_POINT, [ConstInt(0), ConstInt(0), ConstInt(0)]) log_trace.write([], [op]) #the next line will close the 'fd', instead of logger.finish() rfile.close() binary = file.read() is_32bit = chr(sys.maxint == 2**31 - 1) assert binary == (jl.MARK_START_TRACE) + jl.encode_le_addr(1) + \ jl.encode_str('loop') + jl.encode_le_addr(0) + \ jl.encode_str('jdname') + \ (jl.MARK_TRACE) + jl.encode_le_addr(1) + \ (jl.MARK_INPUT_ARGS) + jl.encode_str('') + \ (jl.MARK_INIT_MERGE_POINT) + b'\x05\x00\x01s\x00i\x08s\x00i\x10s' + \ (jl.MARK_MERGE_POINT) + \ b'\xff' + jl.encode_str('/home/pypy/jit.py') + \ b'\x00' + jl.encode_le_64bit(0) + \ b'\xff' + jl.encode_str('enclosed') + \ b'\x00' + jl.encode_le_64bit(99) + \ b'\xff' + jl.encode_str('DEL')
def read_file(filename): try: file = rfile.create_file(filename, 'r') except IOError: debug("Error opening file") raise SystemExit(74) source = file.read() return source
def test_read_a_lot(self): fname = str(self.tmpdir.join('file_read_a_lot')) with open(fname, 'w') as f: f.write('dupa' * 999) f = rfile.create_file(fname, 'r') s = f.read() assert s == 'dupa' * 999 f.close()
def read_file(pathname): try: fd = rfile.create_file(as_cstring(pathname), 'r') try: return from_cstring(fd.read()) finally: fd.close() except IOError as error: message = os.strerror(error.errno).decode('utf-8') raise Error(u"%s: %s" % (pathname.string, message))
def open_file(pathname): try: fd = rfile.create_file(pathname, 'r') try: return Stream(fd.read()) finally: fd.close() except IOError as error: message = os.strerror(error.errno).decode('utf-8') raise Error(u"%s: %s" % (pathname.decode('utf-8'), message))
def open_file(pathname): name = pathobj.os_stringify(pathname).encode('utf-8') try: fd = rfile.create_file(name, 'rb') try: return load(fd) finally: fd.close() except IOError as error: message = os.strerror(error.errno).decode('utf-8') raise OldError(u"%s: %s" % (pathobj.stringify(pathname), message))
def test_auto_close(self): fname = str(self.tmpdir.join('file_auto_close')) f = rfile.create_file(fname, 'w') f.write('a') # remains in buffers assert os.path.getsize(fname) == 0 del f for i in range(5): if os.path.getsize(fname) != 0: break gc.collect() assert os.path.getsize(fname) == 1
def write_file(pathname, data): name = pathobj.os_stringify(pathname).encode("utf-8") try: fd = rfile.create_file(name, "wb") try: dump(fd, data) finally: fd.close() except IOError as error: message = os.strerror(error.errno).decode("utf-8") raise OldError(u"%s: %s" % (pathobj.stringify(pathname), message)) return null
def write_file(pathname, data): name = pathobj.os_stringify(pathname).encode('utf-8') try: fd = rfile.create_file(name, "wb") try: dump(fd, data) finally: fd.close() except IOError as error: message = os.strerror(error.errno).decode('utf-8') raise OldError(u"%s: %s" % (pathobj.stringify(pathname), message)) return null
def test_readline_without_eol_at_the_end(self): fname = str(self.tmpdir.join('file_readline_without_eol_at_the_end')) for n in [1, 10, 97, 98, 99, 100, 101, 102, 103, 150, 196, 197, 198, 199, 200, 201, 202, 203, 204, 250]: s = ''.join([chr(32+(k&63)) for k in range(n)]) with open(fname, 'wb') as f: f.write(s) f = rfile.create_file(fname, 'r') got = f.readline() assert got == s got = f.readline() assert got == '' f.close()
def open_(argv): if len(argv) < 1: raise OldError(u"too few arguments to fs.open()") pathname = pathobj.to_path(argv[0]) path = pathobj.os_stringify(pathname).encode('utf-8') if len(argv) > 1: mode = as_cstring(argv[1]) mode += 'b' else: mode = 'rb' try: return File(rfile.create_file(path, 'rb')) except IOError as error: raise ioerror(pathname, error)
def write_file(pathname, obj, config): name = pathobj.os_stringify(pathname).encode('utf-8') try: fd = rfile.create_file(name, "wb") try: # TODO: sort of defeats the purpose of # incremental encoder. fd.write(configured_stringify(obj, config).encode('utf-8')) fd.write('\n') finally: fd.close() except IOError as error: message = os.strerror(error.errno).decode('utf-8') raise OldError(u"%s: %s" % (pathobj.stringify(pathname), message)) return null
def test_readline_without_eol_at_the_end(self): fname = str(self.tmpdir.join('file_readline_without_eol_at_the_end')) for n in [ 1, 10, 97, 98, 99, 100, 101, 102, 103, 150, 196, 197, 198, 199, 200, 201, 202, 203, 204, 250 ]: s = ''.join([chr(32 + (k & 63)) for k in range(n)]) with open(fname, 'wb') as f: f.write(s) f = rfile.create_file(fname, 'r') got = f.readline() assert got == s got = f.readline() assert got == '' f.close()
def test_readline(self): fname = str(self.tmpdir.join('file_readline')) j = 0 expected = [] with open(fname, 'w') as f: for i in range(250): s = ''.join([chr(32+(k&63)) for k in range(j, j + i)]) j += 1 print >> f, s expected = open(fname).readlines() expected += ['', ''] assert len(expected) == 252 f = rfile.create_file(fname, 'r') for j in range(252): got = f.readline() assert got == expected[j] f.close()
def test_readline(self): fname = str(self.tmpdir.join('file_readline')) j = 0 expected = [] with open(fname, 'w') as f: for i in range(250): s = ''.join([chr(32 + (k & 63)) for k in range(j, j + i)]) j += 1 print >> f, s expected = open(fname).readlines() expected += ['', ''] assert len(expected) == 252 f = rfile.create_file(fname, 'r') for j in range(252): got = f.readline() assert got == expected[j] f.close()
def main(argv): if len(argv) <= 1: return 1 fd = rfile.create_file(argv[1], 'rb') try: source = fd.read() finally: fd.close() code, next_varno = parser.parse(source) program = machine.load(code) succ = machine.Success() try: program.solve(succ, Compound(MAIN, []), next_varno) except machine.Exiting as exit: return exit.status return 0
def test_explicit_enable(self, tmpdir): file = tmpdir.join('jitlog') # use rfile instead of file.open since the host python and compiled # code may use different runtime libraries (win32 visual2008 vs. # visual2019 for instance rfile = create_file(file.strpath, 'wb') fileno = rfile.fileno() with FdValidator(fileno): enable_jitlog = lambda: rjitlog.enable_jitlog(fileno) f = self.run_sample_loop(enable_jitlog) self.meta_interp(f, [10, 0]) # meta_interp calls jitlog.finish which closes the file descriptor # rfile.close() assert os.path.exists(file.strpath) with file.open('rb') as f: # check the file header assert f.read( 3) == jl.MARK_JITLOG_HEADER + jl.JITLOG_VERSION_16BIT_LE assert len(f.read()) > 0
def read_file(argv): if len(argv) < 1: raise OldError(u"too few arguments to fs.read_file()") pathname = pathobj.to_path(argv[0]) path = pathobj.os_stringify(pathname).encode('utf-8') convert = from_cstring if len(argv) > 1: for ch in as_cstring(argv[1]): if ch == 'b': convert = to_uint8array else: raise OldError(u"unknown mode string action") try: fd = rfile.create_file(path, 'rb') try: return convert(fd.read()) finally: fd.close() except IOError as error: raise ioerror(pathname, error)
def get(self): if self.stdin is None: self.stdin = rfile.create_file(self.name) return self.stdin