本文整理匯總了Python中imp.cache_from_source方法的典型用法代碼示例。如果您正苦於以下問題:Python imp.cache_from_source方法的具體用法?Python imp.cache_from_source怎麽用?Python imp.cache_from_source使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類imp
的用法示例。
在下文中一共展示了imp.cache_from_source方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: forget
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import cache_from_source [as 別名]
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'.
示例2: pyc_file_from_path
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import cache_from_source [as 別名]
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
示例3: make_legacy_pyc
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import cache_from_source [as 別名]
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
示例4: cache_from_source
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import cache_from_source [as 別名]
def cache_from_source(py_file, debug=__debug__):
ext = debug and 'c' or 'o'
return py_file + ext
示例5: cache_from_source
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import cache_from_source [as 別名]
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
示例6: pyc_file_from_path
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import cache_from_source [as 別名]
def pyc_file_from_path(path):
"""Given a python source path, locate the .pyc.
See http://www.python.org/dev/peps/pep-3147/
#detecting-pep-3147-availability
http://www.python.org/dev/peps/pep-3147/#file-extension-checks
"""
import imp
has3147 = hasattr(imp, 'get_tag')
if has3147:
return imp.cache_from_source(path)
else:
return simple_pyc_file_from_path(path)
示例7: find_bytecode_files
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import cache_from_source [as 別名]
def find_bytecode_files(python_file):
"""
Find the byte code file(s) generated from a Python file.
:param python_file: The pathname of a ``*.py`` file (a string).
:returns: A generator of pathnames (strings).
Starting from Python 3.2 byte code files are written according to `PEP
3147`_ which also defines :func:`imp.cache_from_source()` to locate
(optimized) byte code files. When this function is available it is used,
when it's not available the corresponding ``*.pyc`` and/or ``*.pyo`` files
are located manually by :func:`find_bytecode_files()`.
.. _PEP 3147: https://www.python.org/dev/peps/pep-3147/
"""
if HAS_PEP_3147:
bytecode_file = imp.cache_from_source(python_file, True)
if os.path.isfile(bytecode_file):
yield bytecode_file
optimized_bytecode_file = imp.cache_from_source(python_file, False)
if os.path.isfile(optimized_bytecode_file):
yield optimized_bytecode_file
else:
for suffix in ('c', 'o'):
bytecode_file = python_file + suffix
if os.path.isfile(bytecode_file):
yield bytecode_file
示例8: cache_from_source
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import cache_from_source [as 別名]
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
示例9: cache_from_source
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import cache_from_source [as 別名]
def cache_from_source(path, debug_override=None):
if debug_override is None:
debug_override = __debug__
basename, ext = os.path.splitext(path)
return basename + '.pyc' if debug_override else '.pyo'
示例10: add
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import cache_from_source [as 別名]
def add(self, path):
path = normalize_path(path)
if not os.path.exists(path):
return
if self._permitted(path):
self.paths.add(path)
else:
self._refuse.add(path)
# __pycache__ files can show up after 'installed-files.txt' is created, due to imports
if os.path.splitext(path)[1] == '.py' and uses_pycache:
self.add(imp.cache_from_source(path))