class TestThreadingLayerSelection(ThreadLayerTestHelper):
    """
    Checks that numba.threading_layer() reports correctly.
    """
    _DEBUG = False

    backends = {
        'tbb': skip_no_tbb,
        'omp': skip_no_omp,
        'workqueue': unittest.skipIf(False, '')
    }

    @classmethod
    def _inject(cls, backend, backend_guard):
        def test_template(self):
            body = """if 1:
                X = np.arange(1000000.)
                Y = np.arange(1000000.)
                Z = busy_func(X, Y)
                assert numba.threading_layer() == '%s'
            """
            runme = self.template % (body % backend)
            cmdline = [sys.executable, '-c', runme]
            env = os.environ.copy()
            env['NUMBA_THREADING_LAYER'] = str(backend)
            out, err = self.run_cmd(cmdline, env=env)
            if self._DEBUG:
                print(out, err)

        injected_test = "test_threading_layer_selector_%s" % backend
        setattr(cls, injected_test,
                tag("important")(backend_guard(test_template)))

    @classmethod
    def generate(cls):
        for backend, backend_guard in cls.backends.items():
            cls._inject(backend, backend_guard)
Beispiel #2
0
try:
    import setuptools
except ImportError:
    setuptools = None

import llvmlite.binding as ll

from numba import unittest_support as unittest
from numba.pycc import main
from numba.pycc.decorators import clear_export_registry
from numba.pycc.platform import find_shared_ending, find_pyext_ending
from numba.pycc.platform import _external_compiler_ok

# if suitable compilers are not present then skip.
_skip_reason = 'AOT compatible compilers missing'
_skip_missing_compilers = unittest.skipIf(not _external_compiler_ok,
                                          _skip_reason)
_skip_reason = 'windows only'
_windows_only = unittest.skipIf(not sys.platform.startswith('win'),
                                _skip_reason)

from .matmul_usecase import has_blas
from .support import TestCase, tag, import_dynamic, temp_directory


base_path = os.path.dirname(os.path.abspath(__file__))


def unset_macosx_deployment_target():
    """Unset MACOSX_DEPLOYMENT_TARGET because we are not building portable
    libraries
    """
Beispiel #3
0
from numba import njit, gdb, gdb_init, gdb_breakpoint, prange, errors
from numba import jit
from numba import unittest_support as unittest
from numba.targets.gdb_hook import _confirm_gdb

from .support import (TestCase, captured_stdout, tag)
from .test_parfors import skip_unsupported as parfors_skip_unsupported

_platform = sys.platform

_unix_like = (_platform.startswith('linux')
              or _platform.startswith('darwin')
              or ('bsd' in _platform))

unix_only = unittest.skipUnless(_unix_like, "unix-like OS is required")
not_unix = unittest.skipIf(_unix_like, "non unix-like OS is required")

_arch_name = platform.machine()
_is_arm = _arch_name in {'aarch64', 'armv7l'}
not_arm = unittest.skipIf(_is_arm, "testing disabled on ARM")

_gdb_cond = os.environ.get('GDB_TEST', None) == '1'
needs_gdb_harness = unittest.skipUnless(_gdb_cond, "needs gdb harness")

# check if gdb is present and working
try:
    _confirm_gdb()
    _HAVE_GDB = True
except Exception:
    _HAVE_GDB = False
Beispiel #4
0
import sys

import numpy as np

from numba.compiler import compile_isolated, Flags
from numba import jit, types, utils, njit, errors
import numba.unittest_support as unittest
from numba import testing
from numba.six import PY2
from .support import TestCase, MemoryLeakMixin, tag

from numba.targets.quicksort import make_py_quicksort, make_jit_quicksort
from numba.targets.mergesort import make_jit_mergesort
from .timsort import make_py_timsort, make_jit_timsort, MergeRun

skip_py_27 = unittest.skipIf(PY2, "Not supported on Python 2")


def make_temp_list(keys, n):
    return [keys[0]] * n


def make_temp_array(keys, n):
    return np.empty(n, keys.dtype)


py_list_timsort = make_py_timsort(make_temp_list)

py_array_timsort = make_py_timsort(make_temp_array)

