Ejemplo n.º 1
0
def new_void_dtype(space, size):
    return W_Dtype(
        types.VoidType(),
        elsize=size,
        num=NPY.VOID,
        kind=NPY.VOIDLTR,
        char=NPY.VOIDLTR,
        w_box_type=space.gettypefor(boxes.W_VoidBox),
    )
Ejemplo n.º 2
0
def variable_dtype(space, name):
    if name[0] in '<>=':
        name = name[1:]
    char = name[0]
    if len(name) == 1:
        size = 0
    else:
        try:
            size = int(name[1:])
        except ValueError:
            raise OperationError(space.w_TypeError,
                                 space.wrap("data type not understood"))
    if char == NPY_CHARLTR:
        char = NPY_STRINGLTR
        size = 1

    if char == NPY_STRINGLTR:
        itemtype = types.StringType(size)
        basename = 'string'
        num = NPY_STRING
        w_box_type = space.gettypefor(interp_boxes.W_StringBox)
    elif char == NPY_VOIDLTR:
        itemtype = types.VoidType(size)
        basename = 'void'
        num = NPY_VOID
        w_box_type = space.gettypefor(interp_boxes.W_VoidBox)
    elif char == NPY_UNICODELTR:
        itemtype = types.UnicodeType(size)
        basename = 'unicode'
        num = NPY_UNICODE
        w_box_type = space.gettypefor(interp_boxes.W_UnicodeBox)
    else:
        assert False

    return W_Dtype(itemtype, num, char,
                   basename + str(8 * itemtype.get_element_size()), char,
                   w_box_type)
