Python imp.cache_from_source() Examples

The following are 30 code examples of imp.cache_from_source(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module imp , or try the search function .
Example #1
Source File: pyfiles.py    From alembic with MIT License 6 votes vote down vote up
def pyc_file_from_path(path):
    """Given a python source path, locate the .pyc.

    """

    if has_pep3147():
        if py35:
            import importlib

            candidate = importlib.util.cache_from_source(path)
        else:
            import imp

            candidate = imp.cache_from_source(path)
        if os.path.exists(candidate):
            return candidate

    # even for pep3147, fall back to the old way of finding .pyc files,
    # to support sourceless operation
    filepath, ext = os.path.splitext(path)
    for ext in get_current_bytecode_suffixes():
        if os.path.exists(filepath + ext):
            return filepath + ext
    else:
        return None 
Example #2
Source File: support.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 6 votes vote down vote up
def forget(modname):
    """'Forget' a module was ever imported.

    This removes the module from sys.modules and deletes any PEP 3147 or
    legacy .pyc and .pyo files.
    """
    unload(modname)
    for dirname in sys.path:
        source = os.path.join(dirname, modname + '.py')
        # It doesn't matter if they exist or not, unlink all possible
        # combinations of PEP 3147 and legacy pyc and pyo files.
        unlink(source + 'c')
        unlink(source + 'o')
        unlink(imp.cache_from_source(source, debug_override=True))
        unlink(imp.cache_from_source(source, debug_override=False))

# On some platforms, should not run gui test even if it is allowed
# in `use_resources'. 
Example #3
Source File: support.py    From deepWordBug with Apache License 2.0 6 votes vote down vote up
def forget(modname):
    """'Forget' a module was ever imported.

    This removes the module from sys.modules and deletes any PEP 3147 or
    legacy .pyc and .pyo files.
    """
    unload(modname)
    for dirname in sys.path:
        source = os.path.join(dirname, modname + '.py')
        # It doesn't matter if they exist or not, unlink all possible
        # combinations of PEP 3147 and legacy pyc and pyo files.
        unlink(source + 'c')
        unlink(source + 'o')
        unlink(imp.cache_from_source(source, debug_override=True))
        unlink(imp.cache_from_source(source, debug_override=False))

# On some platforms, should not run gui test even if it is allowed
# in `use_resources'. 
Example #4
Source File: support.py    From telegram-robot-rss with Mozilla Public License 2.0 6 votes vote down vote up
def forget(modname):
    """'Forget' a module was ever imported.

    This removes the module from sys.modules and deletes any PEP 3147 or
    legacy .pyc and .pyo files.
    """
    unload(modname)
    for dirname in sys.path:
        source = os.path.join(dirname, modname + '.py')
        # It doesn't matter if they exist or not, unlink all possible
        # combinations of PEP 3147 and legacy pyc and pyo files.
        unlink(source + 'c')
        unlink(source + 'o')
        unlink(imp.cache_from_source(source, debug_override=True))
        unlink(imp.cache_from_source(source, debug_override=False))

# On some platforms, should not run gui test even if it is allowed
# in `use_resources'. 
Example #5
Source File: support.py    From cadquery-freecad-module with GNU Lesser General Public License v3.0 6 votes vote down vote up
def forget(modname):
    """'Forget' a module was ever imported.

    This removes the module from sys.modules and deletes any PEP 3147 or
    legacy .pyc and .pyo files.
    """
    unload(modname)
    for dirname in sys.path:
        source = os.path.join(dirname, modname + '.py')
        # It doesn't matter if they exist or not, unlink all possible
        # combinations of PEP 3147 and legacy pyc and pyo files.
        unlink(source + 'c')
        unlink(source + 'o')
        unlink(imp.cache_from_source(source, debug_override=True))
        unlink(imp.cache_from_source(source, debug_override=False))

# On some platforms, should not run gui test even if it is allowed
# in `use_resources'. 
Example #6
Source File: support.py    From jawfish with MIT License 6 votes vote down vote up
def forget(modname):
    """'Forget' a module was ever imported.

    This removes the module from sys.modules and deletes any PEP 3147 or
    legacy .pyc and .pyo files.
    """
    unload(modname)
    for dirname in sys.path:
        source = os.path.join(dirname, modname + '.py')
        # It doesn't matter if they exist or not, unlink all possible
        # combinations of PEP 3147 and legacy pyc and pyo files.
        unlink(source + 'c')
        unlink(source + 'o')
        unlink(imp.cache_from_source(source, debug_override=True))
        unlink(imp.cache_from_source(source, debug_override=False))

# On some platforms, should not run gui test even if it is allowed
# in `use_resources'. 
Example #7
Source File: support.py    From verge3d-blender-addon with GNU General Public License v3.0 6 votes vote down vote up
def forget(modname):
    """'Forget' a module was ever imported.

    This removes the module from sys.modules and deletes any PEP 3147 or
    legacy .pyc and .pyo files.
    """
    unload(modname)
    for dirname in sys.path:
        source = os.path.join(dirname, modname + '.py')
        # It doesn't matter if they exist or not, unlink all possible
        # combinations of PEP 3147 and legacy pyc and pyo files.
        unlink(source + 'c')
        unlink(source + 'o')
        unlink(imp.cache_from_source(source, debug_override=True))
        unlink(imp.cache_from_source(source, debug_override=False))

# On some platforms, should not run gui test even if it is allowed
# in `use_resources'. 
Example #8
Source File: misc.py    From pySINDy with MIT License 5 votes vote down vote up
def cache_from_source(py_file, debug=__debug__):
        ext = debug and 'c' or 'o'
        return py_file + ext 
Example #9
Source File: compat.py    From Hands-On-Application-Development-with-PyCharm with MIT License 5 votes vote down vote up
def cache_from_source(path, debug_override=None):
            assert path.endswith('.py')
            if debug_override is None:
                debug_override = __debug__
            if debug_override:
                suffix = 'c'
            else:
                suffix = 'o'
            return path + suffix 
Example #10
Source File: compat.py    From pySINDy with MIT License 5 votes vote down vote up
def cache_from_source(path, debug_override=None):
            assert path.endswith('.py')
            if debug_override is None:
                debug_override = __debug__
            if debug_override:
                suffix = 'c'
            else:
                suffix = 'o'
            return path + suffix 
Example #11
Source File: misc.py    From Building-Recommendation-Systems-with-Python with MIT License 5 votes vote down vote up
def cache_from_source(py_file, debug=__debug__):
        ext = debug and 'c' or 'o'
        return py_file + ext 
Example #12
Source File: compat.py    From Building-Recommendation-Systems-with-Python with MIT License 5 votes vote down vote up
def cache_from_source(path, debug_override=None):
            assert path.endswith('.py')
            if debug_override is None:
                debug_override = __debug__
            if debug_override:
                suffix = 'c'
            else:
                suffix = 'o'
            return path + suffix 
Example #13
Source File: misc.py    From Building-Recommendation-Systems-with-Python with MIT License 5 votes vote down vote up
def cache_from_source(py_file, debug=__debug__):
        ext = debug and 'c' or 'o'
        return py_file + ext 
Example #14
Source File: test_imp.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_sep_altsep_and_sep_cache_from_source(self):
        # Windows path and PEP 3147 where sep is right of altsep.
        self.assertEqual(
            imp.cache_from_source('\\foo\\bar\\baz/qux.py', True),
            '\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag)) 
Example #15
Source File: test_imp.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_cache_from_source_no_cache_tag(self):
        # Non cache tag means NotImplementedError.
        with support.swap_attr(sys.implementation, 'cache_tag', None):
            with self.assertRaises(NotImplementedError):
                imp.cache_from_source('whatever.py') 
Example #16
Source File: test_imp.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_cache_from_source_no_dot(self):
        # Directory with a dot, filename without dot.
        path = os.path.join('foo.bar', 'file')
        expect = os.path.join('foo.bar', '__pycache__',
                              'file{}.pyc'.format(self.tag))
        self.assertEqual(imp.cache_from_source(path, True), expect) 
Example #17
Source File: test_imp.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_cache_from_source_optimized(self):
        # Given the path to a .py file, return the path to its PEP 3147
        # defined .pyo file (i.e. under __pycache__).
        path = os.path.join('foo', 'bar', 'baz', 'qux.py')
        expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
                              'qux.{}.pyo'.format(self.tag))
        self.assertEqual(imp.cache_from_source(path, False), expect) 
Example #18
Source File: test_imp.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_cache_from_source_cwd(self):
        path = 'foo.py'
        expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag))
        self.assertEqual(imp.cache_from_source(path, True), expect) 