jit_list_timsort = make_jit_timsort(make_temp_list)
Beispiel #5
0
    import setuptools
except ImportError:
    setuptools = None

import llvmlite.binding as ll

from numba import unittest_support as unittest
from numba import utils
from numba.pycc import main
from numba.pycc.decorators import clear_export_registry
from numba.pycc.platform import find_shared_ending, find_pyext_ending
from numba.pycc.platform import _external_compiler_ok

# if suitable compilers are not present then skip.
_skip_reason = 'AOT compatible compilers missing'
_skip_missing_compilers = unittest.skipIf(not _external_compiler_ok,
                                          _skip_reason)
_skip_reason = 'windows only'
_windows_only = unittest.skipIf(not sys.platform.startswith('win'),
                                _skip_reason)

from .support import TestCase, tag, import_dynamic, temp_directory, has_blas

base_path = os.path.dirname(os.path.abspath(__file__))


def unset_macosx_deployment_target():
    """Unset MACOSX_DEPLOYMENT_TARGET because we are not building portable
    libraries
    """
    if 'MACOSX_DEPLOYMENT_TARGET' in os.environ:
        del os.environ['MACOSX_DEPLOYMENT_TARGET']
Beispiel #6
0
from numba import types, typing, utils
from numba.compiler import compile_extra, compile_isolated, Flags, DEFAULT_FLAGS
from numba.lowering import LoweringError
from numba.targets import cpu
from numba.typeinfer import TypingError
import numba.unittest_support as unittest

enable_pyobj_flags = Flags()
enable_pyobj_flags.set("enable_pyobject")

force_pyobj_flags = Flags()
force_pyobj_flags.set("force_pyobject")

no_pyobj_flags = Flags()

skip_on_numpy_16 = unittest.skipIf(np.__version__.startswith("1.6."),
                                   "test requires Numpy 1.7 or later")


class CompilationCache(object):
    """
    A cache of compilation results for various signatures and flags.
    This can make tests significantly faster (or less slow).
    """
    def __init__(self):
        self.typingctx = typing.Context()
        self.targetctx = cpu.CPUContext(self.typingctx)
        self.cr_cache = {}

    def compile(self, func, args, return_type=None, flags=DEFAULT_FLAGS):
        """
        Compile the function or retrieve an already compiled result
Beispiel #7
0
from numba.targets import cpu
import numba.unittest_support as unittest
from numba.runtime import rtsys


enable_pyobj_flags = Flags()
enable_pyobj_flags.set("enable_pyobject")

force_pyobj_flags = Flags()
force_pyobj_flags.set("force_pyobject")

no_pyobj_flags = Flags()


is_on_numpy_16 = numpy_support.version == (1, 6)
skip_on_numpy_16 = unittest.skipIf(is_on_numpy_16,
                                   "test requires Numpy 1.7 or later")


class CompilationCache(object):
    """
    A cache of compilation results for various signatures and flags.
    This can make tests significantly faster (or less slow).
    """

    def __init__(self):
        self.typingctx = typing.Context()
        self.targetctx = cpu.CPUContext(self.typingctx)
        self.cr_cache = {}

    def compile(self, func, args, return_type=None, flags=DEFAULT_FLAGS):
        """
