diff --git a/setuptools/tests/test_wheel.py b/setuptools/tests/test_wheel.py index 8b2faff607..934cf7f3ce 100644 --- a/setuptools/tests/test_wheel.py +++ b/setuptools/tests/test_wheel.py @@ -612,9 +612,17 @@ 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() + # 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 aab7ed0541..ff29e2fb09 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 @@ 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 @@ def tags(self): 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):