From c82d58bfd5c9f58a4871f4e48a19d691ee099d2c Mon Sep 17 00:00:00 2001 From: Maurits van Rees Date: Tue, 31 Jan 2023 17:03:48 +0100 Subject: [PATCH] Cache supported tags for wheels. This fixes https://github.com/pypa/setuptools/issues/3804 --- changelog.d/3804.change.rst | 1 + setuptools/tests/test_wheel.py | 1 + setuptools/wheel.py | 15 +++++++++++---- 3 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 changelog.d/3804.change.rst diff --git a/changelog.d/3804.change.rst b/changelog.d/3804.change.rst new file mode 100644 index 00000000000..86a6597c230 --- /dev/null +++ b/changelog.d/3804.change.rst @@ -0,0 +1 @@ +Cache supported tags for wheels. diff --git a/setuptools/tests/test_wheel.py b/setuptools/tests/test_wheel.py index b2bbdfae7ff..8b2faff6079 100644 --- a/setuptools/tests/test_wheel.py +++ b/setuptools/tests/test_wheel.py @@ -612,6 +612,7 @@ def sys_tags(): for t in parse_tag('cp36-cp36m-manylinux1_x86_64'): yield t monkeypatch.setattr('setuptools.wheel.sys_tags', sys_tags) + monkeypatch.setattr('setuptools.wheel._supported_tags', None) assert Wheel( 'onnxruntime-0.1.2-cp36-cp36m-manylinux1_x86_64.whl').is_compatible() diff --git a/setuptools/wheel.py b/setuptools/wheel.py index 527ed3b2330..c607487c5bf 100644 --- a/setuptools/wheel.py +++ b/setuptools/wheel.py @@ -28,6 +28,8 @@ NAMESPACE_PACKAGE_INIT = \ "__import__('pkg_resources').declare_namespace(__name__)\n" +_supported_tags = None + def unpack(src_dir, dst_dir): '''Move everything under `src_dir` to `dst_dir`, and delete the former.''' @@ -83,10 +85,15 @@ def tags(self): ) def is_compatible(self): - '''Is the wheel is compatible with the current platform?''' - supported_tags = set( - (t.interpreter, t.abi, t.platform) for t in sys_tags()) - return next((True for t in self.tags() if t in supported_tags), False) + '''Is the wheel compatible with the current platform?''' + global _supported_tags + if _supported_tags is None: + # We calculate the supported tags only once, otherwise calling + # this method on thousands of wheels takes seconds instead of + # milliseconds. + _supported_tags = set( + (t.interpreter, t.abi, t.platform) for t in sys_tags()) + return next((True for t in self.tags() if t in _supported_tags), False) def egg_name(self): return pkg_resources.Distribution(