mirror of
https://github.com/python/cpython.git
synced 2025-08-31 22:18:28 +00:00
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
This commit is contained in:
parent
28c013dcb4
commit
d43b30b046
4 changed files with 59 additions and 10 deletions
|
@ -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):
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue