changeset: 90602:9bd844792b32 user: Brett Cannon date: Fri May 09 12:28:22 2014 -0400 files: Doc/library/importlib.rst Doc/whatsnew/3.5.rst Lib/importlib/abc.py Misc/NEWS description: Issue #21156: importlib.abc.InspectLoader.source_to_code() is now a staticmethod. diff -r e9453f6fa787 -r 9bd844792b32 Doc/library/importlib.rst --- a/Doc/library/importlib.rst Fri May 09 11:56:07 2014 -0400 +++ b/Doc/library/importlib.rst Fri May 09 12:28:22 2014 -0400 @@ -499,7 +499,7 @@ .. versionchanged:: 3.4 Raises :exc:`ImportError` instead of :exc:`NotImplementedError`. - .. method:: source_to_code(data, path='') + .. staticmethod:: source_to_code(data, path='') Create a code object from Python source. @@ -508,8 +508,14 @@ the "path" to where the source code originated from, which can be an abstract concept (e.g. location in a zip file). + With the subsequent code object one can execute it in a module by + running ``exec(code, module.__dict__)``. + .. versionadded:: 3.4 + .. versionchanged:: 3.5 + Made the method static. + .. method:: exec_module(module) Implementation of :meth:`Loader.exec_module`. diff -r e9453f6fa787 -r 9bd844792b32 Doc/whatsnew/3.5.rst --- a/Doc/whatsnew/3.5.rst Fri May 09 11:56:07 2014 -0400 +++ b/Doc/whatsnew/3.5.rst Fri May 09 12:28:22 2014 -0400 @@ -158,6 +158,11 @@ *module* contains no docstrings instead of raising :exc:`ValueError` (contributed by Glenn Jones in :issue:`15916`). +* :func:`importlib.abc.InspectLoader.source_to_code` is now a + static method to make it easier to work with source code in a string. + With a module object that you want to initialize you can then use + ``exec(code, module.__dict__)`` to execute the code in the module. + Optimizations ============= diff -r e9453f6fa787 -r 9bd844792b32 Lib/importlib/abc.py --- a/Lib/importlib/abc.py Fri May 09 11:56:07 2014 -0400 +++ b/Lib/importlib/abc.py Fri May 09 12:28:22 2014 -0400 @@ -217,7 +217,8 @@ """ raise ImportError - def source_to_code(self, data, path=''): + @staticmethod + def source_to_code(data, path=''): """Compile 'data' into a code object. The 'data' argument can be anything that compile() can handle. The'path' diff -r e9453f6fa787 -r 9bd844792b32 Misc/NEWS --- a/Misc/NEWS Fri May 09 11:56:07 2014 -0400 +++ b/Misc/NEWS Fri May 09 12:28:22 2014 -0400 @@ -73,6 +73,9 @@ Library ------- +- Issue #21156: importlib.abc.InspectLoader.source_to_code() is now a + staticmethod. + - Issue #21396: Fix TextIOWrapper(..., write_through=True) to not force a flush() on the underlying binary stream. Patch by akira.