class TestThreadingLayerSelection(ThreadLayerTestHelper):
    """
    Checks that numba.threading_layer() reports correctly.
    """
    _DEBUG = False

    backends = {'tbb': skip_no_tbb,
                'omp': skip_no_omp,
                'workqueue': unittest.skipIf(False, '')}

    @classmethod
    def _inject(cls, backend, backend_guard):

        def test_template(self):
            body = """if 1:
                X = np.arange(1000000.)
                Y = np.arange(1000000.)
                Z = busy_func(X, Y)
                assert numba.threading_layer() == '%s'
            """
            runme = self.template % (body % backend)
            cmdline = [sys.executable, '-c', runme]
            env = os.environ.copy()
            env['NUMBA_THREADING_LAYER'] = str(backend)
            out, err = self.run_cmd(cmdline, env=env)
            if self._DEBUG:
                print(out, err)
        injected_test = "test_threading_layer_selector_%s" % backend
        setattr(cls, injected_test,
                tag("important")(backend_guard(test_template)))

    @classmethod
    def generate(cls):
        for backend, backend_guard in cls.backends.items():
            cls._inject(backend, backend_guard)


    @skip_no_tbb
    @skip_unless_py3
    def test_single_thread_tbb(self):
        """
        Tests that TBB works well with single thread
        https://github.com/numba/numba/issues/3440
        """
        runme = """if 1:
            from numba import njit, prange, threading_layer
            @njit(parallel=True)
            def foo(n):
                acc = 0
                for i in prange(n):
                    acc += i
                return acc
            foo(100)
            print(threading_layer())
        """
        cmdline = [sys.executable, '-c', runme]
        env = os.environ.copy()
        env['NUMBA_THREADING_LAYER'] = "tbb"
        env['NUMBA_NUM_THREADS'] = "1"
        out, err = self.run_cmd(cmdline, env=env)
        if self._DEBUG:
            print(out, err)
        assert out.strip() == "tbb"
Beispiel #9
0
def truediv(a, b):
    return a / b

def floordiv(a, b):
    return a // b

def remainder(a, b):
    return a % b

def power(a, b):
    return a ** b

# See https://github.com/numpy/numpy/pull/3691
skipIfFPStatusBug = unittest.skipIf(
    sys.platform == 'win32' and np_version < (1, 8) and sys.maxsize < 2 ** 32,
    "test disabled because of FPU state handling issue on Numpy < 1.8")


class TestExceptions(TestCase):
    """
    Test raising exceptions inside ufuncs.
    """

    def check_ufunc_raise(self, **vectorize_args):
        f = vectorize(['float64(float64)'], **vectorize_args)(sqrt)
        arr = np.array([1, 4, -2, 9, -1, 16], dtype=np.float64)
        out = np.zeros_like(arr)
        with self.assertRaises(ValueError) as cm:
            f(arr, out)
        self.assertIn('Value must be positive', str(cm.exception))
Beispiel #10
0
from numba.utils import PYVERSION
from .support import TestCase, enable_pyobj_flags


def build_set_usecase(*args):
    ns = {}
    src = """if 1:
    def build_set():
        return {%s}
    """ % ', '.join(repr(arg) for arg in args)
    code = compile(src, '<>', 'exec')
    eval(code, ns)
    return ns['build_set']


needs_set_literals = unittest.skipIf(PYVERSION < (2, 7),
                                     "set literals unavailable before Python 2.7")


class SetTestCase(TestCase):

    @needs_set_literals
    def test_build_set(self, flags=enable_pyobj_flags):
        pyfunc = build_set_usecase(1, 2, 3, 2)
        self.run_nullary_func(pyfunc, flags=flags)

    @needs_set_literals
    def test_build_heterogenous_set(self, flags=enable_pyobj_flags):
        pyfunc = build_set_usecase(1, 2.0, 3j, 2)
        self.run_nullary_func(pyfunc, flags=flags)
        # Check that items are inserted in the right order (here the
        # result will be {2}, not {2.0})
Beispiel #11
0
    return pow_usecase


# On 64-bit Windows, MSVC 2008 (Python 2.7) and Numpy 1.9, there's a
# heisenbug that seems to manifest as wrong values returned by the CRT's
# fmodf() function (which is called by LLVM's lowering of the `frem`
# instruction).
# Perhaps Numpy messing with some internal FP state that confuses the CRT?
# Regardless, the only sane thing to do seems to be to skip the tests...

_windows_floordiv_heisenbug = (sys.platform == 'win32'
                               and numpy_support.version[:2] == (1, 9)
                               and MACHINE_BITS == 64)

_avoid_windows_floordiv_heisenbug = unittest.skipIf(
    _windows_floordiv_heisenbug,
    "avoid Windows + Numpy floordiv heisenbug - see PR #2057")


class LiteralOperatorImpl(object):
    @staticmethod
    def add_usecase(x, y):
        return x + y

    @staticmethod
    def iadd_usecase(x, y):
        x += y
        return x

    @staticmethod
    def sub_usecase(x, y):