Ejemplo n.º 3
0
    def __init__(self, space):
        self.w_booldtype = W_Dtype(
            types.Bool(),
            num=NPY_BOOL,
            kind=NPY_GENBOOLLTR,
            name="bool",
            char=NPY_BOOLLTR,
            w_box_type=space.gettypefor(interp_boxes.W_BoolBox),
            alternate_constructors=[space.w_bool],
            aliases=['bool8'],
        )
        self.w_int8dtype = W_Dtype(
            types.Int8(),
            num=NPY_BYTE,
            kind=NPY_SIGNEDLTR,
            name="int8",
            char=NPY_BYTELTR,
            w_box_type=space.gettypefor(interp_boxes.W_Int8Box),
            aliases=['byte'],
        )
        self.w_uint8dtype = W_Dtype(
            types.UInt8(),
            num=NPY_UBYTE,
            kind=NPY_UNSIGNEDLTR,
            name="uint8",
            char=NPY_UBYTELTR,
            w_box_type=space.gettypefor(interp_boxes.W_UInt8Box),
            aliases=['ubyte'],
        )
        self.w_int16dtype = W_Dtype(
            types.Int16(),
            num=NPY_SHORT,
            kind=NPY_SIGNEDLTR,
            name="int16",
            char=NPY_SHORTLTR,
            w_box_type=space.gettypefor(interp_boxes.W_Int16Box),
            aliases=['short'],
        )
        self.w_uint16dtype = W_Dtype(
            types.UInt16(),
            num=NPY_USHORT,
            kind=NPY_UNSIGNEDLTR,
            name="uint16",
            char=NPY_USHORTLTR,
            w_box_type=space.gettypefor(interp_boxes.W_UInt16Box),
            aliases=['ushort'],
        )
        self.w_int32dtype = W_Dtype(
            types.Int32(),
            num=NPY_INT,
            kind=NPY_SIGNEDLTR,
            name="int32",
            char=NPY_INTLTR,
            w_box_type=space.gettypefor(interp_boxes.W_Int32Box),
        )
        self.w_uint32dtype = W_Dtype(
            types.UInt32(),
            num=NPY_UINT,
            kind=NPY_UNSIGNEDLTR,
            name="uint32",
            char=NPY_UINTLTR,
            w_box_type=space.gettypefor(interp_boxes.W_UInt32Box),
        )
        self.w_longdtype = W_Dtype(
            types.Long(),
            num=NPY_LONG,
            kind=NPY_SIGNEDLTR,
            name="int%d" % LONG_BIT,
            char=NPY_LONGLTR,
            w_box_type=space.gettypefor(interp_boxes.W_LongBox),
            alternate_constructors=[
                space.w_int,
                space.gettypefor(interp_boxes.W_IntegerBox),
                space.gettypefor(interp_boxes.W_SignedIntegerBox),
            ],
            aliases=['int'],
        )
        self.w_ulongdtype = W_Dtype(
            types.ULong(),
            num=NPY_ULONG,
            kind=NPY_UNSIGNEDLTR,
            name="uint%d" % LONG_BIT,
            char=NPY_ULONGLTR,
            w_box_type=space.gettypefor(interp_boxes.W_ULongBox),
            alternate_constructors=[
                space.gettypefor(interp_boxes.W_UnsignedIntegerBox),
            ],
            aliases=['uint'],
        )
        self.w_int64dtype = W_Dtype(
            types.Int64(),
            num=NPY_LONGLONG,
            kind=NPY_SIGNEDLTR,
            name="int64",
            char=NPY_LONGLONGLTR,
            w_box_type=space.gettypefor(interp_boxes.W_Int64Box),
            alternate_constructors=[space.w_long],
            aliases=['longlong'],
        )
        self.w_uint64dtype = W_Dtype(
            types.UInt64(),
            num=NPY_ULONGLONG,
            kind=NPY_UNSIGNEDLTR,
            name="uint64",
            char=NPY_ULONGLONGLTR,
            w_box_type=space.gettypefor(interp_boxes.W_UInt64Box),
            aliases=['ulonglong'],
        )
        self.w_float32dtype = W_Dtype(types.Float32(),
                                      num=NPY_FLOAT,
                                      kind=NPY_FLOATINGLTR,
                                      name="float32",
                                      char=NPY_FLOATLTR,
                                      w_box_type=space.gettypefor(
                                          interp_boxes.W_Float32Box),
                                      aliases=['single'])
        self.w_float64dtype = W_Dtype(
            types.Float64(),
            num=NPY_DOUBLE,
            kind=NPY_FLOATINGLTR,
            name="float64",
            char=NPY_DOUBLELTR,
            w_box_type=space.gettypefor(interp_boxes.W_Float64Box),
            alternate_constructors=[
                space.w_float,
                space.gettypefor(interp_boxes.W_NumberBox),
            ],
            aliases=["float", "double"],
        )
        self.w_floatlongdtype = W_Dtype(
            types.FloatLong(),
            num=NPY_LONGDOUBLE,
            kind=NPY_FLOATINGLTR,
            name="float%d" % (interp_boxes.long_double_size * 8),
            char=NPY_LONGDOUBLELTR,
            w_box_type=space.gettypefor(interp_boxes.W_FloatLongBox),
            aliases=["longdouble", "longfloat"],
        )
        self.w_complex64dtype = W_Dtype(
            types.Complex64(),
            num=NPY_CFLOAT,
            kind=NPY_COMPLEXLTR,
            name="complex64",
            char=NPY_CFLOATLTR,
            w_box_type=space.gettypefor(interp_boxes.W_Complex64Box),
            aliases=['csingle'],
            float_type=self.w_float32dtype,
        )
        self.w_complex128dtype = W_Dtype(
            types.Complex128(),
            num=NPY_CDOUBLE,
            kind=NPY_COMPLEXLTR,
            name="complex128",
            char=NPY_CDOUBLELTR,
            w_box_type=space.gettypefor(interp_boxes.W_Complex128Box),
            alternate_constructors=[space.w_complex],
            aliases=["complex", 'cfloat', 'cdouble'],
            float_type=self.w_float64dtype,
        )
        self.w_complexlongdtype = W_Dtype(
            types.ComplexLong(),
            num=NPY_CLONGDOUBLE,
            kind=NPY_COMPLEXLTR,
            name="complex%d" % (interp_boxes.long_double_size * 16),
            char=NPY_CLONGDOUBLELTR,
            w_box_type=space.gettypefor(interp_boxes.W_ComplexLongBox),
            aliases=["clongdouble", "clongfloat"],
            float_type=self.w_floatlongdtype,
        )
        self.w_stringdtype = W_Dtype(
            types.StringType(0),
            num=NPY_STRING,
            kind=NPY_STRINGLTR,
            name='string',
            char=NPY_STRINGLTR,
            w_box_type=space.gettypefor(interp_boxes.W_StringBox),
            alternate_constructors=[
                space.w_str,
                space.gettypefor(interp_boxes.W_CharacterBox)
            ],
            aliases=["str"],
        )
        self.w_unicodedtype = W_Dtype(
            types.UnicodeType(0),
            num=NPY_UNICODE,
            kind=NPY_UNICODELTR,
            name='unicode',
            char=NPY_UNICODELTR,
            w_box_type=space.gettypefor(interp_boxes.W_UnicodeBox),
            alternate_constructors=[space.w_unicode],
        )
        self.w_voiddtype = W_Dtype(
            types.VoidType(0),
            num=NPY_VOID,
            kind=NPY_VOIDLTR,
            name='void',
            char=NPY_VOIDLTR,
            w_box_type=space.gettypefor(interp_boxes.W_VoidBox),
            #alternate_constructors=[space.w_buffer],
            # XXX no buffer in space
            #alternate_constructors=[space.gettypefor(interp_boxes.W_GenericBox)],
            # XXX fix, leads to _coerce error
        )
        self.w_float16dtype = W_Dtype(
            types.Float16(),
            num=NPY_HALF,
            kind=NPY_FLOATINGLTR,
            name="float16",
            char=NPY_HALFLTR,
            w_box_type=space.gettypefor(interp_boxes.W_Float16Box),
        )
        ptr_size = rffi.sizeof(rffi.CCHARP)
        if ptr_size == 4:
            intp_box = interp_boxes.W_Int32Box
            intp_type = types.Int32()
            intp_num = NPY_INT
            uintp_box = interp_boxes.W_UInt32Box
            uintp_type = types.UInt32()
            uintp_num = NPY_UINT
        elif ptr_size == 8:
            intp_box = interp_boxes.W_Int64Box
            intp_type = types.Int64()
            intp_num = NPY_LONG
            uintp_box = interp_boxes.W_UInt64Box
            uintp_type = types.UInt64()
            uintp_num = NPY_ULONG
        else:
            raise ValueError('unknown point size %d' % ptr_size)
        self.w_intpdtype = W_Dtype(
            intp_type,
            num=intp_num,
            kind=NPY_INTPLTR,
            name='intp',
            char=NPY_INTPLTR,
            w_box_type=space.gettypefor(intp_box),
        )
        self.w_uintpdtype = W_Dtype(
            uintp_type,
            num=uintp_num,
            kind=NPY_UINTPLTR,
            name='uintp',
            char=NPY_UINTPLTR,
            w_box_type=space.gettypefor(uintp_box),
        )
        float_dtypes = [
            self.w_float16dtype, self.w_float32dtype, self.w_float64dtype,
            self.w_floatlongdtype
        ]
        complex_dtypes = [
            self.w_complex64dtype, self.w_complex128dtype,
            self.w_complexlongdtype
        ]
        self.builtin_dtypes = [
            self.w_booldtype,
            self.w_int8dtype,
            self.w_uint8dtype,
            self.w_int16dtype,
            self.w_uint16dtype,
            self.w_longdtype,
            self.w_ulongdtype,
            self.w_int32dtype,
            self.w_uint32dtype,
            self.w_int64dtype,
            self.w_uint64dtype,
        ] + float_dtypes + complex_dtypes + [
            self.w_stringdtype,
            self.w_unicodedtype,
            self.w_voiddtype,
            self.w_intpdtype,
            self.w_uintpdtype,
        ]
        self.float_dtypes_by_num_bytes = sorted(
            (dtype.itemtype.get_element_size(), dtype)
            for dtype in float_dtypes)
        self.dtypes_by_num = {}
        self.dtypes_by_name = {}
        # we reverse, so the stuff with lower numbers override stuff with
        # higher numbers
        for dtype in reversed(self.builtin_dtypes):
            self.dtypes_by_num[dtype.num] = dtype
            self.dtypes_by_name[dtype.name] = dtype
            can_name = dtype.kind + str(dtype.itemtype.get_element_size())
            self.dtypes_by_name[can_name] = dtype
            self.dtypes_by_name[NPY_NATBYTE + can_name] = dtype
            self.dtypes_by_name[NPY_NATIVE + can_name] = dtype
            new_name = NPY_OPPBYTE + can_name
            itemtype = type(dtype.itemtype)(False)
            self.dtypes_by_name[new_name] = W_Dtype(
                itemtype,
                dtype.num,
                dtype.kind,
                new_name,
                dtype.char,
                dtype.w_box_type,
                byteorder=NPY_OPPBYTE,
                float_type=dtype.float_type)
            if dtype.kind != dtype.char:
                can_name = dtype.char
                self.dtypes_by_name[NPY_NATBYTE + can_name] = dtype
                self.dtypes_by_name[NPY_NATIVE + can_name] = dtype
                new_name = NPY_OPPBYTE + can_name
                self.dtypes_by_name[new_name] = W_Dtype(
                    itemtype,
                    dtype.num,
                    dtype.kind,
                    new_name,
                    dtype.char,
                    dtype.w_box_type,
                    byteorder=NPY_OPPBYTE,
                    float_type=dtype.float_type)

            for alias in dtype.aliases:
                self.dtypes_by_name[alias] = dtype
            self.dtypes_by_name[dtype.char] = dtype

        typeinfo_full = {
            'LONGLONG': self.w_int64dtype,
            'SHORT': self.w_int16dtype,
            'VOID': self.w_voiddtype,
            'UBYTE': self.w_uint8dtype,
            'UINTP': self.w_ulongdtype,
            'ULONG': self.w_ulongdtype,
            'LONG': self.w_longdtype,
            'UNICODE': self.w_unicodedtype,
            #'OBJECT',
            'ULONGLONG': self.w_uint64dtype,
            'STRING': self.w_stringdtype,
            'CFLOAT': self.w_complex64dtype,
            'CDOUBLE': self.w_complex128dtype,
            'CLONGDOUBLE': self.w_complexlongdtype,
            #'DATETIME',
            'UINT': self.w_uint32dtype,
            'INTP': self.w_intpdtype,
            'UINTP': self.w_uintpdtype,
            'HALF': self.w_float16dtype,
            'BYTE': self.w_int8dtype,
            #'TIMEDELTA',
            'INT': self.w_int32dtype,
            'DOUBLE': self.w_float64dtype,
            'LONGDOUBLE': self.w_floatlongdtype,
            'USHORT': self.w_uint16dtype,
            'FLOAT': self.w_float32dtype,
            'BOOL': self.w_booldtype,
        }

        typeinfo_partial = {
            'Generic': interp_boxes.W_GenericBox,
            'Character': interp_boxes.W_CharacterBox,
            'Flexible': interp_boxes.W_FlexibleBox,
            'Inexact': interp_boxes.W_InexactBox,
            'Integer': interp_boxes.W_IntegerBox,
            'SignedInteger': interp_boxes.W_SignedIntegerBox,
            'UnsignedInteger': interp_boxes.W_UnsignedIntegerBox,
            'ComplexFloating': interp_boxes.W_ComplexFloatingBox,
            'Number': interp_boxes.W_NumberBox,
            'Floating': interp_boxes.W_FloatingBox
        }
        w_typeinfo = space.newdict()
        for k, v in typeinfo_partial.iteritems():
            space.setitem(w_typeinfo, space.wrap(k), space.gettypefor(v))
        for k, dtype in typeinfo_full.iteritems():
            itemsize = dtype.itemtype.get_element_size()
            items_w = [
                space.wrap(dtype.char),
                space.wrap(dtype.num),
                space.wrap(itemsize * 8),  # in case of changing
                # number of bits per byte in the future
                space.wrap(itemsize /
                           (2 if dtype.kind == NPY_COMPLEXLTR else 1) or 1)
            ]
            if dtype.is_int_type():
                if dtype.kind == NPY_GENBOOLLTR:
                    w_maxobj = space.wrap(1)
                    w_minobj = space.wrap(0)
                elif dtype.is_signed():
                    w_maxobj = space.wrap(
                        r_longlong((1 << (itemsize * 8 - 1)) - 1))
                    w_minobj = space.wrap(r_longlong(-1) << (itemsize * 8 - 1))
                else:
                    w_maxobj = space.wrap(r_ulonglong(1 << (itemsize * 8)) - 1)
                    w_minobj = space.wrap(0)
                items_w = items_w + [w_maxobj, w_minobj]
            items_w = items_w + [dtype.w_box_type]
            space.setitem(w_typeinfo, space.wrap(k), space.newtuple(items_w))
        self.w_typeinfo = w_typeinfo
