def test_del_at_beginning_of_loop(self):
        """
        Test issue #1734
        """

        @njit
        def f(arr):
            res = 0

            for i in (0, 1):
                # `del t` is issued here before defining t.  It must be
                # correctly handled by the lowering phase.
                t = arr[i]
                if t[i] > 1:
                    res += t[i]

            return res

        arr = np.ones((2, 2))
        init_stats = rtsys.get_allocation_stats()
        f(arr)
        cur_stats = rtsys.get_allocation_stats()
        self.assertEqual(
            cur_stats.alloc - init_stats.alloc, cur_stats.free - init_stats.free
        )
    def test_invalid_computation_of_lifetime(self):
        """
        Test issue #1573
        """

        @njit
        def if_with_allocation_and_initialization(arr1, test1):
            tmp_arr = np.zeros_like(arr1)

            for i in range(tmp_arr.shape[0]):
                pass

            if test1:
                np.zeros_like(arr1)

            return tmp_arr

        arr = np.random.random((5, 5))  # the values are not consumed

        init_stats = rtsys.get_allocation_stats()
        if_with_allocation_and_initialization(arr, False)
        cur_stats = rtsys.get_allocation_stats()
        self.assertEqual(
            cur_stats.alloc - init_stats.alloc, cur_stats.free - init_stats.free
        )
Example #3
0
    def test_stats_status_toggle(self):

        @njit
        def foo():
            tmp = np.ones(3)
            return np.arange(5 * tmp[0])

        # Switch on stats
        _nrt_python.memsys_enable_stats()
        # check the stats are on
        self.assertTrue(_nrt_python.memsys_stats_enabled())

        for i in range(2):
            # capture the stats state
            stats_1 = rtsys.get_allocation_stats()
            # Switch off stats
            _nrt_python.memsys_disable_stats()
            # check the stats are off
            self.assertFalse(_nrt_python.memsys_stats_enabled())
            # run something that would move the counters were they enabled
            foo()
            # Switch on stats
            _nrt_python.memsys_enable_stats()
            # check the stats are on
            self.assertTrue(_nrt_python.memsys_stats_enabled())
            # capture the stats state (should not have changed)
            stats_2 = rtsys.get_allocation_stats()
            # run something that will move the counters
            foo()
            # capture the stats state (should have changed)
            stats_3 = rtsys.get_allocation_stats()
            # check stats_1 == stats_2
            self.assertEqual(stats_1, stats_2)
            # check stats_2 < stats_3
            self.assertLess(stats_2, stats_3)
Example #4
0
    def test_rtsys_stats_query_raises_exception_when_disabled(self):
        # Checks that the standard rtsys.get_allocation_stats() query raises
        # when stats counters are turned off.

        _nrt_python.memsys_disable_stats()
        self.assertFalse(_nrt_python.memsys_stats_enabled())

        with self.assertRaises(RuntimeError) as raises:
            rtsys.get_allocation_stats()

        self.assertIn("NRT stats are disabled.", str(raises.exception))
Example #5
0
 def assertNoNRTLeak(self):
     """
     A context manager that asserts no NRT leak was created during
     the execution of the enclosed block.
     """
     old = rtsys.get_allocation_stats()
     yield
     new = rtsys.get_allocation_stats()
     total_alloc = new.alloc - old.alloc
     total_free = new.free - old.free
     total_mi_alloc = new.mi_alloc - old.mi_alloc
     total_mi_free = new.mi_free - old.mi_free
     self.assertEqual(total_alloc, total_free,
                      "number of data allocs != number of data frees")
     self.assertEqual(total_mi_alloc, total_mi_free,
                      "number of meminfo allocs != number of meminfo frees")
Example #6
0
    def test_no_return(self):
        """
        Test issue #1291
        """
        @njit
        def foo(n):
            for i in range(n):
                temp = np.zeros(2)
            return 0

        n = 10
        init_stats = rtsys.get_allocation_stats()
        foo(n)
        cur_stats = rtsys.get_allocation_stats()
        self.assertEqual(cur_stats.alloc - init_stats.alloc, n)
        self.assertEqual(cur_stats.free - init_stats.free, n)
Example #7
0
 def assert_no_memory_leak(self):
     old = self.__init_stats
     new = rtsys.get_allocation_stats()
     total_alloc = new.alloc - old.alloc
     total_free = new.free - old.free
     total_mi_alloc = new.mi_alloc - old.mi_alloc
     total_mi_free = new.mi_free - old.mi_free
     self.assertEqual(total_alloc, total_free)
     self.assertEqual(total_mi_alloc, total_mi_free)
Example #8
0
def check_leaks():
    import gc
    from numba.core.runtime import rtsys

    try:
        yield None
    finally:
        gc.collect()

    stats = rtsys.get_allocation_stats()
    assert stats.alloc == stats.free
    assert stats.mi_alloc == stats.mi_free
Example #9
0
    def test_escaping_var_init_in_loop(self):
        """
        Test issue #1297
        """
        @njit
        def g(n):

            x = np.zeros((n, 2))

            for i in range(n):
                y = x[i]

            for i in range(n):
                y = x[i]

            return 0

        init_stats = rtsys.get_allocation_stats()
        g(10)
        cur_stats = rtsys.get_allocation_stats()
        self.assertEqual(cur_stats.alloc - init_stats.alloc, 1)
        self.assertEqual(cur_stats.free - init_stats.free, 1)
Example #10
0
 def memory_leak_setup(self):
     # Clean up any NRT-backed objects hanging in a dead reference cycle
     gc.collect()
     self.__init_stats = rtsys.get_allocation_stats()
Example #11
0
    result = pushpop(size)
    print("== Result ==")
    print(result)
    assert result == list(reversed(range(size)))


def test_exception():
    """
    Test exception raised from a jit method
    """
    stack = Stack()
    stack.push(1)
    assert 1 == stack.pop()
    try:
        # Unfortunately, numba will leak when an exception is thrown.
        stack.pop()
    except ValueError as e:
        assert 'empty' == str(e)


def runme():
    size = 24
    test_pushpop(size)
    test_exception()


if __name__ == '__main__':
    runme()
    print("== Print memory allocation information == ")
    print(rtsys.get_allocation_stats())