MCPcopy Index your code
hub / github.com/python/cpython / source_from_cache

Function source_from_cache

Lib/importlib/_bootstrap_external.py:299–336  ·  view source on GitHub ↗

Given the path to a .pyc. file, return the path to its .py file. The .pyc file does not need to exist; this simply returns the path to the .py file calculated to correspond to the .pyc file. If path does not conform to PEP 3147/488 format, ValueError will be raised. If sys.implemen

(path)

Source from the content-addressed store, hash-verified

297
298
299def source_from_cache(path):
300 """Given the path to a .pyc. file, return the path to its .py file.
301
302 The .pyc file does not need to exist; this simply returns the path to
303 the .py file calculated to correspond to the .pyc file. If path does
304 not conform to PEP 3147/488 format, ValueError will be raised. If
305 sys.implementation.cache_tag is None then NotImplementedError is raised.
306
307 """
308 if sys.implementation.cache_tag is None:
309 raise NotImplementedError('sys.implementation.cache_tag is None')
310 path = _os.fspath(path)
311 head, pycache_filename = _path_split(path)
312 found_in_pycache_prefix = False
313 if sys.pycache_prefix is not None:
314 stripped_path = sys.pycache_prefix.rstrip(path_separators)
315 if head.startswith(stripped_path + path_sep):
316 head = head[len(stripped_path):]
317 found_in_pycache_prefix = True
318 if not found_in_pycache_prefix:
319 head, pycache = _path_split(head)
320 if pycache != _PYCACHE:
321 raise ValueError(f'{_PYCACHE} not bottom-level directory in '
322 f'{path!r}')
323 dot_count = pycache_filename.count('.')
324 if dot_count not in {2, 3}:
325 raise ValueError(f'expected only 2 or 3 dots in {pycache_filename!r}')
326 elif dot_count == 3:
327 optimization = pycache_filename.rsplit('.', 2)[-2]
328 if not optimization.startswith(_OPT):
329 raise ValueError("optimization portion of filename does not start "
330 f"with {_OPT!r}")
331 opt_level = optimization[len(_OPT):]
332 if not opt_level.isalnum():
333 raise ValueError(f"optimization level {optimization!r} is not an "
334 "alphanumeric value")
335 base_filename = pycache_filename.partition('.')[0]
336 return _path_join(head, base_filename + SOURCE_SUFFIXES[0])
337
338
339def _get_sourcefile(bytecode_path):

Callers 2

make_zip_scriptFunction · 0.85
_get_sourcefileFunction · 0.85

Calls 8

_path_splitFunction · 0.85
_path_joinFunction · 0.85
isalnumMethod · 0.80
rstripMethod · 0.45
startswithMethod · 0.45
countMethod · 0.45
rsplitMethod · 0.45
partitionMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…