Mercurial > cpython-lru_cache-weakref
diff Lib/importlib/_bootstrap.py @ 52112:03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
| author | Brett Cannon <bcannon@gmail.com> |
|---|---|
| date | Tue, 10 Mar 2009 03:29:23 +0000 |
| parents | c06f02a5e962 |
| children | afd7118c8ccf |
line wrap: on
line diff
--- a/Lib/importlib/_bootstrap.py Tue Mar 10 00:07:25 2009 +0000 +++ b/Lib/importlib/_bootstrap.py Tue Mar 10 03:29:23 2009 +0000 @@ -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):
