示例#1
0
 def __init__(self, shell):
     super(FortranMagics, self).__init__(shell=shell)
     self._reloads = {}
     self._code_cache = {}
     self._lib_dir = os.path.join(get_ipython_cache_dir(), 'fortran')
     if not os.path.exists(self._lib_dir):
         os.makedirs(self._lib_dir)
示例#2
0
def get_ipython_cache_dir():
    warn(
        "get_ipython_cache_dir has moved to the IPython.paths module since IPython 4.0.",
        DeprecationWarning,
        stacklevel=2)
    from IPython.paths import get_ipython_cache_dir
    return get_ipython_cache_dir()
示例#3
0
文件: magic.py 项目: phdum/cpp2py
 def __init__(self, shell):
     super(Cpp2pyMagics, self).__init__(shell=shell)
     self._reloads = {}
     self._code_cache = {}
     self._lib_dir = os.path.join(get_ipython_cache_dir(), 'cpp2py')
     if not os.path.exists(self._lib_dir):
         os.makedirs(self._lib_dir)
     display_javascript(js, raw=True)
示例#4
0
def test_get_ipython_cache_dir():
    with modified_env({'HOME': HOME_TEST_DIR}):
        if os.name == "posix":
            # test default
            os.makedirs(os.path.join(HOME_TEST_DIR, ".cache"))
            with modified_env({'XDG_CACHE_HOME': None}):
                ipdir = paths.get_ipython_cache_dir()
            assert os.path.join(HOME_TEST_DIR, ".cache", "ipython") == ipdir
            assert_isdir(ipdir)

            # test env override
            with modified_env({"XDG_CACHE_HOME": XDG_CACHE_DIR}):
                ipdir = paths.get_ipython_cache_dir()
            assert_isdir(ipdir)
            assert ipdir == os.path.join(XDG_CACHE_DIR, "ipython")
        else:
            assert paths.get_ipython_cache_dir() == paths.get_ipython_dir()
示例#5
0
def test_get_ipython_cache_dir():
    with modified_env({'HOME': HOME_TEST_DIR}):
        if os.name == 'posix' and sys.platform != 'darwin':
            # test default
            os.makedirs(os.path.join(HOME_TEST_DIR, ".cache"))
            with modified_env({'XDG_CACHE_HOME': None}):
                ipdir = paths.get_ipython_cache_dir()
            nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"),
                            ipdir)
            assert_isdir(ipdir)

            # test env override
            with modified_env({"XDG_CACHE_HOME": XDG_CACHE_DIR}):
                ipdir = paths.get_ipython_cache_dir()
            assert_isdir(ipdir)
            nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython"))
        else:
            nt.assert_equal(paths.get_ipython_cache_dir(),
                            paths.get_ipython_dir())
示例#6
0
def test_get_ipython_cache_dir():
    with modified_env({'HOME': HOME_TEST_DIR}):
        if os.name == 'posix' and sys.platform != 'darwin':
            # test default
            os.makedirs(os.path.join(HOME_TEST_DIR, ".cache"))
            with modified_env({'XDG_CACHE_HOME': None}):
                ipdir = paths.get_ipython_cache_dir()
            nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"),
                            ipdir)
            assert_isdir(ipdir)

            # test env override
            with modified_env({"XDG_CACHE_HOME": XDG_CACHE_DIR}):
                ipdir = paths.get_ipython_cache_dir()
            assert_isdir(ipdir)
            nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython"))
        else:
            nt.assert_equal(paths.get_ipython_cache_dir(),
                            paths.get_ipython_dir())
示例#7
0
def test_get_ipython_cache_dir():
    os.environ["HOME"] = HOME_TEST_DIR
    if os.name == 'posix' and sys.platform != 'darwin':
        # test default
        os.makedirs(os.path.join(HOME_TEST_DIR, ".cache"))
        os.environ.pop("XDG_CACHE_HOME", None)
        ipdir = paths.get_ipython_cache_dir()
        nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"),
                        ipdir)
        nt.assert_true(os.path.isdir(ipdir))

        # test env override
        os.environ["XDG_CACHE_HOME"] = XDG_CACHE_DIR
        ipdir = paths.get_ipython_cache_dir()
        nt.assert_true(os.path.isdir(ipdir))
        nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython"))
    else:
        nt.assert_equal(paths.get_ipython_cache_dir(),
                        paths.get_ipython_dir())
