def HPyBytes_AsString(space, ctx, h): w_obj = handles.deref(space, h) s = space.bytes_w(w_obj) llbuf, llstring, flag = rffi.get_nonmovingbuffer_ll_final_null(s) cb = handles.FreeNonMovingBuffer(llbuf, llstring, flag) handles.attach_release_callback(space, h, cb) return llbuf
def string_to_word(s): assert len(s) == WORD_SIZE ll_chars, llobj, flag = rffi.get_nonmovingbuffer_ll_final_null(s) try: wordarray = rffi.cast(rffi.UNSIGNEDP, ll_chars) return wordarray[0] finally: rffi.free_nonmovingbuffer_ll(ll_chars, llobj, flag)
def __init__(self, space, s): self.space = space self.w_empty_string = space.newutf8("", 0) self.s = s # we put our string in a raw buffer so: # 1) we automatically get the '\0' sentinel at the end of the string, # which means that we never have to check for the "end of string" # 2) we can pass the buffer directly to strtod self.ll_chars, self.llobj, self.flag = rffi.get_nonmovingbuffer_ll_final_null( self.s) self.end_ptr = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw') self.pos = 0 self.intcache = space.fromcache(IntCache) # two caches, one for keys, one for general strings. they both have the # form {hash-as-int: StringCacheEntry} and they don't deal with # collisions at all. For every hash there is simply one string stored # and we ignore collisions. self.cache_keys = {} self.cache_values = {} # we don't cache *all* non-key strings, that would be too expensive. # instead, keep a cache of the last 16 strings hashes around and add a # string to the cache only if its hash is seen a second time self.lru_cache = [0] * self.LRU_SIZE self.lru_index = 0 self.startmap = self.space.fromcache(Terminator) # keep a list of objects that are created with maps that aren't clearly # useful. If they turn out to be useful in the end we are good, # otherwise convert them to dicts (see .close()) self.unclear_objects = [] # this is a freelist of lists that store the decoded value of an # object, before they get copied into the eventual dict self.scratch = [[None] * self.DEFAULT_SIZE_SCRATCH]
def strtod(input): if len(input) > _INT_LIMIT: raise MemoryError if objectmodel.revdb_flag_io_disabled(): return _revdb_strtod(input) 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, llobj, flag = rffi.get_nonmovingbuffer_ll_final_null(input) try: result = dg_strtod(rffi.cast(rffi.CONST_CCHARP, ll_input), end_ptr) endpos = (rffi.cast(lltype.Signed, end_ptr[0]) - rffi.cast(lltype.Signed, ll_input)) finally: rffi.free_nonmovingbuffer_ll(ll_input, llobj, 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 ll(callable, string, *args): ll_chars, llobj, flag = rffi.get_nonmovingbuffer_ll_final_null(string) try: return callable(ll_chars, *args) finally: rffi.free_nonmovingbuffer_ll(ll_chars, llobj, flag)
def write_string_as_charp(target, string): from pypy.module._cffi_backend.ctypefunc import set_mustfree_flag buf, llobj, buf_flag = rffi.get_nonmovingbuffer_ll_final_null(string) set_mustfree_flag(target, ord(buf_flag)) # 4, 5 or 6 rffi.cast(rffi.CCHARPP, target)[0] = buf return llobj