Exemplo n.º 1
0
 def test_records(self):
     d1 = np.dtype([('m', np.int32), ('n', np.int64)])
     d2 = np.dtype([('m', np.int32), ('n', np.int16)])
     v1 = np.empty(1, dtype=d1)[0]
     v2 = np.empty(1, dtype=d2)[0]
     self.assertNotEqual(compute_fingerprint(v1),
                         compute_fingerprint(v2))
Exemplo n.º 2
0
 def test_records(self):
     d1 = np.dtype([('m', np.int32), ('n', np.int64)])
     d2 = np.dtype([('m', np.int32), ('n', np.int16)])
     v1 = np.empty(1, dtype=d1)[0]
     v2 = np.empty(1, dtype=d2)[0]
     self.assertNotEqual(compute_fingerprint(v1),
                         compute_fingerprint(v2))
Exemplo n.º 3
0
 def test_datetime(self):
     a = np.datetime64(1, 'Y')
     b = np.datetime64(2, 'Y')
     c = np.datetime64(2, 's')
     d = np.timedelta64(2, 's')
     self.assertEqual(compute_fingerprint(a), compute_fingerprint(b))
     distinct = set(compute_fingerprint(x) for x in (a, c, d))
     self.assertEqual(len(distinct), 3, distinct)
Exemplo n.º 4
0
 def test_datetime(self):
     a = np.datetime64(1, "Y")
     b = np.datetime64(2, "Y")
     c = np.datetime64(2, "s")
     d = np.timedelta64(2, "s")
     self.assertEqual(compute_fingerprint(a), compute_fingerprint(b))
     distinct = set(compute_fingerprint(x) for x in (a, c, d))
     self.assertEqual(len(distinct), 3, distinct)
Exemplo n.º 5
0
 def test_ints(self):
     s = compute_fingerprint(1)
     for v in (-1, 2 ** 60):
         self.assertEqual(compute_fingerprint(v), s)
     # Different int widths resolve to different fingerprints
     distinct = set()
     for tp in ("int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64"):
         tp = getattr(np, tp)
         distinct.add(compute_fingerprint(tp()))
     self.assertEqual(len(distinct), 8, distinct)
Exemplo n.º 6
0
 def test_ints(self):
     s = compute_fingerprint(1)
     for v in (-1, 2**60):
         self.assertEqual(compute_fingerprint(v), s)
     # Different int widths resolve to different fingerprints
     distinct = set()
     for tp in ('int8', 'int16', 'int32', 'int64', 'uint8', 'uint16',
                'uint32', 'uint64'):
         tp = getattr(np, tp)
         distinct.add(compute_fingerprint(tp()))
     self.assertEqual(len(distinct), 8, distinct)
Exemplo n.º 7
0
    def test_omitted_args(self):
        distinct = DistinctChecker()

        v0 = OmittedArg(0.0)
        v1 = OmittedArg(1.0)
        v2 = OmittedArg(1)

        s = compute_fingerprint(v0)
        self.assertEqual(compute_fingerprint(v1), s)
        distinct.add(s)
        distinct.add(compute_fingerprint(v2))
        distinct.add(compute_fingerprint(0.0))
        distinct.add(compute_fingerprint(1))
Exemplo n.º 8
0
    def test_omitted_args(self):
        distinct = DistinctChecker()

        v0 = OmittedArg(0.0)
        v1 = OmittedArg(1.0)
        v2 = OmittedArg(1)

        s = compute_fingerprint(v0)
        self.assertEqual(compute_fingerprint(v1), s)
        distinct.add(s)
        distinct.add(compute_fingerprint(v2))
        distinct.add(compute_fingerprint(0.0))
        distinct.add(compute_fingerprint(1))
Exemplo n.º 9
0
    def test_sets(self):
        distinct = DistinctChecker()

        s = compute_fingerprint(set([1]))
        self.assertEqual(compute_fingerprint(set([2, 3])), s)
        distinct.add(s)

        distinct.add(compute_fingerprint([1]))
        distinct.add(compute_fingerprint(set([1j])))
        distinct.add(compute_fingerprint(set([4.5, 6.7])))
        distinct.add(compute_fingerprint(set([(1,)])))

        with self.assertRaises(ValueError):
            compute_fingerprint(set())
        with self.assertRaises(NotImplementedError):
            compute_fingerprint(frozenset([2, 3]))