示例#8
0
 def get_cache_path(self):
     # We could also use jupyter_core.paths.jupyter_runtime_dir()
     # In both cases this is a user-wide directory, so we need to
     # be careful when disambiguating if we don't want too many
     # conflicts (see below).
     try:
         from IPython.paths import get_ipython_cache_dir
     except ImportError:
         # older IPython version
         from IPython.utils.path import get_ipython_cache_dir
     return os.path.join(get_ipython_cache_dir(), 'numba_cache')
示例#9
0
文件: caching.py 项目: cpcloud/numba
 def get_cache_path(self):
     # We could also use jupyter_core.paths.jupyter_runtime_dir()
     # In both cases this is a user-wide directory, so we need to
     # be careful when disambiguating if we don't want too many
     # conflicts (see below).
     try:
         from IPython.paths import get_ipython_cache_dir
     except ImportError:
         # older IPython version
         from IPython.utils.path import get_ipython_cache_dir
     return os.path.join(get_ipython_cache_dir(), 'numba')
示例#10
0
 def chpl(self, line, cell):
     code = cell if cell.endswith('\n') else cell + '\n'
     lib_dir = os.path.join(get_ipython_cache_dir(), 'chpl')
     key = (code, line, sys.version_info, sys.executable, time.time())
     if not os.path.exists(lib_dir):
         os.makedirs(lib_dir)
     module_name = "_chpl_magic_" + hashlib.md5(
         str(key).encode('utf-8')).hexdigest()
     src_path = os.path.join(lib_dir, module_name + '.chpl')
     cmd = [
         'chpl', '--library', '--library-python',
         f'--library-dir={lib_dir}',
         f'--library-python-name={module_name+".chpl"}'
     ]
     cmd.extend(line.split())
     # pyname = os.path.join(tdir.name, 'mycode')
     with open(src_path, 'w') as f:
         f.write(code)
     cmd.append(src_path)
     call = Popen(cmd, stdout=PIPE, stderr=PIPE)
     out, err = call.communicate()
     print(out.decode())
     print(err.decode())
     self._load_module(module_name, lib_dir)
示例#11
0
def cache_dir():
    """Root cache directory for pybind11 extension."""
    root = os.path.abspath(os.path.expanduser(get_ipython_cache_dir()))
    return os.path.join(root, 'pybind11')
