Implement get_source for importlib.abc.PyLoader using source_path and get_data.
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index 58b5a46..73f6513 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -369,6 +369,26 @@
source = source.replace(line_endings, b'\n')
return compile(source, source_path, 'exec', dont_inherit=True)
+ # Never use in implementing import! Imports code within the method.
+ def get_source(self, fullname):
+ """Return the source code for a module.
+
+ self.source_path() and self.get_data() are used to implement this
+ method.
+
+ """
+ path = self.source_path(fullname)
+ if path is None:
+ return None
+ try:
+ source_bytes = self.get_data(path)
+ except IOError:
+ return ImportError("source not available through get_data()")
+ import io
+ import tokenize
+ encoding = tokenize.detect_encoding(io.BytesIO(source_bytes).readline)
+ return source_bytes.decode(encoding[0])
+
class PyPycLoader(PyLoader):