Exemplo n.º 10
0
    def test_sets(self):
        distinct = DistinctChecker()

        s = compute_fingerprint(set([1]))
        self.assertEqual(compute_fingerprint(set([2, 3])), s)
        distinct.add(s)

        distinct.add(compute_fingerprint([1]))
        distinct.add(compute_fingerprint(set([1j])))
        distinct.add(compute_fingerprint(set([4.5, 6.7])))
        distinct.add(compute_fingerprint(set([(1, )])))

        with self.assertRaises(ValueError):
            compute_fingerprint(set())
        with self.assertRaises(NotImplementedError):
            compute_fingerprint(frozenset([2, 3]))
Exemplo n.º 11
0
    def test_dtype(self):
        distinct = DistinctChecker()

        s = compute_fingerprint(np.dtype("int64"))
        self.assertEqual(compute_fingerprint(np.dtype("int64")), s)
        distinct.add(s)

        for descr in ("int32", "m8[s]", "m8[W]", "M8[s]"):
            distinct.add(np.dtype(descr))

        distinct.add(recordtype)
        distinct.add(recordtype2)

        # np.recarray() is peculiar: it creates a new dtype instance in
        # its constructor; check that the fingerprint remains efficient
        a = np.recarray(1, dtype=recordtype)
        b = np.recarray(1, dtype=recordtype)
        self.assertEqual(compute_fingerprint(a.dtype), compute_fingerprint(b.dtype))
Exemplo n.º 12
0
    def test_dtype(self):
        distinct = DistinctChecker()

        s = compute_fingerprint(np.dtype("int64"))
        self.assertEqual(compute_fingerprint(np.dtype("int64")), s)
        distinct.add(s)

        for descr in ("int32", "m8[s]", "m8[W]", "M8[s]"):
            distinct.add(np.dtype(descr))

        distinct.add(recordtype)
        distinct.add(recordtype2)

        # np.recarray() is peculiar: it creates a new dtype instance in
        # its constructor; check that the fingerprint remains efficient
        a = np.recarray(1, dtype=recordtype)
        b = np.recarray(1, dtype=recordtype)
        self.assertEqual(compute_fingerprint(a.dtype), compute_fingerprint(b.dtype))
Exemplo n.º 13
0
    def test_tuples(self):
        distinct = DistinctChecker()

        s = compute_fingerprint((1,))
        self.assertEqual(compute_fingerprint((2,)), s)
        distinct.add(s)

        distinct.add(compute_fingerprint(()))
        distinct.add(compute_fingerprint((1, 2, 3)))
        distinct.add(compute_fingerprint((1j, 2, 3)))
        distinct.add(compute_fingerprint((1, (), np.empty(5))))
        distinct.add(compute_fingerprint((1, (), np.empty((5, 1)))))
Exemplo n.º 14
0
    def test_tuples(self):
        distinct = DistinctChecker()

        s = compute_fingerprint((1, ))
        self.assertEqual(compute_fingerprint((2, )), s)
        distinct.add(s)

        distinct.add(compute_fingerprint(()))
        distinct.add(compute_fingerprint((1, 2, 3)))
        distinct.add(compute_fingerprint((1j, 2, 3)))
        distinct.add(compute_fingerprint((1, (), np.empty(5))))
        distinct.add(compute_fingerprint((1, (), np.empty((5, 1)))))
Exemplo n.º 15
0
    def test_lists(self):
        distinct = DistinctChecker()

        s = compute_fingerprint([1])
        self.assertEqual(compute_fingerprint([2, 3]), s)
        distinct.add(s)

        distinct.add(compute_fingerprint([1j]))
        distinct.add(compute_fingerprint([4.5, 6.7]))
        distinct.add(compute_fingerprint([(1, )]))

        with self.assertRaises(ValueError):
            compute_fingerprint([])
