Skip to content

Commit 8af6bd4

Browse files
Use functools.lru_cache to cache supported tags for wheels.
This is a suggestion by @abravalheri for my PR. #3805 (comment)
1 parent 2c23449 commit 8af6bd4

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

setuptools/tests/test_wheel.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,17 @@ def sys_tags():
612612
for t in parse_tag('cp36-cp36m-manylinux1_x86_64'):
613613
yield t
614614
monkeypatch.setattr('setuptools.wheel.sys_tags', sys_tags)
615-
monkeypatch.setattr('setuptools.wheel._supported_tags', None)
616-
assert Wheel(
617-
'onnxruntime-0.1.2-cp36-cp36m-manylinux1_x86_64.whl').is_compatible()
615+
# Clear the supported tags cache, otherwise the sys_tags monkeypatch
616+
# has no effect.
617+
setuptools.wheel._supported_tags.cache_clear()
618+
try:
619+
assert Wheel(
620+
'onnxruntime-0.1.2-cp36-cp36m-manylinux1_x86_64.whl'
621+
).is_compatible()
622+
finally:
623+
# Clear the cache again, otherwise the sys_tags monkeypatch
624+
# is still in effect for the rest of the tests.
625+
setuptools.wheel._supported_tags.cache_clear()
618626

619627

620628
def test_wheel_mode():

setuptools/wheel.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import email
44
import itertools
5+
import functools
56
import os
67
import posixpath
78
import re
@@ -27,7 +28,13 @@
2728
NAMESPACE_PACKAGE_INIT = \
2829
"__import__('pkg_resources').declare_namespace(__name__)\n"
2930

30-
_supported_tags = None
31+
32+
@functools.lru_cache(maxsize=None)
33+
def _get_supported_tags():
34+
# We calculate the supported tags only once, otherwise calling
35+
# this method on thousands of wheels takes seconds instead of
36+
# milliseconds.
37+
return set((t.interpreter, t.abi, t.platform) for t in sys_tags())
3138

3239

3340
def unpack(src_dir, dst_dir):
@@ -85,13 +92,7 @@ def tags(self):
8592

8693
def is_compatible(self):
8794
'''Is the wheel compatible with the current platform?'''
88-
global _supported_tags
89-
if _supported_tags is None:
90-
# We calculate the supported tags only once, otherwise calling
91-
# this method on thousands of wheels takes seconds instead of
92-
# milliseconds.
93-
_supported_tags = set(
94-
(t.interpreter, t.abi, t.platform) for t in sys_tags())
95+
_supported_tags = _get_supported_tags()
9596
return next((True for t in self.tags() if t in _supported_tags), False)
9697

9798
def egg_name(self):

0 commit comments

Comments
 (0)