def ll_join(s, length, items): s_chars = s.chars s_len = len(s_chars) num_items = length if num_items == 0: return s.empty() itemslen = 0 i = 0 while i < num_items: try: itemslen = ovfcheck(itemslen + len(items[i].chars)) except OverflowError: raise MemoryError i += 1 try: seplen = ovfcheck(s_len * (num_items - 1)) except OverflowError: raise MemoryError # a single '+' at the end is allowed to overflow: it gets # a negative result, and the gc will complain result = s.malloc(itemslen + seplen) res_index = len(items[0].chars) s.copy_contents(items[0], result, 0, 0, res_index) i = 1 while i < num_items: s.copy_contents(s, result, 0, res_index, s_len) res_index += s_len lgt = len(items[i].chars) s.copy_contents(items[i], result, 0, res_index, lgt) res_index += lgt i += 1 return result
def unicode_expandtabs__Unicode_ANY(space, w_self, w_tabsize): self = w_self._value tabsize = space.int_w(w_tabsize) parts = self.split(u'\t') result = [parts[0]] prevsize = 0 for ch in parts[0]: prevsize += 1 if ch == u"\n" or ch == u"\r": prevsize = 0 totalsize = prevsize for i in range(1, len(parts)): pad = tabsize - prevsize % tabsize nextpart = parts[i] try: totalsize = ovfcheck(totalsize + pad) totalsize = ovfcheck(totalsize + len(nextpart)) result.append(u' ' * pad) except OverflowError: raise OperationError(space.w_OverflowError, space.wrap('new string is too long')) result.append(nextpart) prevsize = 0 for ch in nextpart: prevsize += 1 if ch in (u"\n", u"\r"): prevsize = 0 return space.wrap(u''.join(result))
def _pow(space, iv, iw, iz): """Helper for pow""" if iw < 0: if iz != 0: raise oefmt(space.w_TypeError, "pow() 2nd argument cannot be negative when 3rd " "argument specified") # bounce it, since it always returns float raise ValueError temp = iv ix = 1 while iw > 0: if iw & 1: ix = ovfcheck(ix * temp) iw >>= 1 # Shift exponent down by 1 bit if iw == 0: break temp = ovfcheck(temp * temp) # Square the value of temp if iz: # If we did a multiplication, perform a modulo ix %= iz temp %= iz if iz: ix %= iz return ix
def write_v(self, cdata, w_ob, optvarsize): # a special case for var-sized C99 arrays from pypy.module._cffi_backend import ctypearray ct = self.ctype if isinstance(ct, ctypearray.W_CTypeArray) and ct.length < 0: space = ct.space w_ob, varsizelength = misc.get_new_array_length(space, w_ob) if optvarsize != -1: # in this mode, the only purpose of this function is to compute # the real size of the structure from a var-sized C99 array assert cdata == lltype.nullptr(rffi.CCHARP.TO) itemsize = ct.ctitem.size try: varsize = ovfcheck(itemsize * varsizelength) size = ovfcheck(self.offset + varsize) except OverflowError: raise OperationError(space.w_OverflowError, space.wrap("array size would overflow a ssize_t")) assert size >= 0 return max(size, optvarsize) # if 'value' was only an integer, get_new_array_length() returns # w_ob = space.w_None. Detect if this was the case, # and if so, stop here, leaving the content uninitialized # (it should be zero-initialized from somewhere else). if space.is_w(w_ob, space.w_None): return optvarsize # if optvarsize == -1: self.write(cdata, w_ob) return optvarsize
def _ll_compute_size(length, size, itemsize): try: varsize = ovfcheck(itemsize * length) tot_size = ovfcheck(size + varsize) except OverflowError: raise MemoryError() return tot_size
def _impl_int_int_pow(space, iv, iw, iz): if iw < 0: if iz != 0: raise OperationError(space.w_TypeError, space.wrap("pow() 2nd argument " "cannot be negative when 3rd argument specified")) ## bounce it, since it always returns float raise FailedToImplementArgs(space.w_ValueError, space.wrap("integer exponentiation")) temp = iv ix = 1 try: while iw > 0: if iw & 1: ix = ovfcheck(ix*temp) iw >>= 1 #/* Shift exponent down by 1 bit */ if iw==0: break temp = ovfcheck(temp*temp) #/* Square the value of temp */ if iz: #/* If we did a multiplication, perform a modulo */ ix = ix % iz; temp = temp % iz; if iz: ix = ix % iz except OverflowError: raise FailedToImplementArgs(space.w_OverflowError, space.wrap("integer exponentiation")) return ix
def malloc_varsize_slowpath(self, typeid, length, force_nonmovable=False): # For objects that are too large, or when the nursery is exhausted. # In order to keep malloc_varsize_clear() as compact as possible, # we recompute what we need in this slow path instead of passing # it all as function arguments. size_gc_header = self.gcheaderbuilder.size_gc_header nonvarsize = size_gc_header + self.fixed_size(typeid) itemsize = self.varsize_item_sizes(typeid) offset_to_length = self.varsize_offset_to_length(typeid) try: varsize = ovfcheck(itemsize * length) totalsize = ovfcheck(nonvarsize + varsize) except OverflowError: raise MemoryError() if self.has_gcptr_in_varsize(typeid): nonlarge_max = self.nonlarge_gcptrs_max else: nonlarge_max = self.nonlarge_max if force_nonmovable or raw_malloc_usage(totalsize) > nonlarge_max: result = self.malloc_varsize_marknsweep(totalsize) flags = self.GCFLAGS_FOR_NEW_EXTERNAL_OBJECTS | GCFLAG_UNVISITED else: result = self.malloc_varsize_collecting_nursery(totalsize) flags = self.GCFLAGS_FOR_NEW_YOUNG_OBJECTS self.init_gc_object(result, typeid, flags) (result + size_gc_header + offset_to_length).signed[0] = length return llmemory.cast_adr_to_ptr(result+size_gc_header, llmemory.GCREF)
def malloc_array(basesize, num_elem, itemsize, ofs_length): try: totalsize = ovfcheck(basesize + ovfcheck(itemsize * num_elem)) except OverflowError: return lltype.nullptr(llmemory.GCREF.TO) res = self.malloc_fn_ptr(totalsize) if res: arrayptr = rffi.cast(rffi.CArrayPtr(lltype.Signed), res) arrayptr[ofs_length/WORD] = num_elem return res
def f(x): try: a = ovfcheck(x + 50) except OverflowError: return 0 try: a += ovfcheck(100 + x) except OverflowError: return 1 return a
def _pow(space, x, y): """ pow - Exponential expression""" x = x.as_number(space) y = y.as_number(space) exponent_is_odd = False try: if (isinstance(x, W_IntObject) and isinstance(y, W_IntObject) and y.intval >= 0): ibase = x.intval iexp = y.intval exponent_is_odd = (iexp % 2 == 1) if iexp == 0: return space.newint(1) elif ibase == 0: return space.newint(0) assert iexp > 0 result = 1 while True: if iexp % 2: result = ovfcheck(result * ibase) iexp >>= 1 if iexp == 0: break ibase = ovfcheck(ibase * ibase) return space.newint(result) except OverflowError: pass fbase = x.float_w(space) fexp = y.float_w(space) if fbase == 0.: if fexp < 0: return space.newfloat(float('inf')) elif fexp == 0.: return space.newfloat(1.) elif math.isnan(fexp): return space.newfloat(float('nan')) else: return space.newfloat(0.) try: p = math.pow(fbase, fexp) return space.newfloat(p) except OverflowError: # NB: the following treats all integers that cannot fit into a double # (i.e. those greater than 2**53) as even, which is wrong, but matches # exactly the behavior of PHP 5.4. if not exponent_is_odd: try: exponent_is_odd = (int(fexp) % 2 != 0) except OverflowError: pass if fbase < 0 and exponent_is_odd: return space.wrap(-rfloat.INFINITY) return space.wrap(rfloat.INFINITY) except ValueError: return space.wrap(rfloat.NAN)
def add(self, offset): res = self.clone() try: res.lower = ovfcheck(res.lower + offset) except OverflowError: res.has_lower = False try: res.upper = ovfcheck(res.upper + offset) except OverflowError: res.has_upper = False return res
def ll_malloc_varsize_no_length(length, size, itemsize): try: fixsize = gc_header_offset + size varsize = ovfcheck(itemsize * length) tot_size = ovfcheck(fixsize + varsize) except OverflowError: raise MemoryError() result = mh._ll_malloc_fixedsize(tot_size) llmemory.raw_memclear(result, tot_size) result += gc_header_offset return result
def fn(x, y): res = -42 try: res = ovfcheck(x+y) except OverflowError: res = 0 try: res += ovfcheck(x+y) except OverflowError: res += 1 return res
def intModPow(self, exponent, modulus): accumulator = 1 multiplier = self._i % modulus while exponent > 0: if exponent & 1: # Odd bit. accumulator = ovfcheck(accumulator * multiplier) % modulus exponent >>= 1 if not exponent: break multiplier = ovfcheck(multiplier * multiplier) % modulus return IntObject(accumulator)
def set_shape(self, space, orig_array, new_shape): if len(new_shape) > NPY.MAXDIMS: raise oefmt(space.w_ValueError, "sequence too large; cannot be greater than %d", NPY.MAXDIMS) try: ovfcheck(support.product_check(new_shape) * self.dtype.elsize) except OverflowError as e: raise oefmt(space.w_ValueError, "array is too big.") strides, backstrides = calc_strides(new_shape, self.dtype, self.order) return SliceArray(self.start, strides, backstrides, new_shape, self, orig_array)
def intPow(self, exponent): accumulator = 1 multiplier = self._i while exponent > 0: if exponent & 1: # Odd bit. accumulator = ovfcheck(accumulator * multiplier) exponent >>= 1 if not exponent: break multiplier = ovfcheck(multiplier * multiplier) return IntObject(accumulator)
def f(y, n): m = 0 while n < 115: myjitdriver.can_enter_jit(n=n, y=y, m=m) myjitdriver.jit_merge_point(n=n, y=y, m=m) y -= 1 try: ovfcheck(-y) except OverflowError: m += 1 y += 1 n += 1 return m
def f(x, y): i = 0 while i < 10: myjitdriver.can_enter_jit(x=x, y=y, i=i) myjitdriver.jit_merge_point(x=x, y=y, i=i) try: ovfcheck(i%x) i += 1 except ZeroDivisionError: i += 1 except OverflowError: i += 2 return 0
def mul_bound(self, other): if self.has_upper and self.has_lower and \ other.has_upper and other.has_lower: try: vals = (ovfcheck(self.upper * other.upper), ovfcheck(self.upper * other.lower), ovfcheck(self.lower * other.upper), ovfcheck(self.lower * other.lower)) return IntBound(min4(vals), max4(vals)) except OverflowError: return IntUnbounded() else: return IntUnbounded()
def f(x, y, n): m = 0 while n < 100: myjitdriver.can_enter_jit(n=n, x=x, y=y, m=m) myjitdriver.jit_merge_point(n=n, x=x, y=y, m=m) y += 1 y &= (LONG_BIT-1) try: ovfcheck(x<<y) except OverflowError: m += 1 n += 1 return m
def div_bound(self, other): if self.has_upper and self.has_lower and other.has_upper and other.has_lower and not other.contains(0): try: vals = ( ovfcheck(self.upper / other.upper), ovfcheck(self.upper / other.lower), ovfcheck(self.lower / other.upper), ovfcheck(self.lower / other.lower), ) return IntBound(min4(vals), max4(vals)) except OverflowError: return IntUnbounded() else: return IntUnbounded()
def stringbuilder_grow(ll_builder, needed): allocated = ll_builder.allocated #if allocated < GROW_FAST_UNTIL: # new_allocated = allocated << 1 #else: extra_size = allocated >> 2 try: new_allocated = ovfcheck(allocated + extra_size) new_allocated = ovfcheck(new_allocated + needed) except OverflowError: raise MemoryError newbuf = mallocfn(new_allocated) copycontentsfn(ll_builder.buf, newbuf, 0, 0, ll_builder.used) ll_builder.buf = newbuf ll_builder.allocated = new_allocated
def arith_remainder_same(self, other): assert isinstance(other, values.W_Fixnum) if other.value == 0: raise Exception("zero_divisor") a = abs(self.value) b = abs(other.value) try: res = rarithmetic.ovfcheck(a % b) if self.value < 0: res = rarithmetic.ovfcheck(-res) except OverflowError: res = a % b res1 = -res if self.value < 0 else res return self.arith_mod(values.W_Bignum(rbigint.fromint(res1))) return values.W_Fixnum(res)
def lshift_bound(self, other): if self.has_upper and self.has_lower and \ other.has_upper and other.has_lower and \ other.known_ge(IntBound(0, 0)) and \ other.known_lt(IntBound(LONG_BIT, LONG_BIT)): try: vals = (ovfcheck(self.upper << other.upper), ovfcheck(self.upper << other.lower), ovfcheck(self.lower << other.upper), ovfcheck(self.lower << other.lower)) return IntBound(min4(vals), max4(vals)) except (OverflowError, ValueError): return IntUnbounded() else: return IntUnbounded()
def mod_ovf_zer(x, y): try: return ovfcheck(x%y) except OverflowError: return 42 except ZeroDivisionError: return 84
def op_int_lshift_ovf(self, x, y): assert is_valid_int(x) assert is_valid_int(y) try: return ovfcheck(x << y) except OverflowError: self.make_llexception()
def to_int(self, s, neg, i, radix): val = 0 for digit in self._digits(s, i, radix): val = ovfcheck(val * radix + digit) if neg: val = -val return val
def arith_shl_same(self, other): assert isinstance(other, values.W_Fixnum) try: res = rarithmetic.ovfcheck(self.value << other.value) except OverflowError: return self.arith_shl(values.W_Bignum(rbigint.fromint(other.value))) return values.W_Fixnum(res)
def f(x, y): try: return ovfcheck(x // y) except OverflowError: return -42 except ZeroDivisionError: return -43
def add_operator(name, arity, dispatch=None, pyfunc=None, pure=False, ovf=False): operator_func = getattr(operator, name, None) if dispatch == 1: bases = [SingleDispatchMixin] elif dispatch == 2: bases = [DoubleDispatchMixin] else: bases = [] if ovf: assert pure base_cls = OverflowingOperation elif pure: base_cls = PureOperation else: base_cls = HLOperation bases.append(base_cls) cls = HLOperationMeta(name, tuple(bases), {'opname': name, 'arity': arity, 'canraise': [], 'dispatch': dispatch}) if pyfunc is not None: func2op[pyfunc] = cls if operator_func: func2op[operator_func] = cls if pyfunc is not None: cls.pyfunc = staticmethod(pyfunc) elif operator_func is not None: cls.pyfunc = staticmethod(operator_func) if ovf: from rpython.rlib.rarithmetic import ovfcheck ovf_func = lambda *args: ovfcheck(cls.pyfunc(*args)) add_operator(name + '_ovf', arity, dispatch, pyfunc=ovf_func) cls.ovf_variant = getattr(op, name + '_ovf')
def sub(self, other): if isinstance(other, W_IntObject): try: return W_IntObject(ovfcheck(self.intval - other.intval)) except OverflowError: b_i = rbigint.fromint(self.intval) o_i = rbigint.fromint(other.intval) return W_BigIntObject(b_i.sub(o_i)) elif isinstance(other, W_BigIntObject): return self.to_bigint().sub(other) else: assert isinstance(other, W_FloatObject) return W_FloatObject(self.tofloat()).sub(other)
def replace(input, sub, by, maxsplit=-1): if isinstance(input, str): Builder = StringBuilder elif isinstance(input, unicode): Builder = UnicodeBuilder else: assert isinstance(input, list) Builder = ByteListBuilder if maxsplit == 0: return input if not sub: upper = len(input) if maxsplit > 0 and maxsplit < upper + 2: upper = maxsplit - 1 assert upper >= 0 try: result_size = ovfcheck(upper * len(by)) result_size = ovfcheck(result_size + upper) result_size = ovfcheck(result_size + len(by)) remaining_size = len(input) - upper result_size = ovfcheck(result_size + remaining_size) except OverflowError: raise builder = Builder(result_size) for i in range(upper): builder.append(by) builder.append(input[i]) builder.append(by) builder.append_slice(input, upper, len(input)) else: # First compute the exact result size cnt = count(input, sub, 0, len(input)) if cnt > maxsplit and maxsplit > 0: cnt = maxsplit diff_len = len(by) - len(sub) try: result_size = ovfcheck(diff_len * cnt) result_size = ovfcheck(result_size + len(input)) except OverflowError: raise builder = Builder(result_size) start = 0 sublen = len(sub) while maxsplit != 0: next = find(input, sub, start, len(input)) if next < 0: break builder.append_slice(input, start, next) builder.append(by) start = next + sublen maxsplit -= 1 # NB. if it's already < 0, it stays < 0 builder.append_slice(input, start, len(input)) return builder.build()
def _pow_nomod(iv, iw): if iw <= 0: if iw == 0: return 1 # bounce it, since it always returns float raise ValueError temp = iv ix = 1 while True: if iw & 1: try: ix = ovfcheck(ix * temp) except OverflowError: raise iw >>= 1 # Shift exponent down by 1 bit if iw == 0: break try: temp = ovfcheck(temp * temp) # Square the value of temp except OverflowError: raise return ix
def typeoffsetof_index(self, index): space = self.space ctitem = self.ctitem if ctitem.size < 0: raise OperationError(space.w_TypeError, space.wrap("pointer to opaque")) try: offset = ovfcheck(index * ctitem.size) except OverflowError: raise OperationError( space.w_OverflowError, space.wrap("array offset would overflow a ssize_t")) return ctitem, offset
def ll_mul(RESLIST, l, factor): length = l.ll_length() if factor < 0: factor = 0 try: resultlen = ovfcheck(length * factor) except OverflowError: raise MemoryError res = RESLIST.ll_newlist(resultlen) j = 0 while j < resultlen: ll_arraycopy(l, res, 0, j, length) j += length return res
def determine_repetitions(self, space, idx): end = idx + 1 repetitions = 0 while end < len(self.fmt) and self.fmt[end].isdigit(): try: repetitions = ovfcheck(repetitions * 10 + (ord(self.fmt[end]) - ord("0"))) except OverflowError: raise space.error(space.w_RangeError, "pack length too big") end += 1 if end == idx + 1: # No explicit repetitions definition repetitions = 1 return (repetitions, end - 1)
def lshift(self, space, shift): # shift > 0, therefore the highest bit of upperbound is not set, # i.e. upperbound is positive upperbound = intmask(r_uint(-1) >> shift) if 0 <= self.value <= upperbound: shifted = intmask(self.value << shift) return space.wrap_positive_32bit_int(shifted) else: try: shifted = ovfcheck(self.value << shift) except OverflowError: raise error.PrimitiveFailedError() return space.wrap_int(shifted) raise PrimitiveFailedError
def func(interp, s_frame, receiver, shift): if isinstance(receiver, int): if not (int_between(-constants.LONG_BIT + 1, shift, constants.LONG_BIT)): raise PrimitiveFailedError if shift >= 0: if isinstance(receiver, int): try: if receiver >= 0: return interp.space.wrap_int(r_uint(ovfcheck(receiver << shift))) else: return interp.space.wrap_int((ovfcheck(receiver << shift))) except OverflowError: raise PrimitiveFailedError elif isinstance(receiver, rbigint): return interp.space.wrap_rbigint(receiver.lshift(shift)) else: raise PrimitiveFailedError # no point in trying else: shift = -shift if isinstance(receiver, int) or isinstance(receiver, r_int64): return interp.space.wrap_int(receiver >> shift) else: return interp.space.wrap_rbigint(receiver.rshift(shift))
def descr_rbinop(self, space, w_other): if not isinstance(w_other, W_IntObject): return space.w_NotImplemented x = self.intval y = w_other.intval if ovf: try: z = ovfcheck(op(y, x)) except OverflowError: return ovf2long(space, y, x) else: z = op(y, x) return wrapint(space, z)
def _mul_helper(self, space, w_repeat, is_inplace): try: repeat = space.getindex_w(w_repeat, space.w_OverflowError) except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise if is_inplace: a = self start = 1 else: a = self.constructor(space) start = 0 if repeat <= start: if repeat <= 0: a.setlen(0, overallocate=False) return a oldlen = self.len try: newlen = ovfcheck(oldlen * repeat) except OverflowError: raise MemoryError # srcbuf = self._buffer srcsize = self.len * self.itemsize for i in range(srcsize): if srcbuf[i] != '\x00': break else: # the source is entirely zero: initialize the target # with zeroes too a.setlen(newlen, zero=True, overallocate=False) return a # a.setlen(newlen, overallocate=False) srcbuf = self._buffer # reload this, in case self is a if oldlen == 1: self._repeat_single_item(a, start, repeat) else: dstbuf = a._buffer if start == 1: dstbuf = rffi.ptradd(dstbuf, srcsize) for r in range(start, repeat): rffi.c_memcpy(rffi.cast(rffi.VOIDP, dstbuf), rffi.cast(rffi.VOIDP, srcbuf), srcsize) dstbuf = rffi.ptradd(dstbuf, srcsize) keepalive_until_here(self) keepalive_until_here(a) return a
def __init__(self, left, right, balanced=False): self.left = left self.right = right try: self.len = ovfcheck(left.length() + right.length()) except OverflowError: raise self._depth = 0 # XXX the balance should become part of the depth self.balanced = balanced if balanced: self.balance_known = True else: self.balance_known = False
def descr_expandtabs(self, space, tabsize=8): value = self._val(space) if not value: return self._empty() if self._use_rstr_ops(space, self): splitted = value.split(self._chr('\t')) else: splitted = split(value, self._chr('\t')) try: if tabsize > 0: ovfcheck(len(splitted) * tabsize) except OverflowError: raise oefmt(space.w_OverflowError, "new string is too long") expanded = oldtoken = splitted.pop(0) for token in splitted: expanded += self._multi_chr(self._chr(' ')) * self._tabindent( oldtoken, tabsize) + token oldtoken = token return self._new(expanded)
def method_left_shift(self, space, other): if other < 0: return space.newint(self.intvalue >> -other) elif other >= LONG_BIT: return space.send(space.newbigint_fromint(self.intvalue), "<<", [space.newint(other)]) else: try: value = ovfcheck(self.intvalue << other) except OverflowError: return space.send(space.newbigint_fromint(self.intvalue), "<<", [space.newint(other)]) else: return space.newint(value)
def prim_add(self, right, universe): if isinstance(right, BigInteger): return universe.new_biginteger(right.get_embedded_biginteger().add( rbigint.fromint(self._embedded_integer))) elif isinstance(right, Double): return self._to_double(universe).prim_add(right, universe) else: l = self._embedded_integer r = right.get_embedded_integer() try: result = ovfcheck(l + r) return universe.new_integer(result) except OverflowError: return universe.new_biginteger( rbigint.fromint(l).add(rbigint.fromint(r)))
def descr_fromfile(self, space, w_f, n): """ fromfile(f, n) Read n objects from the file object f and append them to the end of the array. """ try: size = ovfcheck(self.itemsize * n) except OverflowError: raise MemoryError w_item = space.call_method(w_f, 'read', space.newint(size)) item = space.bytes_w(w_item) self._frombytes(space, item) if len(item) < size: raise oefmt(space.w_EOFError, "not enough items in file")
def ll_extend_with_char_count(lst, char, count): if count <= 0: return len1 = lst.ll_length() try: newlength = ovfcheck(len1 + count) except OverflowError: raise MemoryError lst._ll_resize_ge(newlength) j = len1 if listItemType(lst) is UniChar: char = unichr(ord(char)) while j < newlength: lst.ll_setitem_fast(j, char) j += 1
def _prepare_pointer_call_argument(self, w_init, cdata, keepalives, i): space = self.space if self.accept_str and space.isinstance_w(w_init, space.w_bytes): # special case to optimize strings passed to a "char *" argument value = space.bytes_w(w_init) if isinstance(self.ctitem, ctypeprim.W_CTypePrimitiveBool): self._must_be_string_of_zero_or_one(value) keepalives[i] = value buf, buf_flag = rffi.get_nonmovingbuffer_final_null(value) rffi.cast(rffi.CCHARPP, cdata)[0] = buf return ord(buf_flag) # 4, 5 or 6 # 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 oefmt(space.w_OverflowError, "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 opimpl(self, oparg, next_instr): space = self.space space_op = getattr(space, spaceopname) w_2 = self.popvalue() w_1 = self.popvalue() if type(w_1) is W_IntObject and type(w_2) is W_IntObject: try: z = ovfcheck(op(w_1.intval, w_2.intval)) except OverflowError: w_result = int_op(w_1, space, w_2) else: w_result = space.newint(z) else: w_result = space_op(w_1, w_2) self.pushvalue(w_result)
def _leftShift(ivkbl, frame, interpreter): right = frame.pop() left = frame.pop() universe = interpreter.get_universe() assert isinstance(right, Integer) l = left.get_embedded_integer() r = right.get_embedded_integer() try: if not (l == 0 or 0 <= r < LONG_BIT): raise OverflowError result = ovfcheck(l << r) frame.push(universe.new_integer(result)) except OverflowError: frame.push(universe.new_biginteger( rbigint.fromint(l).lshift(r)))
def descr_rbinop(self, space, w_other): if isinstance(w_other, W_IntObject): x = self.intval y = w_other.intval if ovf: try: z = ovfcheck(op(y, x)) except OverflowError: return ovf2long(space, y, x) else: z = op(y, x) return wrapint(space, z) elif isinstance(w_other, W_AbstractIntObject): self = self.as_w_long(space) return getattr(self, descr_rname)(space, w_other) return space.w_NotImplemented
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_bytes): # from a string, we add the null terminator s = space.bytes_w(w_init) length = len(s) + 1 elif space.isinstance_w(w_init, space.w_unicode): from pypy.module._cffi_backend import wchar_helper w_u = space.convert_arg_to_w_unicode(w_init) if self.ctitem.size == 2: length = wchar_helper.utf8_size_as_char16(w_u._utf8) else: length = w_u._len() length += 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 oefmt(space.w_OverflowError, "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 ll_extend_with_str_slice_minusone_unsafe(lst, s, getstrlen, getstritem): len1 = lst.ll_length() len2m1 = getstrlen(s) - 1 try: newlength = ovfcheck(len1 + len2m1) except OverflowError: raise MemoryError lst._ll_resize_ge(newlength) i = 0 j = len1 while i < len2m1: c = getstritem(s, i) if listItemType(lst) is UniChar: c = unichr(ord(c)) lst.ll_setitem_fast_unsafe(j, c) i += 1 j += 1
def ll_inplace_mul(l, factor): if factor == 1: return l length = l.ll_length() if factor < 0: factor = 0 try: resultlen = ovfcheck(length * factor) except OverflowError: raise MemoryError res = l res._ll_resize(resultlen) j = length while j < resultlen: ll_arraycopy(l, res, 0, j, length) j += length return res
def ll_listsetslice_unsafe(l1, start, stop, l2): len1 = l1.ll_length() len2 = l2.ll_length() if len2 == stop - start: ll_arraycopy(l2, l1, 0, start, len2) elif len2 < stop - start: ll_arraycopy(l2, l1, 0, start, len2) ll_arraycopy(l1, l1, stop, start + len2, len1 - stop) l1._ll_resize_le(len1 + len2 - (stop - start)) else: # len2 > stop - start: try: newlength = ovfcheck(len1 + len2) except OverflowError: raise MemoryError l1._ll_resize_ge(newlength) ll_arraycopy(l1, l1, stop, start + len2, len1 - stop) ll_arraycopy(l2, l1, 0, start, len2)
def method(self, space, w_other): if space.is_kind_of(w_other, space.w_fixnum): other = space.int_w(w_other) try: value = ovfcheck(func(self.intvalue, other)) except OverflowError: return space.send( space.newbigint_fromint(self.intvalue), name, [w_other] ) else: return space.newint(value) elif space.is_kind_of(w_other, space.w_bignum): return space.send(space.newbigint_fromint(self.intvalue), name, [w_other]) elif space.is_kind_of(w_other, space.w_float): return space.newfloat(func(self.intvalue, space.float_w(w_other))) else: return W_NumericObject.retry_binop_coercing(space, self, w_other, name)
def ll_extend_with_str_slice_startonly_unsafe(lst, s, getstrlen, getstritem, start): len1 = lst.ll_length() len2 = getstrlen(s) count2 = len2 - start try: newlength = ovfcheck(len1 + count2) except OverflowError: raise MemoryError lst._ll_resize_ge(newlength) i = start j = len1 while i < len2: c = getstritem(s, i) if listItemType(lst) is UniChar: c = unichr(ord(c)) lst.ll_setitem_fast_unsafe(j, c) i += 1 j += 1
def check_for_interrupts(self, s_frame): display = self.space.display() if display: display.render() # parallel to Interpreter>>#checkForInterrupts # 1. profiling is done using rvmprof # 2. use the same time value as the primitive UTC_MICROSECOND_CLOCK now = self.time_now() # 3. adjust the check counter size, we want to land between 20ms and 100ms diff = now - self.last_check if diff < 20000 and self.interrupt_counter_size != constants.MAXINT: try: self.interrupt_counter_size = ovfcheck( self.interrupt_counter_size * 2) except OverflowError: self.interrupt_counter_size = constants.MAXINT elif diff > 100000 and self.interrupt_counter_size > 100: self.interrupt_counter_size = max(self.interrupt_counter_size / 2, 100) self.last_check = now self.forced_interrupt_checks_count += 1 # 4. check for User Interrupt if self.space.display().has_interrupts_pending(): w_interrupt_sema = self.space.w_interrupt_semaphore() if w_interrupt_sema is not self.space.w_nil: assert isinstance(w_interrupt_sema, W_PointersObject) wrapper.SemaphoreWrapper(self.space, w_interrupt_sema).signal(s_frame, forced=True) # 5. the low space semaphore is signalled in ClassShadow#new # 6. signal the timer if not self.next_wakeup_tick == 0 and now >= self.next_wakeup_tick: self.next_wakeup_tick = 0 semaphore = self.space.w_timerSemaphore() if not semaphore.is_nil(self.space): assert isinstance(semaphore, W_PointersObject) wrapper.SemaphoreWrapper(self.space, semaphore).signal(s_frame, forced=False)
def ll_extend_with_str_slice_startonly(lst, s, getstrlen, getstritem, start): len1 = lst.ll_length() len2 = getstrlen(s) count2 = len2 - start ll_assert(start >= 0, "unexpectedly negative str slice start") assert count2 >= 0, "str slice start larger than str length" try: newlength = ovfcheck(len1 + count2) except OverflowError: raise MemoryError lst._ll_resize_ge(newlength) i = start j = len1 while i < len2: c = getstritem(s, i) if listItemType(lst) is UniChar: c = unichr(ord(c)) lst.ll_setitem_fast(j, c) i += 1 j += 1
def descr_fromfile(self, space, w_f, n): """ fromfile(f, n) Read n objects from the file object f and append them to the end of the array. Also called as read. """ try: size = ovfcheck(self.itemsize * n) except OverflowError: raise MemoryError w_item = space.call_method(w_f, 'read', space.newint(size)) item = space.bytes_w(w_item) if len(item) < size: n = len(item) % self.itemsize elems = max(0, len(item) - (len(item) % self.itemsize)) if n != 0: item = item[0:elems] self.descr_fromstring(space, space.newbytes(item)) raise oefmt(space.w_EOFError, "not enough items in file") self.descr_fromstring(space, w_item)
def ll_str_mul(s, times): if times < 0: times = 0 try: size = ovfcheck(len(s.chars) * times) except OverflowError: raise MemoryError newstr = s.malloc(size) i = 0 if i < size: s.copy_contents(s, newstr, 0, 0, len(s.chars)) i += len(s.chars) while i < size: if i <= size - i: j = i else: j = size - i s.copy_contents(newstr, newstr, 0, i, j) i += j return newstr