Exemplo n.º 16
0
    def test_lists(self):
        distinct = DistinctChecker()

        s = compute_fingerprint([1])
        self.assertEqual(compute_fingerprint([2, 3]), s)
        distinct.add(s)

        distinct.add(compute_fingerprint([1j]))
        distinct.add(compute_fingerprint([4.5, 6.7]))
        distinct.add(compute_fingerprint([(1,)]))

        with self.assertRaises(ValueError):
            compute_fingerprint([])
Exemplo n.º 17
0
    def test_fingerprint_failure(self):
        """
        Failure in computing the fingerprint cannot affect a nopython=False
        function.  On the other hand, with nopython=True, a ValueError should
        be raised to report the failure with fingerprint.
        """
        @jit
        def foo(x):
            return x

        # Empty list will trigger failure in compile_fingerprint
        errmsg = 'cannot compute fingerprint of empty list'
        with self.assertRaises(ValueError) as raises:
            _dispatcher.compute_fingerprint([])
        self.assertIn(errmsg, str(raises.exception))
        # It should work in fallback
        self.assertEqual(foo([]), [])
        # But, not in nopython=True
        strict_foo = jit(nopython=True)(foo.py_func)
        with self.assertRaises(ValueError) as raises:
            strict_foo([])
        self.assertIn(errmsg, str(raises.exception))

        # Test in loop lifting context
        @jit
        def bar():
            object()  # force looplifting
            x = []
            for i in range(10):
                x = foo(x)
            return x

        self.assertEqual(bar(), [])
        # Make sure it was looplifted
        [cr] = bar.overloads.values()
        self.assertEqual(len(cr.lifted), 1)
Exemplo n.º 18
0
    def test_fingerprint_failure(self):
        """
        Failure in computing the fingerprint cannot affect a nopython=False
        function.  On the other hand, with nopython=True, a ValueError should
        be raised to report the failure with fingerprint.
        """
        @jit
        def foo(x):
            return x

        # Empty list will trigger failure in compile_fingerprint
        errmsg = 'cannot compute fingerprint of empty list'
        with self.assertRaises(ValueError) as raises:
            _dispatcher.compute_fingerprint([])
        self.assertIn(errmsg, str(raises.exception))
        # It should work in fallback
        self.assertEqual(foo([]), [])
        # But, not in nopython=True
        strict_foo = jit(nopython=True)(foo.py_func)
        with self.assertRaises(ValueError) as raises:
            strict_foo([])
        self.assertIn(errmsg, str(raises.exception))

        # Test in loop lifting context
        @jit
        def bar():
            object()  # force looplifting
            x = []
            for i in range(10):
                x = foo(x)
            return x

        self.assertEqual(bar(), [])
        # Make sure it was looplifted
        [cr] = bar.overloads.values()
        self.assertEqual(len(cr.lifted), 1)
Exemplo n.º 19
0
    def test_arrays(self):
        distinct = DistinctChecker()

        # 1D
        arr = np.empty(4, dtype=np.float64)
        s = compute_fingerprint(arr)
        distinct.add(s)
        self.assertEqual(compute_fingerprint(arr[:1]), s)
        # Non-contiguous
        distinct.add(compute_fingerprint(arr[::2]))
        # Other type
        distinct.add(compute_fingerprint(arr.astype(np.complex64)))
        # Readonly
        arr.setflags(write=False)
        distinct.add(compute_fingerprint(arr))

        # 2D
        arr = np.empty((4, 4), dtype=np.float64)
        distinct.add(compute_fingerprint(arr))
        # F-contiguous
        distinct.add(compute_fingerprint(arr.T))
        # Non-contiguous
        distinct.add(compute_fingerprint(arr[::2]))

        # 0D
        arr = np.empty((), dtype=np.float64)
        distinct.add(compute_fingerprint(arr))

        # Structured arrays
        arr = np.empty(5, dtype=recordtype)
        s = compute_fingerprint(arr)
        distinct.add(s)
        self.assertEqual(compute_fingerprint(arr[:1]), s)
        arr = np.empty(5, dtype=recordtype2)
        distinct.add(compute_fingerprint(arr))
        arr = np.empty(5, dtype=recordtype3)
        distinct.add(compute_fingerprint(arr))

        # np.recarray() is peculiar: it creates a new dtype instance in
        # its constructor; check that the fingerprint remains efficient
        a = np.recarray(1, dtype=recordtype)
        b = np.recarray(1, dtype=recordtype)
        self.assertEqual(compute_fingerprint(a), compute_fingerprint(b))
