Example #1
0
    def test_string_types(self):
        """
        Test from_dtype() and as_dtype() with the character string types.
        """
        def check(typestring, numba_type):
            # Only native ordering and alignment is supported
            dtype = np.dtype(typestring)
            self.assertEqual(numpy_support.from_dtype(dtype), numba_type)
            self.assertEqual(dtype, numpy_support.as_dtype(numba_type))

        check('S10', types.CharSeq(10))
        check('a11', types.CharSeq(11))
        check('U12', types.UnicodeCharSeq(12))
    def test_struct_types(self):
        def check(dtype, fields, size, aligned):
            tp = numpy_support.from_dtype(dtype)
            self.assertIsInstance(tp, types.Record)
            # Only check for dtype equality, as the Numba type may be interned
            self.assertEqual(tp.dtype, dtype)
            self.assertEqual(tp.fields, fields)
            self.assertEqual(tp.size, size)
            self.assertEqual(tp.aligned, aligned)

        dtype = np.dtype([('a', np.int16), ('b', np.int32)])
        check(dtype,
              fields={'a': (types.int16, 0),
                      'b': (types.int32, 2)},
              size=6, aligned=False)

        dtype = np.dtype([('a', np.int16), ('b', np.int32)], align=True)
        check(dtype,
              fields={'a': (types.int16, 0),
                      'b': (types.int32, 4)},
              size=8, aligned=True)

        dtype = np.dtype([('m', np.int32), ('n', 'S5')])
        check(dtype,
              fields={'m': (types.int32, 0),
                      'n': (types.CharSeq(5), 4)},
              size=9, aligned=False)
Example #3
0
def from_dtype(dtype):
    if dtype.fields is None:
        try:
            basetype = FROM_DTYPE[dtype]
        except KeyError:
            m = re_typestr.match(dtype.str)
            if not m:
                raise NotImplementedError(dtype)
            groups = m.groups()
            typecode = groups[0]
            if typecode == 'U':
                # unicode
                if dtype.byteorder not in '=|':
                    raise NotImplementedError("Does not support non-native "
                                              "byteorder")
                count = dtype.itemsize // sizeof_unicode_char
                assert count == int(groups[1]), "Unicode char size mismatch"
                return types.UnicodeCharSeq(count)

            elif typecode == 'S':
                # char
                count = dtype.itemsize
                assert count == int(groups[1]), "Char size mismatch"
                return types.CharSeq(count)

            raise NotImplementedError(dtype)

        return basetype
    else:
        return from_struct_dtype(dtype)
Example #4
0
 def test_from_dtype(self):
     rec = numpy_support.from_dtype(recordtype)
     self.assertEqual(rec.typeof('a'), types.float64)
     self.assertEqual(rec.typeof('b'), types.int16)
     self.assertEqual(rec.typeof('c'), types.complex64)
     if IS_PY3:
         self.assertEqual(rec.typeof('d'), types.UnicodeCharSeq(5))
     else:
         self.assertEqual(rec.typeof('d'), types.CharSeq(5))
     self.assertEqual(rec.offset('a'), recordtype.fields['a'][1])
     self.assertEqual(rec.offset('b'), recordtype.fields['b'][1])
     self.assertEqual(rec.offset('c'), recordtype.fields['c'][1])
     self.assertEqual(rec.offset('d'), recordtype.fields['d'][1])
     self.assertEqual(recordtype.itemsize, rec.size)