Пример #1
0
    def setUp(self):
        unset_macosx_deployment_target()

        self.tmpdir = temp_directory('test_pycc')
        # Make sure temporary files and directories created by
        # distutils don't clutter the top-level /tmp
        tempfile.tempdir = self.tmpdir
Пример #2
0
    def test_inspect_cli(self):
        # Try CLI on math module
        cmdbase = [sys.executable, "-m", "numba.misc.help.inspector"]

        dirpath = temp_directory("{}.{}".format(__name__, self.__class__.__name__))
        filename = os.path.join(dirpath, "out")

        # Try default format "html"
        expected_file = filename + ".html"
        cmds = cmdbase + ["--file", filename, "math"]
        # File shouldn't exist yet
        self.assertFalse(os.path.isfile(expected_file))
        # Run CLI
        subprocess.check_output(cmds)
        # File should exist now
        self.assertTrue(os.path.isfile(expected_file))

        # Try changing the format to "rst"
        cmds = cmdbase + ["--file", filename, "--format", "rst", "math"]
        expected_file = filename + ".rst"
        # File shouldn't exist yet
        self.assertFalse(os.path.isfile(expected_file))
        # Run CLI
        subprocess.check_output(cmds)
        # File should exist now
        self.assertTrue(os.path.isfile(expected_file))

        # Try unsupported format
        cmds = cmdbase + ["--file", filename, "--format", "foo", "math"]
        # Run CLI
        with self.assertRaises(subprocess.CalledProcessError) as raises:
            subprocess.check_output(cmds, stderr=subprocess.STDOUT)
        self.assertIn("'foo' is not supported", raises.exception.stdout.decode("ascii"))
Пример #3
0
    def run_compile(self, fnlist, parallelism='threading'):
        self._cache_dir = temp_directory(self.__class__.__name__)
        with override_config('CACHE_DIR', self._cache_dir):
            if parallelism == 'threading':
                thread_impl(fnlist)
            elif parallelism == 'multiprocessing_fork':
                fork_proc_impl(fnlist)
            elif parallelism == 'multiprocessing_forkserver':
                forkserver_proc_impl(fnlist)
            elif parallelism == 'multiprocessing_spawn':
                spawn_proc_impl(fnlist)
            elif parallelism == 'multiprocessing_default':
                default_proc_impl(fnlist)
            elif parallelism == 'random':
                ps = [thread_impl, spawn_proc_impl]
                if _HAVE_OS_FORK:
                    ps.append(fork_proc_impl)
                    ps.append(forkserver_proc_impl)

                random.shuffle(ps)
                for impl in ps:
                    impl(fnlist)
            else:
                raise ValueError(
                    'Unknown parallelism supplied %s' % parallelism)
Пример #4
0
 def setUp(self):
     self.tempdir = temp_directory('test_cache')
     sys.path.insert(0, self.tempdir)
     self.modfile = os.path.join(self.tempdir, self.modname + ".py")
     self.cache_dir = os.path.join(self.tempdir, "__pycache__")
     shutil.copy(self.usecases_file, self.modfile)
     self.maxDiff = None
Пример #5
0
def check_access_is_preventable():
    # This exists to check whether it is possible to prevent access to
    # a file/directory through the use of `chmod 500`. If a user has
    # elevated rights (e.g. root) then writes are likely to be possible
    # anyway. Tests that require functioning access prevention are
    # therefore skipped based on the result of this check.
    tempdir = temp_directory('test_cache')
    test_dir = (os.path.join(tempdir, 'writable_test'))
    os.mkdir(test_dir)
    # check a write is possible
    with open(os.path.join(test_dir, 'write_ok'), 'wt') as f:
        f.write('check1')
    # now forbid access
    os.chmod(test_dir, 0o500)
    try:
        with open(os.path.join(test_dir, 'write_forbidden'), 'wt') as f:
            f.write('check2')
        # access prevention is not possible
        return False
    except PermissionError:
        # Check that the cause of the exception is due to access/permission
        # as per
        # https://github.com/conda/conda/blob/4.5.0/conda/gateways/disk/permissions.py#L35-L37  # noqa: E501
        # errno reports access/perm fail so access prevention via
        # `chmod 500` works for this user.
        return True
    finally:
        os.chmod(test_dir, 0o775)
        shutil.rmtree(test_dir)