Beispiel #12
0
force_pyobj_flags = Flags()
force_pyobj_flags.set("force_pyobject")

no_pyobj_flags = Flags()

nrt_flags = Flags()
nrt_flags.set("nrt")

tag = testing.make_tag_decorator(['important', 'long_running'])

_windows_py27 = (sys.platform.startswith('win32')
                 and sys.version_info[:2] == (2, 7))
_32bit = sys.maxsize <= 2**32
_reason = 'parfors not supported'
skip_parfors_unsupported = unittest.skipIf(_32bit or _windows_py27, _reason)
skip_py38_or_later = unittest.skipIf(utils.PYVERSION >= (3, 8),
                                     "unsupported on py3.8 or later")


class CompilationCache(object):
    """
    A cache of compilation results for various signatures and flags.
    This can make tests significantly faster (or less slow).
    """
    def __init__(self):
        self.typingctx = typing.Context()
        self.targetctx = cpu.CPUContext(self.typingctx)
        self.cr_cache = {}

    def compile(self, func, args, return_type=None, flags=DEFAULT_FLAGS):
Beispiel #13
0
from numba import unittest_support as unittest
from numba import (njit, typeof, types, typing, typeof, ir, utils, bytecode,
    jitclass, prange)
from .support import TestCase, tag
from numba.array_analysis import EquivSet, ArrayAnalysis
from numba.compiler import Pipeline, Flags, _PipelineManager
from numba.targets import cpu, registry
from numba.numpy_support import version as numpy_version
from numba.ir_utils import remove_dead

# for parallel tests, marking that Windows with Python 2.7 is not supported
_windows_py27 = (sys.platform.startswith('win32') and
                 sys.version_info[:2] == (2, 7))
_32bit = sys.maxsize <= 2 ** 32
_reason = 'parfors not supported'
skip_unsupported = unittest.skipIf(_32bit or _windows_py27, _reason)


# test class for #3700
@jitclass([('L', types.int32), ('T', types.int32)])
class ExampleClass3700(object):
    def __init__(self, n):
        self.L = n
        self.T = n + 1


class TestEquivSet(TestCase):

    """
    Test array_analysis.EquivSet.
    """
Beispiel #14
0
enable_pyobj_flags = Flags()
enable_pyobj_flags.set("enable_pyobject")

force_pyobj_flags = Flags()
force_pyobj_flags.set("force_pyobject")

no_pyobj_flags = Flags()

nrt_flags = Flags()
nrt_flags.set("nrt")


tag = testing.make_tag_decorator(['important', 'long_running'])

_32bit = sys.maxsize <= 2 ** 32
skip_parfors_unsupported = unittest.skipIf(_32bit, 'parfors not supported')
skip_py38_or_later = unittest.skipIf(
    utils.PYVERSION >= (3, 8),
    "unsupported on py3.8 or later"
)
skip_tryexcept_unsupported = unittest.skipIf(
    utils.PYVERSION < (3, 7),
    "try-except unsupported on py3.6 or earlier"
)
skip_tryexcept_supported = unittest.skipIf(
    utils.PYVERSION >= (3, 7),
    "try-except supported on py3.7 or later"
)

_msg = "SciPy needed for test"
skip_unless_scipy = unittest.skipIf(scipy is None, _msg)
Beispiel #15
0
force_pyobj_flags = Flags()
force_pyobj_flags.set("force_pyobject")

no_pyobj_flags = Flags()

nrt_flags = Flags()
nrt_flags.set("nrt")

tag = testing.make_tag_decorator(['important', 'long_running'])

_windows_py27 = (sys.platform.startswith('win32')
                 and sys.version_info[:2] == (2, 7))
_32bit = sys.maxsize <= 2**32
_reason = 'parfors not supported'
skip_parfors_unsupported = unittest.skipIf(_32bit or _windows_py27, _reason)
skip_py38_or_later = unittest.skipIf(utils.PYVERSION >= (3, 8),
                                     "unsupported on py3.8 or later")
skip_tryexcept_unsupported = unittest.skipIf(
    utils.PYVERSION < (3, 7), "try-except unsupported on py3.6 or earlier")