Exemplo n.º 20
0
 def test_complex(self):
     s = compute_fingerprint(1j)
     self.assertEqual(s, compute_fingerprint(1+0j))
     s = compute_fingerprint(np.complex64())
     self.assertEqual(compute_fingerprint(np.complex64(2.0)), s)
     self.assertNotEqual(compute_fingerprint(np.complex128()), s)
Exemplo n.º 21
0
 def test_enums(self):
     # Enums should fail fingerprinting, even IntEnums
     with self.assertRaises(NotImplementedError):
         compute_fingerprint(Color.red)
     with self.assertRaises(NotImplementedError):
         compute_fingerprint(RequestError.not_found)
Exemplo n.º 22
0
 def test_none(self):
     compute_fingerprint(None)
Exemplo n.º 23
0
    def test_buffers(self):
        distinct = DistinctChecker()

        s = compute_fingerprint(b'')
        self.assertEqual(compute_fingerprint(b'xx'), s)
        distinct.add(s)
        distinct.add(compute_fingerprint(bytearray()))
        distinct.add(compute_fingerprint(memoryview(b'')))
        m_uint8_1d = compute_fingerprint(memoryview(bytearray()))
        distinct.add(m_uint8_1d)

        if sys.version_info >= (3,):
            arr = array.array('B', [42])
            distinct.add(compute_fingerprint(arr))
            self.assertEqual(compute_fingerprint(memoryview(arr)), m_uint8_1d)
            for array_code in 'bi':
                arr = array.array(array_code, [0, 1, 2])
                distinct.add(compute_fingerprint(arr))
                distinct.add(compute_fingerprint(memoryview(arr)))

        arr = np.empty(16, dtype=np.uint8)
        distinct.add(compute_fingerprint(arr))
        self.assertEqual(compute_fingerprint(memoryview(arr)), m_uint8_1d)
        arr = arr.reshape((4, 4))
        distinct.add(compute_fingerprint(arr))
        distinct.add(compute_fingerprint(memoryview(arr)))
        arr = arr.T
        distinct.add(compute_fingerprint(arr))
        distinct.add(compute_fingerprint(memoryview(arr)))
        arr = arr[::2]
        distinct.add(compute_fingerprint(arr))
        distinct.add(compute_fingerprint(memoryview(arr)))

        if sys.version_info >= (3,):
            m = mmap.mmap(-1, 16384)
            distinct.add(compute_fingerprint(m))
            self.assertEqual(compute_fingerprint(memoryview(m)), m_uint8_1d)
Exemplo n.º 24
0
 def test_floats(self):
     s = compute_fingerprint(1.0)
     self.assertEqual(compute_fingerprint(2.0), s)
     s = compute_fingerprint(np.float32())
     self.assertEqual(compute_fingerprint(np.float32(2.0)), s)
     self.assertNotEqual(compute_fingerprint(np.float64()), s)
Exemplo n.º 25
0
 def test_floats(self):
     s = compute_fingerprint(1.0)
     self.assertEqual(compute_fingerprint(2.0), s)
     s = compute_fingerprint(np.float32())
     self.assertEqual(compute_fingerprint(np.float32(2.0)), s)
     self.assertNotEqual(compute_fingerprint(np.float64()), s)
Exemplo n.º 26
0
 def test_complicated_type(self):
     # Generating a large fingerprint
     t = None
     for i in range(1000):
         t = (t,)
     s = compute_fingerprint(t)
Exemplo n.º 27
0
 def test_enums(self):
     # Enums should fail fingerprinting, even IntEnums
     with self.assertRaises(NotImplementedError):
         compute_fingerprint(Color.red)
     with self.assertRaises(NotImplementedError):
         compute_fingerprint(RequestError.not_found)
