|
44 | 44 | SimplePath, |
45 | 45 | ) |
46 | 46 | from .compat import py311 |
| 47 | +from .compat.py314 import lazy_importer |
| 48 | + |
| 49 | +__lazy_modules__ = { |
| 50 | + 'csv', |
| 51 | + 'inspect', |
| 52 | + 'json', |
| 53 | + # 'zipfile' in the stdlib, which needs no overlay |
| 54 | + 'zipp.compat.overlay', |
| 55 | + f'{__name__}._adapters', |
| 56 | +} |
| 57 | +""" |
| 58 | +Imports to defer until first use (PEP 810). |
| 59 | +
|
| 60 | +Each of them costs more than the path that reads it is traveled, so |
| 61 | +deferring keeps that cost off every other caller. Stating it here rather |
| 62 | +than importing inside those functions leaves the dependencies visible. |
| 63 | +
|
| 64 | +Python 3.15 honors this declaration itself; earlier versions reach the |
| 65 | +same end through the context below. |
| 66 | +
|
| 67 | +Note that the adapters are imported by member and not as ``from . |
| 68 | +import _adapters``, which names this package as the module to defer |
| 69 | +and so would defer nothing, the package being mid-import. |
| 70 | +""" |
| 71 | + |
| 72 | +with lazy_importer(): |
| 73 | + import csv |
| 74 | + import inspect |
| 75 | + import json |
| 76 | + |
| 77 | + from zipp.compat.overlay import zipfile |
| 78 | + |
| 79 | + from ._adapters import Message |
47 | 80 |
|
48 | 81 | __all__ = [ |
49 | 82 | 'Distribution', |
@@ -598,10 +631,7 @@ def _has_metadata(self) -> bool: |
598 | 631 |
|
599 | 632 | @staticmethod |
600 | 633 | def _assemble_message(text: str) -> PackageMetadata: |
601 | | - # deferred for performance (python/cpython#109829) |
602 | | - from . import _adapters |
603 | | - |
604 | | - return _adapters.Message(email.message_from_string(text)) |
| 634 | + return Message(email.message_from_string(text)) |
605 | 635 |
|
606 | 636 | def _ensure_metadata_present(self, text: str | None) -> str: |
607 | 637 | if text is not None: |
@@ -662,10 +692,6 @@ def make_file(name, hash=None, size_str=None): |
662 | 692 |
|
663 | 693 | @pass_none |
664 | 694 | def make_files(lines): |
665 | | - # Delay csv import, since Distribution.files is not as widely used |
666 | | - # as other parts of importlib.metadata |
667 | | - import csv |
668 | | - |
669 | 695 | return starmap(make_file, csv.reader(lines)) |
670 | 696 |
|
671 | 697 | @pass_none |
@@ -787,9 +813,6 @@ def origin(self): |
787 | 813 | return self._load_json('direct_url.json') |
788 | 814 |
|
789 | 815 | def _load_json(self, filename): |
790 | | - # Deferred for performance (python/importlib_metadata#503) |
791 | | - import json |
792 | | - |
793 | 816 | return pass_none(json.loads)( |
794 | 817 | self.read_text(filename), |
795 | 818 | object_hook=lambda data: types.SimpleNamespace(**data), |
@@ -907,9 +930,6 @@ def children(self): |
907 | 930 | return [] |
908 | 931 |
|
909 | 932 | def zip_children(self): |
910 | | - # deferred for performance (python/importlib_metadata#502) |
911 | | - from zipp.compat.overlay import zipfile |
912 | | - |
913 | 933 | zip_path = zipfile.Path(self.root) |
914 | 934 | names = zip_path.root.namelist() |
915 | 935 | self.joinpath = zip_path.joinpath |
@@ -1296,9 +1316,6 @@ def _get_toplevel_name(name: IPackagePath) -> str: |
1296 | 1316 | >>> _get_toplevel_name(PackagePath('foo.dist-info')) |
1297 | 1317 | 'foo.dist-info' |
1298 | 1318 | """ |
1299 | | - # Defer import of inspect for performance (python/cpython#118761) |
1300 | | - import inspect |
1301 | | - |
1302 | 1319 | return _topmost(name) or inspect.getmodulename(name) or str(name) |
1303 | 1320 |
|
1304 | 1321 |
|
|
0 commit comments