示例#12
0
    def cython(self, line, cell):
        """Compile and import everything from a Cython code cell.

        The contents of the cell are written to a `.pyx` file in the
        directory `IPYTHONDIR/cython` using a filename with the hash of the
        code. This file is then cythonized and compiled. The resulting module
        is imported and all of its symbols are injected into the user's
        namespace. The usage is similar to that of `%%cython_pyximport` but
        you don't have to pass a module name::

            %%cython
            def f(x):
                return 2.0*x

        To compile OpenMP codes, pass the required  `--compile-args`
        and `--link-args`.  For example with gcc::

            %%cython --compile-args=-fopenmp --link-args=-fopenmp
            ...

        To enable profile guided optimisation, pass the ``--pgo`` option.
        Note that the cell itself needs to take care of establishing a suitable
        profile when executed. This can be done by implementing the functions to
        optimise, and then calling them directly in the same cell on some realistic
        training data like this::

            %%cython --pgo
            def critical_function(data):
                for item in data:
                    ...

            # execute function several times to build profile
            from somewhere import some_typical_data
            for _ in range(100):
                critical_function(some_typical_data)

        In Python 3.5 and later, you can distinguish between the profile and
        non-profile runs as follows::

            if "_pgo_" in __name__:
                ...  # execute critical code here
        """
        args = magic_arguments.parse_argstring(self.cython, line)
        code = cell if cell.endswith('\n') else cell + '\n'
        lib_dir = os.path.join(get_ipython_cache_dir(), 'cython')
        key = (code, line, sys.version_info, sys.executable, cython_version)

        if not os.path.exists(lib_dir):
            os.makedirs(lib_dir)

        if args.pgo:
            key += ('pgo',)
        if args.force:
            # Force a new module name by adding the current time to the
            # key which is hashed to determine the module name.
            key += (time.time(),)

        if args.name:
            module_name = py3compat.unicode_to_str(args.name)
        else:
            module_name = "_cython_magic_" + hashlib.sha1(str(key).encode('utf-8')).hexdigest()
        html_file = os.path.join(lib_dir, module_name + '.html')
        module_path = os.path.join(lib_dir, module_name + self.so_ext)

        have_module = os.path.isfile(module_path)
        need_cythonize = args.pgo or not have_module

        if args.annotate:
            if not os.path.isfile(html_file):
                need_cythonize = True

        extension = None
        if need_cythonize:
            extensions = self._cythonize(module_name, code, lib_dir, args, quiet=args.quiet)
            assert len(extensions) == 1
            extension = extensions[0]
            self._code_cache[key] = module_name

            if args.pgo:
                self._profile_pgo_wrapper(extension, lib_dir)

        self._build_extension(extension, lib_dir, pgo_step_name='use' if args.pgo else None,
                              quiet=args.quiet)

        module = imp.load_dynamic(module_name, module_path)
        self._import_all(module)

        if args.annotate:
            try:
                with io.open(html_file, encoding='utf-8') as f:
                    annotated_html = f.read()
            except IOError as e:
                # File could not be opened. Most likely the user has a version
                # of Cython before 0.15.1 (when `cythonize` learned the
                # `force` keyword argument) and has already compiled this
                # exact source without annotation.
                print('Cython completed successfully but the annotated '
                      'source could not be read.', file=sys.stderr)
                print(e, file=sys.stderr)
            else:
                return display.HTML(self.clean_annotated_html(annotated_html))
示例#13
0
    def mypyc(self, line, cell):
        """Compile and import everything from a mypyc code cell.

        The contents of the cell are written to a `.py` file in the
        directory `IPYTHONDIR/mypyc` using a filename with the hash of the
        code. This file is then mypycified and compiled. The resulting module
        is imported and all of its symbols are injected into the user's
        namespace.

            %%mypyc
            def mypyc_fib(n: int) -> float:
                i: int
                a: float = 0.0
                b: float = 1.0
                for i in range(n):
                    a, b = a + b, a
                return a
        """
        args = magic_arguments.parse_argstring(self.mypyc, line)
        code = cell if cell.endswith("\n") else cell + "\n"
        lib_dir = os.path.join(get_ipython_cache_dir(), "mypyc")
        key = (code, line, sys.version_info, sys.executable, mypy_version)

        if not os.path.exists(lib_dir):
            os.makedirs(lib_dir)

        if args.force:
            # Force a new module name by adding the current time to the
            # key which is hashed to determine the module name.
            key += (time.time(),)

        module_name = (
            "_mypyc_magic_" + hashlib.sha1(str(key).encode("utf-8")).hexdigest()
        )
        module_path = os.path.join(lib_dir, module_name + self.so_ext)
        need_mypycify = not os.path.isfile(module_path)

        extension = None
        if need_mypycify:
            extensions = self._mypycify(
                module_name, code, lib_dir, args, quiet=args.quiet
            )
            if extensions is None:
                # Compilation failed and printed error message
                return None
            assert len(extensions) == 1
            extension = extensions[0]
            self._code_cache[key] = module_name

        try:
            self._build_extension(extension, lib_dir, quiet=args.quiet)
        except distutils.errors.CompileError:
            # Build failed and printed error message
            return None

        loader = importlib.machinery.ExtensionFileLoader(module_name, module_path)
        spec = importlib.machinery.ModuleSpec(
            name=module_name, loader=loader, origin=module_path
        )
        module = importlib._bootstrap._load(spec)
        self._import_all(module)