Пример #6
0
    def test_omitted(self):

        # Test in a new directory
        cache_dir = temp_directory(self.__class__.__name__)
        ctx = multiprocessing.get_context()
        result_queue = ctx.Queue()
        proc = ctx.Process(
            target=omitted_child_test_wrapper,
            args=(result_queue, cache_dir, False),
        )
        proc.start()
        proc.join()
        success, output = result_queue.get()

        # Ensure the child process is completed before checking its output
        if not success:
            self.fail(output)

        self.assertEqual(output, 1000,
                         "Omitted function returned an incorrect output")

        proc = ctx.Process(target=omitted_child_test_wrapper,
                           args=(result_queue, cache_dir, True))
        proc.start()
        proc.join()
        success, output = result_queue.get()

        # Ensure the child process is completed before checking its output
        if not success:
            self.fail(output)

        self.assertEqual(output, 1000,
                         "Omitted function returned an incorrect output")
Пример #7
0
    def test_inspect_cli(self):
        # Try CLI on math module
        cmdbase = [sys.executable, '-m', 'numba.misc.help.inspector']

        dirpath = temp_directory('{}.{}'.format(__name__,
                                                self.__class__.__name__))
        filename = os.path.join(dirpath, 'out')

        # Try default format "html"
        expected_file = filename + '.html'
        cmds = cmdbase + ['--file', filename, 'math']
        # File shouldn't exist yet
        self.assertFalse(os.path.isfile(expected_file))
        # Run CLI
        subprocess.check_output(cmds)
        # File should exist now
        self.assertTrue(os.path.isfile(expected_file))

        # Try changing the format to "rst"
        cmds = cmdbase + ['--file', filename, '--format', 'rst', 'math']
        expected_file = filename + '.rst'
        # File shouldn't exist yet
        self.assertFalse(os.path.isfile(expected_file))
        # Run CLI
        subprocess.check_output(cmds)
        # File should exist now
        self.assertTrue(os.path.isfile(expected_file))

        # Try unsupported format
        cmds = cmdbase + ['--file', filename, '--format', 'foo', 'math']
        # Run CLI
        with self.assertRaises(subprocess.CalledProcessError) as raises:
            subprocess.check_output(cmds, stderr=subprocess.STDOUT)
        self.assertIn("\'foo\' is not supported",
                      raises.exception.stdout.decode('ascii'))
Пример #8
0
    def setUp(self):
        unset_macosx_deployment_target()

        # Copy the test project into a temp directory to avoid
        # keeping any build leftovers in the source tree
        self.tmpdir = temp_directory('test_pycc_distutils')
        source_dir = os.path.join(base_path, 'pycc_distutils_usecase')
        self.usecase_dir = os.path.join(self.tmpdir, 'work')
        shutil.copytree(source_dir, self.usecase_dir)
Пример #9
0
    def setUp(self):
        self.tempdir = temp_directory('test_cache_file_loc')

        self.file1 = os.path.join(self.tempdir, 'file1.py')
        with open(self.file1, 'w') as fout:
            print(self.source_text_file1, file=fout)

        self.file2 = os.path.join(self.tempdir, 'file2.py')
        with open(self.file2, 'w') as fout:
            print(self.source_text_file2, file=fout)
Пример #10
0
 def setUp(self):
     self.tempdir = temp_directory('test_cache_file_loc')
     sys.path.insert(0, self.tempdir)
     self.modname = 'module_name_that_is_unlikely'
     self.assertNotIn(self.modname, sys.modules)
     self.modname_bar1 = self.modname
     self.modname_bar2 = '.'.join([self.modname, 'foo'])
     foomod = os.path.join(self.tempdir, self.modname)
     os.mkdir(foomod)
     with open(os.path.join(foomod, '__init__.py'), 'w') as fout:
         print(self.source_text_1, file=fout)
     with open(os.path.join(foomod, 'foo.py'), 'w') as fout:
         print(self.source_text_2, file=fout)
Пример #11
0
    def run_compile(self, fnlist):
        self._cache_dir = temp_directory(self.__class__.__name__)
        with override_config('CACHE_DIR', self._cache_dir):

            def chooser():
                for _ in range(10):
                    fn = random.choice(fnlist)
                    fn()

            ths = [threading.Thread(target=chooser) for i in range(4)]
            for th in ths:
                th.start()
            for th in ths:
                th.join()