skip_tryexcept_supported = unittest.skipIf(
    utils.PYVERSION >= (3, 7), "try-except supported on py3.7 or later")

_msg = "SciPy needed for test"
skip_unless_scipy = unittest.skipIf(scipy is None, _msg)


class CompilationCache(object):
    """
    A cache of compilation results for various signatures and flags.
    This can make tests significantly faster (or less slow).
Beispiel #16
0
def skip_on_cudasim(reason):
    return unittest.skipIf(config.ENABLE_CUDASIM, reason)
Beispiel #17
0
from numba import njit, gdb, gdb_init, gdb_breakpoint, prange, errors
from numba import jit
from numba import unittest_support as unittest
from numba.targets.gdb_hook import _confirm_gdb

from .support import (TestCase, captured_stdout, tag)
from .test_parfors import skip_unsupported as parfors_skip_unsupported

_platform = sys.platform

_unix_like = (_platform.startswith('linux') or _platform.startswith('darwin')
              or ('bsd' in _platform))

unix_only = unittest.skipUnless(_unix_like, "unix-like OS is required")
not_unix = unittest.skipIf(_unix_like, "non unix-like OS is required")

_gdb_cond = os.environ.get('GDB_TEST', None) == '1'
needs_gdb_harness = unittest.skipUnless(_gdb_cond, "needs gdb harness")

# check if gdb is present and working
try:
    _confirm_gdb()
    _HAVE_GDB = True
except Exception:
    _HAVE_GDB = False

_msg = "functioning gdb with correct ptrace permissions is required"
needs_gdb = unittest.skipUnless(_HAVE_GDB, _msg)
long_running = tag('long_running')
Beispiel #18
0
try:
    import setuptools
except ImportError:
    setuptools = None

import llvmlite.binding as ll

from numba import unittest_support as unittest
from numba.pycc import main
from numba.pycc.decorators import clear_export_registry
from numba.pycc.platform import find_shared_ending, find_pyext_ending
from numba.pycc.platform import _external_compiler_ok

# if suitable compilers are not present then skip.
_skip_reason = 'AOT compatible compilers missing'
_skip_missing_compilers = unittest.skipIf(not _external_compiler_ok,
                                          _skip_reason)

from .matmul_usecase import has_blas
from .support import TestCase, tag, import_dynamic, temp_directory

base_path = os.path.dirname(os.path.abspath(__file__))


def unset_macosx_deployment_target():
    """Unset MACOSX_DEPLOYMENT_TARGET because we are not building portable
    libraries
    """
    if 'MACOSX_DEPLOYMENT_TARGET' in os.environ:
        del os.environ['MACOSX_DEPLOYMENT_TARGET']

Beispiel #19
0
    return pow_usecase


# On 64-bit Windows, MSVC 2008 (Python 2.7) and Numpy 1.9, there's a
# heisenbug that seems to manifest as wrong values returned by the CRT's
# fmodf() function (which is called by LLVM's lowering of the `frem`
# instruction).
# Perhaps Numpy messing with some internal FP state that confuses the CRT?
# Regardless, the only sane thing to do seems to be to skip the tests...

_windows_floordiv_heisenbug = (
    sys.platform == 'win32' and numpy_support.version[:2] == (1, 9)
    and MACHINE_BITS == 64)

_avoid_windows_floordiv_heisenbug = unittest.skipIf(
    _windows_floordiv_heisenbug,
    "avoid Windows + Numpy floordiv heisenbug - see PR #2057")


class LiteralOperatorImpl(object):

    @staticmethod
    def add_usecase(x, y):
        return x + y

    @staticmethod
    def iadd_usecase(x, y):
        x += y
        return x

    @staticmethod
Beispiel #20
0
    nrt,
)
from numba.extending import intrinsic, include_path
from numba.typing import signature
from numba.targets.imputils import impl_ret_untracked
from llvmlite import ir
import llvmlite.binding as llvm
from numba import cffi_support
from numba.unsafe.nrt import NRT_get_api

from .support import MemoryLeakMixin, TestCase, temp_directory, import_dynamic

enable_nrt_flags = Flags()
enable_nrt_flags.set("nrt")

linux_only = unittest.skipIf(not sys.platform.startswith('linux'),
                             'linux only test')
x86_only = unittest.skipIf(platform.machine() not in ('i386', 'x86_64'),
                           'x86 only test')


class Dummy(object):
    alive = 0

    def __init__(self):
        type(self).alive += 1

    def __del__(self):
        type(self).alive -= 1


class TestNrtMemInfoNotInitialized(unittest.TestCase):
Beispiel #21
0
def skip_on_cudasim(reason):
    return unittest.skipIf(config.ENABLE_CUDASIM, reason)
Beispiel #22
0
from numba.lowering import LoweringError
from numba.targets import cpu
from numba.typeinfer import TypingError
import numba.unittest_support as unittest


enable_pyobj_flags = Flags()
enable_pyobj_flags.set("enable_pyobject")

force_pyobj_flags = Flags()
force_pyobj_flags.set("force_pyobject")

no_pyobj_flags = Flags()


skip_on_numpy_16 = unittest.skipIf(np.__version__.startswith("1.6."),
                                   "test requires Numpy 1.7 or later")


class CompilationCache(object):
    """
    A cache of compilation results for various signatures and flags.
    This can make tests significantly faster (or less slow).
    """

    def __init__(self):
        self.typingctx = typing.Context()
        self.targetctx = cpu.CPUContext(self.typingctx)
        self.cr_cache = {}

    def compile(self, func, args, return_type=None, flags=DEFAULT_FLAGS):
        """