Ejemplo n.º 4
0
def descr__new__(space,
                 w_subtype,
                 w_dtype,
                 w_align=None,
                 w_copy=None,
                 w_shape=None):
    # w_align and w_copy are necessary for pickling
    cache = get_dtype_cache(space)

    if w_shape is not None and (space.isinstance_w(w_shape, space.w_int)
                                or space.len_w(w_shape) > 0):
        subdtype = descr__new__(space, w_subtype, w_dtype, w_align, w_copy)
        assert isinstance(subdtype, W_Dtype)
        size = 1
        if space.isinstance_w(w_shape, space.w_int):
            w_shape = space.newtuple([w_shape])
        shape = []
        for w_dim in space.fixedview(w_shape):
            dim = space.int_w(w_dim)
            shape.append(dim)
            size *= dim
        return W_Dtype(
            types.VoidType(subdtype.itemtype.get_element_size() * size),
            NPY_VOID,
            NPY_VOIDLTR,
            "void" + str(8 * subdtype.itemtype.get_element_size() * size),
            NPY_VOIDLTR,
            space.gettypefor(interp_boxes.W_VoidBox),
            shape=shape,
            subdtype=subdtype)

    if space.is_none(w_dtype):
        return cache.w_float64dtype
    elif space.isinstance_w(w_dtype, w_subtype):
        return w_dtype
    elif space.isinstance_w(w_dtype, space.w_str):
        name = space.str_w(w_dtype)
        if ',' in name:
            return dtype_from_spec(space, name)
        try:
            return cache.dtypes_by_name[name]
        except KeyError:
            pass
        if name[0] in 'VSUc' or name[0] in '<>=' and name[1] in 'VSUc':
            return variable_dtype(space, name)
        raise OperationError(space.w_TypeError,
                             space.wrap("data type %s not understood" % name))
    elif space.isinstance_w(w_dtype, space.w_list):
        return dtype_from_list(space, w_dtype)
    elif space.isinstance_w(w_dtype, space.w_tuple):
        w_dtype0 = space.getitem(w_dtype, space.wrap(0))
        w_dtype1 = space.getitem(w_dtype, space.wrap(1))
        subdtype = descr__new__(space, w_subtype, w_dtype0, w_align, w_copy)
        assert isinstance(subdtype, W_Dtype)
        if subdtype.get_size() == 0:
            name = "%s%d" % (subdtype.kind, space.int_w(w_dtype1))
            return descr__new__(space, w_subtype, space.wrap(name), w_align,
                                w_copy)
        return descr__new__(space,
                            w_subtype,
                            w_dtype0,
                            w_align,
                            w_copy,
                            w_shape=w_dtype1)
    elif space.isinstance_w(w_dtype, space.w_dict):
        return dtype_from_dict(space, w_dtype)
    for dtype in cache.builtin_dtypes:
        if w_dtype in dtype.alternate_constructors:
            return dtype
        if w_dtype is dtype.w_box_type:
            return dtype
    msg = "data type not understood (value of type %T not expected here)"
    raise operationerrfmt(space.w_TypeError, msg, w_dtype)