Пример #12
0
    def compile_cffi_module(self, name, source, cdef):
        from cffi import FFI

        ffi = FFI()
        ffi.set_source(name, source, include_dirs=[include_path()])
        ffi.cdef(cdef)
        tmpdir = temp_directory("cffi_test_{}".format(name))
        ffi.compile(tmpdir=tmpdir)
        sys.path.append(tmpdir)
        try:
            mod = import_dynamic(name)
        finally:
            sys.path.remove(tmpdir)

        return ffi, mod
Пример #13
0
    def test_caching_overload_objmode(self):
        cache_dir = temp_directory(self.__class__.__name__)
        with override_config("CACHE_DIR", cache_dir):

            def realwork(x):
                # uses numpy code
                arr = np.arange(x) / x
                return np.linalg.norm(arr)

            def python_code(x):
                # create indirections
                return realwork(x)

            @overload(with_objmode_cache_ov_example)
            def _ov_with_objmode_cache_ov_example(x):
                def impl(x):
                    with objmode(y="float64"):
                        y = python_code(x)
                    return y

                return impl

            @njit(cache=True)
            def testcase(x):
                return with_objmode_cache_ov_example(x)

            expect = realwork(123)
            got = testcase(123)
            self.assertEqual(got, expect)

            testcase_cached = njit(cache=True)(testcase.py_func)
            got = testcase_cached(123)
            self.assertEqual(got, expect)

            self.assertEqual(
                testcase_cached._cache_hits[testcase.signatures[0]],
                1,
            )
            self.assertEqual(
                testcase_cached._cache_misses[testcase.signatures[0]],
                0,
            )
Пример #14
0
    def create_temp_module(self, source_lines=None, **jit_options):
        # Use try/finally so cleanup happens even when an exception is raised
        try:
            if source_lines is None:
                source_lines = self.source_lines
            tempdir = temp_directory('test_extension_type')
            # Generate random module name
            temp_module_name = 'test_extension_type_{}'.format(
                str(uuid.uuid4()).replace('-', '_'))
            temp_module_path = os.path.join(tempdir, temp_module_name + '.py')

            with open(temp_module_path, 'w') as f:
                lines = source_lines.format(jit_options=jit_options)
                f.write(lines)
            # Add test_module to sys.path so it can be imported
            sys.path.insert(0, tempdir)
            test_module = importlib.import_module(temp_module_name)
            yield test_module
        finally:
            sys.modules.pop(temp_module_name, None)
            sys.path.remove(tempdir)
            shutil.rmtree(tempdir)
    def run_compile(self, fnlist, parallelism="threading"):
        self._cache_dir = temp_directory(self.__class__.__name__)
        with override_config("CACHE_DIR", self._cache_dir):
            if parallelism == "threading":
                thread_impl(fnlist)
            elif parallelism == "multiprocessing_fork":
                fork_proc_impl(fnlist)
            elif parallelism == "multiprocessing_forkserver":
                forkserver_proc_impl(fnlist)
            elif parallelism == "multiprocessing_spawn":
                spawn_proc_impl(fnlist)
            elif parallelism == "multiprocessing_default":
                default_proc_impl(fnlist)
            elif parallelism == "random":
                ps = [thread_impl, spawn_proc_impl]
                if _HAVE_OS_FORK:
                    ps.append(fork_proc_impl)
                    ps.append(forkserver_proc_impl)

                random.shuffle(ps)
                for impl in ps:
                    impl(fnlist)
            else:
                raise ValueError("Unknown parallelism supplied %s" % parallelism)
Пример #16
0
 def setUp(self):
     # use support.temp_directory, it can do the clean up
     self.tmppath = temp_directory('config_tmp')
     super(TestConfig, self).setUp()