Beispiel #23
0
from numba.compiler import compile_extra, compile_isolated, Flags, DEFAULT_FLAGS
from numba.targets import cpu
import numba.unittest_support as unittest


enable_pyobj_flags = Flags()
enable_pyobj_flags.set("enable_pyobject")

force_pyobj_flags = Flags()
force_pyobj_flags.set("force_pyobject")

no_pyobj_flags = Flags()


is_on_numpy_16 = np.__version__.startswith("1.6.")
skip_on_numpy_16 = unittest.skipIf(is_on_numpy_16,
                                   "test requires Numpy 1.7 or later")


class CompilationCache(object):
    """
    A cache of compilation results for various signatures and flags.
    This can make tests significantly faster (or less slow).
    """

    def __init__(self):
        self.typingctx = typing.Context()
        self.targetctx = cpu.CPUContext(self.typingctx)
        self.cr_cache = {}

    def compile(self, func, args, return_type=None, flags=DEFAULT_FLAGS):
        """
Beispiel #24
0
from __future__ import print_function, unicode_literals

import platform
import numpy as np

import numba.unittest_support as unittest
from numba import jit, utils, from_dtype, types
from numba.typed import Dict
from numba.tests.support import TestCase

skip_py2 = unittest.skipIf(not utils.IS_PY3, "not supported in Python 2")
require_py37 = unittest.skipIf(utils.PYVERSION < (3, 7),
                               "requires Python 3.7+")


def getitem(x, i):
    return x[i]


def getitem2(x, i, j):
    return x[i][j]


def setitem(x, i, v):
    x[i] = v
    return x


def setitem2(x, i, y, j):
    x[i] = y[j]
    return x
Beispiel #25
0
def reflect_dual(sa, sb):
    sa.add(sb.pop())
    return sa is sb


def unique_usecase(src):
    seen = set()
    res = []
    for v in src:
        if v not in seen:
            seen.add(v)
            res.append(v)
    return res


needs_set_literals = unittest.skipIf(sys.version_info < (2, 7),
                                     "set literals unavailable before Python 2.7")


class BaseTest(MemoryLeakMixin, TestCase):

    def setUp(self):
        super(BaseTest, self).setUp()
        self.rnd = random.Random(42)

    def _range(self, stop):
        return np.arange(int(stop))

    def _random_choice(self, seq, n):
        """
        Choose *n* possibly duplicate items from sequence.
        """
Beispiel #26
0
def reflect_dual(sa, sb):
    sa.add(sb.pop())
    return sa is sb


def unique_usecase(src):
    seen = set()
    res = []
    for v in src:
        if v not in seen:
            seen.add(v)
            res.append(v)
    return res


needs_set_literals = unittest.skipIf(
    sys.version_info < (2, 7), "set literals unavailable before Python 2.7")


class BaseTest(MemoryLeakMixin, TestCase):
    def setUp(self):
        super(BaseTest, self).setUp()
        self.rnd = random.Random(42)

    def _range(self, stop):
        return np.arange(int(stop))

    def _random_choice(self, seq, n):
        """
        Choose *n* possibly duplicate items from sequence.
        """
        l = [self.rnd.choice(list(seq)) for i in range(n)]
Beispiel #27
0
def floordiv(a, b):
    return a // b


def remainder(a, b):
    return a % b


def power(a, b):
    return a**b


# See https://github.com/numpy/numpy/pull/3691
skipIfFPStatusBug = unittest.skipIf(
    sys.platform == 'win32' and np_version < (1, 8) and sys.maxsize < 2**32,
    "test disabled because of FPU state handling issue on Numpy < 1.8")


class TestExceptions(TestCase):
    """
    Test raising exceptions inside ufuncs.
    """
    def check_ufunc_raise(self, **vectorize_args):
        f = vectorize(['float64(float64)'], **vectorize_args)(sqrt)
        arr = np.array([1, 4, -2, 9, -1, 16], dtype=np.float64)
        out = np.zeros_like(arr)
        with self.assertRaises(ValueError) as cm:
            f(arr, out)
        self.assertIn('Value must be positive', str(cm.exception))
        # All values were computed except for the ones giving an error
from numba.interpreter import Interpreter
from numba import typing, errors
from numba.targets.registry import cpu_target
from numba.targets import cpu
from numba.compiler import compile_ir, DEFAULT_FLAGS
from numba import njit, typeof, objmode
from .support import MemoryLeak, TestCase, captured_stdout


try:
    import scipy
except ImportError:
    scipy = None

_msg = "SciPy needed for test"
skip_unless_scipy = unittest.skipIf(scipy is None, _msg)


def get_func_ir(func):
    func_id = FunctionIdentity.from_function(func)
    bc = ByteCode(func_id=func_id)
    interp = Interpreter(func_id)
    func_ir = interp.interpret(bc)
    return func_ir


def lift1():
    print("A")
    with bypass_context:
        print("B")
        b()
class TestSpecificBackend(TestParallelBackendBase):
    """
    This is quite contrived, for each test in the TestParallelBackend tests it
    generates a test that will run the TestParallelBackend test in a new python
    process with an environment modified to ensure a specific threadsafe backend
    is used. This is with view of testing the backends independently and in an
    isolated manner such that if they hang/crash/have issues, it doesn't kill
    the test suite.
    """
    _DEBUG = False

    backends = {
        'tbb': skip_no_tbb,
        'omp': skip_no_omp,
        'workqueue': unittest.skipIf(False, '')
    }

    def run_cmd(self, cmdline, env):
        popen = subprocess.Popen(cmdline,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 env=env)
        # finish in 5 minutes or kill it
        timeout = threading.Timer(5 * 60., popen.kill)
        try:
            timeout.start()
            out, err = popen.communicate()
            if popen.returncode != 0:
                raise AssertionError(
                    "process failed with code %s: stderr follows\n%s\n" %
                    (popen.returncode, err.decode()))
            return out.decode(), err.decode()
        finally:
            timeout.cancel()
        return None, None

    def run_test_in_separate_process(self, test, threading_layer):
        env_copy = os.environ.copy()
        env_copy['NUMBA_THREADING_LAYER'] = str(threading_layer)
        cmdline = [sys.executable, "-m", "numba.runtests", test]
        return self.run_cmd(cmdline, env_copy)

    @classmethod
    def _inject(cls, p, name, backend, backend_guard):
        themod = cls.__module__
        thecls = TestParallelBackend.__name__
        methname = "test_" + p + '_' + name
        injected_method = '%s.%s.%s' % (themod, thecls, methname)

        def test_template(self):
            o, e = self.run_test_in_separate_process(injected_method, backend)
            if self._DEBUG:
                print('stdout:\n "%s"\n stderr:\n "%s"' % (o, e))
            self.assertIn('OK', e)
            self.assertTrue('FAIL' not in e)
            self.assertTrue('ERROR' not in e)

        injected_test = "test_%s_%s_%s" % (p, name, backend)
        # Mark as important for appveyor
        setattr(cls, injected_test,
                tag("important")(backend_guard(test_template)))

    @classmethod
    def generate(cls):
        for backend, backend_guard in cls.backends.items():
            for p in cls.parallelism:
                for name in cls.runners.keys():
                    # handle known problem cases...

                    # GNU OpenMP is not fork safe
                    if (p in ('multiprocessing_fork', 'random')
                            and backend == 'omp'
                            and sys.platform.startswith('linux')):
                        continue

                    # workqueue is not thread safe
                    if (p in ('threading', 'random')
                            and backend == 'workqueue'):
                        continue

                    cls._inject(p, name, backend, backend_guard)
Beispiel #30
0
try:
    import setuptools
except ImportError:
    setuptools = None

import llvmlite.binding as ll

from numba import unittest_support as unittest
from numba.pycc import main
from numba.pycc.decorators import clear_export_registry
from numba.pycc.platform import find_shared_ending, find_pyext_ending
from numba.pycc.platform import _external_compiler_ok

# if suitable compilers are not present then skip.
_skip_reason = 'AOT compatible compilers missing'
_skip_missing_compilers = unittest.skipIf(not _external_compiler_ok,
                                          _skip_reason)

from .matmul_usecase import has_blas
from .support import TestCase, tag, import_dynamic, temp_directory


base_path = os.path.dirname(os.path.abspath(__file__))


def unset_macosx_deployment_target():
    """Unset MACOSX_DEPLOYMENT_TARGET because we are not building portable
    libraries
    """
    if 'MACOSX_DEPLOYMENT_TARGET' in os.environ:
        del os.environ['MACOSX_DEPLOYMENT_TARGET']
from numba import unittest_support as unittest
from numba import njit, typeof, types, typing, typeof, ir, utils, bytecode
from .support import TestCase, tag
from numba.array_analysis import EquivSet, ArrayAnalysis
from numba.compiler import Pipeline, Flags, _PipelineManager
from numba.targets import cpu, registry
from numba.numpy_support import version as numpy_version
from numba.ir_utils import remove_dead

# for parallel tests, marking that Windows with Python 2.7 is not supported
_windows_py27 = (sys.platform.startswith('win32') and
                 sys.version_info[:2] == (2, 7))
_32bit = sys.maxsize <= 2 ** 32
_reason = 'parfors not supported'
skip_unsupported = unittest.skipIf(_32bit or _windows_py27, _reason)

class TestEquivSet(TestCase):

    """
    Test array_analysis.EquivSet.
    """
    @tag('important')
    def test_insert_equiv(self):
        s1 = EquivSet()
        s1.insert_equiv('a', 'b')
        self.assertTrue(s1.is_equiv('a', 'b'))
        self.assertTrue(s1.is_equiv('b', 'a'))
        s1.insert_equiv('c', 'd')
        self.assertTrue(s1.is_equiv('c', 'd'))
        self.assertFalse(s1.is_equiv('c', 'a'))
Beispiel #32
0
from numba import njit, targets, typing
from numba.compiler import compile_isolated, Flags, types
from numba.runtime import rtsys
from numba.runtime import nrtopt
from numba.extending import intrinsic
from numba.typing import signature
from numba.targets.imputils import impl_ret_untracked
from llvmlite import ir
import llvmlite.binding as llvm

from .support import MemoryLeakMixin, TestCase

enable_nrt_flags = Flags()
enable_nrt_flags.set("nrt")

linux_only = unittest.skipIf(not sys.platform.startswith('linux'),
                             'linux only test')
x86_only = unittest.skipIf(platform.machine() not in ('i386', 'x86_64'),
                           'x86 only test')


class Dummy(object):
    alive = 0

    def __init__(self):
        type(self).alive += 1

    def __del__(self):
        type(self).alive -= 1


class TestNrtMemInfoNotInitialized(unittest.TestCase):