summaryrefslogtreecommitdiffstats
path: root/Lib
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
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')
-rw-r--r--Lib/importlib/_bootstrap.py12
-rw-r--r--Lib/test/test_importlib/source/test_abc_loader.py15
2 files changed, 18 insertions, 9 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):
diff --git a/Lib/test/test_importlib/source/test_abc_loader.py b/Lib/test/test_importlib/source/test_abc_loader.py
index 78a8faa..44bec32 100644
--- a/Lib/test/test_importlib/source/test_abc_loader.py
+++ b/Lib/test/test_importlib/source/test_abc_loader.py
@@ -148,6 +148,11 @@ class SourceOnlyLoaderTests(SourceLoaderTestHarness):
code_object = self.loader.get_code(self.name)
self.verify_code(code_object)
+ def test_compile_source(self):
+ # Verify the compiled code object.
+ code = self.loader.compile_source(self.loader.source, self.path)
+ self.verify_code(code)
+
def test_load_module(self):
# Loading a module should set __name__, __loader__, __package__,
# __path__ (for packages), __file__, and __cached__.
@@ -395,12 +400,10 @@ class AbstractMethodImplTests(unittest.TestCase):
def test_main():
from test.support import run_unittest
- run_unittest(SkipWritingBytecodeTests, RegeneratedBytecodeTests,
- BadBytecodeFailureTests, MissingPathsTests,
- SourceOnlyLoaderTests,
- SourceLoaderBytecodeTests,
- SourceLoaderGetSourceTests,
- AbstractMethodImplTests)
+ run_unittest(SourceOnlyLoaderTests,
+ SourceLoaderBytecodeTests,
+ SourceLoaderGetSourceTests,
+ AbstractMethodImplTests)
if __name__ == '__main__':