示例#14
0
    def cython(self, line, cell):
        """Compile and import everything from a Cython code cell.

        The contents of the cell are written to a `.pyx` file in the
        directory `IPYTHONDIR/cython` using a filename with the hash of the
        code. This file is then cythonized and compiled. The resulting module
        is imported and all of its symbols are injected into the user's
        namespace. The usage is similar to that of `%%cython_pyximport` but
        you don't have to pass a module name::

            %%cython
            def f(x):
                return 2.0*x

        To compile OpenMP codes, pass the required  `--compile-args`
        and `--link-args`.  For example with gcc::

            %%cython --compile-args=-fopenmp --link-args=-fopenmp
            ...
        """
        args = magic_arguments.parse_argstring(self.cython, line)
        code = cell if cell.endswith("\n") else cell + "\n"
        lib_dir = os.path.join(get_ipython_cache_dir(), "cython")
        quiet = True
        key = code, line, sys.version_info, sys.executable, cython_version

        if not os.path.exists(lib_dir):
            os.makedirs(lib_dir)

        if args.force:
            # Force a new module name by adding the current time to the
            # key which is hashed to determine the module name.
            key += (time.time(),)

        if args.name:
            module_name = py3compat.unicode_to_str(args.name)
        else:
            module_name = "_cython_magic_" + hashlib.md5(str(key).encode("utf-8")).hexdigest()
        module_path = os.path.join(lib_dir, module_name + self.so_ext)

        have_module = os.path.isfile(module_path)
        need_cythonize = not have_module

        if args.annotate:
            html_file = os.path.join(lib_dir, module_name + ".html")
            if not os.path.isfile(html_file):
                need_cythonize = True

        if need_cythonize:
            c_include_dirs = args.include
            if "numpy" in code:
                import numpy

                c_include_dirs.append(numpy.get_include())
            pyx_file = os.path.join(lib_dir, module_name + ".pyx")
            pyx_file = py3compat.cast_bytes_py2(pyx_file, encoding=sys.getfilesystemencoding())
            with io.open(pyx_file, "w", encoding="utf-8") as f:
                f.write(code)
            extension = Extension(
                name=module_name,
                sources=[pyx_file],
                include_dirs=c_include_dirs,
                library_dirs=args.library_dirs,
                extra_compile_args=args.compile_args,
                extra_link_args=args.link_args,
                libraries=args.lib,
                language="c++" if args.cplus else "c",
            )
            build_extension = self._get_build_extension()
            try:
                opts = dict(quiet=quiet, annotate=args.annotate, force=True)
                if args.language_level is not None:
                    assert args.language_level in (2, 3)
                    opts["language_level"] = args.language_level
                elif sys.version_info[0] > 2:
                    opts["language_level"] = 3
                build_extension.extensions = cythonize([extension], **opts)
            except CompileError:
                return

        if not have_module:
            build_extension.build_temp = os.path.dirname(pyx_file)
            build_extension.build_lib = lib_dir
            build_extension.run()
            self._code_cache[key] = module_name

        module = imp.load_dynamic(module_name, module_path)
        self._import_all(module)

        if args.annotate:
            try:
                with io.open(html_file, encoding="utf-8") as f:
                    annotated_html = f.read()
            except IOError as e:
                # File could not be opened. Most likely the user has a version
                # of Cython before 0.15.1 (when `cythonize` learned the
                # `force` keyword argument) and has already compiled this
                # exact source without annotation.
                print("Cython completed successfully but the annotated " "source could not be read.", file=sys.stderr)
                print(e, file=sys.stderr)
            else:
                return display.HTML(self.clean_annotated_html(annotated_html))
