diff Lib/importlib/_bootstrap.py @ 80507:e30bcce5c634

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.
author Brett Cannon <brett@python.org>
date Sun, 18 Nov 2012 10:03:31 -0500
parents 9a984c051cde
children a82ee9a1457a
line wrap: on
line diff
--- a/Lib/importlib/_bootstrap.py	Sun Nov 18 14:06:54 2012 +0200
+++ b/Lib/importlib/_bootstrap.py	Sun Nov 18 10:03:31 2012 -0500
@@ -931,6 +931,14 @@
             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 @@
                             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):