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

Class SourceFileLoader

Lib/importlib/_bootstrap_external.py:934–976  ·  view source on GitHub ↗

Concrete implementation of SourceLoader using the file system.

Source from the content-addressed store, hash-verified

932
933
934class SourceFileLoader(FileLoader, SourceLoader):
935
936 """Concrete implementation of SourceLoader using the file system."""
937
938 def path_stats(self, path):
939 """Return the metadata for the path."""
940 st = _path_stat(path)
941 return {'mtime': st.st_mtime, 'size': st.st_size}
942
943 def _cache_bytecode(self, source_path, bytecode_path, data):
944 # Adapt between the two APIs
945 mode = _calc_mode(source_path)
946 return self.set_data(bytecode_path, data, _mode=mode)
947
948 def set_data(self, path, data, *, _mode=0o666):
949 """Write bytes data to a file."""
950 parent, filename = _path_split(path)
951 path_parts = []
952 # Figure out what directories are missing.
953 while parent and not _path_isdir(parent):
954 parent, part = _path_split(parent)
955 path_parts.append(part)
956 # Create needed directories.
957 for part in reversed(path_parts):
958 parent = _path_join(parent, part)
959 try:
960 _os.mkdir(parent)
961 except FileExistsError:
962 # Probably another Python process already created the dir.
963 continue
964 except OSError as exc:
965 # Could be a permission error, read-only filesystem: just forget
966 # about writing the data.
967 _bootstrap._verbose_message('could not create {!r}: {!r}',
968 parent, exc)
969 return
970 try:
971 _write_atomic(path, data, _mode)
972 _bootstrap._verbose_message('created {!r}', path)
973 except OSError as exc:
974 # Same as above: just don't write the bytecode.
975 _bootstrap._verbose_message('could not create {!r}: {!r}', path,
976 exc)
977
978
979class SourcelessFileLoader(FileLoader, _LoaderBasics):

Callers 1

_fix_up_moduleFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…