Ejemplo n.º 5
0
    def __init__(self, space):
        self.w_booldtype = W_Dtype(
            types.Bool(),
            num=0,
            kind=BOOLLTR,
            name="bool",
            char="?",
            w_box_type=space.gettypefor(interp_boxes.W_BoolBox),
            alternate_constructors=[space.w_bool],
        )
        self.w_int8dtype = W_Dtype(types.Int8(),
                                   num=1,
                                   kind=SIGNEDLTR,
                                   name="int8",
                                   char="b",
                                   w_box_type=space.gettypefor(
                                       interp_boxes.W_Int8Box))
        self.w_uint8dtype = W_Dtype(
            types.UInt8(),
            num=2,
            kind=UNSIGNEDLTR,
            name="uint8",
            char="B",
            w_box_type=space.gettypefor(interp_boxes.W_UInt8Box),
        )
        self.w_int16dtype = W_Dtype(
            types.Int16(),
            num=3,
            kind=SIGNEDLTR,
            name="int16",
            char="h",
            w_box_type=space.gettypefor(interp_boxes.W_Int16Box),
        )
        self.w_uint16dtype = W_Dtype(
            types.UInt16(),
            num=4,
            kind=UNSIGNEDLTR,
            name="uint16",
            char="H",
            w_box_type=space.gettypefor(interp_boxes.W_UInt16Box),
        )
        self.w_int32dtype = W_Dtype(
            types.Int32(),
            num=5,
            kind=SIGNEDLTR,
            name="int32",
            char="i",
            w_box_type=space.gettypefor(interp_boxes.W_Int32Box),
        )
        self.w_uint32dtype = W_Dtype(
            types.UInt32(),
            num=6,
            kind=UNSIGNEDLTR,
            name="uint32",
            char="I",
            w_box_type=space.gettypefor(interp_boxes.W_UInt32Box),
        )
        if LONG_BIT == 32:
            name = "int32"
        elif LONG_BIT == 64:
            name = "int64"
        self.w_longdtype = W_Dtype(
            types.Long(),
            num=7,
            kind=SIGNEDLTR,
            name=name,
            char="l",
            w_box_type=space.gettypefor(interp_boxes.W_LongBox),
            alternate_constructors=[space.w_int],
        )
        self.w_ulongdtype = W_Dtype(
            types.ULong(),
            num=8,
            kind=UNSIGNEDLTR,
            name="u" + name,
            char="L",
            w_box_type=space.gettypefor(interp_boxes.W_ULongBox),
        )
        self.w_int64dtype = W_Dtype(
            types.Int64(),
            num=9,
            kind=SIGNEDLTR,
            name="int64",
            char="q",
            w_box_type=space.gettypefor(interp_boxes.W_Int64Box),
            alternate_constructors=[space.w_long],
        )
        self.w_uint64dtype = W_Dtype(
            types.UInt64(),
            num=10,
            kind=UNSIGNEDLTR,
            name="uint64",
            char="Q",
            w_box_type=space.gettypefor(interp_boxes.W_UInt64Box),
        )
        self.w_float32dtype = W_Dtype(
            types.Float32(),
            num=11,
            kind=FLOATINGLTR,
            name="float32",
            char="f",
            w_box_type=space.gettypefor(interp_boxes.W_Float32Box),
        )
        self.w_float64dtype = W_Dtype(
            types.Float64(),
            num=12,
            kind=FLOATINGLTR,
            name="float64",
            char="d",
            w_box_type=space.gettypefor(interp_boxes.W_Float64Box),
            alternate_constructors=[space.w_float],
            aliases=["float"],
        )
        self.w_stringdtype = W_Dtype(
            types.StringType(1),
            num=18,
            kind=STRINGLTR,
            name='string',
            char='S',
            w_box_type=space.gettypefor(interp_boxes.W_StringBox),
            alternate_constructors=[space.w_str],
        )
        self.w_unicodedtype = W_Dtype(
            types.UnicodeType(1),
            num=19,
            kind=UNICODELTR,
            name='unicode',
            char='U',
            w_box_type=space.gettypefor(interp_boxes.W_UnicodeBox),
            alternate_constructors=[space.w_unicode],
        )
        self.w_voiddtype = W_Dtype(
            types.VoidType(0),
            num=20,
            kind=VOIDLTR,
            name='void',
            char='V',
            w_box_type=space.gettypefor(interp_boxes.W_VoidBox),
            #alternate_constructors=[space.w_buffer],
            # XXX no buffer in space
        )
        self.builtin_dtypes = [
            self.w_booldtype,
            self.w_int8dtype,
            self.w_uint8dtype,
            self.w_int16dtype,
            self.w_uint16dtype,
            self.w_int32dtype,
            self.w_uint32dtype,
            self.w_longdtype,
            self.w_ulongdtype,
            self.w_int64dtype,
            self.w_uint64dtype,
            self.w_float32dtype,
            self.w_float64dtype,
            self.w_stringdtype,
            self.w_unicodedtype,
            self.w_voiddtype,
        ]
        self.float_dtypes_by_num_bytes = sorted(
            (dtype.itemtype.get_element_size(), dtype)
            for dtype in [self.w_float32dtype, self.w_float64dtype])
        self.dtypes_by_name = {}
        # we reverse, so the stuff with lower numbers override stuff with
        # higher numbers
        for dtype in reversed(self.builtin_dtypes):
            self.dtypes_by_name[dtype.name] = dtype
            can_name = dtype.kind + str(dtype.itemtype.get_element_size())
            self.dtypes_by_name[can_name] = dtype
            self.dtypes_by_name[byteorder_prefix + can_name] = dtype
            self.dtypes_by_name['=' + can_name] = dtype
            new_name = nonnative_byteorder_prefix + can_name
            itemtypename = dtype.itemtype.__class__.__name__
            itemtype = getattr(types, 'NonNative' + itemtypename)()
            self.dtypes_by_name[new_name] = W_Dtype(itemtype,
                                                    dtype.num,
                                                    dtype.kind,
                                                    new_name,
                                                    dtype.char,
                                                    dtype.w_box_type,
                                                    native=False)
            for alias in dtype.aliases:
                self.dtypes_by_name[alias] = dtype
            self.dtypes_by_name[dtype.char] = dtype

        typeinfo_full = {
            'LONGLONG': self.w_int64dtype,
            'SHORT': self.w_int16dtype,
            'VOID': self.w_voiddtype,
            #'LONGDOUBLE':,
            'UBYTE': self.w_uint8dtype,
            'UINTP': self.w_ulongdtype,
            'ULONG': self.w_ulongdtype,
            'LONG': self.w_longdtype,
            'UNICODE': self.w_unicodedtype,
            #'OBJECT',
            'ULONGLONG': self.w_uint64dtype,
            'STRING': self.w_stringdtype,
            #'CDOUBLE',
            #'DATETIME',
            'UINT': self.w_uint32dtype,
            'INTP': self.w_longdtype,
            #'HALF',
            'BYTE': self.w_int8dtype,
            #'CFLOAT': ,
            #'TIMEDELTA',
            'INT': self.w_int32dtype,
            'DOUBLE': self.w_float64dtype,
            'USHORT': self.w_uint16dtype,
            'FLOAT': self.w_float32dtype,
            'BOOL': self.w_booldtype,
            #, 'CLONGDOUBLE']
        }
        typeinfo_partial = {
            'Generic': interp_boxes.W_GenericBox,
            'Character': interp_boxes.W_CharacterBox,
            'Flexible': interp_boxes.W_FlexibleBox,
            'Inexact': interp_boxes.W_InexactBox,
            'Integer': interp_boxes.W_IntegerBox,
            'SignedInteger': interp_boxes.W_SignedIntegerBox,
            'UnsignedInteger': interp_boxes.W_UnsignedIntegerBox,
            #'ComplexFloating',
            'Number': interp_boxes.W_NumberBox,
            'Floating': interp_boxes.W_FloatingBox
        }
        w_typeinfo = space.newdict()
        for k, v in typeinfo_partial.iteritems():
            space.setitem(w_typeinfo, space.wrap(k), space.gettypefor(v))
        for k, dtype in typeinfo_full.iteritems():
            itemsize = dtype.itemtype.get_element_size()
            items_w = [
                space.wrap(dtype.char),
                space.wrap(dtype.num),
                space.wrap(itemsize * 8),  # in case of changing
                # number of bits per byte in the future
                space.wrap(itemsize or 1)
            ]
            if dtype.is_int_type():
                if dtype.kind == BOOLLTR:
                    w_maxobj = space.wrap(1)
                    w_minobj = space.wrap(0)
                elif dtype.is_signed():
                    w_maxobj = space.wrap(
                        r_longlong((1 << (itemsize * 8 - 1)) - 1))
                    w_minobj = space.wrap(r_longlong(-1) << (itemsize * 8 - 1))
                else:
                    w_maxobj = space.wrap(r_ulonglong(1 << (itemsize * 8)) - 1)
                    w_minobj = space.wrap(0)
                items_w = items_w + [w_maxobj, w_minobj]
            items_w = items_w + [dtype.w_box_type]

            w_tuple = space.newtuple(items_w)
            space.setitem(w_typeinfo, space.wrap(k), w_tuple)
        self.w_typeinfo = w_typeinfo