Exemplo n.º 28
0
    def test_buffers(self):
        distinct = DistinctChecker()

        s = compute_fingerprint(b'')
        self.assertEqual(compute_fingerprint(b'xx'), s)
        distinct.add(s)
        distinct.add(compute_fingerprint(bytearray()))
        distinct.add(compute_fingerprint(memoryview(b'')))
        m_uint8_1d = compute_fingerprint(memoryview(bytearray()))
        distinct.add(m_uint8_1d)

        arr = array.array('B', [42])
        distinct.add(compute_fingerprint(arr))
        self.assertEqual(compute_fingerprint(memoryview(arr)), m_uint8_1d)
        for array_code in 'bi':
            arr = array.array(array_code, [0, 1, 2])
            distinct.add(compute_fingerprint(arr))
            distinct.add(compute_fingerprint(memoryview(arr)))

        arr = np.empty(16, dtype=np.uint8)
        distinct.add(compute_fingerprint(arr))
        self.assertEqual(compute_fingerprint(memoryview(arr)), m_uint8_1d)
        arr = arr.reshape((4, 4))
        distinct.add(compute_fingerprint(arr))
        distinct.add(compute_fingerprint(memoryview(arr)))
        arr = arr.T
        distinct.add(compute_fingerprint(arr))
        distinct.add(compute_fingerprint(memoryview(arr)))
        arr = arr[::2]
        distinct.add(compute_fingerprint(arr))
        distinct.add(compute_fingerprint(memoryview(arr)))

        m = mmap.mmap(-1, 16384)
        distinct.add(compute_fingerprint(m))
        self.assertEqual(compute_fingerprint(memoryview(m)), m_uint8_1d)
Exemplo n.º 29
0
 def test_none(self):
     compute_fingerprint(None)
Exemplo n.º 30
0
 def test_bool(self):
     s = compute_fingerprint(True)
     self.assertEqual(compute_fingerprint(False), s)
     self.assertNotEqual(compute_fingerprint(1), s)
Exemplo n.º 31
0
 def test_bool(self):
     s = compute_fingerprint(True)
     self.assertEqual(compute_fingerprint(False), s)
     self.assertNotEqual(compute_fingerprint(1), s)
Exemplo n.º 32
0
 def test_complex(self):
     s = compute_fingerprint(1j)
     self.assertEqual(s, compute_fingerprint(1 + 0j))
     s = compute_fingerprint(np.complex64())
     self.assertEqual(compute_fingerprint(np.complex64(2.0)), s)
     self.assertNotEqual(compute_fingerprint(np.complex128()), s)
Exemplo n.º 33
0
 def test_complicated_type(self):
     # Generating a large fingerprint
     t = None
     for i in range(1000):
         t = (t, )
     s = compute_fingerprint(t)
Exemplo n.º 34
0
    def test_arrays(self):
        distinct = DistinctChecker()

        # 1D
        arr = np.empty(4, dtype=np.float64)
        s = compute_fingerprint(arr)
        distinct.add(s)
        self.assertEqual(compute_fingerprint(arr[:1]), s)
        # Non-contiguous
        distinct.add(compute_fingerprint(arr[::2]))
        # Other type
        distinct.add(compute_fingerprint(arr.astype(np.complex64)))
        # Readonly
        arr.setflags(write=False)
        distinct.add(compute_fingerprint(arr))

        # 2D
        arr = np.empty((4, 4), dtype=np.float64)
        distinct.add(compute_fingerprint(arr))
        # F-contiguous
        distinct.add(compute_fingerprint(arr.T))
        # Non-contiguous
        distinct.add(compute_fingerprint(arr[::2]))

        # 0D
        arr = np.empty((), dtype=np.float64)
        distinct.add(compute_fingerprint(arr))

        # Structured arrays
        arr = np.empty(5, dtype=recordtype)
        s = compute_fingerprint(arr)
        distinct.add(s)
        self.assertEqual(compute_fingerprint(arr[:1]), s)
        arr = np.empty(5, dtype=recordtype2)
        distinct.add(compute_fingerprint(arr))
        arr = np.empty(5, dtype=recordtype3)
        distinct.add(compute_fingerprint(arr))

        # np.recarray() is peculiar: it creates a new dtype instance in
        # its constructor; check that the fingerprint remains efficient
        a = np.recarray(1, dtype=recordtype)
        b = np.recarray(1, dtype=recordtype)
        self.assertEqual(compute_fingerprint(a),
                         compute_fingerprint(b))