def rsocket_startup(): wsadata = lltype.malloc(_c.WSAData, flavor='raw', zero=True) try: res = _c.WSAStartup(0x0101, wsadata) assert res == 0 finally: lltype.free(wsadata, flavor='raw')
def _invoke(self, args): arity = len(args) tp_arity = len(self._c_fn_type._arg_types) if self._c_fn_type._is_variadic: if arity < tp_arity: runtime_error(u"Wrong number of args to fn: got " + unicode(str(arity)) + u", expected at least " + unicode(str(tp_arity))) else: if arity != tp_arity: runtime_error(u"Wrong number of args to fn: got " + unicode(str(arity)) + u", expected " + unicode(str(tp_arity))) exb, tokens = self.prep_exb(args) cd = jit.promote(self._c_fn_type.get_cd()) #fp = jit.promote(self._f_ptr) jit_ffi_call(cd, self._f_ptr, exb) ret_val = self.get_ret_val_from_buffer(exb) for x in range(len(args)): t = tokens[x] if t is not None: t.finalize_token() lltype.free(exb, flavor="raw") keepalive_until_here(args) return ret_val
def win32_fstat_llimpl(fd): handle = rwin32.get_osfhandle(fd) filetype = win32traits.GetFileType(handle) if filetype == win32traits.FILE_TYPE_CHAR: # console or LPT device return make_stat_result((win32traits._S_IFCHR, 0, 0, 0, 0, 0, 0, 0, 0, 0)) elif filetype == win32traits.FILE_TYPE_PIPE: # socket or named pipe return make_stat_result((win32traits._S_IFIFO, 0, 0, 0, 0, 0, 0, 0, 0, 0)) elif filetype == win32traits.FILE_TYPE_UNKNOWN: error = rwin32.GetLastError() if error != 0: raise WindowsError(error, "os_fstat failed") # else: unknown but valid file # normal disk file (FILE_TYPE_DISK) info = lltype.malloc(win32traits.BY_HANDLE_FILE_INFORMATION, flavor='raw', zero=True) try: res = win32traits.GetFileInformationByHandle(handle, info) if res == 0: raise WindowsError(rwin32.GetLastError(), "os_fstat failed") return by_handle_info_to_stat(info) finally: lltype.free(info, flavor='raw')
def _free(self): if tracker.DO_TRACING: ll_buf = rffi.cast(lltype.Signed, self._ll_buffer) tracker.trace_free(ll_buf) lltype.free(self._ll_buffer, flavor='raw') self.ll_buffer = lltype.nullptr(rffi.VOIDP.TO) self._ll_buffer = self.ll_buffer
def _free_buffers(self, ll_result, ll_args): if ll_result: self._free_buffer_maybe(rffi.cast(rffi.VOIDP, ll_result), self.restype) for i in range(len(self.argtypes)): argtype = self.argtypes[i] self._free_buffer_maybe(ll_args[i], argtype) lltype.free(ll_args, flavor='raw')
def _select(self, for_writing): """Returns 0 when reading/writing is possible, 1 when timing out and -1 on error.""" timeout = self.timeout if timeout <= 0.0 or self.fd == _c.INVALID_SOCKET: # blocking I/O or no socket. return 0 tv = rffi.make(_c.timeval) rffi.setintfield(tv, 'c_tv_sec', int(timeout)) rffi.setintfield(tv, 'c_tv_usec', int((timeout-int(timeout)) * 1000000)) fds = lltype.malloc(_c.fd_set.TO, flavor='raw') _c.FD_ZERO(fds) _c.FD_SET(self.fd, fds) null = lltype.nullptr(_c.fd_set.TO) if for_writing: n = _c.select(self.fd + 1, null, fds, null, tv) else: n = _c.select(self.fd + 1, fds, null, null, tv) lltype.free(fds, flavor='raw') lltype.free(tv, flavor='raw') if n < 0: return -1 if n == 0: return 1 return 0
def free_temp_buffers(self, space): for buf in self.to_free: if not we_are_translated(): buf[0] = '\00' # invalidate the buffer, so that # test_keepalive_temp_buffer can fail lltype.free(buf, flavor='raw') self.to_free = []
def __init__(self, space, fargs, fresult, ellipsis): extra = self._compute_extra_text(fargs, fresult, ellipsis) size = rffi.sizeof(rffi.VOIDP) W_CTypePtrBase.__init__(self, space, size, extra, 2, fresult, could_cast_anything=False) self.fargs = fargs self.ellipsis = bool(ellipsis) # fresult is stored in self.ctitem if not ellipsis: # Functions with '...' varargs are stored without a cif_descr # at all. The cif is computed on every call from the actual # types passed in. For all other functions, the cif_descr # is computed here. builder = CifDescrBuilder(fargs, fresult) try: builder.rawallocate(self) except OperationError, e: if not e.match(space, space.w_NotImplementedError): raise # else, eat the NotImplementedError. We will get the # exception if we see an actual call if self.cif_descr: # should not be True, but you never know lltype.free(self.cif_descr, flavor='raw') self.cif_descr = lltype.nullptr(CIF_DESCRIPTION)
def test_relocation(): from rpython.rtyper.lltypesystem import lltype, rffi from rpython.jit.backend.x86 import codebuf for target in [0x01020304, -0x05060708, 0x0102030405060708]: if target > sys.maxint: continue mc = codebuf.MachineCodeBlockWrapper() mc.CALL(ImmedLoc(target)) length = mc.get_relative_pos() buf = lltype.malloc(rffi.CCHARP.TO, length, flavor='raw') rawstart = rffi.cast(lltype.Signed, buf) if IS_X86_32: assert length == 5 assert mc.relocations == [5] expected = "\xE8" + struct.pack('<i', target - (rawstart + 5)) elif IS_X86_64: assert mc.relocations is None if 0 <= target <= 0xffffffff: assert length == 9 expected = ( "\x41\xBB\x04\x03\x02\x01" # MOV %r11, target "\x41\xFF\xD3") # CALL *%r11 elif -0x80000000 <= target < 0: assert length == 10 expected = ( "\x49\xC7\xC3\xF8\xF8\xF9\xFA" # MOV %r11, target "\x41\xFF\xD3") # CALL *%r11 else: assert length == 13 expected = ( "\x49\xBB\x08\x07\x06\x05\x04\x03\x02\x01" # MOV %r11, targ "\x41\xFF\xD3") # CALL *%r11 mc.copy_to_raw_memory(rawstart) assert ''.join([buf[i] for i in range(length)]) == expected lltype.free(buf, flavor='raw')
def read(self, size=-1): # XXX CPython uses a more delicate logic here self._check_closed() ll_file = self._ll_file if size == 0: return "" elif size < 0: # read the entire contents buf = lltype.malloc(rffi.CCHARP.TO, BASE_BUF_SIZE, flavor='raw') try: s = StringBuilder() while True: returned_size = self._fread(buf, BASE_BUF_SIZE, ll_file) returned_size = intmask(returned_size) # is between 0 and BASE_BUF_SIZE if returned_size == 0: if c_feof(ll_file): # ok, finished return s.build() raise _error(ll_file) s.append_charpsize(buf, returned_size) finally: lltype.free(buf, flavor='raw') else: # size > 0 with rffi.scoped_alloc_buffer(size) as buf: returned_size = self._fread(buf.raw, size, ll_file) returned_size = intmask(returned_size) # is between 0 and size if returned_size == 0: if not c_feof(ll_file): raise _error(ll_file) s = buf.str(returned_size) assert s is not None return s
def __del__(self): if self.ll_cif: lltype.free(self.ll_cif, flavor='raw', track_allocation=False) self.ll_cif = lltype.nullptr(FFI_CIFP.TO) if self.ll_argtypes: lltype.free(self.ll_argtypes, flavor='raw', track_allocation=False) self.ll_argtypes = lltype.nullptr(FFI_TYPE_PP.TO)
def _call(self, funcaddr, args_w): space = self.space cif_descr = self.cif_descr size = cif_descr.exchange_size mustfree_max_plus_1 = 0 buffer = lltype.malloc(rffi.CCHARP.TO, size, flavor='raw') try: for i in range(len(args_w)): data = rffi.ptradd(buffer, cif_descr.exchange_args[i]) w_obj = args_w[i] argtype = self.fargs[i] if argtype.convert_argument_from_object(data, w_obj): # argtype is a pointer type, and w_obj a list/tuple/str mustfree_max_plus_1 = i + 1 jit_libffi.jit_ffi_call(cif_descr, rffi.cast(rffi.VOIDP, funcaddr), buffer) resultdata = rffi.ptradd(buffer, cif_descr.exchange_result) w_res = self.ctitem.copy_and_convert_to_object(resultdata) finally: for i in range(mustfree_max_plus_1): argtype = self.fargs[i] if isinstance(argtype, W_CTypePointer): data = rffi.ptradd(buffer, cif_descr.exchange_args[i]) flag = get_mustfree_flag(data) if flag == 1: raw_cdata = rffi.cast(rffi.CCHARPP, data)[0] lltype.free(raw_cdata, flavor='raw') lltype.free(buffer, flavor='raw') keepalive_until_here(args_w) return w_res
def test_struct_fields(self): longsize = 4 if IS_32_BIT else 8 POINT = lltype.Struct('POINT', ('x', rffi.LONG), ('y', rffi.SHORT), ('z', rffi.VOIDP), ) y_ofs = longsize z_ofs = longsize*2 p = lltype.malloc(POINT, flavor='raw') p.x = 42 p.y = rffi.cast(rffi.SHORT, -1) p.z = rffi.cast(rffi.VOIDP, 0x1234) addr = rffi.cast(rffi.VOIDP, p) assert struct_getfield_int(types.slong, addr, 0) == 42 assert struct_getfield_int(types.sshort, addr, y_ofs) == -1 assert struct_getfield_int(types.pointer, addr, z_ofs) == 0x1234 # struct_setfield_int(types.slong, addr, 0, 43) struct_setfield_int(types.sshort, addr, y_ofs, 0x1234FFFE) # 0x1234 is masked out struct_setfield_int(types.pointer, addr, z_ofs, 0x4321) assert p.x == 43 assert p.y == -2 assert rffi.cast(rffi.LONG, p.z) == 0x4321 # lltype.free(p, flavor='raw')
def test_byval_argument(self): """ struct Point { Signed x; Signed y; }; RPY_EXPORTED Signed sum_point(struct Point p) { return p.x + p.y; } """ libfoo = CDLL(self.libfoo_name) ffi_point_struct = make_struct_ffitype_e(0, 0, [types.signed, types.signed]) ffi_point = ffi_point_struct.ffistruct sum_point = (libfoo, 'sum_point', [ffi_point], types.signed) # ARRAY = rffi.CArray(rffi.SIGNED) buf = lltype.malloc(ARRAY, 2, flavor='raw') buf[0] = 30 buf[1] = 12 adr = rffi.cast(rffi.VOIDP, buf) res = self.call(sum_point, [('arg_raw', adr)], rffi.SIGNED, jitif=["byval"]) assert res == 42 # check that we still have the ownership on the buffer assert buf[0] == 30 assert buf[1] == 12 lltype.free(buf, flavor='raw') lltype.free(ffi_point_struct, flavor='raw')
def strxfrm(space, s): "string -> string. Returns a string that behaves for cmp locale-aware." n1 = len(s) + 1 buf = lltype.malloc(rffi.CCHARP.TO, n1, flavor="raw", zero=True) s_c = rffi.str2charp(s) try: n2 = _strxfrm(buf, s_c, n1) + 1 finally: rffi.free_charp(s_c) if n2 > n1: # more space needed lltype.free(buf, flavor="raw") buf = lltype.malloc(rffi.CCHARP.TO, intmask(n2), flavor="raw", zero=True) s_c = rffi.str2charp(s) try: _strxfrm(buf, s_c, n2) finally: rffi.free_charp(s_c) val = rffi.charp2str(buf) lltype.free(buf, flavor="raw") return space.wrap(val)
def test_array_fields(self): POINT = lltype.Struct("POINT", ("x", lltype.Float), ("y", lltype.Float), ) points = lltype.malloc(rffi.CArray(POINT), 2, flavor="raw") points[0].x = 1.0 points[0].y = 2.0 points[1].x = 3.0 points[1].y = 4.0 points = rffi.cast(rffi.CArrayPtr(lltype.Char), points) assert array_getitem(types.double, 16, points, 0, 0) == 1.0 assert array_getitem(types.double, 16, points, 0, 8) == 2.0 assert array_getitem(types.double, 16, points, 1, 0) == 3.0 assert array_getitem(types.double, 16, points, 1, 8) == 4.0 # array_setitem(types.double, 16, points, 0, 0, 10.0) array_setitem(types.double, 16, points, 0, 8, 20.0) array_setitem(types.double, 16, points, 1, 0, 30.0) array_setitem(types.double, 16, points, 1, 8, 40.0) # assert array_getitem(types.double, 16, points, 0, 0) == 10.0 assert array_getitem(types.double, 16, points, 0, 8) == 20.0 assert array_getitem(types.double, 16, points, 1, 0) == 30.0 assert array_getitem(types.double, 16, points, 1, 8) == 40.0 # lltype.free(points, flavor="raw")
def listdir_llimpl(path): mask = make_listdir_mask(path) filedata = lltype.malloc(win32traits.WIN32_FIND_DATA, flavor='raw') try: result = [] hFindFile = win32traits.FindFirstFile(mask, filedata) if hFindFile == rwin32.INVALID_HANDLE_VALUE: error = rwin32.GetLastError() if error == win32traits.ERROR_FILE_NOT_FOUND: return result else: raise WindowsError(error, "FindFirstFile failed") while True: name = traits.charp2str(rffi.cast(traits.CCHARP, filedata.c_cFileName)) if not skip_listdir(name): result.append(name) if not win32traits.FindNextFile(hFindFile, filedata): break # FindNextFile sets error to ERROR_NO_MORE_FILES if # it got to the end of the directory error = rwin32.GetLastError() win32traits.FindClose(hFindFile) if error == win32traits.ERROR_NO_MORE_FILES: return result else: raise WindowsError(error, "FindNextFile failed") finally: lltype.free(filedata, flavor='raw')
def f(): s = str2charp("xxx") l_res = z(s) res = charp2str(l_res) lltype.free(l_res, flavor='raw') free_charp(s) return len(res)
def fstat(fd): if not _WIN32: with lltype.scoped_alloc(STAT_STRUCT.TO) as stresult: handle_posix_error("fstat", c_fstat(fd, stresult)) return build_stat_result(stresult) else: handle = rwin32.get_osfhandle(fd) win32traits = make_win32_traits(string_traits) filetype = win32traits.GetFileType(handle) if filetype == win32traits.FILE_TYPE_CHAR: # console or LPT device return make_stat_result((win32traits._S_IFCHR, 0, 0, 0, 0, 0, 0, 0, 0, 0)) elif filetype == win32traits.FILE_TYPE_PIPE: # socket or named pipe return make_stat_result((win32traits._S_IFIFO, 0, 0, 0, 0, 0, 0, 0, 0, 0)) elif filetype == win32traits.FILE_TYPE_UNKNOWN: error = rwin32.GetLastError_saved() if error != 0: raise WindowsError(error, "os_fstat failed") # else: unknown but valid file # normal disk file (FILE_TYPE_DISK) info = lltype.malloc(win32traits.BY_HANDLE_FILE_INFORMATION, flavor="raw", zero=True) try: res = win32traits.GetFileInformationByHandle(handle, info) if res == 0: raise WindowsError(rwin32.GetLastError_saved(), "os_fstat failed") return win32_by_handle_info_to_stat(win32traits, info) finally: lltype.free(info, flavor="raw")
def attach(ob, wr, final_list): assert ob.c_ob_refcnt >= REFCNT_FROM_PYPY p = wr() if p is not None: assert ob.c_ob_pypy_link _adr2pypy[ob.c_ob_pypy_link] = p final_list.append(ob) return p else: ob.c_ob_pypy_link = 0 if ob.c_ob_refcnt >= REFCNT_FROM_PYPY_LIGHT: ob.c_ob_refcnt -= REFCNT_FROM_PYPY_LIGHT ob.c_ob_pypy_link = 0 if ob.c_ob_refcnt == 0: lltype.free(ob, flavor='raw', track_allocation=track_allocation) else: assert ob.c_ob_refcnt >= REFCNT_FROM_PYPY assert ob.c_ob_refcnt < int(REFCNT_FROM_PYPY_LIGHT * 0.99) ob.c_ob_refcnt -= REFCNT_FROM_PYPY ob.c_ob_pypy_link = 0 if ob.c_ob_refcnt == 0: ob.c_ob_refcnt = 1 _d_list.append(ob) return None
def recvfrom(self, buffersize, flags=0): """Like recv(buffersize, flags) but also return the sender's address.""" read_bytes = -1 timeout = self._select(False) if timeout == 1: raise SocketTimeout elif timeout == 0: with rffi.scoped_alloc_buffer(buffersize) as buf: address, addr_p, addrlen_p = self._addrbuf() try: read_bytes = _c.recvfrom(self.fd, buf.raw, buffersize, flags, addr_p, addrlen_p) addrlen = rffi.cast(lltype.Signed, addrlen_p[0]) finally: lltype.free(addrlen_p, flavor='raw') address.unlock() if read_bytes >= 0: if addrlen: address.addrlen = addrlen else: address = None data = buf.str(read_bytes) return (data, address) raise self.error_handler()
def close(self): """Closes the described file. Attention! Unlike Python semantics, `close' does not return `None' upon success but `0', to be able to return an exit code for popen'ed files. The actual return value may be determined with os.WEXITSTATUS. """ res = 0 ll_file = self._ll_file if ll_file: # double close is allowed self._ll_file = lltype.nullptr(FILEP.TO) do_close = self._close2[0] try: if do_close: res = do_close(ll_file) if res == -1: errno = rposix.get_saved_errno() raise IOError(errno, os.strerror(errno)) finally: if self._setbuf: lltype.free(self._setbuf, flavor='raw') self._setbuf = lltype.nullptr(rffi.CCHARP.TO) return res
def read(self, size=-1): # XXX CPython uses a more delicate logic here ll_file = self.ll_file if not ll_file: raise ValueError("I/O operation on closed file") if size < 0: # read the entire contents buf = lltype.malloc(rffi.CCHARP.TO, BASE_BUF_SIZE, flavor='raw') try: s = StringBuilder() while True: returned_size = c_fread(buf, 1, BASE_BUF_SIZE, ll_file) returned_size = intmask(returned_size) # is between 0 and BASE_BUF_SIZE if returned_size == 0: if c_feof(ll_file): # ok, finished return s.build() raise _error(ll_file) s.append_charpsize(buf, returned_size) finally: lltype.free(buf, flavor='raw') else: raw_buf, gc_buf = rffi.alloc_buffer(size) try: returned_size = c_fread(raw_buf, 1, size, ll_file) returned_size = intmask(returned_size) # is between 0 and size if returned_size == 0: if not c_feof(ll_file): raise _error(ll_file) s = rffi.str_from_buffer(raw_buf, gc_buf, size, returned_size) finally: rffi.keep_buffer_alive_until_here(raw_buf, gc_buf) return s
def f(d): va = lltype.malloc(T, d, flavor='raw', zero=True) vb = lltype.malloc(T, d, flavor='raw', zero=True) for j in range(d): va[j] = rffi.r_int(j) vb[j] = rffi.r_int(j) i = 0 while i < d: myjitdriver.jit_merge_point() if i < 0: raise IndexError if i >= d: raise IndexError a = va[i] if i < 0: raise IndexError if i >= d: raise IndexError b = vb[i] ec = intmask(a)+intmask(b) if i < 0: raise IndexError if i >= d: raise IndexError va[i] = rffi.r_int(ec) i += 1 lltype.free(va, flavor='raw') lltype.free(vb, flavor='raw') return 0
def inflateEnd(stream): """ Free the resources associated with the inflate stream. Note that this may raise RZlibError. """ _inflateEnd(stream) lltype.free(stream, flavor='raw')
def walk_stack_from(self): curframe = lltype.malloc(WALKFRAME, flavor='raw') otherframe = lltype.malloc(WALKFRAME, flavor='raw') # Walk over all the pieces of stack. They are in a circular linked # list of structures of 7 words, the 2 first words being prev/next. # The anchor of this linked list is: anchor = llmemory.cast_ptr_to_adr(gcrootanchor) initialframedata = anchor.address[1] stackscount = 0 while initialframedata != anchor: # while we have not looped back self.walk_frames(curframe, otherframe, initialframedata) # Then proceed to the next piece of stack initialframedata = initialframedata.address[1] stackscount += 1 # # for the JIT: rpy_fastgil may contain an extra framedata rpy_fastgil = rgil.gil_fetch_fastgil().signed[0] if rpy_fastgil != 1: ll_assert(rpy_fastgil != 0, "walk_stack_from doesn't have the GIL") initialframedata = rffi.cast(llmemory.Address, rpy_fastgil) self.walk_frames(curframe, otherframe, initialframedata) stackscount += 1 # expected = rffi.stackcounter.stacks_counter if NonConstant(0): rffi.stackcounter.stacks_counter += 42 # hack to force it ll_assert(not (stackscount < expected), "non-closed stacks around") ll_assert(not (stackscount > expected), "stacks counter corruption?") lltype.free(otherframe, flavor='raw') lltype.free(curframe, flavor='raw')
def detect_floatformat(): from rpython.rtyper.lltypesystem import rffi, lltype buf = lltype.malloc(rffi.CCHARP.TO, 8, flavor='raw') rffi.cast(rffi.DOUBLEP, buf)[0] = 9006104071832581.0 packed = rffi.charpsize2str(buf, 8) if packed == "\x43\x3f\xff\x01\x02\x03\x04\x05": double_format = 'IEEE, big-endian' elif packed == "\x05\x04\x03\x02\x01\xff\x3f\x43": double_format = 'IEEE, little-endian' else: double_format = 'unknown' lltype.free(buf, flavor='raw') # buf = lltype.malloc(rffi.CCHARP.TO, 4, flavor='raw') rffi.cast(rffi.FLOATP, buf)[0] = rarithmetic.r_singlefloat(16711938.0) packed = rffi.charpsize2str(buf, 4) if packed == "\x4b\x7f\x01\x02": float_format = 'IEEE, big-endian' elif packed == "\x02\x01\x7f\x4b": float_format = 'IEEE, little-endian' else: float_format = 'unknown' lltype.free(buf, flavor='raw') return double_format, float_format
def test_follow_jump_instructions_32(): buf = lltype.malloc(rffi.CCHARP.TO, 80, flavor='raw') raw = rffi.cast(lltype.Signed, buf) mc = Fake32CodeBlockWrapper(); mc.WORD = 4; mc.relocations = [] mc.RET() mc.copy_to_raw_memory(raw) mc = Fake32CodeBlockWrapper(); mc.WORD = 4; mc.relocations = [] assert follow_jump(raw) == raw mc.JMP(imm(raw)) mc.copy_to_raw_memory(raw + 20) assert buf[20] == '\xE9' # JMP assert buf[21] == '\xE7' # -25 assert buf[22] == '\xFF' assert buf[23] == '\xFF' assert buf[24] == '\xFF' mc = Fake32CodeBlockWrapper(); mc.WORD = 4; mc.relocations = [] assert follow_jump(raw + 20) == raw mc.JMP(imm(raw)) mc.copy_to_raw_memory(raw + 40) assert buf[40] == '\xE9' # JMP assert buf[41] == '\xD3' # -45 assert buf[42] == '\xFF' assert buf[43] == '\xFF' assert buf[44] == '\xFF' assert follow_jump(raw + 40) == raw lltype.free(buf, flavor='raw')
def walk_stack_from(self): curframe = lltype.malloc(WALKFRAME, flavor='raw') otherframe = lltype.malloc(WALKFRAME, flavor='raw') # Walk over all the pieces of stack. They are in a circular linked # list of structures of 7 words, the 2 first words being prev/next. # The anchor of this linked list is: anchor = llmemory.cast_ptr_to_adr(gcrootanchor) initialframedata = anchor.address[1] stackscount = 0 while initialframedata != anchor: # while we have not looped back self.fill_initial_frame(curframe, initialframedata) # Loop over all the frames in the stack while self.walk_to_parent_frame(curframe, otherframe): swap = curframe curframe = otherframe # caller becomes callee otherframe = swap # Then proceed to the next piece of stack initialframedata = initialframedata.address[1] stackscount += 1 # expected = rffi.stackcounter.stacks_counter if NonConstant(0): rffi.stackcounter.stacks_counter += 42 # hack to force it ll_assert(not (stackscount < expected), "non-closed stacks around") ll_assert(not (stackscount > expected), "stacks counter corruption?") lltype.free(otherframe, flavor='raw') lltype.free(curframe, flavor='raw')
def delitem(self, space, i, j): if i < 0: i += self.len if i < 0: i = 0 if j < 0: j += self.len if j < 0: j = 0 if j > self.len: j = self.len if i >= j: return None oldbuffer = self.buffer self.buffer = lltype.malloc( mytype.arraytype, max(self.len - (j - i), 0), flavor='raw', add_memory_pressure=True) if i: rffi.c_memcpy( rffi.cast(rffi.VOIDP, self.buffer), rffi.cast(rffi.VOIDP, oldbuffer), i * mytype.bytes ) if j < self.len: rffi.c_memcpy( rffi.cast(rffi.VOIDP, rffi.ptradd(self.buffer, i)), rffi.cast(rffi.VOIDP, rffi.ptradd(oldbuffer, j)), (self.len - j) * mytype.bytes ) self.len -= j - i self.allocated = self.len if oldbuffer: lltype.free(oldbuffer, flavor='raw')
def ioctl_w(self, space, cmd, w_option): from rpython.rtyper.lltypesystem import rffi, lltype from rpython.rlib import rwin32 from rpython.rlib.rsocket import _c recv_ptr = lltype.malloc(rwin32.LPDWORD.TO, 1, flavor='raw') try: if cmd == _c.SIO_RCVALL: value_size = rffi.sizeof(rffi.INTP) elif cmd == _c.SIO_KEEPALIVE_VALS: value_size = rffi.sizeof(_c.tcp_keepalive) else: raise oefmt(space.w_ValueError, "invalid ioctl command %d", cmd) value_ptr = lltype.malloc(rffi.VOIDP.TO, value_size, flavor='raw') try: if cmd == _c.SIO_RCVALL: option_ptr = rffi.cast(rffi.INTP, value_ptr) option_ptr[0] = space.int_w(w_option) elif cmd == _c.SIO_KEEPALIVE_VALS: w_onoff, w_time, w_interval = space.unpackiterable(w_option, 3) option_ptr = rffi.cast(lltype.Ptr(_c.tcp_keepalive), value_ptr) option_ptr.c_onoff = space.uint_w(w_onoff) option_ptr.c_keepalivetime = space.uint_w(w_time) option_ptr.c_keepaliveinterval = space.uint_w(w_interval) res = _c.WSAIoctl( self.sock.fd, cmd, value_ptr, value_size, rffi.NULL, 0, recv_ptr, rffi.NULL, rffi.NULL) if res < 0: raise converted_error(space, rsocket.last_error()) finally: if value_ptr: lltype.free(value_ptr, flavor='raw') return space.wrap(recv_ptr[0]) finally: lltype.free(recv_ptr, flavor='raw')
def f(): from rpython.rtyper.lltypesystem import rffi alist = [A() for i in range(50)] idarray = lltype.malloc(rffi.SIGNEDP.TO, len(alist), flavor='raw') # Compute the id of all the elements of the list. The goal is # to not allocate memory, so that if the GC needs memory to # remember the ids, it will trigger some collections itself i = 0 while i < len(alist): idarray[i] = compute_unique_id(alist[i]) i += 1 j = 0 while j < 2: if j == 1: # allocate some stuff between the two iterations [A() for i in range(20)] i = 0 while i < len(alist): assert idarray[i] == compute_unique_id(alist[i]) i += 1 j += 1 lltype.free(idarray, flavor='raw') return 0
def SetValueEx(space, w_hkey, value_name, w_reserved, typ, w_value): """SetValueEx(key, value_name, reserved, type, value) - Stores data in the value field of an open registry key. key is an already open key, or any one of the predefined HKEY_* constants. value_name is a string containing the name of the value to set, or None type is an integer that specifies the type of the data. This should be one of: REG_BINARY -- Binary data in any form. REG_DWORD -- A 32-bit number. REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format. REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format. REG_EXPAND_SZ -- A null-terminated string that contains unexpanded references to environment variables (for example, %PATH%). REG_LINK -- A Unicode symbolic link. REG_MULTI_SZ -- An sequence of null-terminated strings, terminated by two null characters. Note that Python handles this termination automatically. REG_NONE -- No defined value type. REG_RESOURCE_LIST -- A device-driver resource list. REG_SZ -- A null-terminated string. reserved can be anything - zero is always passed to the API. value is a string that specifies the new value. This method can also set additional value and type information for the specified key. The key identified by the key parameter must have been opened with KEY_SET_VALUE access. To open the key, use the CreateKeyEx() or OpenKeyEx() methods. Value lengths are limited by available memory. Long values (more than 2048 bytes) should be stored as files with the filenames stored in the configuration registry. This helps the registry perform efficiently.""" hkey = hkey_w(w_hkey, space) buf, buflen = convert_to_regdata(space, w_value, typ) try: ret = rwinreg.RegSetValueEx(hkey, value_name, 0, typ, buf, buflen) finally: lltype.free(buf, flavor='raw') if ret != 0: raiseWindowsError(space, ret, 'RegSetValueEx')
def test_jit_ffi_vref(self): py.test.skip("unsupported so far") from rpython.rlib import clibffi from rpython.rlib.jit_libffi import jit_ffi_prep_cif, jit_ffi_call math_sin = intmask( ctypes.cast(ctypes.CDLL(None).sin, ctypes.c_void_p).value) math_sin = rffi.cast(rffi.VOIDP, math_sin) cd = lltype.malloc(CIF_DESCRIPTION, 1, flavor='raw') cd.abi = clibffi.FFI_DEFAULT_ABI cd.nargs = 1 cd.rtype = clibffi.cast_type_to_ffitype(rffi.DOUBLE) atypes = lltype.malloc(clibffi.FFI_TYPE_PP.TO, 1, flavor='raw') atypes[0] = clibffi.cast_type_to_ffitype(rffi.DOUBLE) cd.atypes = atypes cd.exchange_size = 64 # 64 bytes of exchange data cd.exchange_result = 24 cd.exchange_args[0] = 16 def f(): # jit_ffi_prep_cif(cd) # assert rffi.sizeof(rffi.DOUBLE) == 8 exb = lltype.malloc(rffi.DOUBLEP.TO, 8, flavor='raw') exb[2] = 1.23 jit_ffi_call(cd, math_sin, rffi.cast(rffi.CCHARP, exb)) res = exb[3] lltype.free(exb, flavor='raw') # return res # res = self.interp_operations(f, []) lltype.free(cd, flavor='raw') assert res == math.sin(1.23) lltype.free(atypes, flavor='raw')
def strtod(input): if len(input) > _INT_LIMIT: raise MemoryError end_ptr = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw') try: # note: don't use the class scoped_view_charp here, it # break some tests because this function is used by the GC ll_input, flag = rffi.get_nonmovingbuffer_final_null(input) try: result = dg_strtod(ll_input, end_ptr) endpos = (rffi.cast(lltype.Signed, end_ptr[0]) - rffi.cast(lltype.Signed, ll_input)) finally: rffi.free_nonmovingbuffer(input, ll_input, flag) finally: lltype.free(end_ptr, flavor='raw') if endpos == 0 or endpos < len(input): raise ValueError("invalid input at position %d" % (endpos, )) return result
def inet_ntop(family, packed): "packed string -> human-readable string" if family == AF_INET: srcsize = sizeof(_c.in_addr) dstsize = _c.INET_ADDRSTRLEN elif AF_INET6 is not None and family == AF_INET6: srcsize = sizeof(_c.in6_addr) dstsize = _c.INET6_ADDRSTRLEN else: raise RSocketError("unknown address family") if len(packed) != srcsize: raise ValueError("packed IP wrong length for inet_ntop") with rffi.scoped_nonmovingbuffer(packed) as srcbuf: dstbuf = mallocbuf(dstsize) try: res = _c.inet_ntop(family, rffi.cast(rffi.VOIDP, srcbuf), dstbuf, dstsize) if not res: raise last_error() return rffi.charp2str(res) finally: lltype.free(dstbuf, flavor='raw')
def _prepare_pointer_call_argument(self, w_init, cdata): space = self.space if (space.isinstance_w(w_init, space.w_list) or space.isinstance_w(w_init, space.w_tuple)): length = space.int_w(space.len(w_init)) elif space.isinstance_w(w_init, space.w_basestring): # from a string, we add the null terminator length = space.int_w(space.len(w_init)) + 1 elif self.is_file: result = self.prepare_file(w_init) if result: rffi.cast(rffi.CCHARPP, cdata)[0] = result return 2 return 0 else: return 0 itemsize = self.ctitem.size if itemsize <= 0: if isinstance(self.ctitem, ctypevoid.W_CTypeVoid): itemsize = 1 else: return 0 try: datasize = ovfcheck(length * itemsize) except OverflowError: raise OperationError( space.w_OverflowError, space.wrap("array size would overflow a ssize_t")) result = lltype.malloc(rffi.CCHARP.TO, datasize, flavor='raw', zero=True) try: self.convert_array_from_object(result, w_init) except Exception: lltype.free(result, flavor='raw') raise rffi.cast(rffi.CCHARPP, cdata)[0] = result return 1
def compile(vm): mod = vm.get_funcs_mod() (pat,),_ = vm.decode_args("S") assert isinstance(pat, Con_String) errptr = lltype.malloc(rffi.CCHARPP.TO, 1, flavor="raw") erroff = lltype.malloc(rffi.INTP.TO, 1, flavor="raw") try: cp = pcre_compile(pat.v, PCRE_DOTALL | PCRE_MULTILINE, errptr, erroff, None) if cp is None: raise Exception("XXX") finally: lltype.free(errptr, flavor="raw") lltype.free(erroff, flavor="raw") with lltype.scoped_alloc(rffi.INTP.TO, 1) as num_capsp: r = int(pcre_fullinfo(cp, None, PCRE_INFO_CAPTURECOUNT, num_capsp)) if r != 0: raise Exception("XXX") num_caps = int(num_capsp[0]) return Pattern(vm, mod.get_defn(vm, "Pattern"), cp, num_caps)
def _rehash_arenas_lists(self): # # Rehash arenas into the correct arenas_lists[i]. If # 'self.current_arena' contains an arena too, it remains there. (self.old_arenas_lists, self.arenas_lists) = (self.arenas_lists, self.old_arenas_lists) # i = 0 while i < self.max_pages_per_arena: self.arenas_lists[i] = ARENA_NULL i += 1 # i = 0 while i < self.max_pages_per_arena: arena = self.old_arenas_lists[i] while arena != ARENA_NULL: nextarena = arena.nextarena # if arena.nfreepages == arena.totalpages: # # The whole arena is empty. Free it. llarena.arena_reset(arena.base, self.arena_size, 4) llarena.arena_free(arena.base) lltype.free(arena, flavor='raw', track_allocation=False) # else: # Insert 'arena' in the correct arenas_lists[n] n = arena.nfreepages ll_assert( n < self.max_pages_per_arena, "totalpages != nfreepages >= max_pages_per_arena") arena.nextarena = self.arenas_lists[n] self.arenas_lists[n] = arena # arena = nextarena i += 1 # self.min_empty_nfreepages = 1
def _call(self, funcaddr, args_w): space = self.space cif_descr = self.cif_descr # 'self' should have been promoted here size = cif_descr.exchange_size mustfree_max_plus_1 = 0 buffer = lltype.malloc(rffi.CCHARP.TO, size, flavor='raw') try: keepalives = [None] * len(args_w) # None or strings for i in range(len(args_w)): data = rffi.ptradd(buffer, cif_descr.exchange_args[i]) w_obj = args_w[i] argtype = self.fargs[i] if argtype.convert_argument_from_object( data, w_obj, keepalives, i): # argtype is a pointer type, and w_obj a list/tuple/str mustfree_max_plus_1 = i + 1 jit_libffi.jit_ffi_call(cif_descr, rffi.cast(rffi.VOIDP, funcaddr), buffer) resultdata = rffi.ptradd(buffer, cif_descr.exchange_result) w_res = self.ctitem.copy_and_convert_to_object(resultdata) finally: for i in range(mustfree_max_plus_1): argtype = self.fargs[i] if isinstance(argtype, W_CTypePointer): data = rffi.ptradd(buffer, cif_descr.exchange_args[i]) flag = get_mustfree_flag(data) raw_cdata = rffi.cast(rffi.CCHARPP, data)[0] if flag == 1: lltype.free(raw_cdata, flavor='raw') elif flag >= 4: value = keepalives[i] assert value is not None rffi.free_nonmovingbuffer(value, raw_cdata, chr(flag)) lltype.free(buffer, flavor='raw') keepalive_until_here(args_w) return w_res
def test_pointer_as_argument(self): """#include <stdlib.h> RPY_EXPORTED Signed inc(Signed* x) { Signed oldval; if (x == NULL) return -1; oldval = *x; *x = oldval+1; return oldval; } """ libfoo = self.get_libfoo() func = (libfoo, 'inc', [types.pointer], types.signed) null = lltype.nullptr(rffi.SIGNEDP.TO) res = self.call(func, [null], rffi.SIGNED) assert res == -1 # ptr_result = lltype.malloc(rffi.SIGNEDP.TO, 1, flavor='raw') ptr_result[0] = 41 res = self.call(func, [ptr_result], rffi.SIGNED) if self.__class__ is TestLibffiCall: # the function was called only once assert res == 41 assert ptr_result[0] == 42 lltype.free(ptr_result, flavor='raw') # the test does not make sense when run with the JIT through # meta_interp, because the __del__ are not properly called (hence # we "leak" memory) del libfoo gc.collect() assert not ALLOCATED else: # the function as been called 9 times assert res == 50 assert ptr_result[0] == 51 lltype.free(ptr_result, flavor='raw')
def fcntl(space, w_fd, op, w_arg): """fcntl(fd, op, [arg]) Perform the requested operation on file descriptor fd. The operation is defined by op and is operating system dependent. These constants are available from the fcntl module. The argument arg is optional, and defaults to 0; it may be an int or a string. If arg is given as a string, the return value of fcntl is a string of that length, containing the resulting value put in the arg buffer by the operating system. If the arg given is an integer or if none is specified, the result value is an integer corresponding to the return value of the fcntl call in the C code. """ fd = space.c_filedescriptor_w(w_fd) op = rffi.cast(rffi.INT, op) # C long => C int try: arg = space.getarg_w('s#', w_arg) except OperationError as e: if not e.match(space, space.w_TypeError): raise else: ll_arg = rffi.str2charp(arg) try: rv = fcntl_str(fd, op, ll_arg) if rv < 0: raise _get_error(space, "fcntl") arg = rffi.charpsize2str(ll_arg, len(arg)) return space.newbytes(arg) finally: lltype.free(ll_arg, flavor='raw') intarg = space.int_w(w_arg) intarg = rffi.cast(rffi.INT, intarg) # C long => C int rv = fcntl_int(fd, op, intarg) if rv < 0: raise _get_error(space, "fcntl") return space.newint(rv)
def fstat(fd): if not _WIN32: with lltype.scoped_alloc(STAT_STRUCT.TO) as stresult: handle_posix_error('fstat', c_fstat(fd, stresult)) return build_stat_result(stresult) else: handle = rwin32.get_osfhandle(fd) win32traits = make_win32_traits(string_traits) filetype = win32traits.GetFileType(handle) if filetype == win32traits.FILE_TYPE_CHAR: # console or LPT device return make_stat_result((win32traits._S_IFCHR, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) elif filetype == win32traits.FILE_TYPE_PIPE: # socket or named pipe return make_stat_result((win32traits._S_IFIFO, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) elif filetype == win32traits.FILE_TYPE_UNKNOWN: error = rwin32.GetLastError_saved() if error != 0: raise WindowsError(error, "os_fstat failed") # else: unknown but valid file # normal disk file (FILE_TYPE_DISK) info = lltype.malloc(win32traits.BY_HANDLE_FILE_INFORMATION, flavor='raw', zero=True) try: res = win32traits.GetFileInformationByHandle(handle, info) if res == 0: raise WindowsError(rwin32.GetLastError_saved(), "os_fstat failed") return win32_by_handle_info_to_stat(win32traits, info) finally: lltype.free(info, flavor='raw')
def test_addr_raw_packet(): from pypy.module._socket.interp_socket import addr_as_object if not hasattr(rsocket._c, 'sockaddr_ll'): py.test.skip("posix specific test") # HACK: To get the correct interface numer of lo, which in most cases is 1, # but can be anything (i.e. 39), we need to call the libc function # if_nametoindex to get the correct index import ctypes libc = ctypes.CDLL(ctypes.util.find_library('c')) ifnum = libc.if_nametoindex('lo') c_addr_ll = lltype.malloc(rsocket._c.sockaddr_ll, flavor='raw') addrlen = rffi.sizeof(rsocket._c.sockaddr_ll) c_addr = rffi.cast(lltype.Ptr(rsocket._c.sockaddr), c_addr_ll) rffi.setintfield(c_addr_ll, 'c_sll_ifindex', ifnum) rffi.setintfield(c_addr_ll, 'c_sll_protocol', 8) rffi.setintfield(c_addr_ll, 'c_sll_pkttype', 13) rffi.setintfield(c_addr_ll, 'c_sll_hatype', 0) rffi.setintfield(c_addr_ll, 'c_sll_halen', 3) c_addr_ll.c_sll_addr[0] = 'a' c_addr_ll.c_sll_addr[1] = 'b' c_addr_ll.c_sll_addr[2] = 'c' rffi.setintfield(c_addr, 'c_sa_family', socket.AF_PACKET) # fd needs to be somehow valid s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) fd = s.fileno() w_obj = addr_as_object(rsocket.make_address(c_addr, addrlen), fd, space) lltype.free(c_addr_ll, flavor='raw') assert space.is_true( space.eq( w_obj, space.newtuple([ space.wrap('lo'), space.wrap(socket.ntohs(8)), space.wrap(13), space.wrap(False), space.wrap("abc"), ])))
def builtin_ffi_call(ctx): # parameter validation assert len(ctx.params) == 3 and \ ctx.params[0].type == 'str' and \ ctx.params[1].type == 'str' and \ ctx.params[2].type == 'array' # extract parameters libname = ctx.params[0].strvalue symname = ctx.params[1].strvalue args = ctx.params[2].arrayvalue lib = ctx.machine.space.ffi_libs[libname].lib ff = ctx.machine.space.ffi_functions[(libname, symname)] cif = ff.cif func = rdynload.dlsym(lib, rffi.str2charp(symname)) # prepare exchange exc = lltype.malloc(rffi.VOIDP.TO, cif.exchange_size, flavor='raw') # cast ao val to ffi val ptr = exc for i in range(cif.nargs): ptr = rffi.ptradd(exc, cif.exchange_args[i]) _cast_aovalue_to_ffivalue(ctx.machine.space, args[i], ff.atypes[i], ptr) # ffi call jit_libffi.jit_ffi_call(cif, func, exc) # cast ffi val back to ao val ptr = rffi.ptradd(exc, cif.exchange_result) val = _cast_ffivalue_to_aovalue(ctx.machine.space, ptr, ff.rtype) # free exc lltype.free(exc, flavor='raw') # return val ctx.tos.push(val)
def walk_stack_from(self): curframe = lltype.malloc(WALKFRAME, flavor='raw') otherframe = lltype.malloc(WALKFRAME, flavor='raw') # Walk over all the pieces of stack. They are in a circular linked # list of structures of 7 words, the 2 first words being prev/next. # The anchor of this linked list is: anchor = llmemory.cast_ptr_to_adr(gcrootanchor) initialframedata = anchor.address[1] stackscount = 0 while initialframedata != anchor: # while we have not looped back self.walk_frames(curframe, otherframe, initialframedata) # Then proceed to the next piece of stack initialframedata = initialframedata.address[1] stackscount += 1 # # for the JIT: rpy_fastgil may contain an extra framedata rpy_fastgil = rgil.gil_fetch_fastgil().signed[0] if rpy_fastgil != 1: ll_assert(rpy_fastgil != 0, "walk_stack_from doesn't have the GIL") initialframedata = rffi.cast(llmemory.Address, rpy_fastgil) # # very rare issue: initialframedata.address[0] is uninitialized # in this case, but "retaddr = callee.frame_address.address[0]" # reads it. If it happens to be exactly a valid return address # inside the C code, then bad things occur. initialframedata.address[0] = llmemory.NULL # self.walk_frames(curframe, otherframe, initialframedata) stackscount += 1 # expected = rffi.stackcounter.stacks_counter if NonConstant(0): rffi.stackcounter.stacks_counter += 42 # hack to force it ll_assert(not (stackscount < expected), "non-closed stacks around") ll_assert(not (stackscount > expected), "stacks counter corruption?") lltype.free(otherframe, flavor='raw') lltype.free(curframe, flavor='raw')
def test_blockbuildermixin(translated=True): mc = BlockBuilderMixin(translated) writtencode = [] for i in range(mc.SUBBLOCK_SIZE * 2 + 3): assert mc.get_relative_pos() == i mc.writechar(chr(i % 255)) writtencode.append(chr(i % 255)) if translated: assert mc._cursubindex == 3 assert mc._cursubblock assert mc._cursubblock.prev assert mc._cursubblock.prev.prev assert not mc._cursubblock.prev.prev.prev # for i in range(0, mc.SUBBLOCK_SIZE * 2 + 3, 2): mc.overwrite(i, chr((i + 63) % 255)) writtencode[i] = chr((i + 63) % 255) # p = lltype.malloc(rffi.CCHARP.TO, mc.SUBBLOCK_SIZE * 2 + 3, flavor='raw') addr = rffi.cast(lltype.Signed, p) mc.copy_to_raw_memory(addr) # for i in range(mc.SUBBLOCK_SIZE * 2 + 3): assert p[i] == writtencode[i] # debug._log = debug.DebugLog() try: mc._dump(addr, 'test-logname-section') log = list(debug._log) finally: debug._log = None encoded = ''.join(writtencode).encode('hex').upper() ataddr = '@%x' % addr assert log == [('test-logname-section', [('debug_print', 'SYS_EXECUTABLE', '??'), ('debug_print', 'CODE_DUMP', ataddr, '+0 ', encoded)])] lltype.free(p, flavor='raw')
def timelib_time_from_string(time_string): time_string = time_string or 'now' error = '' ll_s = rffi.str2charp(time_string) error_c = lltype.malloc(timelib_error_containerP.TO, 1, flavor='raw') ll_res = timelib_strtotime(ll_s, len(time_string), error_c, timelib_builtin_db(), tzinfo_callback) error_count = rffi.cast(lltype.Signed, error_c[0].c_error_count) if error_count: position = int(error_c[0].c_error_messages[0].c_position) message = rffi.charp2str(error_c[0].c_error_messages[0].c_message) char = error_c[0].c_error_messages[0].c_character error = "Failed to parse time string (%s) at position %s (%s): %s" % ( time_string, position, char, message) lltype.free(error_c, flavor='raw') return ll_res, error
def _setRowCount(self): if self.statementType == roci.OCI_STMT_SELECT: self.rowCount = 0 self.actualRows = -1 self.rowNum = 0 elif self.statementType in (roci.OCI_STMT_INSERT, roci.OCI_STMT_UPDATE, roci.OCI_STMT_DELETE): attrptr = lltype.malloc(rffi.CArrayPtr(roci.ub4).TO, 1, flavor='raw') try: status = roci.OCIAttrGet(self.handle, roci.OCI_HTYPE_STMT, rffi.cast(roci.dvoidp, attrptr), lltype.nullptr(roci.Ptr(roci.ub4).TO), roci.OCI_ATTR_ROW_COUNT, self.environment.errorHandle) self.environment.checkForError(status, "Cursor_SetRowCount()") self.rowCount = rffi.cast(lltype.Signed, attrptr[0]) finally: lltype.free(attrptr, flavor='raw') else: self.rowCount = -1
def _performDefine(self): # determine number of items in select-list attrptr = lltype.malloc(rffi.CArrayPtr(roci.ub4).TO, 1, flavor='raw') try: status = roci.OCIAttrGet(self.handle, roci.OCI_HTYPE_STMT, rffi.cast(roci.dvoidp, attrptr), lltype.nullptr(roci.Ptr(roci.ub4).TO), roci.OCI_ATTR_PARAM_COUNT, self.environment.errorHandle) self.environment.checkForError(status, "Cursor_PerformDefine()") numParams = attrptr[0] finally: lltype.free(attrptr, flavor='raw') self.fetchVariables = [] # define a variable for each select-item self.fetchArraySize = self.arraySize for i in range(numParams): var = interp_variable.define(self, i + 1, self.fetchArraySize) assert isinstance(var, interp_variable.W_Variable) self.fetchVariables.append(var)
def f(i): exbuf = lltype.malloc(rffi.CCHARP.TO, (len(avalues) + 2) * 16, flavor='raw') targetptr = rffi.ptradd(exbuf, 16) for avalue in unroll_avalues: TYPE = rffi.CArray(lltype.typeOf(avalue)) if i >= 9: # a guard that can fail pass rffi.cast(lltype.Ptr(TYPE), targetptr)[0] = avalue targetptr = rffi.ptradd(targetptr, 16) jit_ffi_call(cif_description, func_addr, exbuf) if rvalue is None: res = 654321 else: TYPE = rffi.CArray(lltype.typeOf(rvalue)) res = rffi.cast(lltype.Ptr(TYPE), targetptr)[0] lltype.free(exbuf, flavor='raw') if lltype.typeOf(res) is lltype.SingleFloat: res = float(res) return res
def newtest(self): memory_ptrs = [] a = PPCBuilder() for (bytes, type, values) in memory: # alloc adr = lltype.malloc(rffi.CArray(char), bytes, flavor="raw") memory_ptrs.append(adr) address = adr for i, value in enumerate(values): rffi.cast(rffi.CArrayPtr(type), adr)[i] = rffi.cast(type, value) expected = test( self, a, *[rffi.cast(lltype.Signed, m) for m in memory_ptrs]) f = a.get_assembler_function() f() for expect, type, ptr in expected: value = rffi.cast(rffi.CArrayPtr(type), ptr)[0] assert value == expect while memory_ptrs: ptr = memory_ptrs.pop() lltype.free(ptr, flavor="raw")
def test_call_time(self): libc = self.get_libc() # XXX assume time_t is long ulong = cast_type_to_ffitype(rffi.ULONG) try: ctime = libc.getpointer('time', [ffi_type_pointer], ulong) except KeyError: # This function is named differently since msvcr80 ctime = libc.getpointer('_time32', [ffi_type_pointer], ulong) ctime.push_arg(lltype.nullptr(rffi.CArray(rffi.LONG))) t0 = ctime.call(rffi.LONG) time.sleep(2) ctime.push_arg(lltype.nullptr(rffi.CArray(rffi.LONG))) t1 = ctime.call(rffi.LONG) assert t1 > t0 l_t = lltype.malloc(rffi.CArray(rffi.LONG), 1, flavor='raw') ctime.push_arg(l_t) t1 = ctime.call(rffi.LONG) assert l_t[0] == t1 lltype.free(l_t, flavor='raw') del ctime del libc assert not ALLOCATED
def _select(self, for_writing): """Returns 0 when reading/writing is possible, 1 when timing out and -1 on error.""" if self.timeout <= 0.0 or self.fd == _c.INVALID_SOCKET: # blocking I/O or no socket. return 0 pollfd = rffi.make(_c.pollfd) try: rffi.setintfield(pollfd, 'c_fd', self.fd) if for_writing: rffi.setintfield(pollfd, 'c_events', _c.POLLOUT) else: rffi.setintfield(pollfd, 'c_events', _c.POLLIN) timeout = int(self.timeout * 1000.0 + 0.5) n = _c.poll(rffi.cast(lltype.Ptr(_c.pollfdarray), pollfd), 1, timeout) finally: lltype.free(pollfd, flavor='raw') if n < 0: return -1 if n == 0: return 1 return 0
def test_collect_o_dies(self): trigger = [] rawrefcount.init(lambda: trigger.append(1)) p = W_Root(42) ob = lltype.malloc(PyObjectS, flavor='raw', zero=True) rawrefcount.create_link_pyobj(p, ob) ob.c_ob_refcnt += REFCNT_FROM_PYPY assert rawrefcount._o_list == [ob] wr_ob = weakref.ref(ob) wr_p = weakref.ref(p) del ob, p rawrefcount._collect() ob = wr_ob() assert ob is not None assert trigger == [1] assert rawrefcount.next_dead(PyObject) == ob assert rawrefcount.next_dead(PyObject) == lltype.nullptr(PyObjectS) assert rawrefcount.next_dead(PyObject) == lltype.nullptr(PyObjectS) assert rawrefcount._o_list == [] assert wr_p() is None assert ob.c_ob_refcnt == 1 # from the pending list assert ob.c_ob_pypy_link == 0 lltype.free(ob, flavor='raw')
def tuple_attach(space, py_obj, w_obj): """ Fills a newly allocated PyTupleObject with the given tuple object. The buffer must not be modified. """ items_w = space.fixedview(w_obj) l = len(items_w) p = lltype.malloc(ObjectItems, l, flavor='raw', add_memory_pressure=True) i = 0 try: while i < l: p[i] = make_ref(space, items_w[i]) i += 1 except: while i > 0: i -= 1 decref(space, p[i]) lltype.free(p, flavor='raw') raise py_tup = rffi.cast(PyTupleObject, py_obj) py_tup.c_ob_size = l py_tup.c_ob_item = p
def deflateInit(level=Z_DEFAULT_COMPRESSION, method=Z_DEFLATED, wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=Z_DEFAULT_STRATEGY): """ Allocate and return an opaque 'stream' object that can be used to compress data. """ stream = lltype.malloc(z_stream, flavor='raw', zero=True) rgc.add_memory_pressure(rffi.sizeof(z_stream)) err = _deflateInit2(stream, level, method, wbits, memLevel, strategy) if err == Z_OK: return stream else: try: if err == Z_STREAM_ERROR: raise ValueError("Invalid initialization option") else: raise RZlibError.fromstream( stream, err, "while creating compression object") finally: lltype.free(stream, flavor='raw')
def PyRun_File(space, fp, filename, start, w_globals, w_locals): """This is a simplified interface to PyRun_FileExFlags() below, leaving closeit set to 0 and flags set to NULL.""" BUF_SIZE = 8192 source = "" filename = rffi.charp2str(filename) buf = lltype.malloc(rffi.CCHARP.TO, BUF_SIZE, flavor='raw') if not is_valid_fp(fp): lltype.free(buf, flavor='raw') PyErr_SetFromErrno(space, space.w_IOError) return None try: while True: count = fread(buf, 1, BUF_SIZE, fp) count = rffi.cast(lltype.Signed, count) source += rffi.charpsize2str(buf, count) if count < BUF_SIZE: if feof(fp): break PyErr_SetFromErrno(space, space.w_IOError) finally: lltype.free(buf, flavor='raw') return run_string(space, source, filename, start, w_globals, w_locals)
def get_darwin_sysctl_signed(sysctl_name): rval_p = lltype.malloc(rffi.LONGLONGP.TO, 1, flavor='raw') try: len_p = lltype.malloc(rffi.SIZE_TP.TO, 1, flavor='raw') try: size = rffi.sizeof(rffi.LONGLONG) rval_p[0] = rffi.cast(rffi.LONGLONG, 0) len_p[0] = rffi.cast(rffi.SIZE_T, size) # XXX a hack for llhelper not being robust-enough result = sysctlbyname(sysctl_name, rffi.cast(rffi.VOIDP, rval_p), len_p, lltype.nullptr(rffi.VOIDP.TO), rffi.cast(rffi.SIZE_T, 0)) rval = 0 if (rffi.cast(lltype.Signed, result) == 0 and rffi.cast(lltype.Signed, len_p[0]) == size): rval = rffi.cast(lltype.Signed, rval_p[0]) if rffi.cast(rffi.LONGLONG, rval) != rval_p[0]: rval = 0 # overflow! return rval finally: lltype.free(len_p, flavor='raw') finally: lltype.free(rval_p, flavor='raw')
def _Pattern_match_search(vm, anchored): mod = vm.get_funcs_mod() (self, s_o, sp_o),_ = vm.decode_args(mand="!S", opt="I", self_of=Pattern) assert isinstance(self, Pattern) assert isinstance(s_o, Con_String) ovect_size = (1 + self.num_caps) * 3 ovect = lltype.malloc(rffi.INTP.TO, ovect_size, flavor="raw") if anchored: flags = PCRE_ANCHORED else: flags = 0 sp = translate_idx_obj(vm, sp_o, len(s_o.v)) with rffi.scoped_nonmovingbuffer(s_o.v) as rs: r = int(pcre_exec(self.cp, None, rs, len(s_o.v), sp, flags, ovect, ovect_size)) if r < 0: if r == PCRE_ERROR_NOMATCH: lltype.free(ovect, flavor="raw") return vm.get_builtin(BUILTIN_FAIL_OBJ) else: raise Exception("XXX") return Match(vm, mod.get_defn(vm, "Match"), ovect, self.num_caps, s_o)