@@ -1693,64 +1693,48 @@ The :mod:`!test.support.import_helper` module provides support for import tests.
16931693
16941694.. versionadded :: 3.10
16951695
1696+ Creating Module Objects
1697+ -----------------------
16961698
1697- .. function :: forget(modname )
1699+ .. function :: create_module(name, loader=None, *, ispkg=False )
16981700
1699- Remove the module named *modname * from :data: `sys.modules ` and delete any
1700- byte-compiled files of the module.
1701+ Create a new, empty :term: `module ` or :term: `package ` and return it.
17011702
1703+ *name *, *loader * and *ispkg * (as ``is_package ``) are passed to
1704+ :class: `importlib.machinery.ModuleSpec `.
17021705
1703- .. function :: import_fresh_module(name, fresh=(), blocked=(), *, deprecated=False, usefrozen=False)
17041706
1705- This function imports and returns a fresh copy of the named Python module
1706- by removing the named module from :data: `sys.modules ` before doing the import.
1707- Note that unlike :func: `reload `, the original module is not affected by
1708- this operation.
1707+ .. function :: add_module(spec, *, parents=True)
17091708
1710- *fresh * is an iterable of additional module names that are also removed
1711- from the :data: `sys.modules ` cache before doing the import.
1709+ Create a :term: `module ` from a name or spec and add it to :data: `sys.modules `.
17121710
1713- *blocked * is an iterable of module names that are replaced with ``None ``
1714- in the module cache during the import to ensure that attempts to import
1715- them raise :exc: `ImportError `.
1711+ If *parents * is ``True `` then also create any missing parent modules.
17161712
1717- The named module and any modules named in the *fresh * and *blocked *
1718- parameters are saved before starting the import and then reinserted into
1719- :data: `sys.modules ` when the fresh import is complete.
1713+ Return the new module.
17201714
1721- Module and package deprecation messages are suppressed during this import
1722- if *deprecated * is ``True ``.
17231715
1724- If *usefrozen * is False (the default) then the frozen importer is
1725- disabled (except for essential modules like ``importlib._bootstrap ``).
1716+ .. function :: add_package(spec, *, parents=True)
17261717
1727- This function will raise :exc: `ImportError ` if the named module cannot be
1728- imported.
1718+ Create a :term: `package ` from a name and add it to :data: `sys.modules `.
17291719
1730- Example use::
1720+ If * parents * is `` True `` then also create any missing parent modules.
17311721
1732- # Get copies of the warnings module for testing without affecting the
1733- # version being used by the rest of the test suite. One copy uses the
1734- # C implementation, the other is forced to use the pure Python fallback
1735- # implementation
1736- py_warnings = import_fresh_module('warnings', blocked=['_warnings'])
1737- c_warnings = import_fresh_module('warnings', fresh=['_warnings'])
1722+ Return the new package.
17381723
1739- .. versionadded :: 3.1
17401724
1725+ Manipulating sys.modules
1726+ ------------------------
17411727
1742- .. function :: import_module(name, deprecated=False, *, required_on=())
17431728
1744- This function imports and returns the named module. Unlike a normal
1745- import, this function raises :exc: `unittest.SkipTest ` if the module
1746- cannot be imported.
1729+ .. function :: unload(name)
17471730
1748- Module and package deprecation messages are suppressed during this import
1749- if *deprecated * is ``True ``. If a module is required on a platform but
1750- optional for others, set *required_on * to an iterable of platform prefixes
1751- which will be compared against :data: `sys.platform `.
1731+ Remove the module named *name * from :data: `sys.modules `.
17521732
1753- .. versionadded :: 3.1
1733+
1734+ .. function :: forget(modname)
1735+
1736+ Remove the module named *modname * from :data: `sys.modules ` and delete any
1737+ byte-compiled files of the module.
17541738
17551739
17561740.. function :: modules_setup()
@@ -1760,23 +1744,14 @@ The :mod:`!test.support.import_helper` module provides support for import tests.
17601744
17611745.. function :: modules_cleanup(oldmodules)
17621746
1763- Remove modules except for *oldmodules * and ``encodings `` in order to
1764- preserve internal cache.
1747+ Remove modules except for *oldmodules * and ``encodings `` from :data: ` sys.modules `
1748+ in order to preserve internal cache.
17651749
17661750
1767- .. function :: unload(name)
1768-
1769- Delete *name * from :data: `sys.modules `.
1770-
1771-
1772- .. function :: make_legacy_pyc(source, allow_compile=False)
1773-
1774- Move a :pep: `3147 `/:pep: `488 ` pyc file to its legacy pyc location and return the file
1775- system path to the legacy pyc file. The *source * value is the file system
1776- path to the source file. It does not need to exist, however the PEP
1777- 3147/488 pyc file must exist or *allow_compile * must be set.
1751+ .. function :: isolated_modules()
17781752
1779- *allow_compile * will create a .pyc file if it does not exist.
1753+ A context manager that makes a copy of :data: `sys.modules ` on entry and restores
1754+ it on exit.
17801755
17811756
17821757.. class :: CleanImport(*module_names, usefrozen=False)
@@ -1795,46 +1770,62 @@ The :mod:`!test.support.import_helper` module provides support for import tests.
17951770 Example usage::
17961771
17971772 with CleanImport('foo'):
1798- importlib.import_module('foo') # New reference.
1799-
1773+ importlib.import_module('foo') # New reference.
18001774
1801- .. class :: DirsOnSysPath(*paths)
18021775
1803- A context manager to temporarily add directories to :data: `sys.path `.
1776+ Special importers
1777+ -----------------
18041778
1805- This makes a copy of :data: `sys.path `, appends any directories given
1806- as positional arguments, then reverts :data: `sys.path ` to the copied
1807- settings when the context ends.
1808-
1809- Note that *all * :data: `sys.path ` modifications in the body of the
1810- context manager, including replacement of the object,
1811- will be reverted at the end of the block.
1779+ .. function :: import_module(name, deprecated=False, *, required_on=())
18121780
1781+ This function imports and returns the named module. Unlike a normal
1782+ import, this function raises :exc: `unittest.SkipTest ` if the module
1783+ cannot be imported.
18131784
1814- .. function :: create_module(name, loader=None, *, ispkg=False)
1785+ Module and package deprecation messages are suppressed during this import
1786+ if *deprecated * is ``True ``. If a module is required on a platform but
1787+ optional for others, set *required_on * to an iterable of platform prefixes
1788+ which will be compared against :data: `sys.platform `.
18151789
1816- Create a new, empty :term: ` module ` or :term: ` package ` and return it.
1790+ .. versionadded :: 3.1
18171791
1818- *name *, *loader * and *ispkg * (as ``is_package ``) are passed to
1819- :class: `importlib.machinery.ModuleSpec `.
1792+ .. function :: import_fresh_module(name, fresh=(), blocked=(), *, deprecated=False, usefrozen=False)
18201793
1794+ This function imports and returns a fresh copy of the named Python module
1795+ by removing the named module from :data: `sys.modules ` before doing the import.
1796+ Note that unlike :func: `reload `, the original module is not affected by
1797+ this operation.
18211798
1822- .. function :: add_module(spec, *, parents=True)
1799+ *fresh * is an iterable of additional module names that are also removed
1800+ from the :data: `sys.modules ` cache before doing the import.
18231801
1824- Create a :term: `module ` from a name or spec and add it to :data: `sys.modules `.
1802+ *blocked * is an iterable of module names that are replaced with ``None ``
1803+ in the module cache during the import to ensure that attempts to import
1804+ them raise :exc: `ImportError `.
18251805
1826- If *parents * is ``True `` then also create any missing parent modules.
1806+ The named module and any modules named in the *fresh * and *blocked *
1807+ parameters are saved before starting the import and then reinserted into
1808+ :data: `sys.modules ` when the fresh import is complete.
18271809
1828- Return the new module.
1810+ Module and package deprecation messages are suppressed during this import
1811+ if *deprecated * is ``True ``.
18291812
1813+ If *usefrozen * is False (the default) then the frozen importer is
1814+ disabled (except for essential modules like ``importlib._bootstrap ``).
18301815
1831- .. function :: add_package(spec, *, parents=True)
1816+ This function will raise :exc: `ImportError ` if the named module cannot be
1817+ imported.
18321818
1833- Create a :term: ` package ` from a name and add it to :data: ` sys.modules `.
1819+ Example use::
18341820
1835- If *parents * is ``True `` then also create any missing parent modules.
1821+ # Get copies of the warnings module for testing without affecting the
1822+ # version being used by the rest of the test suite. One copy uses the
1823+ # C implementation, the other is forced to use the pure Python fallback
1824+ # implementation
1825+ py_warnings = import_fresh_module('warnings', blocked=['_warnings'])
1826+ c_warnings = import_fresh_module('warnings', fresh=['_warnings'])
18361827
1837- Return the new package.
1828+ .. versionadded :: 3.1
18381829
18391830
18401831.. function :: ensure_module_imported(name, *, clearnone=True)
@@ -1849,6 +1840,47 @@ The :mod:`!test.support.import_helper` module provides support for import tests.
18491840 would block the import.
18501841
18511842
1843+ Manipulating sys.path
1844+ ---------------------
1845+
1846+ .. class :: DirsOnSysPath(*paths)
1847+
1848+ A context manager to temporarily add directories to :data: `sys.path `.
1849+
1850+ This makes a copy of :data: `sys.path `, appends any directories given
1851+ as positional arguments, then reverts :data: `sys.path ` to the copied
1852+ settings when the context ends.
1853+
1854+ Note that *all * :data: `sys.path ` modifications in the body of the
1855+ context manager, including replacement of the object,
1856+ will be reverted at the end of the block.
1857+
1858+
1859+ .. function :: ready_to_import(name=None, source="")
1860+
1861+ A context manager that will create a new python module *name * with *source *
1862+ source code in a temporary directory and inserts this directory at the start
1863+ of :data: `sys.path `.
1864+
1865+ If the name matches an existing module, this will be cleared from :data: `sys.modules `
1866+ on entry and restored on exit.
1867+
1868+ Yields (name, path_to_script)
1869+
1870+
1871+ Manipulating internals
1872+ ----------------------
1873+
1874+ .. function :: make_legacy_pyc(source, allow_compile=False)
1875+
1876+ Move a :pep: `3147 `/:pep: `488 ` pyc file to its legacy pyc location and return the file
1877+ system path to the legacy pyc file. The *source * value is the file system
1878+ path to the source file. It does not need to exist, however the PEP
1879+ 3147/488 pyc file must exist or *allow_compile * must be set.
1880+
1881+ *allow_compile * will create a .pyc file if it does not exist.
1882+
1883+
18521884.. function :: frozen_modules(enabled=True)
18531885
18541886 A context manager that forces frozen modules to be used or excluded.
@@ -1857,12 +1889,6 @@ The :mod:`!test.support.import_helper` module provides support for import tests.
18571889 modules will always be imported frozen.
18581890
18591891
1860- .. function :: isolated_modules()
1861-
1862- A context manager that makes a copy of :data: `sys.modules ` on entry and restores
1863- it on exit.
1864-
1865-
18661892.. function :: multi_interp_extensions_check(enabled=True)
18671893
18681894 A context manager that forces (if ``True ``) or prevents legacy (single-phase init)
@@ -1873,17 +1899,8 @@ The :mod:`!test.support.import_helper` module provides support for import tests.
18731899 setting.
18741900
18751901
1876- .. function :: ready_to_import(name=None, source="")
1877-
1878- A context manager that will create a new python module *name * with *source *
1879- source code in a temporary directory and inserts this directory at the start
1880- of :data: `sys.path `.
1881-
1882- If the name already defines a module, this will be cleared from :data: `sys.modules `
1883- on entry and restored on exit.
1884-
1885- Yields (name, path_to_script)
1886-
1902+ Lazy imports
1903+ ------------
18871904
18881905.. function :: ensure_lazy_imports(imported_module, modules_to_block, *, additional_code=None)
18891906
0 commit comments