Пример #17
0
def load_ool_module():
    """
    Compile an out-of-line module, return the corresponding ffi and
    module objects.
    """
    from cffi import FFI

    numba_complex = """
    typedef struct _numba_complex {
        double real;
        double imag;
    } numba_complex;
    """

    bool_define = """
    #ifdef _MSC_VER
        #define false 0
        #define true 1
        #define bool int
    #else
        #include <stdbool.h>
    #endif
    """

    defs = (numba_complex + """
    bool boolean(void);
    double sin(double x);
    double cos(double x);
    int foo(int a, int b, int c);
    void vsSin(int n, float* x, float* y);
    void vdSin(int n, double* x, double* y);
    void vector_real(numba_complex *c, double *real, int n);
    void vector_imag(numba_complex *c, double *imag, int n);
    """)

    source = (numba_complex + bool_define + """
    static bool boolean(void)
    {
        return true;
    }

    static int foo(int a, int b, int c)
    {
        return a + b * c;
    }

    void vsSin(int n, float* x, float* y) {
        int i;
        for (i=0; i<n; i++)
            y[i] = sin(x[i]);
    }

    void vdSin(int n, double* x, double* y) {
        int i;
        for (i=0; i<n; i++)
            y[i] = sin(x[i]);
    }

    static void vector_real(numba_complex *c, double *real, int n) {
        int i;
        for (i = 0; i < n; i++)
            real[i] = c[i].real;
    }

    static void vector_imag(numba_complex *c, double *imag, int n) {
        int i;
        for (i = 0; i < n; i++)
            imag[i] = c[i].imag;
    }
    """)

    ffi = FFI()
    ffi.set_source("cffi_usecases_ool", source)
    ffi.cdef(defs, override=True)
    tmpdir = temp_directory("test_cffi")
    ffi.compile(tmpdir=tmpdir)
    sys.path.append(tmpdir)
    try:
        mod = import_dynamic("cffi_usecases_ool")
        cffi_support.register_module(mod)
        cffi_support.register_type(mod.ffi.typeof("struct _numba_complex"),
                                   complex128)
        return mod.ffi, mod
    finally:
        sys.path.remove(tmpdir)
Пример #18
0
 def test_caching_overload_method(self):
     self._cache_dir = temp_directory(self.__class__.__name__)
     with override_config('CACHE_DIR', self._cache_dir):
         self.run_caching_overload_method()
Пример #19
0
 def setUp(self):
     self._cache_dir = temp_directory(TestStructRefCaching.__name__)
     self._cache_override = override_config('CACHE_DIR', self._cache_dir)
     self._cache_override.__enter__()
     warnings.simplefilter("error")
Пример #20
0
def load_ool_module():
    """
    Compile an out-of-line module, return the corresponding ffi and
    module objects.
    """
    from cffi import FFI

    numba_complex = """
    typedef struct _numba_complex {
        double real;
        double imag;
    } numba_complex;
    """

    defs = numba_complex + """
    double sin(double x);
    double cos(double x);
    int foo(int a, int b, int c);
    void vsSin(int n, float* x, float* y);
    void vdSin(int n, double* x, double* y);
    void vector_real(numba_complex *c, double *real, int n);
    void vector_imag(numba_complex *c, double *imag, int n);
    """

    source = numba_complex + """
    static int foo(int a, int b, int c)
    {
        return a + b * c;
    }

    void vsSin(int n, float* x, float* y) {
        int i;
        for (i=0; i<n; i++)
            y[i] = sin(x[i]);
    }

    void vdSin(int n, double* x, double* y) {
        int i;
        for (i=0; i<n; i++)
            y[i] = sin(x[i]);
    }

    static void vector_real(numba_complex *c, double *real, int n) {
        int i;
        for (i = 0; i < n; i++)
            real[i] = c[i].real;
    }

    static void vector_imag(numba_complex *c, double *imag, int n) {
        int i;
        for (i = 0; i < n; i++)
            imag[i] = c[i].imag;
    }
    """

    ffi = FFI()
    ffi.set_source('cffi_usecases_ool', source)
    ffi.cdef(defs, override=True)
    tmpdir = temp_directory('test_cffi')
    ffi.compile(tmpdir=tmpdir)
    sys.path.append(tmpdir)
    try:
        mod = import_dynamic('cffi_usecases_ool')
        cffi_support.register_module(mod)
        cffi_support.register_type(mod.ffi.typeof('struct _numba_complex'),
                                   complex128)
        return mod.ffi, mod
    finally:
        sys.path.remove(tmpdir)
Пример #21
0
 def setUp(self):
     self._cache_dir = temp_directory(TestStructRefCaching.__name__)
     self._cache_override = override_config('CACHE_DIR', self._cache_dir)
     self._cache_override.__enter__()
     warnings.simplefilter("error")
     warnings.filterwarnings(action="ignore", module="typeguard")