Ejemplo n.º 6
0
    def __init__(self, space):
        self.w_booldtype = W_Dtype(
            types.Bool(space),
            w_box_type=space.gettypefor(boxes.W_BoolBox),
        )
        self.w_int8dtype = W_Dtype(
            types.Int8(space),
            w_box_type=space.gettypefor(boxes.W_Int8Box),
        )
        self.w_uint8dtype = W_Dtype(
            types.UInt8(space),
            w_box_type=space.gettypefor(boxes.W_UInt8Box),
        )
        self.w_int16dtype = W_Dtype(
            types.Int16(space),
            w_box_type=space.gettypefor(boxes.W_Int16Box),
        )
        self.w_uint16dtype = W_Dtype(
            types.UInt16(space),
            w_box_type=space.gettypefor(boxes.W_UInt16Box),
        )
        self.w_int32dtype = W_Dtype(
            types.Int32(space),
            w_box_type=space.gettypefor(boxes.W_Int32Box),
        )
        self.w_uint32dtype = W_Dtype(
            types.UInt32(space),
            w_box_type=space.gettypefor(boxes.W_UInt32Box),
        )
        self.w_longdtype = W_Dtype(
            types.Long(space),
            w_box_type=space.gettypefor(boxes.W_LongBox),
        )
        self.w_ulongdtype = W_Dtype(
            types.ULong(space),
            w_box_type=space.gettypefor(boxes.W_ULongBox),
        )
        self.w_int64dtype = W_Dtype(
            types.Int64(space),
            w_box_type=space.gettypefor(boxes.W_Int64Box),
        )
        self.w_uint64dtype = W_Dtype(
            types.UInt64(space),
            w_box_type=space.gettypefor(boxes.W_UInt64Box),
        )
        self.w_float32dtype = W_Dtype(
            types.Float32(space),
            w_box_type=space.gettypefor(boxes.W_Float32Box),
        )
        self.w_float64dtype = W_Dtype(
            types.Float64(space),
            w_box_type=space.gettypefor(boxes.W_Float64Box),
        )
        self.w_floatlongdtype = W_Dtype(
            types.FloatLong(space),
            w_box_type=space.gettypefor(boxes.W_FloatLongBox),
        )
        self.w_complex64dtype = W_Dtype(
            types.Complex64(space),
            w_box_type=space.gettypefor(boxes.W_Complex64Box),
        )
        self.w_complex128dtype = W_Dtype(
            types.Complex128(space),
            w_box_type=space.gettypefor(boxes.W_Complex128Box),
        )
        self.w_complexlongdtype = W_Dtype(
            types.ComplexLong(space),
            w_box_type=space.gettypefor(boxes.W_ComplexLongBox),
        )
        self.w_stringdtype = W_Dtype(
            types.StringType(space),
            elsize=0,
            w_box_type=space.gettypefor(boxes.W_StringBox),
        )
        self.w_unicodedtype = W_Dtype(
            types.UnicodeType(space),
            elsize=0,
            w_box_type=space.gettypefor(boxes.W_UnicodeBox),
        )
        self.w_voiddtype = W_Dtype(
            types.VoidType(space),
            elsize=0,
            w_box_type=space.gettypefor(boxes.W_VoidBox),
        )
        self.w_float16dtype = W_Dtype(
            types.Float16(space),
            w_box_type=space.gettypefor(boxes.W_Float16Box),
        )
        self.w_objectdtype = W_Dtype(
            types.ObjectType(space),
            w_box_type=space.gettypefor(boxes.W_ObjectBox),
        )
        aliases = {
            NPY.BOOL: ['bool_', 'bool8'],
            NPY.BYTE: ['byte'],
            NPY.UBYTE: ['ubyte'],
            NPY.SHORT: ['short'],
            NPY.USHORT: ['ushort'],
            NPY.LONG: ['int'],
            NPY.ULONG: ['uint'],
            NPY.LONGLONG: ['longlong'],
            NPY.ULONGLONG: ['ulonglong'],
            NPY.FLOAT: ['single'],
            NPY.DOUBLE: ['float', 'double'],
            NPY.LONGDOUBLE: ['longdouble', 'longfloat'],
            NPY.CFLOAT: ['csingle'],
            NPY.CDOUBLE: ['complex', 'cfloat', 'cdouble'],
            NPY.CLONGDOUBLE: ['clongdouble', 'clongfloat'],
            NPY.STRING: ['string_', 'str'],
            NPY.UNICODE: ['unicode_'],
            NPY.OBJECT: ['object_'],
        }
        self.alternate_constructors = {
            NPY.BOOL: [space.w_bool],
            NPY.LONG: [
                space.w_int,
                space.gettypefor(boxes.W_IntegerBox),
                space.gettypefor(boxes.W_SignedIntegerBox)
            ],
            NPY.ULONG: [space.gettypefor(boxes.W_UnsignedIntegerBox)],
            NPY.LONGLONG: [space.w_long],
            NPY.DOUBLE: [
                space.w_float,
                space.gettypefor(boxes.W_NumberBox),
                space.gettypefor(boxes.W_FloatingBox)
            ],
            NPY.CDOUBLE:
            [space.w_complex,
             space.gettypefor(boxes.W_ComplexFloatingBox)],
            NPY.STRING: [space.w_str,
                         space.gettypefor(boxes.W_CharacterBox)],
            NPY.UNICODE: [space.w_unicode],
            NPY.VOID: [space.gettypefor(boxes.W_GenericBox)],
            #space.w_buffer,  # XXX no buffer in space
            NPY.OBJECT: [space.gettypefor(boxes.W_ObjectBox), space.w_object],
        }
        float_dtypes = [
            self.w_float16dtype, self.w_float32dtype, self.w_float64dtype,
            self.w_floatlongdtype
        ]
        complex_dtypes = [
            self.w_complex64dtype, self.w_complex128dtype,
            self.w_complexlongdtype
        ]
        self.component_dtypes = {
            NPY.CFLOAT: self.w_float32dtype,
            NPY.CDOUBLE: self.w_float64dtype,
            NPY.CLONGDOUBLE: self.w_floatlongdtype,
        }
        integer_dtypes = [
            self.w_int8dtype, self.w_uint8dtype, self.w_int16dtype,
            self.w_uint16dtype, self.w_int32dtype, self.w_uint32dtype,
            self.w_longdtype, self.w_ulongdtype, self.w_int64dtype,
            self.w_uint64dtype
        ]
        self.builtin_dtypes = ([self.w_booldtype] + integer_dtypes +
                               float_dtypes + complex_dtypes + [
                                   self.w_stringdtype,
                                   self.w_unicodedtype,
                                   self.w_voiddtype,
                                   self.w_objectdtype,
                               ])
        self.integer_dtypes = integer_dtypes
        self.float_dtypes = float_dtypes
        self.complex_dtypes = complex_dtypes
        self.float_dtypes_by_num_bytes = sorted(
            (dtype.elsize, dtype) for dtype in float_dtypes)
        self.dtypes_by_num = {}
        self.dtypes_by_name = {}
        # we reverse, so the stuff with lower numbers override stuff with
        # higher numbers
        # However, Long/ULong always take precedence over Intxx
        for dtype in reversed([self.w_longdtype, self.w_ulongdtype] +
                              self.builtin_dtypes):
            dtype.fields = None  # mark these as builtin
            self.dtypes_by_num[dtype.num] = dtype
            self.dtypes_by_name[dtype.get_name()] = dtype
            for can_name in [dtype.kind + str(dtype.elsize), dtype.char]:
                self.dtypes_by_name[can_name] = dtype
                self.dtypes_by_name[NPY.NATBYTE + can_name] = dtype
                self.dtypes_by_name[NPY.NATIVE + can_name] = dtype
                self.dtypes_by_name[NPY.IGNORE + can_name] = dtype
            if dtype.num in aliases:
                for alias in aliases[dtype.num]:
                    self.dtypes_by_name[alias] = dtype
        if self.w_longdtype.elsize == self.w_int32dtype.elsize:
            intp_dtype = self.w_int32dtype
            uintp_dtype = self.w_uint32dtype
        else:
            intp_dtype = self.w_longdtype
            uintp_dtype = self.w_ulongdtype
        self.dtypes_by_name['p'] = self.dtypes_by_name['intp'] = intp_dtype
        self.dtypes_by_name['P'] = self.dtypes_by_name['uintp'] = uintp_dtype

        typeinfo_full = {
            'LONGLONG': self.w_int64dtype,
            'SHORT': self.w_int16dtype,
            'VOID': self.w_voiddtype,
            'UBYTE': self.w_uint8dtype,
            'UINTP': self.w_ulongdtype,
            'ULONG': self.w_ulongdtype,
            'LONG': self.w_longdtype,
            'UNICODE': self.w_unicodedtype,
            #'OBJECT',
            'ULONGLONG': self.w_uint64dtype,
            'STRING': self.w_stringdtype,
            'CFLOAT': self.w_complex64dtype,
            'CDOUBLE': self.w_complex128dtype,
            'CLONGDOUBLE': self.w_complexlongdtype,
            #'DATETIME',
            'UINT': self.w_uint32dtype,
            'INTP': self.w_longdtype,
            'HALF': self.w_float16dtype,
            'BYTE': self.w_int8dtype,
            #'TIMEDELTA',
            'INT': self.w_int32dtype,
            'DOUBLE': self.w_float64dtype,
            'LONGDOUBLE': self.w_floatlongdtype,
            'USHORT': self.w_uint16dtype,
            'FLOAT': self.w_float32dtype,
            'BOOL': self.w_booldtype,
            'OBJECT': self.w_objectdtype,
        }

        typeinfo_partial = {
            'Generic': boxes.W_GenericBox,
            'Character': boxes.W_CharacterBox,
            'Flexible': boxes.W_FlexibleBox,
            'Inexact': boxes.W_InexactBox,
            'Integer': boxes.W_IntegerBox,
            'SignedInteger': boxes.W_SignedIntegerBox,
            'UnsignedInteger': boxes.W_UnsignedIntegerBox,
            'ComplexFloating': boxes.W_ComplexFloatingBox,
            'Number': boxes.W_NumberBox,
            'Floating': boxes.W_FloatingBox
        }
        w_typeinfo = space.newdict()
        for k, v in typeinfo_partial.iteritems():
            space.setitem(w_typeinfo, space.wrap(k), space.gettypefor(v))
        for k, dtype in typeinfo_full.iteritems():
            itembits = dtype.elsize * 8
            if k in ('INTP', 'UINTP'):
                char = getattr(NPY, k + 'LTR')
            else:
                char = dtype.char
            items_w = [
                space.wrap(char),
                space.wrap(dtype.num),
                space.wrap(itembits),
                space.wrap(dtype.itemtype.get_element_size())
            ]
            if dtype.is_int():
                if dtype.is_bool():
                    w_maxobj = space.wrap(1)
                    w_minobj = space.wrap(0)
                elif dtype.is_signed():
                    w_maxobj = space.wrap(r_longlong((1 << (itembits - 1)) -
                                                     1))
                    w_minobj = space.wrap(r_longlong(-1) << (itembits - 1))
                else:
                    w_maxobj = space.wrap(r_ulonglong(1 << itembits) - 1)
                    w_minobj = space.wrap(0)
                items_w = items_w + [w_maxobj, w_minobj]
            items_w = items_w + [dtype.w_box_type]
            space.setitem(w_typeinfo, space.wrap(k), space.newtuple(items_w))
        self.w_typeinfo = w_typeinfo
