diff options
author | Brett Cannon <bcannon@gmail.com> | 2009-03-10 03:29:23 (GMT) |
---|---|---|
committer | Brett Cannon <bcannon@gmail.com> | 2009-03-10 03:29:23 (GMT) |
commit | d43b30b046ea151612494ed6d44aed3df71b480a (patch) | |
tree | 72a3a4773d9e73d93e8c59157df11da5669f58e4 /Lib/importlib/_bootstrap.py | |
parent | 28c013dcb464346b7fbefd73ad3cd35c66f98ae3 (diff) | |
download | cpython-d43b30b046ea151612494ed6d44aed3df71b480a.zip cpython-d43b30b046ea151612494ed6d44aed3df71b480a.tar.gz cpython-d43b30b046ea151612494ed6d44aed3df71b480a.tar.bz2 |
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Diffstat (limited to 'Lib/importlib/_bootstrap.py')
-rw-r--r-- | Lib/importlib/_bootstrap.py | 20 |
1 files changed, 20 insertions, 0 deletions
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 @@ class PyLoader: 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): |