Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6eaac13

Browse files
committed
Issue #21156: importlib.abc.InspectLoader.source_to_code() is now a
staticmethod.
1 parent a237a98 commit 6eaac13

4 files changed

Lines changed: 17 additions & 2 deletions

File tree

Doc/library/importlib.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ ABC hierarchy::
499499
.. versionchanged:: 3.4
500500
Raises :exc:`ImportError` instead of :exc:`NotImplementedError`.
501501

502-
.. method:: source_to_code(data, path='<string>')
502+
.. staticmethod:: source_to_code(data, path='<string>')
503503

504504
Create a code object from Python source.
505505

@@ -508,8 +508,14 @@ ABC hierarchy::
508508
the "path" to where the source code originated from, which can be an
509509
abstract concept (e.g. location in a zip file).
510510

511+
With the subsequent code object one can execute it in a module by
512+
running ``exec(code, module.__dict__)``.
513+
511514
.. versionadded:: 3.4
512515

516+
.. versionchanged:: 3.5
517+
Made the method static.
518+
513519
.. method:: exec_module(module)
514520

515521
Implementation of :meth:`Loader.exec_module`.

Doc/whatsnew/3.5.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ Improved Modules
158158
*module* contains no docstrings instead of raising :exc:`ValueError`
159159
(contributed by Glenn Jones in :issue:`15916`).
160160

161+
* :func:`importlib.abc.InspectLoader.source_to_code` is now a
162+
static method to make it easier to work with source code in a string.
163+
With a module object that you want to initialize you can then use
164+
``exec(code, module.__dict__)`` to execute the code in the module.
165+
161166

162167
Optimizations
163168
=============

Lib/importlib/abc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ def get_source(self, fullname):
217217
"""
218218
raise ImportError
219219

220-
def source_to_code(self, data, path='<string>'):
220+
@staticmethod
221+
def source_to_code(data, path='<string>'):
221222
"""Compile 'data' into a code object.
222223
223224
The 'data' argument can be anything that compile() can handle. The'path'

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ Core and Builtins
7373
Library
7474
-------
7575

76+
- Issue #21156: importlib.abc.InspectLoader.source_to_code() is now a
77+
staticmethod.
78+
7679
- Issue #21396: Fix TextIOWrapper(..., write_through=True) to not force a
7780
flush() on the underlying binary stream. Patch by akira.
7881

0 commit comments

Comments
 (0)