Ejemplo n.º 7
0
def new_void_dtype(space, size):
    return W_Dtype(
        types.VoidType(space),
        elsize=size,
        w_box_type=space.gettypefor(boxes.W_VoidBox),
    )
Ejemplo n.º 8
0
def descr__new__(space,
                 w_subtype,
                 w_dtype,
                 w_align=None,
                 w_copy=None,
                 w_shape=None):
    # w_align and w_copy are necessary for pickling
    cache = get_dtype_cache(space)

    if w_shape is not None and (space.isinstance_w(w_shape, space.w_int)
                                or space.len_w(w_shape) > 0):
        subdtype = descr__new__(space, w_subtype, w_dtype, w_align, w_copy)
        assert isinstance(subdtype, W_Dtype)
        size = 1
        if space.isinstance_w(w_shape, space.w_int):
            w_shape = space.newtuple([w_shape])
        shape = []
        for w_dim in space.fixedview(w_shape):
            dim = space.int_w(w_dim)
            shape.append(dim)
            size *= dim
        if size == 1:
            return subdtype
        size *= subdtype.elsize
        return W_Dtype(types.VoidType(space),
                       space.gettypefor(boxes.W_VoidBox),
                       shape=shape,
                       subdtype=subdtype,
                       elsize=size)

    if space.is_none(w_dtype):
        return cache.w_float64dtype
    if space.isinstance_w(w_dtype, w_subtype):
        return w_dtype
    if space.isinstance_w(w_dtype, space.w_unicode):
        w_dtype = space.wrap(space.str_w(w_dtype))  # may raise if invalid
    if space.isinstance_w(w_dtype, space.w_str):
        name = space.str_w(w_dtype)
        if _check_for_commastring(name):
            return dtype_from_spec(space, w_dtype)
        cname = name[1:] if name[0] == NPY.OPPBYTE else name
        try:
            dtype = cache.dtypes_by_name[cname]
        except KeyError:
            pass
        else:
            if name[0] == NPY.OPPBYTE:
                dtype = dtype.descr_newbyteorder(space)
            return dtype
        if name[0] in 'VSUca' or name[0] in '<>=|' and name[1] in 'VSUca':
            return variable_dtype(space, name)
        raise oefmt(space.w_TypeError, 'data type "%s" not understood', name)
    elif space.isinstance_w(w_dtype, space.w_list):
        return dtype_from_list(space, w_dtype, False)
    elif space.isinstance_w(w_dtype, space.w_tuple):
        w_dtype0 = space.getitem(w_dtype, space.wrap(0))
        w_dtype1 = space.getitem(w_dtype, space.wrap(1))
        subdtype = descr__new__(space, w_subtype, w_dtype0, w_align, w_copy)
        assert isinstance(subdtype, W_Dtype)
        if subdtype.elsize == 0:
            name = "%s%d" % (subdtype.kind, space.int_w(w_dtype1))
            return descr__new__(space, w_subtype, space.wrap(name), w_align,
                                w_copy)
        return descr__new__(space,
                            w_subtype,
                            w_dtype0,
                            w_align,
                            w_copy,
                            w_shape=w_dtype1)
    elif space.isinstance_w(w_dtype, space.w_dict):
        return dtype_from_dict(space, w_dtype)
    for dtype in cache.builtin_dtypes:
        if dtype.num in cache.alternate_constructors and \
                w_dtype in cache.alternate_constructors[dtype.num]:
            return dtype
        if w_dtype is dtype.w_box_type:
            return dtype
    if space.isinstance_w(w_dtype, space.w_type):
        return cache.w_objectdtype
    raise oefmt(space.w_TypeError, "data type not understood")