Example #19
Source File: test_imp.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_cache_from_source(self):
        # Given the path to a .py file, return the path to its PEP 3147
        # defined .pyc file (i.e. under __pycache__).
        path = os.path.join('foo', 'bar', 'baz', 'qux.py')
        expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
                              'qux.{}.pyc'.format(self.tag))
        self.assertEqual(imp.cache_from_source(path, True), expect) 
Example #20
Source File: compat.py    From hacktoberfest2018 with GNU General Public License v3.0 5 votes vote down vote up
def cache_from_source(path, debug_override=None):
            assert path.endswith('.py')
            if debug_override is None:
                debug_override = __debug__
            if debug_override:
                suffix = 'c'
            else:
                suffix = 'o'
            return path + suffix 
Example #21
Source File: misc.py    From hacktoberfest2018 with GNU General Public License v3.0 5 votes vote down vote up
def cache_from_source(py_file, debug=__debug__):
        ext = debug and 'c' or 'o'
        return py_file + ext 
Example #22
Source File: compat.py    From hacktoberfest2018 with GNU General Public License v3.0 5 votes vote down vote up
def cache_from_source(path, debug_override=None):
            assert path.endswith('.py')
            if debug_override is None:
                debug_override = __debug__
            if debug_override:
                suffix = 'c'
            else:
                suffix = 'o'
            return path + suffix 
