| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import sys |
| 5 |
from pypy.interpreter.gateway import unwrap_spec |
| 6 |
from pypy.interpreter.error import oefmt |
| 7 |
from pypy.interpreter.argument import Arguments |
| 8 |
from rpython.rlib import rutf8, rbigint |
| 9 |
from rpython.rlib.rstruct import ieee |
| 10 |
from rpython.rtyper.lltypesystem import rffi |
| 11 |
|
| 12 |
from pypy.module.array import interp_array |
| 13 |
|
| 14 |
UNKNOWN_FORMAT = -1 |
| 15 |
UNSIGNED_INT8 = 0 |
| 16 |
SIGNED_INT8 = 1 |
| 17 |
UNSIGNED_INT16_LE = 2 |
| 18 |
UNSIGNED_INT16_BE = 3 |
| 19 |
SIGNED_INT16_LE = 4 |
| 20 |
SIGNED_INT16_BE = 5 |
| 21 |
UNSIGNED_INT32_LE = 6 |
| 22 |
UNSIGNED_INT32_BE = 7 |
| 23 |
SIGNED_INT32_LE = 8 |
| 24 |
SIGNED_INT32_BE = 9 |
| 25 |
UNSIGNED_INT64_LE = 10 |
| 26 |
UNSIGNED_INT64_BE = 11 |
| 27 |
SIGNED_INT64_LE = 12 |
| 28 |
SIGNED_INT64_BE = 13 |
| 29 |
IEEE_754_FLOAT_LE = 14 |
| 30 |
IEEE_754_FLOAT_BE = 15 |
| 31 |
IEEE_754_DOUBLE_LE = 16 |
| 32 |
IEEE_754_DOUBLE_BE = 17 |
| 33 |
UTF16_LE = 18 |
| 34 |
UTF16_BE = 19 |
| 35 |
UTF32_LE = 20 |
| 36 |
UTF32_BE = 21 |
| 37 |
|
| 38 |
IS_BIG_ENDIAN = sys.byteorder == 'big' |
| 39 |
|
| 40 |
class MachineFormat(object): |
| 41 |
def __init__(self, bytes, signed, big_endian): |
| 42 |
self.bytes = bytes |
| 43 |
self.signed = signed |
| 44 |
self.big_endian = big_endian |
| 45 |
|
| 46 |
format_descriptors = { |
| 47 |
UNSIGNED_INT8: MachineFormat(1, False, False), |
| 48 |
SIGNED_INT8: MachineFormat(1, True, False), |
| 49 |
UNSIGNED_INT16_LE: MachineFormat(2, False, False), |
| 50 |
UNSIGNED_INT16_BE: MachineFormat(2, False, True), |
| 51 |
SIGNED_INT16_LE: MachineFormat(2, True, False), |
| 52 |
SIGNED_INT16_BE: MachineFormat(2, True, True), |
| 53 |
UNSIGNED_INT32_LE: MachineFormat(4, False, False), |
| 54 |
UNSIGNED_INT32_BE: MachineFormat(4, False, True), |
| 55 |
SIGNED_INT32_LE: MachineFormat(4, True, False), |
| 56 |
SIGNED_INT32_BE: MachineFormat(4, True, True), |
| 57 |
UNSIGNED_INT64_LE: MachineFormat(8, False, False), |
| 58 |
UNSIGNED_INT64_BE: MachineFormat(8, False, True), |
| 59 |
SIGNED_INT64_LE: MachineFormat(8, True, False), |
| 60 |
SIGNED_INT64_BE: MachineFormat(8, True, True), |
| 61 |
IEEE_754_FLOAT_LE: MachineFormat(4, False, False), |
| 62 |
IEEE_754_FLOAT_BE: MachineFormat(4, False, True), |
| 63 |
IEEE_754_DOUBLE_LE: MachineFormat(8, False, False), |
| 64 |
IEEE_754_DOUBLE_BE: MachineFormat(8, False, True), |
| 65 |
UTF16_LE: MachineFormat(4, False, False), |
| 66 |
UTF16_BE: MachineFormat(4, False, True), |
| 67 |
UTF32_LE: MachineFormat(8, False, False), |
| 68 |
UTF32_BE: MachineFormat(8, False, True), |
| 69 |
} |
| 70 |
MACHINE_FORMAT_CODE_MIN = min(format_descriptors) |
| 71 |
MACHINE_FORMAT_CODE_MAX = max(format_descriptors) |
| 72 |
|
| 73 |
|
| 74 |
@unwrap_spec(typecode='text', mformat_code=int) |
| 75 |
def array_reconstructor(space, w_cls, typecode, mformat_code, w_items): |
| 76 |
|
| 77 |
|
| 78 |
if mformat_code == typecode_to_mformat_code(typecode): |
| 79 |
return interp_array.w_array( |
| 80 |
space, w_cls, typecode, Arguments(space, [w_items])) |
| 81 |
|
| 82 |
if typecode not in interp_array.types: |
| 83 |
raise oefmt(space.w_ValueError, "invalid type code") |
| 84 |
if (mformat_code < MACHINE_FORMAT_CODE_MIN or |
| 85 |
mformat_code > MACHINE_FORMAT_CODE_MAX): |
| 86 |
raise oefmt(space.w_ValueError, "invalid machine format code") |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
if (mformat_code == IEEE_754_FLOAT_LE or |
| 93 |
mformat_code == IEEE_754_FLOAT_BE or |
| 94 |
mformat_code == IEEE_754_DOUBLE_LE or |
| 95 |
mformat_code == IEEE_754_DOUBLE_BE): |
| 96 |
|
| 97 |
descr = format_descriptors[mformat_code] |
| 98 |
memstr = space.bytes_w(w_items) |
| 99 |
step = descr.bytes |
| 100 |
converted_items = [ |
| 101 |
space.newfloat(ieee.unpack_float( |
| 102 |
memstr[i:i+step], |
| 103 |
descr.big_endian)) |
| 104 |
for i in range(0, len(memstr), step)] |
| 105 |
w_converted_items = space.newlist(converted_items) |
| 106 |
|
| 107 |
elif mformat_code == UTF16_LE: |
| 108 |
w_converted_items = space.call_method( |
| 109 |
w_items, "decode", space.newtext("utf-16-le")) |
| 110 |
elif mformat_code == UTF16_BE: |
| 111 |
w_converted_items = space.call_method( |
| 112 |
w_items, "decode", space.newtext("utf-16-be")) |
| 113 |
elif mformat_code == UTF32_LE: |
| 114 |
w_converted_items = space.call_method( |
| 115 |
w_items, "decode", space.newtext("utf-32-le")) |
| 116 |
elif mformat_code == UTF32_BE: |
| 117 |
w_converted_items = space.call_method( |
| 118 |
w_items, "decode", space.newtext("utf-32-be")) |
| 119 |
else: |
| 120 |
descr = format_descriptors[mformat_code] |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
for tc in interp_array.unroll_typecodes: |
| 131 |
typecode_descr = interp_array.types[tc] |
| 132 |
if (typecode_descr.is_integer_type() and |
| 133 |
typecode_descr.bytes == descr.bytes and |
| 134 |
typecode_descr.signed == descr.signed): |
| 135 |
typecode = tc |
| 136 |
break |
| 137 |
|
| 138 |
memstr = space.bytes_w(w_items) |
| 139 |
step = descr.bytes |
| 140 |
converted_items = [ |
| 141 |
space.newlong_from_rbigint(rbigint.rbigint.frombytes( |
| 142 |
memstr[i:i+step], |
| 143 |
descr.big_endian and 'big' or 'little', |
| 144 |
descr.signed)) |
| 145 |
for i in range(0, len(memstr), step)] |
| 146 |
w_converted_items = space.newlist(converted_items) |
| 147 |
|
| 148 |
return interp_array.w_array( |
| 149 |
space, w_cls, typecode, Arguments(space, [w_converted_items])) |
| 150 |
|
| 151 |
def typecode_to_mformat_code(typecode): |
| 152 |
intsize = 0 |
| 153 |
if typecode == 'b': |
| 154 |
return SIGNED_INT8 |
| 155 |
elif typecode == 'B': |
| 156 |
return UNSIGNED_INT8 |
| 157 |
elif typecode == 'u': |
| 158 |
if rutf8.MAXUNICODE == 0xffff: |
| 159 |
return UTF16_LE + IS_BIG_ENDIAN |
| 160 |
else: |
| 161 |
return UTF32_LE + IS_BIG_ENDIAN |
| 162 |
elif typecode == 'f': |
| 163 |
return IEEE_754_FLOAT_LE + IS_BIG_ENDIAN |
| 164 |
elif typecode == 'd': |
| 165 |
return IEEE_754_DOUBLE_LE + IS_BIG_ENDIAN |
| 166 |
|
| 167 |
elif typecode == 'h': |
| 168 |
intsize = rffi.sizeof(rffi.SHORT) |
| 169 |
is_signed = True |
| 170 |
elif typecode == 'H': |
| 171 |
intsize = rffi.sizeof(rffi.SHORT) |
| 172 |
is_signed = False |
| 173 |
elif typecode == 'i': |
| 174 |
intsize = rffi.sizeof(rffi.INT) |
| 175 |
is_signed = True |
| 176 |
elif typecode == 'I': |
| 177 |
intsize = rffi.sizeof(rffi.INT) |
| 178 |
is_signed = False |
| 179 |
elif typecode == 'l': |
| 180 |
intsize = rffi.sizeof(rffi.LONG) |
| 181 |
is_signed = True |
| 182 |
elif typecode == 'L': |
| 183 |
intsize = rffi.sizeof(rffi.LONG) |
| 184 |
is_signed = False |
| 185 |
elif typecode == 'q': |
| 186 |
intsize = rffi.sizeof(rffi.LONGLONG) |
| 187 |
is_signed = True |
| 188 |
elif typecode == 'Q': |
| 189 |
intsize = rffi.sizeof(rffi.LONGLONG) |
| 190 |
is_signed = False |
| 191 |
else: |
| 192 |
return UNKNOWN_FORMAT |
| 193 |
if intsize == 2: |
| 194 |
return UNSIGNED_INT16_LE + IS_BIG_ENDIAN + (2 * is_signed) |
| 195 |
elif intsize == 4: |
| 196 |
return UNSIGNED_INT32_LE + IS_BIG_ENDIAN + (2 * is_signed) |
| 197 |
elif intsize == 8: |
| 198 |
return UNSIGNED_INT64_LE + IS_BIG_ENDIAN + (2 * is_signed) |
| 199 |
return UNKNOWN_FORMAT |