summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/_bootstrap.py
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-11-18 15:03:31 (GMT)
committerBrett Cannon <brett@python.org>2012-11-18 15:03:31 (GMT)
commit5650e4f41cc65fae44e44702919df592f15211f3 (patch)
tree97f15ed45c4bdcf40670b50a9c93314a5e00187d /Lib/importlib/_bootstrap.py
parent195ad6ce052bfdda8f47ffbf14becb95253bafc6 (diff)
downloadcpython-5650e4f41cc65fae44e44702919df592f15211f3.zip
cpython-5650e4f41cc65fae44e44702919df592f15211f3.tar.gz
cpython-5650e4f41cc65fae44e44702919df592f15211f3.tar.bz2
Issue #15627: Add the compile_source() method to
importlib.abc.SourceLoader. This provides an easy hook into the import system to allow for source transformations, AST optimizations, etc.
Diffstat (limited to 'Lib/importlib/_bootstrap.py')
-rw-r--r--Lib/importlib/_bootstrap.py12
1 files changed, 9 insertions, 3 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index 4ddeadf..a924c79 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -931,6 +931,14 @@ class SourceLoader(_LoaderBasics):
raise ImportError("Failed to decode source file",
name=fullname) from exc
+ def compile_source(self, data, path):
+ """Return the code object compiled from source.
+
+ The 'data' argument can be any object type that compile() supports.
+ """
+ return _call_with_frames_removed(compile, data, path, 'exec',
+ dont_inherit=True)
+
def get_code(self, fullname):
"""Concrete implementation of InspectLoader.get_code.
@@ -976,9 +984,7 @@ class SourceLoader(_LoaderBasics):
raise ImportError(msg.format(bytecode_path),
name=fullname, path=bytecode_path)
source_bytes = self.get_data(source_path)
- code_object = _call_with_frames_removed(compile,
- source_bytes, source_path, 'exec',
- dont_inherit=True)
+ code_object = self.compile_source(source_bytes, source_path)
_verbose_message('code object from {}', source_path)
if (not sys.dont_write_bytecode and bytecode_path is not None and
source_mtime is not None):