示例#15
0
    def cython(self, line, cell):
        """Compile and import everything from a Cython code cell.

        The contents of the cell are written to a `.pyx` file in the
        directory `IPYTHONDIR/cython` using a filename with the hash of the
        code. This file is then cythonized and compiled. The resulting module
        is imported and all of its symbols are injected into the user's
        namespace. The usage is similar to that of `%%cython_pyximport` but
        you don't have to pass a module name::

            %%cython
            def f(x):
                return 2.0*x

        To compile OpenMP codes, pass the required  `--compile-args`
        and `--link-args`.  For example with gcc::

            %%cython --compile-args=-fopenmp --link-args=-fopenmp
            ...
        """
        args = magic_arguments.parse_argstring(self.cython, line)
        code = cell if cell.endswith('\n') else cell + '\n'
        lib_dir = os.path.join(get_ipython_cache_dir(), 'cython')
        quiet = True
        key = code, line, sys.version_info, sys.executable, cython_version

        if not os.path.exists(lib_dir):
            os.makedirs(lib_dir)

        if args.force:
            # Force a new module name by adding the current time to the
            # key which is hashed to determine the module name.
            key += time.time(),

        if args.name:
            module_name = py3compat.unicode_to_str(args.name)
        else:
            module_name = "_cython_magic_" + hashlib.md5(
                str(key).encode('utf-8')).hexdigest()
        module_path = os.path.join(lib_dir, module_name + self.so_ext)

        have_module = os.path.isfile(module_path)
        need_cythonize = not have_module

        if args.annotate:
            html_file = os.path.join(lib_dir, module_name + '.html')
            if not os.path.isfile(html_file):
                need_cythonize = True

        if need_cythonize:
            c_include_dirs = args.include
            c_src_files = map(str, args.src)
            if 'numpy' in code:
                import numpy
                c_include_dirs.append(numpy.get_include())
            pyx_file = os.path.join(lib_dir, module_name + '.pyx')
            pyx_file = py3compat.cast_bytes_py2(
                pyx_file, encoding=sys.getfilesystemencoding())
            with io.open(pyx_file, 'w', encoding='utf-8') as f:
                f.write(code)
            extension = Extension(
                name=module_name,
                sources=[pyx_file] + c_src_files,
                include_dirs=c_include_dirs,
                library_dirs=args.library_dirs,
                extra_compile_args=args.compile_args,
                extra_link_args=args.link_args,
                libraries=args.lib,
                language='c++' if args.cplus else 'c',
            )
            build_extension = self._get_build_extension()
            try:
                opts = dict(
                    quiet=quiet,
                    annotate=args.annotate,
                    force=True,
                )
                if args.language_level is not None:
                    assert args.language_level in (2, 3)
                    opts['language_level'] = args.language_level
                elif sys.version_info[0] > 2:
                    opts['language_level'] = 3
                build_extension.extensions = cythonize([extension], **opts)
            except CompileError:
                return

        if not have_module:
            build_extension.build_temp = os.path.dirname(pyx_file)
            build_extension.build_lib = lib_dir
            build_extension.run()
            self._code_cache[key] = module_name

        module = imp.load_dynamic(module_name, module_path)
        self._import_all(module)

        if args.annotate:
            try:
                with io.open(html_file, encoding='utf-8') as f:
                    annotated_html = f.read()
            except IOError as e:
                # File could not be opened. Most likely the user has a version
                # of Cython before 0.15.1 (when `cythonize` learned the
                # `force` keyword argument) and has already compiled this
                # exact source without annotation.
                print(
                    'Cython completed successfully but the annotated '
                    'source could not be read.',
                    file=sys.stderr)
                print(e, file=sys.stderr)
            else:
                return display.HTML(self.clean_annotated_html(annotated_html))
示例#16
0
文件: path.py 项目: xguse/ipython
def get_ipython_cache_dir():
    warn("get_ipython_cache_dir has moved to the IPython.paths module")
    from IPython.paths import get_ipython_cache_dir
    return get_ipython_cache_dir()
示例#17
0
文件: path.py 项目: marcosptf/fedora
def get_ipython_cache_dir():
    warn("get_ipython_cache_dir has moved to the IPython.paths module")
    from IPython.paths import get_ipython_cache_dir
    return get_ipython_cache_dir()
示例#18
0
文件: path.py 项目: Carreau/ipython
def get_ipython_cache_dir():
    warn("get_ipython_cache_dir has moved to the IPython.paths module since IPython 4.0.", stacklevel=2)
    from IPython.paths import get_ipython_cache_dir
    return get_ipython_cache_dir()