Example #23
Source File: misc.py    From hacktoberfest2018 with GNU General Public License v3.0 5 votes vote down vote up
def cache_from_source(py_file, debug=__debug__):
        ext = debug and 'c' or 'o'
        return py_file + ext 
Example #24
Source File: compat.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def cache_from_source(path, debug_override=None):
            assert path.endswith('.py')
            if debug_override is None:
                debug_override = __debug__
            if debug_override:
                suffix = 'c'
            else:
                suffix = 'o'
            return path + suffix 
Example #25
Source File: misc.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def cache_from_source(py_file, debug=__debug__):
        ext = debug and 'c' or 'o'
        return py_file + ext 
Example #26
Source File: support.py    From jawfish with MIT License 5 votes vote down vote up
def make_legacy_pyc(source):
    """Move a PEP 3147 pyc/pyo file to its legacy pyc/pyo location.

    The choice of .pyc or .pyo extension is done based on the __debug__ flag
    value.

    :param source: The file system path to the source file.  The source file
        does not need to exist, however the PEP 3147 pyc file must exist.
    :return: The file system path to the legacy pyc file.
    """
    pyc_file = imp.cache_from_source(source)
    up_one = os.path.dirname(os.path.abspath(source))
    legacy_pyc = os.path.join(up_one, source + ('c' if __debug__ else 'o'))
    os.rename(pyc_file, legacy_pyc)
    return legacy_pyc 
Example #27
Source File: support.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 5 votes vote down vote up
def make_legacy_pyc(source):
    """Move a PEP 3147 pyc/pyo file to its legacy pyc/pyo location.

    The choice of .pyc or .pyo extension is done based on the __debug__ flag
    value.

    :param source: The file system path to the source file.  The source file
        does not need to exist, however the PEP 3147 pyc file must exist.
    :return: The file system path to the legacy pyc file.
    """
    pyc_file = imp.cache_from_source(source)
    up_one = os.path.dirname(os.path.abspath(source))
    legacy_pyc = os.path.join(up_one, source + ('c' if __debug__ else 'o'))
    os.rename(pyc_file, legacy_pyc)
    return legacy_pyc 
Example #28
Source File: unpyc3.py    From unpyc3 with GNU General Public License v3.0 5 votes vote down vote up
def dec_module(path):
    if path.endswith(".py"):
        path = imp.cache_from_source(path)
    elif not path.endswith(".pyc") and not path.endswith(".pyo"):
        raise ValueError("path must point to a .py or .pyc file")
    stream = open(path, "rb")
    code_obj = read_code(stream)
    code = Code(code_obj)
    return code.get_suite(include_declarations=False, look_for_docstring=True) 
Example #29
Source File: misc.py    From python with Apache License 2.0 5 votes vote down vote up
def cache_from_source(py_file, debug=__debug__):
        ext = debug and 'c' or 'o'
        return py_file + ext 
Example #30
Source File: compat.py    From Building-Recommendation-Systems-with-Python with MIT License 5 votes vote down vote up
def cache_from_source(path, debug_override=None):
            assert path.endswith('.py')
            if debug_override is None:
                debug_override = __debug__
            if debug_override:
                suffix = 'c'
            else:
                suffix = 'o'
            return path + suffix