summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaurits van Rees <maurits@vanrees.org>2023-03-07 22:36:16 +0100
committerMaurits van Rees <maurits@vanrees.org>2023-03-07 22:43:50 +0100
commit8af6bd4818558e4e4427e730ede8bc7ba17ca000 (patch)
treec7c0a356fea1919b544c22d50accc815be1db857
parent2c234499777a5d3f5a213fbfc42b289c207c411b (diff)
downloadpython-setuptools-git-8af6bd4818558e4e4427e730ede8bc7ba17ca000.tar.gz
Use functools.lru_cache to cache supported tags for wheels.
This is a suggestion by @abravalheri for my PR. https://github.com/pypa/setuptools/pull/3805#issuecomment-1434361907
-rw-r--r--setuptools/tests/test_wheel.py14
-rw-r--r--setuptools/wheel.py17
2 files changed, 20 insertions, 11 deletions
diff --git a/setuptools/tests/test_wheel.py b/setuptools/tests/test_wheel.py
index 8b2faff6..934cf7f3 100644
--- a/setuptools/tests/test_wheel.py
+++ b/setuptools/tests/test_wheel.py
@@ -612,9 +612,17 @@ def test_wheel_is_compatible(monkeypatch):
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()
+ # Clear the supported tags cache, otherwise the sys_tags monkeypatch
+ # has no effect.
+ setuptools.wheel._supported_tags.cache_clear()
+ try:
+ assert Wheel(
+ 'onnxruntime-0.1.2-cp36-cp36m-manylinux1_x86_64.whl'
+ ).is_compatible()
+ finally:
+ # Clear the cache again, otherwise the sys_tags monkeypatch
+ # is still in effect for the rest of the tests.
+ setuptools.wheel._supported_tags.cache_clear()
def test_wheel_mode():
diff --git a/setuptools/wheel.py b/setuptools/wheel.py
index aab7ed05..ff29e2fb 100644
--- a/setuptools/wheel.py
+++ b/setuptools/wheel.py
@@ -2,6 +2,7 @@
import email
import itertools
+import functools
import os
import posixpath
import re
@@ -27,7 +28,13 @@ WHEEL_NAME = re.compile(
NAMESPACE_PACKAGE_INIT = \
"__import__('pkg_resources').declare_namespace(__name__)\n"
-_supported_tags = None
+
+@functools.lru_cache(maxsize=None)
+def _get_supported_tags():
+ # We calculate the supported tags only once, otherwise calling
+ # this method on thousands of wheels takes seconds instead of
+ # milliseconds.
+ return set((t.interpreter, t.abi, t.platform) for t in sys_tags())
def unpack(src_dir, dst_dir):
@@ -85,13 +92,7 @@ class Wheel:
def is_compatible(self):
'''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())
+ _supported_tags = _get_supported_tags()
return next((True for t in self.tags() if t in _supported_tags), False)
def egg_name(self):