You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Issue #26186: Remove the restriction that built-in and extension
modules can't be lazily loaded.
Thanks to Python 3.6 allowing for types.ModuleType to have its
__class__ mutated, the restriction can be lifted by calling
create_module() on the wrapped loader.
If ``spec.loader.create_module()`` does not return ``None``, then any
1206
-
pre-existing attributes will not be reset. Also, no :exc:`AttributeError`
1207
-
will be raised if triggered while accessing **spec** or setting an attribute
1208
-
on the module.
1210
+
If :meth:`spec.loader.create_module <importlib.abc.Loader.create_module>`
1211
+
does not return ``None``, then any pre-existing attributes will not be reset.
1212
+
Also, no :exc:`AttributeError` will be raised if triggered while accessing
1213
+
**spec** or setting an attribute on the module.
1209
1214
1210
1215
This function is preferred over using :class:`types.ModuleType` to create a
1211
1216
new module as **spec** is used to set as many import-controlled attributes on
@@ -1267,7 +1272,8 @@ an :term:`importer`.
1267
1272
1268
1273
.. decorator:: set_package
1269
1274
1270
-
A :term:`decorator` for :meth:`importlib.abc.Loader.load_module` to set the :attr:`__package__` attribute on the returned module. If :attr:`__package__`
1275
+
A :term:`decorator` for :meth:`importlib.abc.Loader.load_module` to set the
1276
+
:attr:`__package__` attribute on the returned module. If :attr:`__package__`
1271
1277
is set and has a value other than ``None`` it will not be changed.
1272
1278
1273
1279
.. deprecated:: 3.4
@@ -1300,13 +1306,12 @@ an :term:`importer`.
1300
1306
This class **only** works with loaders that define
1301
1307
:meth:`~importlib.abc.Loader.exec_module` as control over what module type
1302
1308
is used for the module is required. For those same reasons, the loader's
1303
-
:meth:`~importlib.abc.Loader.create_module` method will be ignored (i.e., the
1304
-
loader's method should only return ``None``; this excludes
1305
-
:class:`BuiltinImporter` and :class:`ExtensionFileLoader`). Finally,
1306
-
modules which substitute the object placed into :attr:`sys.modules` will
1307
-
not work as there is no way to properly replace the module references
1308
-
throughout the interpreter safely; :exc:`ValueError` is raised if such a
1309
-
substitution is detected.
1309
+
:meth:`~importlib.abc.Loader.create_module` method must return ``None`` or a
1310
+
type for which its ``__class__`` attribute can be mutated along with not
1311
+
using :term:`slots <__slots__>`. Finally, modules which substitute the object
1312
+
placed into :attr:`sys.modules` will not work as there is no way to properly
1313
+
replace the module references throughout the interpreter safely;
1314
+
:exc:`ValueError` is raised if such a substitution is detected.
1310
1315
1311
1316
.. note::
1312
1317
For projects where startup time is critical, this class allows for
@@ -1317,6 +1322,11 @@ an :term:`importer`.
1317
1322
1318
1323
.. versionadded:: 3.5
1319
1324
1325
+
.. versionchanged:: 3.6
1326
+
Began calling :meth:`~importlib.abc.Loader.create_module`, removing the
1327
+
compatibility warning for :class:`importlib.machinery.BuiltinImporter` and
1328
+
:class:`importlib.machinery.ExtensionFileLoader`.
1329
+
1320
1330
.. classmethod:: factory(loader)
1321
1331
1322
1332
A static method which returns a callable that creates a lazy loader. This
Copy file name to clipboardExpand all lines: Doc/whatsnew/3.6.rst
+13Lines changed: 13 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -291,6 +291,16 @@ The idlelib package is being modernized and refactored to make IDLE look and wor
291
291
In compensation, the eventual result with be that some idlelib classes will be easier to use, with better APIs and docstrings explaining them. Additional useful information will be added to idlelib when available.
292
292
293
293
294
+
importlib
295
+
---------
296
+
297
+
:class:`importlib.util.LazyLoader` now calls
298
+
:meth:`~importlib.abc.Loader.create_module` on the wrapped loader, removing the
299
+
restriction that :class:`importlib.machinery.BuiltinImporter` and
300
+
:class:`importlib.machinery.ExtensionFileLoader` couldn't be used with
301
+
:class:`importlib.util.LazyLoader`.
302
+
303
+
294
304
os
295
305
--
296
306
@@ -620,6 +630,9 @@ that may require changes to your code.
620
630
Changes in the Python API
621
631
-------------------------
622
632
633
+
* When :meth:`importlib.abc.Loader.exec_module` is defined,
634
+
:meth:`importlib.abc.Loader.create_module` must also be defined.
635
+
623
636
* The format of the ``co_lnotab`` attribute of code objects changed to support
624
637
negative line number delta. By default, Python does not emit bytecode with
625
638
negative line number delta. Functions using ``frame.f_lineno``,
0 commit comments