Esempio n. 1
0
    def test_buffers(self):
        b = b"xx"
        ty = typeof(b)
        self.assertEqual(ty, types.Bytes(types.uint8, 1, "C"))
        self.assertFalse(ty.mutable)
        ty = typeof(memoryview(b))
        self.assertEqual(ty,
                         types.MemoryView(types.uint8, 1, "C", readonly=True))
        self.assertFalse(ty.mutable)
        ty = typeof(array.array('i', [0, 1, 2]))
        self.assertEqual(ty, types.PyArray(types.int32, 1, "C"))
        self.assertTrue(ty.mutable)

        b = bytearray(10)
        ty = typeof(b)
        self.assertEqual(ty, types.ByteArray(types.uint8, 1, "C"))
        self.assertTrue(ty.mutable)
Esempio n. 2
0
from llvmlite import ir

from numba import types, cgutils, unicode
from numba.extending import (overload, intrinsic, overload_method, lower_cast,
                             register_jitable)

# bytes and str arrays items are of type CharSeq and UnicodeCharSeq,
# respectively.  See numpy/types/npytypes.py for CharSeq,
# UnicodeCharSeq definitions.  The corresponding data models are
# defined in numpy/datamodel/models.py. Boxing/unboxing of item types
# are defined in numpy/targets/boxing.py, see box_unicodecharseq,
# unbox_unicodecharseq, box_charseq, unbox_charseq.

s1_dtype = np.dtype('S1')
assert s1_dtype.itemsize == 1
bytes_type = types.Bytes(types.uint8, 1, "C", readonly=True)

# Currently, NumPy supports only UTF-32 arrays but this may change in
# future and the approach used here for supporting str arrays may need
# a revision depending on how NumPy will support UTF-8 and UTF-16
# arrays.
u1_dtype = np.dtype('U1')
unicode_byte_width = u1_dtype.itemsize
unicode_uint = {1: np.uint8, 2: np.uint16, 4: np.uint32}[unicode_byte_width]
unicode_kind = {
    1: unicode.PY_UNICODE_1BYTE_KIND,
    2: unicode.PY_UNICODE_2BYTE_KIND,
    4: unicode.PY_UNICODE_4BYTE_KIND
}[unicode_byte_width]