def __init__(self, shape, dtype, order, strides, backstrides, storage=lltype.nullptr(RAW_STORAGE), zero=True): gcstruct = V_OBJECTSTORE flags = NPY.ARRAY_ALIGNED | NPY.ARRAY_WRITEABLE try: length = support.product_check(shape) self.size = ovfcheck(length * dtype.elsize) except OverflowError: raise oefmt(dtype.itemtype.space.w_ValueError, "array is too big.") if storage == lltype.nullptr(RAW_STORAGE): if dtype.num == NPY.OBJECT: storage = dtype.itemtype.malloc(length * dtype.elsize, zero=True) gcstruct = _create_objectstore(storage, length, dtype.elsize) else: storage = dtype.itemtype.malloc(length * dtype.elsize, zero=zero) flags |= NPY.ARRAY_OWNDATA start = calc_start(shape, strides) ConcreteArrayNotOwning.__init__(self, shape, dtype, order, strides, backstrides, storage, start=start) self.gcstruct = gcstruct if is_c_contiguous(self): flags |= NPY.ARRAY_C_CONTIGUOUS if is_f_contiguous(self): flags |= NPY.ARRAY_F_CONTIGUOUS self.flags = flags
def do_poll(self, space, timeout): from pypy.module._multiprocessing.interp_win32 import ( _PeekNamedPipe, _GetTickCount, _Sleep) from rpython.rlib import rwin32 from pypy.interpreter.error import wrap_windowserror bytes_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1, flavor='raw') try: if not _PeekNamedPipe(self.handle, rffi.NULL, 0, lltype.nullptr(rwin32.LPDWORD.TO), bytes_ptr, lltype.nullptr(rwin32.LPDWORD.TO)): raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) bytes = bytes_ptr[0] finally: lltype.free(bytes_ptr, flavor='raw') if timeout == 0.0: return bytes > 0 block = timeout < 0 if not block: # XXX does not check for overflow deadline = intmask(_GetTickCount()) + int(1000 * timeout + 0.5) else: deadline = 0 _Sleep(0) delay = 1 while True: bytes_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1, flavor='raw') try: if not _PeekNamedPipe(self.handle, rffi.NULL, 0, lltype.nullptr(rwin32.LPDWORD.TO), bytes_ptr, lltype.nullptr(rwin32.LPDWORD.TO)): raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) bytes = bytes_ptr[0] finally: lltype.free(bytes_ptr, flavor='raw') if bytes > 0: return True if not block: now = intmask(_GetTickCount()) if now > deadline: return False diff = deadline - now if delay > diff: delay = diff else: delay += 1 if delay >= 20: delay = 20 _Sleep(delay)
def set_video_mode(self, w, h, d): if not (w > 0 and h > 0): return assert d in (1, 2, 4, 8, 16, 32) if d < MINIMUM_DEPTH: d = BELOW_MINIMUM_DEPTH self.width = intmask(w) self.height = intmask(h) self.depth = intmask(d) if self.window == lltype.nullptr(RSDL.WindowPtr.TO): self.create_window_and_renderer(x=RSDL.WINDOWPOS_UNDEFINED, y=RSDL.WINDOWPOS_UNDEFINED, width=w, height=h) if self.screen_texture != lltype.nullptr(RSDL.TexturePtr.TO): RSDL.DestroyTexture(self.screen_texture) self.screen_texture = RSDL.CreateTexture( self.renderer, DEPTH_TO_PIXELFORMAT[d], RSDL.TEXTUREACCESS_STREAMING, w, h) if not self.screen_texture: print 'Could not create screen texture' raise RuntimeError(RSDL.GetError()) self.lock() if d == 16: self.bpp = 2 elif d == 32: self.bpp = 4 else: assert False self.pitch = self.width * self.bpp self.full_damage()
def _ll_dict_del(d, index): d.entries.mark_deleted(index) d.num_live_items -= 1 # clear the key and the value if they are GC pointers ENTRIES = lltype.typeOf(d.entries).TO ENTRY = ENTRIES.OF entry = d.entries[index] if ENTRIES.must_clear_key: entry.key = lltype.nullptr(ENTRY.key.TO) if ENTRIES.must_clear_value: entry.value = lltype.nullptr(ENTRY.value.TO) if d.num_live_items == 0: # Dict is now empty. Reset these fields. d.num_ever_used_items = 0 d.lookup_function_no &= FUNC_MASK elif index == d.num_ever_used_items - 1: # The last element of the ordereddict has been deleted. Instead of # simply marking the item as dead, we can safely reuse it. Since it's # also possible that there are more dead items immediately behind the # last one, we reclaim all the dead items at the end of the ordereditem # at the same point. i = d.num_ever_used_items - 2 while i >= 0 and not d.entries.valid(i): i -= 1 j = i + 1 assert j >= 0 d.num_ever_used_items = j # If the dictionary is at least 87.5% dead items, then consider shrinking # it. if d.num_live_items + DICT_INITSIZE <= len(d.entries) / 8: ll_dict_resize(d)
def run(self, executable, argv, env, stdinFount=None, stdoutDrain=None, stderrDrain=None, stdin=False, stdout=False, stderr=False): # Sixth incarnation: Now with streamcaps! # Unwrap argv. l = [] for arg in argv: bs = unwrapBytes(arg) assert bs is not None, "proven impossible" l.append(bs) argv = l # Unwrap and prep environment. d = {} for (k, v) in env.items(): d[unwrapBytes(k)] = unwrapBytes(v) packedEnv = [k + '=' + v for (k, v) in d.items()] env = d vat = currentVat.get() # Set up the list of streams and attach streamcaps. stdinSink = nullSink stdoutSource = stderrSource = emptySource streams = [] if stdin: stream = ruv.rffi.cast(ruv.stream_tp, ruv.alloc_pipe(vat.uv_loop)) streams.append(stream) wrapped = ruv.wrapStream(stream, 1) stdinSink = StreamSink(wrapped, vat) else: streams.append(nullptr(ruv.stream_t)) if stdout: stream = ruv.rffi.cast(ruv.stream_tp, ruv.alloc_pipe(vat.uv_loop)) streams.append(stream) wrapped = ruv.wrapStream(stream, 1) stdoutSource = StreamSource(wrapped, vat) else: streams.append(nullptr(ruv.stream_t)) if stderr: stream = ruv.rffi.cast(ruv.stream_tp, ruv.alloc_pipe(vat.uv_loop)) streams.append(stream) wrapped = ruv.wrapStream(stream, 1) stderrSource = StreamSource(wrapped, vat) else: streams.append(nullptr(ruv.stream_t)) try: process = ruv.allocProcess() sub = SubProcess(vat, process, argv, env, stdin=stdinSink, stdout=stdoutSource, stderr=stderrSource) vat.enqueueEvent(SpawnProcessIOEvent( vat, sub, process, executable, argv, packedEnv, streams)) return sub except ruv.UVError as uve: raise userError(u"makeProcess: Couldn't spawn process: %s" % uve.repr().decode("utf-8"))
def __init__(self, space, name, w_ctype, ptr=lltype.nullptr(rffi.CCHARP.TO), fetch_addr=lltype.nullptr(rffi.VOIDP.TO)): self.space = space self.name = name self.w_ctype = w_ctype self.ptr = ptr self.fetch_addr = fetch_addr
def __init__(self, shape, dtype, order, strides, backstrides, storage=lltype.nullptr(RAW_STORAGE), zero=True): if storage == lltype.nullptr(RAW_STORAGE): storage = dtype.itemtype.malloc(support.product(shape) * dtype.elsize, zero=zero) ConcreteArrayNotOwning.__init__(self, shape, dtype, order, strides, backstrides, storage)
def EnumKey(space, w_hkey, index): """string = EnumKey(key, index) - Enumerates subkeys of an open registry key. key is an already open key, or any one of the predefined HKEY_* constants. index is an integer that identifies the index of the key to retrieve. The function retrieves the name of one subkey each time it is called. It is typically called repeatedly until an EnvironmentError exception is raised, indicating no more values are available.""" hkey = hkey_w(w_hkey, space) null_dword = lltype.nullptr(rwin32.LPDWORD.TO) # The Windows docs claim that the max key name length is 255 # characters, plus a terminating nul character. However, # empirical testing demonstrates that it is possible to # create a 256 character key that is missing the terminating # nul. RegEnumKeyEx requires a 257 character buffer to # retrieve such a key name. with lltype.scoped_alloc(rffi.CCHARP.TO, 257) as buf: with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as retValueSize: retValueSize[0] = r_uint(257) # includes NULL terminator ret = rwinreg.RegEnumKeyEx(hkey, index, buf, retValueSize, null_dword, None, null_dword, lltype.nullptr(rwin32.PFILETIME.TO)) if ret != 0: raiseWindowsError(space, ret, 'RegEnumKeyEx') return space.wrap(rffi.charp2str(buf))
def PyBuffer_FillInfo(space, view, obj, buf, length, readonly, flags): """ Fills in a buffer-info structure correctly for an exporter that can only share a contiguous chunk of memory of "unsigned bytes" of the given length. Returns 0 on success and -1 (with raising an error) on error. This is not a complete re-implementation of the CPython API; it only provides a subset of CPython's behavior. """ if flags & PyBUF_WRITABLE and readonly: raise OperationError(space.w_ValueError, space.wrap("Object is not writable")) view.c_buf = buf view.c_len = length view.c_obj = obj Py_IncRef(space, obj) view.c_itemsize = 1 rffi.setintfield(view, "c_readonly", readonly) rffi.setintfield(view, "c_ndim", 0) view.c_format = lltype.nullptr(rffi.CCHARP.TO) view.c_shape = lltype.nullptr(Py_ssize_tP.TO) view.c_strides = lltype.nullptr(Py_ssize_tP.TO) view.c_suboffsets = lltype.nullptr(Py_ssize_tP.TO) view.c_internal = lltype.nullptr(rffi.VOIDP.TO) return 0
def unicode_attach(space, py_obj, w_obj): "Fills a newly allocated PyUnicodeObject with a unicode string" py_unicode = rffi.cast(PyUnicodeObject, py_obj) py_unicode.c_length = len(space.unicode_w(w_obj)) py_unicode.c_str = lltype.nullptr(rffi.CWCHARP.TO) py_unicode.c_hash = space.hash_w(w_obj) py_unicode.c_defenc = lltype.nullptr(PyObject.TO)
def test_AsEncodedObject(self, space, api): ptr = space.wrap('abc') errors = rffi.str2charp("strict") encoding = rffi.str2charp("hex") res = api.PyString_AsEncodedObject( ptr, encoding, errors) assert space.unwrap(res) == "616263" res = api.PyString_AsEncodedObject( ptr, encoding, lltype.nullptr(rffi.CCHARP.TO)) assert space.unwrap(res) == "616263" rffi.free_charp(encoding) encoding = rffi.str2charp("unknown_encoding") self.raises(space, api, LookupError, api.PyString_AsEncodedObject, ptr, encoding, errors) rffi.free_charp(encoding) rffi.free_charp(errors) res = api.PyString_AsEncodedObject( ptr, lltype.nullptr(rffi.CCHARP.TO), lltype.nullptr(rffi.CCHARP.TO)) assert space.unwrap(res) == "abc" self.raises(space, api, TypeError, api.PyString_AsEncodedObject, space.wrap(2), lltype.nullptr(rffi.CCHARP.TO), lltype.nullptr(rffi.CCHARP.TO) )
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 PyErr_GetExcInfo(space, ptype, pvalue, ptraceback): """---Cython extension--- Retrieve the exception info, as known from ``sys.exc_info()``. This refers to an exception that was already caught, not to an exception that was freshly raised. Returns new references for the three objects, any of which may be *NULL*. Does not modify the exception info state. .. note:: This function is not normally used by code that wants to handle exceptions. Rather, it can be used when code needs to save and restore the exception state temporarily. Use :c:func:`PyErr_SetExcInfo` to restore or clear the exception state. """ ec = space.getexecutioncontext() operror = ec.sys_exc_info() if operror: ptype[0] = make_ref(space, operror.w_type) pvalue[0] = make_ref(space, operror.get_w_value(space)) ptraceback[0] = make_ref(space, space.wrap(operror.get_traceback())) else: ptype[0] = lltype.nullptr(PyObject.TO) pvalue[0] = lltype.nullptr(PyObject.TO) ptraceback[0] = lltype.nullptr(PyObject.TO)
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 f(): lst = lltype.malloc(A, 5) return (lst[0] == lltype.nullptr(S) and lst[1] == lltype.nullptr(S) and lst[2] == lltype.nullptr(S) and lst[3] == lltype.nullptr(S) and lst[4] == lltype.nullptr(S))
def test_ascii_codec(self, space, api): s = 'abcdefg' data = rffi.str2charp(s) w_u = api.PyUnicode_DecodeASCII(data, len(s), lltype.nullptr(rffi.CCHARP.TO)) assert space.eq_w(w_u, space.wrap(u"abcdefg")) rffi.free_charp(data) s = 'abcd\xFF' data = rffi.str2charp(s) self.raises(space, api, UnicodeDecodeError, api.PyUnicode_DecodeASCII, data, len(s), lltype.nullptr(rffi.CCHARP.TO)) rffi.free_charp(data) uni = u'abcdefg' data = rffi.unicode2wcharp(uni) w_s = api.PyUnicode_EncodeASCII(data, len(uni), lltype.nullptr(rffi.CCHARP.TO)) assert space.eq_w(space.wrap("abcdefg"), w_s) rffi.free_wcharp(data) u = u'äbcdéfg' data = rffi.unicode2wcharp(u) w_s = api.PyUnicode_EncodeASCII(data, len(u), lltype.nullptr(rffi.CCHARP.TO)) self.raises(space, api, UnicodeEncodeError, api.PyUnicode_EncodeASCII, data, len(u), lltype.nullptr(rffi.CCHARP.TO)) rffi.free_wcharp(data)
def set_video_mode(self, w, h, d): if not (w > 0 and h > 0): return assert d in [1, 2, 4, 8, 16, 32] if d < MINIMUM_DEPTH: d = MINIMUM_DEPTH self.width = intmask(w) self.height = intmask(h) self.depth = intmask(d) if self.window == lltype.nullptr(RSDL.WindowPtr.TO): self.create_window_and_renderer(x=RSDL.WINDOWPOS_UNDEFINED, y=RSDL.WINDOWPOS_UNDEFINED, width=w, height=h) if self.screen_texture != lltype.nullptr(RSDL.TexturePtr.TO): RSDL.DestroyTexture(self.screen_texture) if self.screen_surface != lltype.nullptr(RSDL.Surface): RSDL.FreeSurface(self.screen_surface) self.has_surface = True self.screen_texture = RSDL.CreateTexture( self.renderer, RSDL.PIXELFORMAT_ARGB8888, RSDL.TEXTUREACCESS_STREAMING, w, h) if not self.screen_texture: print "Could not create screen texture" raise RuntimeError(RSDL.GetError()) self.screen_surface = RSDL.CreateRGBSurface(0, w, h, d, 0, 0, 0, 0) assert self.screen_surface, RSDL.GetError() self.bpp = intmask(self.screen_surface.c_format.c_BytesPerPixel) if d == MINIMUM_DEPTH: self.set_squeak_colormap(self.screen_surface) self.pitch = self.width * self.bpp
def __init__(self, space): self.space = space self.reset() self.programname = lltype.nullptr(rffi.CCHARP.TO) self.version = lltype.nullptr(rffi.CCHARP.TO) pyobj_dealloc_action = PyObjDeallocAction(space) self.dealloc_trigger = lambda: pyobj_dealloc_action.fire()
def memmove(space, w_dest, w_src, n): if n < 0: raise oefmt(space.w_ValueError, "negative size") # cases... src_buf = None src_data = lltype.nullptr(rffi.CCHARP.TO) if isinstance(w_src, cdataobj.W_CData): src_data = unsafe_escaping_ptr_for_ptr_or_array(w_src) src_is_ptr = True else: src_buf = _fetch_as_read_buffer(space, w_src) try: src_data = src_buf.get_raw_address() src_is_ptr = True except ValueError: src_is_ptr = False if src_is_ptr: src_string = None else: if n == src_buf.getlength(): src_string = src_buf.as_str() else: src_string = src_buf.getslice(0, n, 1, n) dest_buf = None dest_data = lltype.nullptr(rffi.CCHARP.TO) if isinstance(w_dest, cdataobj.W_CData): dest_data = unsafe_escaping_ptr_for_ptr_or_array(w_dest) dest_is_ptr = True else: dest_buf = _fetch_as_write_buffer(space, w_dest) try: dest_data = dest_buf.get_raw_address() dest_is_ptr = True except ValueError: dest_is_ptr = False if dest_is_ptr: if src_is_ptr: c_memmove(dest_data, src_data, rffi.cast(rffi.SIZE_T, n)) else: copy_string_to_raw(llstr(src_string), dest_data, 0, n) else: # nowadays this case should be rare or impossible: as far as # I know, all common types implementing the *writable* buffer # interface now support get_raw_address() if src_is_ptr: for i in range(n): dest_buf.setitem(i, src_data[i]) else: for i in range(n): dest_buf.setitem(i, src_string[i]) keepalive_until_here(src_buf) keepalive_until_here(dest_buf) keepalive_until_here(w_src) keepalive_until_here(w_dest)
def init_typeobject(space): # Probably a hack space.model.typeorder[W_PyCTypeObject] = [(W_PyCTypeObject, None), (W_TypeObject, None), (W_Root, None)] make_typedescr(space.w_type.instancetypedef, basestruct=PyTypeObject, alloc=type_alloc, attach=type_attach, realize=type_realize, dealloc=type_dealloc) # some types are difficult to create because of cycles. # - object.ob_type = type # - type.ob_type = type # - tuple.ob_type = type # - type.tp_base = object # - tuple.tp_base = object # - type.tp_bases is a tuple # - object.tp_bases is a tuple # - tuple.tp_bases is a tuple # insert null placeholders to please create_ref() track_reference(space, lltype.nullptr(PyObject.TO), space.w_type) track_reference(space, lltype.nullptr(PyObject.TO), space.w_object) track_reference(space, lltype.nullptr(PyObject.TO), space.w_tuple) track_reference(space, lltype.nullptr(PyObject.TO), space.w_str) # create the objects py_type = create_ref(space, space.w_type) py_object = create_ref(space, space.w_object) py_tuple = create_ref(space, space.w_tuple) py_str = create_ref(space, space.w_str) # form cycles pto_type = rffi.cast(PyTypeObjectPtr, py_type) py_type.c_ob_type = pto_type py_object.c_ob_type = pto_type py_tuple.c_ob_type = pto_type pto_object = rffi.cast(PyTypeObjectPtr, py_object) pto_type.c_tp_base = pto_object pto_tuple = rffi.cast(PyTypeObjectPtr, py_tuple) pto_tuple.c_tp_base = pto_object pto_type.c_tp_bases.c_ob_type = pto_tuple pto_object.c_tp_bases.c_ob_type = pto_tuple pto_tuple.c_tp_bases.c_ob_type = pto_tuple for typ in (py_type, py_object, py_tuple, py_str): heaptype = rffi.cast(PyHeapTypeObject, typ) heaptype.c_ht_name.c_ob_type = pto_type # Restore the mapping track_reference(space, py_type, space.w_type, replace=True) track_reference(space, py_object, space.w_object, replace=True) track_reference(space, py_tuple, space.w_tuple, replace=True) track_reference(space, py_str, space.w_str, replace=True)
def select(space, w_iwtd, w_owtd, w_ewtd, w_timeout): """Wait until one or more file descriptors are ready for some kind of I/O. The first three arguments are sequences of file descriptors to be waited for: rlist -- wait until ready for reading wlist -- wait until ready for writing xlist -- wait for an ``exceptional condition'' If only one kind of condition is required, pass [] for the other lists. A file descriptor is either a socket or file object, or a small integer gotten from a fileno() method call on one of those. The optional 4th argument specifies a timeout in seconds; it may be a floating point number to specify fractions of seconds. If it is absent or None, the call will never time out. The return value is a tuple of three lists corresponding to the first three arguments; each contains the subset of the corresponding file descriptors that are ready. *** IMPORTANT NOTICE *** On Windows, only sockets are supported; on Unix, all file descriptors. """ iwtd_w = space.listview(w_iwtd) owtd_w = space.listview(w_owtd) ewtd_w = space.listview(w_ewtd) if space.is_w(w_timeout, space.w_None): timeout = -1.0 else: timeout = space.float_w(w_timeout) ll_inl = lltype.nullptr(_c.fd_set.TO) ll_outl = lltype.nullptr(_c.fd_set.TO) ll_errl = lltype.nullptr(_c.fd_set.TO) ll_timeval = lltype.nullptr(_c.timeval) try: if len(iwtd_w) > 0: ll_inl = lltype.malloc(_c.fd_set.TO, flavor='raw') if len(owtd_w) > 0: ll_outl = lltype.malloc(_c.fd_set.TO, flavor='raw') if len(ewtd_w) > 0: ll_errl = lltype.malloc(_c.fd_set.TO, flavor='raw') if timeout >= 0.0: ll_timeval = rffi.make(_c.timeval) i = int(timeout) rffi.setintfield(ll_timeval, 'c_tv_sec', i) rffi.setintfield(ll_timeval, 'c_tv_usec', int((timeout-i)*1000000)) # Call this as a separate helper to avoid a large piece of code # in try:finally:. Needed for calling further _always_inline_ # helpers like _build_fd_set(). return _call_select(space, iwtd_w, owtd_w, ewtd_w, ll_inl, ll_outl, ll_errl, ll_timeval) finally: if ll_timeval: lltype.free(ll_timeval, flavor='raw') if ll_errl: lltype.free(ll_errl, flavor='raw') if ll_outl: lltype.free(ll_outl, flavor='raw') if ll_inl: lltype.free(ll_inl, flavor='raw')
def __init__(self, shape, dtype, order, strides, backstrides, storage=lltype.nullptr(RAW_STORAGE)): null_storage = lltype.nullptr(RAW_STORAGE) ConcreteArrayNotOwning.__init__(self, shape, dtype, order, strides, backstrides, null_storage) if storage == lltype.nullptr(RAW_STORAGE): self.storage = dtype.itemtype.malloc(self.size) else: self.storage = storage
def __del__(self): AbstractFuncPtr.__del__(self) if self.ll_closure: closureHeap.free(self.ll_closure) self.ll_closure = lltype.nullptr(FFI_CLOSUREP.TO) if self.ll_userdata: lltype.free(self.ll_userdata, flavor='raw', track_allocation=False) self.ll_userdata = lltype.nullptr(USERDATA_P.TO)
def flip(self, force=False): if self._defer_updates and not force: return assert RSDL.UpdateTexture(self.screen_texture, lltype.nullptr(RSDL.Rect), self.screen_surface.c_pixels, self.screen_surface.c_pitch) \ == 0, RSDL.GetError() assert RSDL.RenderCopy(self.renderer, self.screen_texture, lltype.nullptr(RSDL.Rect), lltype.nullptr(RSDL.Rect)) \ == 0, RSDL.GetError() RSDL.RenderPresent(self.renderer)
def test_malloc_fixedsize_no_cleanup(self): p = self.malloc(S) import pytest #ensure the memory is uninitialized with pytest.raises(lltype.UninitializedMemoryAccess): x1 = p.x #ensure all the ptr fields are zeroed assert p.prev == lltype.nullptr(S) assert p.next == lltype.nullptr(S)
def test_malloc_struct_of_ptr_struct(self): S3 = lltype.GcForwardReference() S3.become(lltype.GcStruct('S3', ('gcptr_struct', S), ('prev', lltype.Ptr(S)), ('next', lltype.Ptr(S)))) s3 = self.malloc(S3) assert s3.gcptr_struct.prev == lltype.nullptr(S) assert s3.gcptr_struct.next == lltype.nullptr(S)
def test_malloc_array_of_ptr_struct(self): ARR_OF_PTR_STRUCT = lltype.GcArray(lltype.Ptr(S)) arr_of_ptr_struct = self.malloc(ARR_OF_PTR_STRUCT,5) for i in range(5): assert arr_of_ptr_struct[i] == lltype.nullptr(S) assert arr_of_ptr_struct[i] == lltype.nullptr(S) arr_of_ptr_struct[i] = self.malloc(S) assert arr_of_ptr_struct[i].prev == lltype.nullptr(S) assert arr_of_ptr_struct[i].next == lltype.nullptr(S)
def select(inl, outl, excl, timeout=-1.0): nfds = 0 if inl: ll_inl = lltype.malloc(_c.fd_set.TO, flavor='raw') _c.FD_ZERO(ll_inl) for i in inl: _c.FD_SET(i, ll_inl) if i > nfds: nfds = i else: ll_inl = lltype.nullptr(_c.fd_set.TO) if outl: ll_outl = lltype.malloc(_c.fd_set.TO, flavor='raw') _c.FD_ZERO(ll_outl) for i in outl: _c.FD_SET(i, ll_outl) if i > nfds: nfds = i else: ll_outl = lltype.nullptr(_c.fd_set.TO) if excl: ll_excl = lltype.malloc(_c.fd_set.TO, flavor='raw') _c.FD_ZERO(ll_excl) for i in excl: _c.FD_SET(i, ll_excl) if i > nfds: nfds = i else: ll_excl = lltype.nullptr(_c.fd_set.TO) if timeout != -1.0: ll_timeval = rffi.make(_c.timeval) rffi.setintfield(ll_timeval, 'c_tv_sec', int(timeout)) rffi.setintfield(ll_timeval, 'c_tv_usec', int((timeout-int(timeout)) * 1000000)) else: ll_timeval = lltype.nullptr(_c.timeval) try: res = _c.select(nfds + 1, ll_inl, ll_outl, ll_excl, ll_timeval) if res == -1: raise SelectError(_c.geterrno()) if res == 0: return ([], [], []) else: return ( [i for i in inl if _c.FD_ISSET(i, ll_inl)], [i for i in outl if _c.FD_ISSET(i, ll_outl)], [i for i in excl if _c.FD_ISSET(i, ll_excl)]) finally: if ll_inl: lltype.free(ll_inl, flavor='raw') if ll_outl: lltype.free(ll_outl, flavor='raw') if ll_excl: lltype.free(ll_excl, flavor='raw') if ll_timeval: lltype.free(ll_timeval, flavor='raw')
def _strftime(interp, is_gmt, format_string, timestamp): offset = lltype.nullptr(timelib.timelib_time_offset.TO) ta = lltype.malloc(timelib.tm, flavor='raw', zero=True) timelib_time = timelib.timelib_time_ctor() if is_gmt: timelib_timezone = lltype.nullptr(timelib.timelib_tzinfo.TO) timelib.timelib_unixtime2gmt(timelib_time, timestamp) else: timelib_timezone = interp.get_default_timezone("getdate").timelib_timezone timelib_time.c_tz_info = timelib_timezone timelib_time.c_zone_type = timelib.TIMELIB_ZONETYPE_ID timelib.timelib_unixtime2local(timelib_time, timestamp) ta.c_tm_sec = rffi.cast(rffi.INT, timelib_time.c_s) ta.c_tm_min = rffi.cast(rffi.INT, timelib_time.c_i) ta.c_tm_hour = rffi.cast(rffi.INT, timelib_time.c_h) ta.c_tm_mday = rffi.cast(rffi.INT, timelib_time.c_d) ta.c_tm_mon = rffi.cast(rffi.INT, timelib_time.c_m - 1) ta.c_tm_year = rffi.cast(rffi.INT, timelib_time.c_y - 1900) ta.c_tm_wday = rffi.cast(rffi.INT, timelib.timelib_day_of_week( timelib_time.c_y, timelib_time.c_m, timelib_time.c_d )) ta.c_tm_yday = rffi.cast(rffi.INT, timelib.timelib_day_of_year( timelib_time.c_y, timelib_time.c_m, timelib_time.c_d )) if is_gmt: ta.c_tm_isdst = rffi.cast(rffi.INT, 0) ta.c_tm_gmtoff = rffi.cast(lltype.Signed, 0) ta.c_tm_zone = rffi.str2charp("GMT") else: offset = timelib.timelib_get_time_zone_info(timestamp, timelib_timezone) ta.c_tm_isdst = rffi.cast(rffi.INT, offset.c_is_dst) ta.c_tm_gmtoff = rffi.cast(lltype.Signed, offset.c_offset) ta.c_tm_zone = offset.c_abbr # stolen from PyPy i = 1024 while True: outbuf = lltype.malloc(rffi.CCHARP.TO, i, flavor='raw') try: buflen = timelib.c_strftime(outbuf, i, format_string, ta) if buflen > 0 or i >= 256 * len(format_string): return rffi.charp2strn(outbuf, intmask(buflen)) finally: timelib.timelib_time_dtor(timelib_time) lltype.free(outbuf, flavor='raw') if offset: timelib.timelib_time_offset_dtor(offset) i += i
def test_create_link_pyobj(self): p = W_Root(42) ob = lltype.malloc(PyObjectS, flavor='raw', zero=True) assert rawrefcount.from_obj(PyObject, p) == lltype.nullptr(PyObjectS) assert rawrefcount.to_obj(W_Root, ob) == None rawrefcount.create_link_pyobj(p, ob) assert ob.c_ob_refcnt == 0 ob.c_ob_refcnt += REFCNT_FROM_PYPY assert rawrefcount.from_obj(PyObject, p) == lltype.nullptr(PyObjectS) assert rawrefcount.to_obj(W_Root, ob) == p lltype.free(ob, flavor='raw')
def f(): return external(lltype.nullptr(T.TO))
class CifDescrBuilder(object): rawmem = lltype.nullptr(rffi.CCHARP.TO) def __init__(self, fargs, fresult): self.fargs = fargs self.fresult = fresult def fb_alloc(self, size): size = llmemory.raw_malloc_usage(size) if not self.bufferp: self.nb_bytes += size return lltype.nullptr(rffi.CCHARP.TO) else: result = self.bufferp self.bufferp = rffi.ptradd(result, size) return result def fb_fill_type(self, ctype, is_result_type): return ctype._get_ffi_type(self, is_result_type) def fb_struct_ffi_type(self, ctype, is_result_type=False): # We can't pass a struct that was completed by verify(). # Issue: assume verify() is given "struct { long b; ...; }". # Then it will complete it in the same way whether it is actually # "struct { long a, b; }" or "struct { double a; long b; }". # But on 64-bit UNIX, these two structs are passed by value # differently: e.g. on x86-64, "b" ends up in register "rsi" in # the first case and "rdi" in the second case. # # Another reason for 'custom_field_pos' would be anonymous # nested structures: we lost the information about having it # here, so better safe (and forbid it) than sorry (and maybe # crash). space = self.space ctype.force_lazy_struct() if ctype._custom_field_pos: # these NotImplementedErrors may be caught and ignored until # a real call is made to a function of this type place = "return value" if is_result_type else "argument" raise oefmt( space.w_NotImplementedError, "ctype '%s' not supported as %s (it is a struct declared " "with \"...;\", but the C calling convention may depend " "on the missing fields)", ctype.name, place) # walk the fields, expanding arrays into repetitions; first, # only count how many flattened fields there are nflat = 0 for i, cf in enumerate(ctype._fields_list): if cf.is_bitfield(): place = "return value" if is_result_type else "argument" raise oefmt( space.w_NotImplementedError, "ctype '%s' not supported as %s" " (it is a struct with bit fields)", ctype.name, place) flat = 1 ct = cf.ctype while isinstance(ct, ctypearray.W_CTypeArray): flat *= ct.length ct = ct.ctitem if flat <= 0: place = "return value" if is_result_type else "argument" raise oefmt( space.w_NotImplementedError, "ctype '%s' not supported as %s (it is a struct" " with a zero-length array)", ctype.name, place) nflat += flat if USE_C_LIBFFI_MSVC and is_result_type: # MSVC returns small structures in registers. Pretend int32 or # int64 return type. This is needed as a workaround for what # is really a bug of libffi_msvc seen as an independent library # (ctypes has a similar workaround). if ctype.size <= 4: return clibffi.ffi_type_sint32 if ctype.size <= 8: return clibffi.ffi_type_sint64 # allocate an array of (nflat + 1) ffi_types elements = self.fb_alloc(rffi.sizeof(FFI_TYPE_P) * (nflat + 1)) elements = rffi.cast(FFI_TYPE_PP, elements) # fill it with the ffi types of the fields nflat = 0 for i, cf in enumerate(ctype._fields_list): flat = 1 ct = cf.ctype while isinstance(ct, ctypearray.W_CTypeArray): flat *= ct.length ct = ct.ctitem ffi_subtype = self.fb_fill_type(ct, False) if elements: for j in range(flat): elements[nflat] = ffi_subtype nflat += 1 # zero-terminate the array if elements: elements[nflat] = lltype.nullptr(FFI_TYPE_P.TO) # allocate and fill an ffi_type for the struct itself ffistruct = self.fb_alloc(rffi.sizeof(FFI_TYPE)) ffistruct = rffi.cast(FFI_TYPE_P, ffistruct) if ffistruct: rffi.setintfield(ffistruct, 'c_size', ctype.size) rffi.setintfield(ffistruct, 'c_alignment', ctype.alignof()) rffi.setintfield(ffistruct, 'c_type', clibffi.FFI_TYPE_STRUCT) ffistruct.c_elements = elements return ffistruct def fb_build(self): # Build a CIF_DESCRIPTION. Actually this computes the size and # allocates a larger amount of data. It starts with a # CIF_DESCRIPTION and continues with data needed for the CIF: # # - the argument types, as an array of 'ffi_type *'. # # - optionally, the result's and the arguments' ffi type data # (this is used only for 'struct' ffi types; in other cases the # 'ffi_type *' just points to static data like 'ffi_type_sint32'). # nargs = len(self.fargs) # start with a cif_description (cif and exchange_* fields) self.fb_alloc(llmemory.sizeof(CIF_DESCRIPTION, nargs)) # next comes an array of 'ffi_type*', one per argument atypes = self.fb_alloc(rffi.sizeof(FFI_TYPE_P) * nargs) self.atypes = rffi.cast(FFI_TYPE_PP, atypes) # next comes the result type data self.rtype = self.fb_fill_type(self.fresult, True) # next comes each argument's type data for i, farg in enumerate(self.fargs): atype = self.fb_fill_type(farg, False) if self.atypes: self.atypes[i] = atype def align_arg(self, n): return (n + 7) & ~7 def fb_build_exchange(self, cif_descr): nargs = len(self.fargs) # first, enough room for an array of 'nargs' pointers exchange_offset = rffi.sizeof(rffi.CCHARP) * nargs exchange_offset = self.align_arg(exchange_offset) cif_descr.exchange_result = exchange_offset # then enough room for the result, rounded up to sizeof(ffi_arg) exchange_offset += max(rffi.getintfield(self.rtype, 'c_size'), SIZE_OF_FFI_ARG) # loop over args for i, farg in enumerate(self.fargs): if isinstance(farg, W_CTypePointer): exchange_offset += 1 # for the "must free" flag exchange_offset = self.align_arg(exchange_offset) cif_descr.exchange_args[i] = exchange_offset exchange_offset += rffi.getintfield(self.atypes[i], 'c_size') # store the exchange data size cif_descr.exchange_size = exchange_offset def fb_extra_fields(self, cif_descr): cif_descr.abi = clibffi.FFI_DEFAULT_ABI # XXX cif_descr.nargs = len(self.fargs) cif_descr.rtype = self.rtype cif_descr.atypes = self.atypes @jit.dont_look_inside def rawallocate(self, ctypefunc): space = ctypefunc.space self.space = space # compute the total size needed in the CIF_DESCRIPTION buffer self.nb_bytes = 0 self.bufferp = lltype.nullptr(rffi.CCHARP.TO) self.fb_build() # allocate the buffer if we_are_translated(): rawmem = lltype.malloc(rffi.CCHARP.TO, self.nb_bytes, flavor='raw') rawmem = rffi.cast(CIF_DESCRIPTION_P, rawmem) else: # gross overestimation of the length below, but too bad rawmem = lltype.malloc(CIF_DESCRIPTION_P.TO, self.nb_bytes, flavor='raw') # the buffer is automatically managed from the W_CTypeFunc instance ctypefunc.cif_descr = rawmem # call again fb_build() to really build the libffi data structures self.bufferp = rffi.cast(rffi.CCHARP, rawmem) self.fb_build() assert self.bufferp == rffi.ptradd(rffi.cast(rffi.CCHARP, rawmem), self.nb_bytes) # fill in the 'exchange_*' fields self.fb_build_exchange(rawmem) # fill in the extra fields self.fb_extra_fields(rawmem) # call libffi's ffi_prep_cif() function res = jit_libffi.jit_ffi_prep_cif(rawmem) if res != clibffi.FFI_OK: raise OperationError( space.w_SystemError, space.wrap("libffi failed to build this function type"))
def g(n): if n > 100: raise FooError(n) return lltype.nullptr(llmemory.GCREF.TO)
W__StructDescr.typedef = TypeDef( '_StructDescr', __new__ = interp2app(descr_new_structdescr), ffitype = interp_attrproperty('w_ffitype', W__StructDescr), define_fields = interp2app(W__StructDescr.define_fields), allocate = interp2app(W__StructDescr.allocate), fromaddress = interp2app(W__StructDescr.fromaddress), ) # ============================================================================== NULL = lltype.nullptr(rffi.VOIDP.TO) class W__StructInstance(W_Root): _immutable_fields_ = ['structdescr', 'rawmem'] def __init__(self, structdescr, allocate=True, autofree=True, rawmem=NULL): self.structdescr = structdescr self.autofree = autofree if allocate: assert not rawmem assert autofree size = structdescr.w_ffitype.sizeof() self.rawmem = lltype.malloc(rffi.VOIDP.TO, size, flavor='raw', zero=True, add_memory_pressure=True) else:
def fb_struct_ffi_type(self, ctype, is_result_type=False): # We can't pass a struct that was completed by verify(). # Issue: assume verify() is given "struct { long b; ...; }". # Then it will complete it in the same way whether it is actually # "struct { long a, b; }" or "struct { double a; long b; }". # But on 64-bit UNIX, these two structs are passed by value # differently: e.g. on x86-64, "b" ends up in register "rsi" in # the first case and "rdi" in the second case. # # Another reason for 'custom_field_pos' would be anonymous # nested structures: we lost the information about having it # here, so better safe (and forbid it) than sorry (and maybe # crash). space = self.space ctype.force_lazy_struct() if ctype._custom_field_pos: # these NotImplementedErrors may be caught and ignored until # a real call is made to a function of this type place = "return value" if is_result_type else "argument" raise oefmt( space.w_NotImplementedError, "ctype '%s' not supported as %s (it is a struct declared " "with \"...;\", but the C calling convention may depend " "on the missing fields)", ctype.name, place) # walk the fields, expanding arrays into repetitions; first, # only count how many flattened fields there are nflat = 0 for i, cf in enumerate(ctype._fields_list): if cf.is_bitfield(): place = "return value" if is_result_type else "argument" raise oefmt( space.w_NotImplementedError, "ctype '%s' not supported as %s" " (it is a struct with bit fields)", ctype.name, place) flat = 1 ct = cf.ctype while isinstance(ct, ctypearray.W_CTypeArray): flat *= ct.length ct = ct.ctitem if flat <= 0: place = "return value" if is_result_type else "argument" raise oefmt( space.w_NotImplementedError, "ctype '%s' not supported as %s (it is a struct" " with a zero-length array)", ctype.name, place) nflat += flat if USE_C_LIBFFI_MSVC and is_result_type: # MSVC returns small structures in registers. Pretend int32 or # int64 return type. This is needed as a workaround for what # is really a bug of libffi_msvc seen as an independent library # (ctypes has a similar workaround). if ctype.size <= 4: return clibffi.ffi_type_sint32 if ctype.size <= 8: return clibffi.ffi_type_sint64 # allocate an array of (nflat + 1) ffi_types elements = self.fb_alloc(rffi.sizeof(FFI_TYPE_P) * (nflat + 1)) elements = rffi.cast(FFI_TYPE_PP, elements) # fill it with the ffi types of the fields nflat = 0 for i, cf in enumerate(ctype._fields_list): flat = 1 ct = cf.ctype while isinstance(ct, ctypearray.W_CTypeArray): flat *= ct.length ct = ct.ctitem ffi_subtype = self.fb_fill_type(ct, False) if elements: for j in range(flat): elements[nflat] = ffi_subtype nflat += 1 # zero-terminate the array if elements: elements[nflat] = lltype.nullptr(FFI_TYPE_P.TO) # allocate and fill an ffi_type for the struct itself ffistruct = self.fb_alloc(rffi.sizeof(FFI_TYPE)) ffistruct = rffi.cast(FFI_TYPE_P, ffistruct) if ffistruct: rffi.setintfield(ffistruct, 'c_size', ctype.size) rffi.setintfield(ffistruct, 'c_alignment', ctype.alignof()) rffi.setintfield(ffistruct, 'c_type', clibffi.FFI_TYPE_STRUCT) ffistruct.c_elements = elements return ffistruct
def descr_new(space, w_subtype, w_user, w_password, w_dsn, min, max, increment, w_connectiontype=None, threaded=False, getmode=roci.OCI_SPOOL_ATTRVAL_NOWAIT, events=False, homogeneous=True): self = space.allocate_instance(W_SessionPool, w_subtype) W_SessionPool.__init__(self) if w_connectiontype is not None: if not space.is_true( space.issubtype(w_connectiontype, get(space).w_Connection)): raise OperationError( interp_error.get(space).w_ProgrammingError, space.wrap( "connectiontype must be a subclass of Connection")) self.w_connectionType = w_connectiontype else: self.w_connectionType = get(space).w_Connection self.w_username = w_user self.w_password = w_password self.w_tnsentry = w_dsn self.minSessions = min self.maxSessions = max self.sessionIncrement = increment self.homogeneous = homogeneous # set up the environment self.environment = interp_environ.Environment.create( space, threaded, events) # create the session pool handle handleptr = lltype.malloc(rffi.CArrayPtr(roci.OCIServer).TO, 1, flavor='raw') try: status = roci.OCIHandleAlloc( self.environment.handle, handleptr, roci.OCI_HTYPE_SPOOL, 0, lltype.nullptr(rffi.CArray(roci.dvoidp))) self.environment.checkForError( status, "SessionPool_New(): allocate handle") self.handle = handleptr[0] finally: lltype.free(handleptr, flavor='raw') # prepare pool mode poolMode = roci.OCI_SPC_STMTCACHE if self.homogeneous: poolMode |= roci.OCI_SPC_HOMOGENEOUS # create the session pool user_buf = config.StringBuffer() user_buf.fill(space, self.w_username) password_buf = config.StringBuffer() password_buf.fill(space, self.w_password) dsn_buf = config.StringBuffer() dsn_buf.fill(space, self.w_tnsentry) poolnameptr = lltype.malloc(rffi.CArrayPtr(roci.oratext).TO, 1, flavor='raw') poolnamelenptr = lltype.malloc(rffi.CArrayPtr(roci.ub4).TO, 1, flavor='raw') try: status = roci.OCISessionPoolCreate( self.environment.handle, self.environment.errorHandle, self.handle, poolnameptr, poolnamelenptr, dsn_buf.ptr, dsn_buf.size, min, max, increment, user_buf.ptr, user_buf.size, password_buf.ptr, password_buf.size, poolMode) self.environment.checkForError(status, "SessionPool_New(): create pool") self.w_name = config.w_string( space, poolnameptr[0], rffi.cast(lltype.Signed, poolnamelenptr[0])) finally: user_buf.clear() password_buf.clear() dsn_buf.clear() lltype.free(poolnameptr, flavor='raw') lltype.free(poolnamelenptr, flavor='raw') return space.wrap(self)
def q_light_finalizer(self, typeid): typeinfo = self.get(typeid) if typeinfo.infobits & T_HAS_LIGHTWEIGHT_FINALIZER: return typeinfo.finalizer return lltype.nullptr(GCData.FINALIZER_FUNC)
def __init__(self, fd, mode): self.llf = rffi_fdopen(fd, mode) if not self.llf: raise OSError(rposix.get_saved_errno(), "fdopen failed") rffi_setbuf(self.llf, lltype.nullptr(rffi.CCHARP.TO))
def _PyUnicode_AsDefaultEncodedString(space, w_unicode, errors): return PyUnicode_AsEncodedString(space, w_unicode, lltype.nullptr(rffi.CCHARP.TO), errors)
class RFile(object): _setbuf = lltype.nullptr(rffi.CCHARP.TO) _univ_newline = False _newlinetypes = NEWLINE_UNKNOWN _skipnextlf = False def __init__(self, ll_file, mode=None, close2=_fclose2): self._ll_file = ll_file if mode is not None: self._univ_newline = 'U' in mode self._close2 = close2 def _setbufsize(self, bufsize): if bufsize >= 0: if bufsize == 0: mode = _IONBF elif bufsize == 1: mode = _IOLBF bufsize = BUFSIZ else: mode = _IOFBF if self._setbuf: lltype.free(self._setbuf, flavor='raw') if mode == _IONBF: self._setbuf = lltype.nullptr(rffi.CCHARP.TO) else: self._setbuf = lltype.malloc(rffi.CCHARP.TO, bufsize, flavor='raw') c_setvbuf(self._ll_file, self._setbuf, mode, bufsize) def __del__(self): """Closes the described file when the object's last reference goes away. Unlike an explicit call to close(), this is meant as a last-resort solution and cannot release the GIL or return an error code.""" ll_file = self._ll_file if ll_file: do_close = self._close2[1] if do_close: do_close(ll_file) # return value ignored if self._setbuf: lltype.free(self._setbuf, flavor='raw') def _cleanup_(self): self._ll_file = lltype.nullptr(FILEP.TO) 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 _check_closed(self): if not self._ll_file: raise ValueError("I/O operation on closed file") def _fread(self, buf, n, stream): if not self._univ_newline: return c_fread(buf, 1, n, stream) i = 0 dst = buf newlinetypes = self._newlinetypes skipnextlf = self._skipnextlf while n: nread = c_fread(dst, 1, n, stream) if nread == 0: break src = dst n -= nread shortread = n != 0 while nread: nread -= 1 c = src[0] src = rffi.ptradd(src, 1) if c == '\r': dst[0] = '\n' dst = rffi.ptradd(dst, 1) i += 1 skipnextlf = True elif skipnextlf and c == '\n': skipnextlf = False newlinetypes |= NEWLINE_CRLF n += 1 else: if c == '\n': newlinetypes |= NEWLINE_LF elif skipnextlf: newlinetypes |= NEWLINE_CR dst[0] = c dst = rffi.ptradd(dst, 1) i += 1 skipnextlf = False if shortread: if skipnextlf and c_feof(stream): newlinetypes |= NEWLINE_CR break self._newlinetypes = newlinetypes self._skipnextlf = skipnextlf return i 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 _readline1(self, raw_buf): ll_file = self._ll_file for i in range(BASE_LINE_SIZE): raw_buf[i] = '\n' result = c_fgets(raw_buf, BASE_LINE_SIZE, ll_file) if not result: if c_feof(ll_file): # ok return 0 raise _error(ll_file) # Assume that fgets() works as documented, and additionally # never writes beyond the final \0, which the CPython # fileobject.c says appears to be the case everywhere. # The only case where the buffer was not big enough is the # case where the buffer is full, ends with \0, and doesn't # end with \n\0. p = 0 while raw_buf[p] != '\n': p += 1 if p == BASE_LINE_SIZE: # fgets read whole buffer without finding newline return -1 # p points to first \n if p + 1 < BASE_LINE_SIZE and raw_buf[p + 1] == '\0': # \n followed by \0, fgets read and found newline return p + 1 else: # \n not followed by \0, fgets read but didnt find newline assert p > 0 and raw_buf[p - 1] == '\0' return p - 1 def readline(self, size=-1): self._check_closed() if size == 0: return "" elif size < 0 and not self._univ_newline: with rffi.scoped_alloc_buffer(BASE_LINE_SIZE) as buf: c = self._readline1(buf.raw) if c >= 0: return buf.str(c) # this is the rare case: the line is longer than BASE_LINE_SIZE s = StringBuilder() while True: s.append_charpsize(buf.raw, BASE_LINE_SIZE - 1) c = self._readline1(buf.raw) if c >= 0: break s.append_charpsize(buf.raw, c) return s.build() else: # size > 0 or self._univ_newline ll_file = self._ll_file c = 0 s = StringBuilder() if self._univ_newline: newlinetypes = self._newlinetypes skipnextlf = self._skipnextlf while size < 0 or s.getlength() < size: c = c_getc(ll_file) if c == EOF: break if skipnextlf: skipnextlf = False if c == ord('\n'): newlinetypes |= NEWLINE_CRLF c = c_getc(ll_file) if c == EOF: break else: newlinetypes |= NEWLINE_CR if c == ord('\r'): skipnextlf = True c = ord('\n') elif c == ord('\n'): newlinetypes |= NEWLINE_LF s.append(chr(c)) if c == ord('\n'): break if c == EOF: if skipnextlf: newlinetypes |= NEWLINE_CR self._newlinetypes = newlinetypes self._skipnextlf = skipnextlf else: while s.getlength() < size: c = c_getc(ll_file) if c == EOF: break s.append(chr(c)) if c == ord('\n'): break if c == EOF: if c_ferror(ll_file): raise _error(ll_file) return s.build() @enforceargs(None, str) def write(self, value): self._check_closed() with rffi.scoped_nonmovingbuffer(value) as ll_value: # note that since we got a nonmoving buffer, it is either raw # or already cannot move, so the arithmetics below are fine length = len(value) bytes = c_fwrite(ll_value, 1, length, self._ll_file) if bytes != length: errno = rposix.get_saved_errno() c_clearerr(self._ll_file) raise IOError(errno, os.strerror(errno)) def flush(self): self._check_closed() res = c_fflush(self._ll_file) if res != 0: errno = rposix.get_saved_errno() raise IOError(errno, os.strerror(errno)) def truncate(self, arg=-1): self._check_closed() if arg == -1: arg = self.tell() self.flush() res = c_ftruncate(self.fileno(), arg) if res == -1: errno = rposix.get_saved_errno() raise IOError(errno, os.strerror(errno)) def seek(self, pos, whence=0): self._check_closed() res = c_fseek(self._ll_file, pos, whence) if res == -1: errno = rposix.get_saved_errno() raise IOError(errno, os.strerror(errno)) self._skipnextlf = False def tell(self): self._check_closed() res = intmask(c_ftell(self._ll_file)) if res == -1: errno = rposix.get_saved_errno() raise IOError(errno, os.strerror(errno)) if self._skipnextlf: c = c_getc(self._ll_file) if c == ord('\n'): self._newlinetypes |= NEWLINE_CRLF res += 1 self._skipnextlf = False elif c != EOF: c_ungetc(c, self._ll_file) return res def fileno(self): self._check_closed() return intmask(c_fileno(self._ll_file)) def isatty(self): self._check_closed() return os.isatty(c_fileno(self._ll_file)) def __enter__(self): return self def __exit__(self, *args): self.close()
class LLtypeMixin(object): def get_class_of_box(self, box): base = box.getref_base() return lltype.cast_opaque_ptr(rclass.OBJECTPTR, base).typeptr node_vtable = lltype.malloc(OBJECT_VTABLE, immortal=True) node_vtable.name = rclass.alloc_array_name('node') node_vtable_adr = llmemory.cast_ptr_to_adr(node_vtable) node_vtable2 = lltype.malloc(OBJECT_VTABLE, immortal=True) node_vtable2.name = rclass.alloc_array_name('node2') node_vtable_adr2 = llmemory.cast_ptr_to_adr(node_vtable2) node_vtable3 = lltype.malloc(OBJECT_VTABLE, immortal=True) node_vtable3.name = rclass.alloc_array_name('node3') node_vtable3.subclassrange_min = 3 node_vtable3.subclassrange_max = 3 node_vtable_adr3 = llmemory.cast_ptr_to_adr(node_vtable3) cpu = runner.LLGraphCPU(None) NODE = lltype.GcForwardReference() S = lltype.GcForwardReference() NODE.become( lltype.GcStruct('NODE', ('parent', OBJECT), ('value', lltype.Signed), ('floatval', lltype.Float), ('charval', lltype.Char), ('nexttuple', lltype.Ptr(S)), ('next', lltype.Ptr(NODE)))) S.become( lltype.GcStruct('TUPLE', ('a', lltype.Signed), ('abis', lltype.Signed), ('b', lltype.Ptr(NODE)))) NODE2 = lltype.GcStruct('NODE2', ('parent', NODE), ('other', lltype.Ptr(NODE))) NODE3 = lltype.GcForwardReference() NODE3.become( lltype.GcStruct('NODE3', ('parent', OBJECT), ('value', lltype.Signed), ('next', lltype.Ptr(NODE3)), hints={'immutable': True})) big_fields = [('big' + i, lltype.Signed) for i in string.ascii_lowercase] BIG = lltype.GcForwardReference() BIG.become(lltype.GcStruct('BIG', *big_fields, hints={'immutable': True})) for field, _ in big_fields: locals()[field + 'descr'] = cpu.fielddescrof(BIG, field) node = lltype.malloc(NODE) node.value = 5 node.next = node node.parent.typeptr = node_vtable nodeaddr = lltype.cast_opaque_ptr(llmemory.GCREF, node) #nodebox = InputArgRef(lltype.cast_opaque_ptr(llmemory.GCREF, node)) node2 = lltype.malloc(NODE2) node2.parent.parent.typeptr = node_vtable2 node2addr = lltype.cast_opaque_ptr(llmemory.GCREF, node2) myptr = lltype.cast_opaque_ptr(llmemory.GCREF, node) mynodeb = lltype.malloc(NODE) myarray = lltype.cast_opaque_ptr( llmemory.GCREF, lltype.malloc(lltype.GcArray(lltype.Signed), 13, zero=True)) mynodeb.parent.typeptr = node_vtable myptrb = lltype.cast_opaque_ptr(llmemory.GCREF, mynodeb) myptr2 = lltype.malloc(NODE2) myptr2.parent.parent.typeptr = node_vtable2 myptr2 = lltype.cast_opaque_ptr(llmemory.GCREF, myptr2) nullptr = lltype.nullptr(llmemory.GCREF.TO) mynode3 = lltype.malloc(NODE3) mynode3.parent.typeptr = node_vtable3 mynode3.value = 7 mynode3.next = mynode3 myptr3 = lltype.cast_opaque_ptr(llmemory.GCREF, mynode3) # a NODE2 mynode4 = lltype.malloc(NODE3) mynode4.parent.typeptr = node_vtable3 myptr4 = lltype.cast_opaque_ptr(llmemory.GCREF, mynode4) # a NODE3 nullptr = lltype.nullptr(llmemory.GCREF.TO) #nodebox2 = InputArgRef(lltype.cast_opaque_ptr(llmemory.GCREF, node2)) nodesize = cpu.sizeof(NODE, node_vtable) node_tid = nodesize.get_type_id() nodesize2 = cpu.sizeof(NODE2, node_vtable2) nodesize3 = cpu.sizeof(NODE3, node_vtable3) valuedescr = cpu.fielddescrof(NODE, 'value') floatdescr = cpu.fielddescrof(NODE, 'floatval') chardescr = cpu.fielddescrof(NODE, 'charval') nextdescr = cpu.fielddescrof(NODE, 'next') nexttupledescr = cpu.fielddescrof(NODE, 'nexttuple') otherdescr = cpu.fielddescrof(NODE2, 'other') valuedescr3 = cpu.fielddescrof(NODE3, 'value') nextdescr3 = cpu.fielddescrof(NODE3, 'next') assert valuedescr3.is_always_pure() assert nextdescr3.is_always_pure() accessor = FieldListAccessor() accessor.initialize(None, {'inst_field': IR_QUASIIMMUTABLE}) QUASI = lltype.GcStruct('QUASIIMMUT', ('inst_field', lltype.Signed), ('mutate_field', rclass.OBJECTPTR), hints={'immutable_fields': accessor}) quasisize = cpu.sizeof(QUASI, None) quasi = lltype.malloc(QUASI, immortal=True) quasi.inst_field = -4247 quasifielddescr = cpu.fielddescrof(QUASI, 'inst_field') quasiptr = lltype.cast_opaque_ptr(llmemory.GCREF, quasi) quasiimmutdescr = QuasiImmutDescr(cpu, quasiptr, quasifielddescr, cpu.fielddescrof(QUASI, 'mutate_field')) NODEOBJ = lltype.GcStruct('NODEOBJ', ('parent', OBJECT), ('ref', lltype.Ptr(OBJECT))) nodeobj = lltype.malloc(NODEOBJ) nodeobjvalue = lltype.cast_opaque_ptr(llmemory.GCREF, nodeobj) refdescr = cpu.fielddescrof(NODEOBJ, 'ref') INTOBJ_NOIMMUT = lltype.GcStruct('INTOBJ_NOIMMUT', ('parent', OBJECT), ('intval', lltype.Signed)) INTOBJ_IMMUT = lltype.GcStruct('INTOBJ_IMMUT', ('parent', OBJECT), ('intval', lltype.Signed), hints={'immutable': True}) intobj_noimmut_vtable = lltype.malloc(OBJECT_VTABLE, immortal=True) intobj_immut_vtable = lltype.malloc(OBJECT_VTABLE, immortal=True) noimmut_intval = cpu.fielddescrof(INTOBJ_NOIMMUT, 'intval') immut_intval = cpu.fielddescrof(INTOBJ_IMMUT, 'intval') immut = lltype.malloc(INTOBJ_IMMUT, zero=True) immutaddr = lltype.cast_opaque_ptr(llmemory.GCREF, immut) noimmut_descr = cpu.sizeof(INTOBJ_NOIMMUT, intobj_noimmut_vtable) immut_descr = cpu.sizeof(INTOBJ_IMMUT, intobj_immut_vtable) PTROBJ_IMMUT = lltype.GcStruct('PTROBJ_IMMUT', ('parent', OBJECT), ('ptrval', lltype.Ptr(OBJECT)), hints={'immutable': True}) ptrobj_immut_vtable = lltype.malloc(OBJECT_VTABLE, immortal=True) ptrobj_immut_descr = cpu.sizeof(PTROBJ_IMMUT, ptrobj_immut_vtable) immut_ptrval = cpu.fielddescrof(PTROBJ_IMMUT, 'ptrval') arraydescr = cpu.arraydescrof(lltype.GcArray(lltype.Signed)) int32arraydescr = cpu.arraydescrof(lltype.GcArray(rffi.INT)) int16arraydescr = cpu.arraydescrof(lltype.GcArray(rffi.SHORT)) float32arraydescr = cpu.arraydescrof(lltype.GcArray(lltype.SingleFloat)) arraydescr_tid = arraydescr.get_type_id() array = lltype.malloc(lltype.GcArray(lltype.Signed), 15, zero=True) arrayref = lltype.cast_opaque_ptr(llmemory.GCREF, array) array2 = lltype.malloc(lltype.GcArray(lltype.Ptr(S)), 15, zero=True) array2ref = lltype.cast_opaque_ptr(llmemory.GCREF, array2) gcarraydescr = cpu.arraydescrof(lltype.GcArray(llmemory.GCREF)) gcarraydescr_tid = gcarraydescr.get_type_id() floatarraydescr = cpu.arraydescrof(lltype.GcArray(lltype.Float)) arrayimmutdescr = cpu.arraydescrof( lltype.GcArray(lltype.Signed, hints={"immutable": True})) immutarray = lltype.cast_opaque_ptr( llmemory.GCREF, lltype.malloc(arrayimmutdescr.A, 13, zero=True)) gcarrayimmutdescr = cpu.arraydescrof( lltype.GcArray(llmemory.GCREF, hints={"immutable": True})) floatarrayimmutdescr = cpu.arraydescrof( lltype.GcArray(lltype.Float, hints={"immutable": True})) # a GcStruct not inheriting from OBJECT tpl = lltype.malloc(S, zero=True) tupleaddr = lltype.cast_opaque_ptr(llmemory.GCREF, tpl) nodefull2 = lltype.malloc(NODE, zero=True) nodefull2addr = lltype.cast_opaque_ptr(llmemory.GCREF, nodefull2) ssize = cpu.sizeof(S, None) adescr = cpu.fielddescrof(S, 'a') abisdescr = cpu.fielddescrof(S, 'abis') bdescr = cpu.fielddescrof(S, 'b') #sbox = BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, lltype.malloc(S))) arraydescr2 = cpu.arraydescrof(lltype.GcArray(lltype.Ptr(S))) T = lltype.GcStruct('TUPLE', ('c', lltype.Signed), ('d', lltype.Ptr(lltype.GcArray(lltype.Ptr(NODE))))) W_ROOT = lltype.GcStruct( 'W_ROOT', ('parent', OBJECT), ('inst_w_seq', llmemory.GCREF), ('inst_index', lltype.Signed), ('inst_w_list', llmemory.GCREF), ('inst_length', lltype.Signed), ('inst_start', lltype.Signed), ('inst_step', lltype.Signed)) inst_w_seq = cpu.fielddescrof(W_ROOT, 'inst_w_seq') inst_index = cpu.fielddescrof(W_ROOT, 'inst_index') inst_length = cpu.fielddescrof(W_ROOT, 'inst_length') inst_start = cpu.fielddescrof(W_ROOT, 'inst_start') inst_step = cpu.fielddescrof(W_ROOT, 'inst_step') inst_w_list = cpu.fielddescrof(W_ROOT, 'inst_w_list') w_root_vtable = lltype.malloc(OBJECT_VTABLE, immortal=True) tsize = cpu.sizeof(T, None) cdescr = cpu.fielddescrof(T, 'c') ddescr = cpu.fielddescrof(T, 'd') arraydescr3 = cpu.arraydescrof(lltype.GcArray(lltype.Ptr(NODE3))) U = lltype.GcStruct('U', ('parent', OBJECT), ('one', lltype.Ptr(lltype.GcArray(lltype.Ptr(NODE))))) u_vtable = lltype.malloc(OBJECT_VTABLE, immortal=True) u_vtable_adr = llmemory.cast_ptr_to_adr(u_vtable) SIMPLE = lltype.GcStruct('simple', ('parent', OBJECT), ('value', lltype.Signed)) simplevalue = cpu.fielddescrof(SIMPLE, 'value') simple_vtable = lltype.malloc(OBJECT_VTABLE, immortal=True) simpledescr = cpu.sizeof(SIMPLE, simple_vtable) simple = lltype.malloc(SIMPLE, zero=True) simpleaddr = lltype.cast_opaque_ptr(llmemory.GCREF, simple) #usize = cpu.sizeof(U, ...) onedescr = cpu.fielddescrof(U, 'one') FUNC = lltype.FuncType([lltype.Signed], lltype.Signed) plaincalldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo.MOST_GENERAL) elidablecalldescr = cpu.calldescrof( FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo([valuedescr], [], [], [valuedescr], [], [], EffectInfo.EF_ELIDABLE_CANNOT_RAISE)) elidable2calldescr = cpu.calldescrof( FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo([valuedescr], [], [], [valuedescr], [], [], EffectInfo.EF_ELIDABLE_OR_MEMORYERROR)) elidable3calldescr = cpu.calldescrof( FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo([valuedescr], [], [], [valuedescr], [], [], EffectInfo.EF_ELIDABLE_CAN_RAISE)) nonwritedescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo([], [], [], [], [], [])) writeadescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo([], [], [], [adescr], [], [])) writearraydescr = cpu.calldescrof( FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo([], [], [], [adescr], [arraydescr], [])) writevalue3descr = cpu.calldescrof( FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo([], [], [], [valuedescr3], [], [])) readadescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo([adescr], [], [], [], [], [])) mayforcevirtdescr = cpu.calldescrof( FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo([nextdescr], [], [], [], [], [], EffectInfo.EF_FORCES_VIRTUAL_OR_VIRTUALIZABLE, can_invalidate=True)) arraycopydescr = cpu.calldescrof( FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo([], [arraydescr], [], [], [arraydescr], [], EffectInfo.EF_CANNOT_RAISE, oopspecindex=EffectInfo.OS_ARRAYCOPY)) raw_malloc_descr = cpu.calldescrof( FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo([], [], [], [], [], [], EffectInfo.EF_CAN_RAISE, oopspecindex=EffectInfo.OS_RAW_MALLOC_VARSIZE_CHAR)) raw_free_descr = cpu.calldescrof( FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo([], [], [], [], [], [], EffectInfo.EF_CANNOT_RAISE, oopspecindex=EffectInfo.OS_RAW_FREE)) chararray = lltype.GcArray(lltype.Char) chararraydescr = cpu.arraydescrof(chararray) u2array = lltype.GcArray(rffi.USHORT) u2arraydescr = cpu.arraydescrof(u2array) nodefull = lltype.malloc(NODE2, zero=True) nodefull.parent.next = lltype.cast_pointer(lltype.Ptr(NODE), nodefull) nodefull.parent.nexttuple = tpl nodefulladdr = lltype.cast_opaque_ptr(llmemory.GCREF, nodefull) # array of structs (complex data) complexarray = lltype.GcArray( lltype.Struct( "complex", ("real", lltype.Float), ("imag", lltype.Float), )) complexarraydescr = cpu.arraydescrof(complexarray) complexrealdescr = cpu.interiorfielddescrof(complexarray, "real") compleximagdescr = cpu.interiorfielddescrof(complexarray, "imag") complexarraycopydescr = cpu.calldescrof( FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo([], [complexarraydescr], [], [], [complexarraydescr], [], EffectInfo.EF_CANNOT_RAISE, oopspecindex=EffectInfo.OS_ARRAYCOPY)) rawarraydescr = cpu.arraydescrof( lltype.Array(lltype.Signed, hints={'nolength': True})) rawarraydescr_char = cpu.arraydescrof( lltype.Array(lltype.Char, hints={'nolength': True})) rawarraydescr_float = cpu.arraydescrof( lltype.Array(lltype.Float, hints={'nolength': True})) fc_array = lltype.GcArray( lltype.Struct("floatchar", ("float", lltype.Float), ("char", lltype.Char))) fc_array_descr = cpu.arraydescrof(fc_array) fc_array_floatdescr = cpu.interiorfielddescrof(fc_array, "float") fc_array_chardescr = cpu.interiorfielddescrof(fc_array, "char") for _name, _os in [ ('strconcatdescr', 'OS_STR_CONCAT'), ('strslicedescr', 'OS_STR_SLICE'), ('strequaldescr', 'OS_STR_EQUAL'), ('streq_slice_checknull_descr', 'OS_STREQ_SLICE_CHECKNULL'), ('streq_slice_nonnull_descr', 'OS_STREQ_SLICE_NONNULL'), ('streq_slice_char_descr', 'OS_STREQ_SLICE_CHAR'), ('streq_nonnull_descr', 'OS_STREQ_NONNULL'), ('streq_nonnull_char_descr', 'OS_STREQ_NONNULL_CHAR'), ('streq_checknull_char_descr', 'OS_STREQ_CHECKNULL_CHAR'), ('streq_lengthok_descr', 'OS_STREQ_LENGTHOK'), ]: if _name in ('strconcatdescr', 'strslicedescr'): _extra = EffectInfo.EF_ELIDABLE_OR_MEMORYERROR else: _extra = EffectInfo.EF_ELIDABLE_CANNOT_RAISE _oopspecindex = getattr(EffectInfo, _os) locals()[_name] = \ cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo([], [], [], [], [], [], _extra, oopspecindex=_oopspecindex)) # _oopspecindex = getattr(EffectInfo, _os.replace('STR', 'UNI')) locals()[_name.replace('str', 'unicode')] = \ cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo([], [], [], [], [], [], _extra, oopspecindex=_oopspecindex)) s2u_descr = cpu.calldescrof( FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo([], [], [], [], [], [], EffectInfo.EF_ELIDABLE_CAN_RAISE, oopspecindex=EffectInfo.OS_STR2UNICODE)) # class LoopToken(AbstractDescr): pass asmdescr = LoopToken() # it can be whatever, it's not a descr though from rpython.jit.metainterp.virtualref import VirtualRefInfo class FakeWarmRunnerDesc: pass FakeWarmRunnerDesc.cpu = cpu vrefinfo = VirtualRefInfo(FakeWarmRunnerDesc) virtualtokendescr = vrefinfo.descr_virtual_token virtualforceddescr = vrefinfo.descr_forced FUNC = lltype.FuncType([], lltype.Void) ei = EffectInfo([], [], [], [], [], [], EffectInfo.EF_CANNOT_RAISE, can_invalidate=False, oopspecindex=EffectInfo.OS_JIT_FORCE_VIRTUALIZABLE) clear_vable = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, ei) jit_virtual_ref_vtable = vrefinfo.jit_virtual_ref_vtable jvr_vtable_adr = llmemory.cast_ptr_to_adr(jit_virtual_ref_vtable) vref_descr = cpu.sizeof(vrefinfo.JIT_VIRTUAL_REF, jit_virtual_ref_vtable) FUNC = lltype.FuncType([lltype.Signed, lltype.Signed], lltype.Signed) ei = EffectInfo([], [], [], [], [], [], EffectInfo.EF_ELIDABLE_CANNOT_RAISE, can_invalidate=False, oopspecindex=EffectInfo.OS_INT_PY_DIV) int_py_div_descr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, ei) ei = EffectInfo([], [], [], [], [], [], EffectInfo.EF_ELIDABLE_CANNOT_RAISE, can_invalidate=False, oopspecindex=EffectInfo.OS_INT_UDIV) int_udiv_descr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, ei) ei = EffectInfo([], [], [], [], [], [], EffectInfo.EF_ELIDABLE_CANNOT_RAISE, can_invalidate=False, oopspecindex=EffectInfo.OS_INT_PY_MOD) int_py_mod_descr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, ei) FUNC = lltype.FuncType([], llmemory.GCREF) ei = EffectInfo([], [], [], [], [], [], EffectInfo.EF_ELIDABLE_CAN_RAISE) plain_r_calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, ei) namespace = locals()
def null_instance(self): return nullptr(self.object_type)
def test_as_voidptr(self, space, api): w_l = api.PyLong_FromVoidPtr(lltype.nullptr(rffi.VOIDP.TO)) assert space.unwrap(w_l) == 0L assert api.PyLong_AsVoidPtr(w_l) == lltype.nullptr(rffi.VOIDP.TO)
def _cleanup_(self): self._ll_file = lltype.nullptr(FILEP.TO)
def __init__(self, space): W_ArrayBase.__init__(self, space) self.buffer = lltype.nullptr(mytype.arraytype)
def get_compiled_regex_cache(interp, regex): pce = interp.space.regex_cache.get(regex) if pce is not None: return pce if '\x00' in regex: raise ExitFunctionWithError("Null byte in regex") # Parse through the leading whitespace, and display a warning if we # get to the end without encountering a delimiter. i = 0 while i < len(regex) and regex[i].isspace(): i += 1 if i == len(regex): raise ExitFunctionWithError("Empty regular expression") # Get the delimiter and display a warning if it is alphanumeric # or a backslash. delimiter = regex[i] if delimiter.isalnum() or delimiter == '\\': raise ExitFunctionWithError("Delimiter must not be alphanumeric " "or backslash") i += 1 pattern_start = i start_delimiter = delimiter if delimiter == '(': delimiter = ')' elif delimiter == '[': delimiter = ']' elif delimiter == '{': delimiter = '}' elif delimiter == '<': delimiter = '>' end_delimiter = delimiter if start_delimiter == end_delimiter: # We need to iterate through the pattern, searching for the # ending delimiter, but skipping the backslashed delimiters. # If the ending delimiter is not found, display a warning. while i < len(regex): if regex[i] == '\\': i += 1 elif regex[i] == end_delimiter: break i += 1 else: raise ExitFunctionWithError("No ending delimiter '%s' found" % delimiter[:]) else: # We iterate through the pattern, searching for the matching # ending delimiter. For each matching starting delimiter, we # increment nesting level, and decrement it for each matching # ending delimiter. If we reach the end of the pattern without # matching, display a warning. brackets = 1 # brackets nesting level while i < len(regex): if regex[i] == '\\': i += 1 elif regex[i] == end_delimiter: brackets -= 1 if brackets == 0: break elif regex[i] == start_delimiter: brackets += 1 i += 1 else: raise ExitFunctionWithError("No ending matching delimiter '%s' " "found" % delimiter[:]) # Move on to the options pattern_end = i i += 1 # Parse through the options, setting appropriate flags. Display # a warning if we encounter an unknown modifier. coptions = 0 poptions = 0 do_study = False while i < len(regex): option = regex[i] i += 1 # Perl compatible options if option == 'i': coptions |= _pcre.PCRE_CASELESS elif option == 'm': coptions |= _pcre.PCRE_MULTILINE elif option == 's': coptions |= _pcre.PCRE_DOTALL elif option == 'x': coptions |= _pcre.PCRE_EXTENDED # PCRE specific options elif option == 'A': coptions |= _pcre.PCRE_ANCHORED elif option == 'D': coptions |= _pcre.PCRE_DOLLAR_ENDONLY elif option == 'S': do_study = True elif option == 'U': coptions |= _pcre.PCRE_UNGREEDY elif option == 'X': coptions |= _pcre.PCRE_EXTRA elif option == 'u': coptions |= _pcre.PCRE_UTF8 if _pcre.PCRE_UCP is not None: coptions |= _pcre.PCRE_UCP # Custom preg options elif option == 'e': poptions |= PREG_REPLACE_EVAL raise ExitFunctionWithError("The deprecated /e modifier is not " "supported by hippy") elif option == ' ': pass elif option == '\n': pass else: raise ExitFunctionWithError("Unknown modifier '%s'" % option[:]) # XXX missing: #if HAVE_SETLOCALE # if (strcmp(locale, "C")) # tables = pcre_maketables(); #endif # Make a copy of the actual pattern. length = pattern_end - pattern_start pattern = lltype.malloc(rffi.CCHARP.TO, length + 1, flavor='raw') copy_string_to_raw(llstr(regex), pattern, pattern_start, length) pattern[length] = '\x00' # Compile pattern and display a warning if compilation failed. p_error = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw', zero=True) p_erroffset = lltype.malloc(rffi.INTP.TO, 1, flavor='raw', zero=True) tables = lltype.nullptr(rffi.CCHARP.TO) re = _pcre.pcre_compile(pattern, coptions, p_error, p_erroffset, tables) error = p_error[0] erroffset = rffi.cast(lltype.Signed, p_erroffset[0]) lltype.free(p_erroffset, flavor='raw') lltype.free(p_error, flavor='raw') lltype.free(pattern, flavor='raw') # All three raw mallocs above are now freed if not re: raise ExitFunctionWithError("Compilation failed: %s at offset %d" % (rffi.charp2str(error), erroffset)) # If study option was specified, study the pattern and # store the result in extra for passing to pcre_exec. extra = lltype.nullptr(_pcre.pcre_extra) if do_study: soptions = 0 #if _pcre.PCRE_STUDY_JIT_COMPILE is not None: # soptions |= _pcre.PCRE_STUDY_JIT_COMPILE p_error = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw', zero=True) extra = _pcre.pcre_study(re, soptions, p_error) error = p_error[0] lltype.free(p_error, flavor='raw') if error: interp.warn("Error while studying pattern") if not extra: extra = _pcre.hippy_pcre_extra_malloc() rffi.setintfield( extra, 'c_flags', rffi.getintfield(extra, 'c_flags') | _pcre.PCRE_EXTRA_MATCH_LIMIT | _pcre.PCRE_EXTRA_MATCH_LIMIT_RECURSION) capturecount = getfullinfo_int(re, extra, _pcre.PCRE_INFO_CAPTURECOUNT) assert capturecount >= 0 subpat_names = make_subpats_table(capturecount, re, extra) pce = PCE( re, extra, poptions, coptions, # XXX also locale and tables capturecount, subpat_names) interp.space.regex_cache.set(regex, pce) return pce
def ll_inst_type(obj): if obj: return obj.typeptr else: # type(None) -> NULL (for now) return nullptr(typeOf(obj).TO.typeptr.TO)
assert False return intbounds.get_integer_min(not _is_signed_kind(self.FIELD), rffi.sizeof(self.FIELD)) def get_integer_max(self): if getkind(self.FIELD) != 'int': assert False return intbounds.get_integer_max(not _is_signed_kind(self.FIELD), rffi.sizeof(self.FIELD)) _example_res = { 'v': None, 'r': lltype.nullptr(llmemory.GCREF.TO), 'i': 0, 'f': 0.0 } class LLGraphCPU(model.AbstractCPU): from rpython.jit.metainterp.typesystem import llhelper as ts supports_floats = True supports_longlong = r_uint is not r_ulonglong supports_singlefloats = True translate_support_code = False is_llgraph = True def __init__(self, rtyper, stats=None, *ignored_args, **kwds): model.AbstractCPU.__init__(self)
def descr_control(self, space, w_changelist, max_events, w_timeout): self.check_closed(space) if max_events < 0: raise oefmt(space.w_ValueError, "Length of eventlist must be 0 or positive, got %d", max_events) if space.is_w(w_changelist, space.w_None): changelist_len = 0 else: changelist_len = space.len_w(w_changelist) with lltype.scoped_alloc(rffi.CArray(kevent), changelist_len) as changelist: with lltype.scoped_alloc(rffi.CArray(kevent), max_events) as eventlist: with lltype.scoped_alloc(timespec) as timeout: if not space.is_w(w_timeout, space.w_None): _timeout = space.float_w(w_timeout) if _timeout < 0: raise oefmt(space.w_ValueError, "Timeout must be None or >= 0, got %s", str(_timeout)) sec = int(_timeout) nsec = int(1e9 * (_timeout - sec)) rffi.setintfield(timeout, 'c_tv_sec', sec) rffi.setintfield(timeout, 'c_tv_nsec', nsec) ptimeout = timeout else: ptimeout = lltype.nullptr(timespec) if not space.is_w(w_changelist, space.w_None): i = 0 for w_ev in space.listview(w_changelist): ev = space.interp_w(W_Kevent, w_ev) changelist[i].c_ident = ev.ident changelist[i].c_filter = ev.filter changelist[i].c_flags = ev.flags changelist[i].c_fflags = ev.fflags changelist[i].c_data = ev.data changelist[i].c_udata = ev.udata i += 1 pchangelist = changelist else: pchangelist = lltype.nullptr(rffi.CArray(kevent)) nfds = syscall_kevent(self.kqfd, pchangelist, changelist_len, eventlist, max_events, ptimeout) if nfds < 0: raise exception_from_saved_errno(space, space.w_OSError) else: elist_w = [None] * nfds for i in xrange(nfds): evt = eventlist[i] w_event = W_Kevent(space) w_event.ident = evt.c_ident w_event.filter = evt.c_filter w_event.flags = evt.c_flags w_event.fflags = evt.c_fflags w_event.data = evt.c_data w_event.udata = evt.c_udata elist_w[i] = w_event return space.newlist(elist_w)
def unwrap_virtualizable_box(self, virtualizable_box): return virtualizable_box.getref(llmemory.GCREF) def is_vtypeptr(self, TYPE): return TYPE == self.VTYPEPTR # ____________________________________________________________ # # The 'vable_token' field of a virtualizable is either NULL, points # to the JITFRAME object for the current assembler frame, or is # the special value TOKEN_TRACING_RESCALL. It is: # # 1. NULL (TOKEN_NONE) if not in the JIT at all, except as described below. # # 2. NULL when tracing is in progress; except: # # 3. equal to TOKEN_TRACING_RESCALL during tracing when we do a # residual call, calling random unknown other parts of the interpreter; # it is reset to NULL as soon as something occurs to the virtualizable. # # 4. when running the machine code with a virtualizable, it is set # to the JITFRAME, as obtained with the FORCE_TOKEN operation. _DUMMY = lltype.GcStruct('JITFRAME_DUMMY') _dummy = lltype.malloc(_DUMMY) TOKEN_NONE = lltype.nullptr(llmemory.GCREF.TO) TOKEN_TRACING_RESCALL = lltype.cast_opaque_ptr(llmemory.GCREF, _dummy)
class TestRegallocGcIntegration(BaseTestRegalloc): cpu = CPU(None, None) cpu.gc_ll_descr = GcLLDescr_boehm(None, None, None) cpu.setup_once() S = lltype.GcForwardReference() S.become(lltype.GcStruct('S', ('field', lltype.Ptr(S)), ('int', lltype.Signed))) fielddescr = cpu.fielddescrof(S, 'field') struct_ptr = lltype.malloc(S) struct_ref = lltype.cast_opaque_ptr(llmemory.GCREF, struct_ptr) child_ptr = lltype.nullptr(S) struct_ptr.field = child_ptr intdescr = cpu.fielddescrof(S, 'int') ptr0 = struct_ref targettoken = TargetToken() targettoken2 = TargetToken() namespace = locals().copy() def test_basic(self): ops = ''' [p0] p1 = getfield_gc_r(p0, descr=fielddescr) finish(p1) ''' self.interpret(ops, [self.struct_ptr]) assert not self.getptr(0, lltype.Ptr(self.S)) def test_guard(self): ops = ''' [i0, p0, i1, p1] p3 = getfield_gc_r(p0, descr=fielddescr) guard_true(i0) [p0, i1, p1, p3] ''' s1 = lltype.malloc(self.S) s2 = lltype.malloc(self.S) s1.field = s2 self.interpret(ops, [0, s1, 1, s2]) frame = lltype.cast_opaque_ptr(jitframe.JITFRAMEPTR, self.deadframe) # p0 and p3 should be in registers, p1 not so much assert self.getptr(0, lltype.Ptr(self.S)) == s1 # the gcmap should contain three things, p0, p1 and p3 # p3 stays in a register # while p0 and p1 are on the frame b = getmap(frame) nos = [len(b) - 1 - i.start() for i in re.finditer('1', b)] nos.reverse() if self.cpu.backend_name.startswith('x86'): if self.cpu.IS_64_BIT: assert nos == [0, 1, 31] else: assert nos == [0, 1, 25] elif self.cpu.backend_name.startswith('arm'): assert nos == [0, 1, 47] elif self.cpu.backend_name.startswith('ppc64'): assert nos == [0, 1, 33] elif self.cpu.backend_name.startswith('zarch'): assert nos == [0, 1, 29] elif self.cpu.backend_name.startswith('aarch64'): assert nos == [0, 1, 27] else: raise Exception("write the data here") assert frame.jf_frame[nos[0]] assert frame.jf_frame[nos[1]] assert frame.jf_frame[nos[2]] def test_rewrite_constptr(self): ops = ''' [] p1 = getfield_gc_r(ConstPtr(struct_ref), descr=fielddescr) finish(p1) ''' self.interpret(ops, []) assert not self.getptr(0, lltype.Ptr(self.S)) def test_bug_0(self): ops = ''' [i0, i1, i2, i3, i4, i5, i6, i7, i8] label(i0, i1, i2, i3, i4, i5, i6, i7, i8, descr=targettoken) guard_value(i2, 1) [i2, i3, i4, i5, i6, i7, i0, i1, i8] guard_class(i4, 138998336) [i4, i5, i6, i7, i0, i1, i8] i11 = getfield_gc_i(i4, descr=intdescr) guard_nonnull(i11) [i4, i5, i6, i7, i0, i1, i11, i8] i13 = getfield_gc_i(i11, descr=intdescr) guard_isnull(i13) [i4, i5, i6, i7, i0, i1, i11, i8] i15 = getfield_gc_i(i4, descr=intdescr) i17 = int_lt(i15, 0) guard_false(i17) [i4, i5, i6, i7, i0, i1, i11, i15, i8] i18 = getfield_gc_i(i11, descr=intdescr) i19 = int_ge(i15, i18) guard_false(i19) [i4, i5, i6, i7, i0, i1, i11, i15, i8] i20 = int_lt(i15, 0) guard_false(i20) [i4, i5, i6, i7, i0, i1, i11, i15, i8] i21 = getfield_gc_i(i11, descr=intdescr) i22 = getfield_gc_i(i11, descr=intdescr) i23 = int_mul(i15, i22) i24 = int_add(i21, i23) i25 = getfield_gc_i(i4, descr=intdescr) i27 = int_add(i25, 1) setfield_gc(i4, i27, descr=intdescr) i29 = getfield_raw_i(144839744, descr=intdescr) i31 = int_and(i29, -2141192192) i32 = int_is_true(i31) guard_false(i32) [i4, i6, i7, i0, i1, i24] i33 = getfield_gc_i(i0, descr=intdescr) guard_value(i33, ConstPtr(ptr0)) [i4, i6, i7, i0, i1, i33, i24] jump(i0, i1, 1, 17, i4, ConstPtr(ptr0), i6, i7, i24, descr=targettoken) ''' self.interpret(ops, [0, 0, 0, 0, 0, 0, 0, 0, 0], run=False)
def __del__(self): if self.autofree and self.rawmem: lltype.free(self.rawmem, flavor='raw') self.rawmem = lltype.nullptr(rffi.VOIDP.TO)
def error_value(T): if isinstance(T, lltype.Primitive): return PrimitiveErrorValue[T] elif isinstance(T, lltype.Ptr): return lltype.nullptr(T.TO) assert 0, "not implemented yet"
def prepare_file(self, w_ob): from pypy.module._file.interp_file import W_File if isinstance(w_ob, W_File): return prepare_file_argument(self.space, w_ob) else: return lltype.nullptr(rffi.CCHARP.TO)
# low might just happen to have the value INVALID_FILE_SIZE # so we need to check the last error also INVALID_FILE_SIZE = -1 if low == INVALID_FILE_SIZE: err = rwin32.GetLastError_saved() if err: raise WindowsError(err, "mmap") return low, high finally: lltype.free(high_ref, flavor='raw') INVALID_HANDLE = INVALID_HANDLE_VALUE PAGESIZE = _get_page_size() ALLOCATIONGRANULARITY = _get_allocation_granularity() NULL = lltype.nullptr(PTR.TO) NODATA = lltype.nullptr(PTR.TO) class MMap(object): def __init__(self, access, offset): self.size = 0 self.pos = 0 self.access = access self.offset = offset if _MS_WINDOWS: self.map_handle = NULL_HANDLE self.file_handle = NULL_HANDLE self.tagname = "" elif _POSIX:
def null_instance(self): return nullptr(self.pbc_type)
def unicode_attach(space, py_obj, w_obj, w_userdata=None): "Fills a newly allocated PyUnicodeObject with a unicode string" value = space.unicode_w(w_obj) set_wsize(py_obj, len(value)) set_wbuffer(py_obj, lltype.nullptr(rffi.CWCHARP.TO)) _readify(space, py_obj, value)
def select(space, w_iwtd, w_owtd, w_ewtd, w_timeout): """Wait until one or more file descriptors are ready for some kind of I/O. The first three arguments are sequences of file descriptors to be waited for: rlist -- wait until ready for reading wlist -- wait until ready for writing xlist -- wait for an ``exceptional condition'' If only one kind of condition is required, pass [] for the other lists. A file descriptor is either a socket or file object, or a small integer gotten from a fileno() method call on one of those. The optional 4th argument specifies a timeout in seconds; it may be a floating point number to specify fractions of seconds. If it is absent or None, the call will never time out. The return value is a tuple of three lists corresponding to the first three arguments; each contains the subset of the corresponding file descriptors that are ready. *** IMPORTANT NOTICE *** On Windows, only sockets are supported; on Unix, all file descriptors. """ iwtd_w = space.unpackiterable(w_iwtd) owtd_w = space.unpackiterable(w_owtd) ewtd_w = space.unpackiterable(w_ewtd) if space.is_w(w_timeout, space.w_None): timeout = -1.0 else: timeout = space.float_w(w_timeout) ll_inl = lltype.nullptr(_c.fd_set.TO) ll_outl = lltype.nullptr(_c.fd_set.TO) ll_errl = lltype.nullptr(_c.fd_set.TO) ll_timeval = lltype.nullptr(_c.timeval) try: if len(iwtd_w) > 0: ll_inl = lltype.malloc(_c.fd_set.TO, flavor='raw') if len(owtd_w) > 0: ll_outl = lltype.malloc(_c.fd_set.TO, flavor='raw') if len(ewtd_w) > 0: ll_errl = lltype.malloc(_c.fd_set.TO, flavor='raw') if timeout >= 0.0: ll_timeval = rffi.make(_c.timeval) i = int(timeout) rffi.setintfield(ll_timeval, 'c_tv_sec', i) rffi.setintfield(ll_timeval, 'c_tv_usec', int((timeout-i)*1000000)) # Call this as a separate helper to avoid a large piece of code # in try:finally:. Needed for calling further _always_inline_ # helpers like _build_fd_set(). return _call_select(space, iwtd_w, owtd_w, ewtd_w, ll_inl, ll_outl, ll_errl, ll_timeval) finally: if ll_timeval: lltype.free(ll_timeval, flavor='raw') if ll_errl: lltype.free(ll_errl, flavor='raw') if ll_outl: lltype.free(ll_outl, flavor='raw') if ll_inl: lltype.free(ll_inl, flavor='raw')
def _free(self): self.pending = None if self.encodebuf: c_codecs.pypy_cjk_enc_free(self.encodebuf) self.encodebuf = lltype.nullptr(c_codecs.ENCODEBUF_P.TO)
order = support.get_order_as_CF(self.order, order) impl = ConcreteArray(shape, dtype, order, t_strides, backstrides) if copy: loop.setslice(space, impl.get_shape(), impl, self) return impl OBJECTSTORE = lltype.GcStruct('ObjectStore', ('length', lltype.Signed), ('step', lltype.Signed), ('storage', llmemory.Address), rtti=True) offset_of_storage = llmemory.offsetof(OBJECTSTORE, 'storage') offset_of_length = llmemory.offsetof(OBJECTSTORE, 'length') offset_of_step = llmemory.offsetof(OBJECTSTORE, 'step') V_OBJECTSTORE = lltype.nullptr(OBJECTSTORE) def customtrace(gc, obj, callback, arg): #debug_print('in customtrace w/obj', obj) length = (obj + offset_of_length).signed[0] step = (obj + offset_of_step).signed[0] storage = (obj + offset_of_storage).address[0] #debug_print('tracing', length, 'objects in ndarray.storage') i = 0 while i < length: gc._trace_callback(callback, arg, storage) storage += step i += 1