[pypy-dev] [pypy-svn] r75824 - in pypy/branch/interplevel-array/pypy/module/array: . test
Maciej Fijalkowski
fijall at gmail.com
Sun Jul 4 22:25:30 CEST 2010
> +
> + def item_w(self, w_item):
> + space=self.space
> + if self.typecode == 'c':
> + return self.space.str_w(w_item)
> + elif self.typecode == 'u':
> + return self.space.unicode_w(w_item)
> +
> + elif self.typecode == 'b':
> + item=self.space.int_w(w_item)
> + if item<-128:
> + msg='signed char is less than minimum'
> + raise OperationError(space.w_OverflowError, space.wrap(msg))
> + elif item>127:
> + msg='signed char is greater than maximum'
> + raise OperationError(space.w_OverflowError, space.wrap(msg))
> + return rffi.cast(rffi.SIGNEDCHAR, item)
> + elif self.typecode == 'B':
> + item=self.space.int_w(w_item)
> + if item<0:
> + msg='unsigned byte integer is less than minimum'
> + raise OperationError(space.w_OverflowError, space.wrap(msg))
> + elif item>255:
> + msg='unsigned byte integer is greater than maximum'
> + raise OperationError(space.w_OverflowError, space.wrap(msg))
> + return rffi.cast(rffi.UCHAR, item)
> +
> + elif self.typecode == 'h':
> + item=self.space.int_w(w_item)
> + if item<-32768:
> + msg='signed short integer is less than minimum'
> + raise OperationError(space.w_OverflowError, space.wrap(msg))
> + elif item>32767:
> + msg='signed short integer is greater than maximum'
> + raise OperationError(space.w_OverflowError, space.wrap(msg))
> + return rffi.cast(rffi.SHORT, item)
> + elif self.typecode == 'H':
> + item=self.space.int_w(w_item)
> + if item<0:
> + msg='unsigned short integer is less than minimum'
> + raise OperationError(space.w_OverflowError, space.wrap(msg))
> + elif item>65535:
> + msg='unsigned short integer is greater than maximum'
> + raise OperationError(space.w_OverflowError, space.wrap(msg))
> + return rffi.cast(rffi.USHORT, item)
> +
> + elif self.typecode in ('i', 'l'):
> + item=self.space.int_w(w_item)
> + if item<-2147483648:
> + msg='signed integer is less than minimum'
> + raise OperationError(space.w_OverflowError, space.wrap(msg))
> + elif item>2147483647:
> + msg='signed integer is greater than maximum'
> + raise OperationError(space.w_OverflowError, space.wrap(msg))
> + return rffi.cast(lltype.Signed, item)
> + elif self.typecode in ('I', 'L'):
> + item=self.space.int_w(w_item)
> + if item<0:
> + msg='unsigned integer is less than minimum'
> + raise OperationError(space.w_OverflowError, space.wrap(msg))
> + elif item>4294967295:
> + msg='unsigned integer is greater than maximum'
> + raise OperationError(space.w_OverflowError, space.wrap(msg))
> + return rffi.cast(lltype.Unsigned, item)
> +
> + elif self.typecode == 'f':
> + item=self.space.float_w(w_item)
> + return rffi.cast(lltype.SingleFloat, item)
> + elif self.typecode == 'd':
> + return self.space.float_w(w_item)
> +
Hey. This looks a bit ugly, you can definitely do it with some
constant dict or something (we have special support for iterating over
constants and unrolling the iteration, look for unrolling_iterable).
Also, annotator can fold a bunch of ifs into a switch, but not if "in"
operator is used (or is fine though).
More information about the Pypy-dev
mailing list