From bbf825eee764cae0bc44077ccc957a733d53d095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20S=C3=BAkup?= Date: Fri, 15 Nov 2019 08:52:35 +0100 Subject: Fix _imp module behaviour if is defined paths in find_spec call fixes #1896 --- setuptools/_imp.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'setuptools') diff --git a/setuptools/_imp.py b/setuptools/_imp.py index a3cce9b2..6ccec579 100644 --- a/setuptools/_imp.py +++ b/setuptools/_imp.py @@ -19,7 +19,10 @@ PY_FROZEN = 7 def find_module(module, paths=None): """Just like 'imp.find_module()', but with package support""" - spec = importlib.util.find_spec(module, paths) + if isinstance(paths, list): + spec = importlib.machinery.PathFinder().find_spec(module, paths) + else: + spec = importlib.util.find_spec(module, paths) if spec is None: raise ImportError("Can't find %s" % module) if not spec.has_location and hasattr(spec, 'submodule_search_locations'): @@ -60,14 +63,20 @@ def find_module(module, paths=None): def get_frozen_object(module, paths=None): - spec = importlib.util.find_spec(module, paths) + if isinstance(paths, list): + spec = importlib.machinery.PathFinder().find_spec(module, paths) + else: + spec = importlib.util.find_spec(module, paths) if not spec: raise ImportError("Can't find %s" % module) return spec.loader.get_code(module) def get_module(module, paths, info): - spec = importlib.util.find_spec(module, paths) + if isinstance(paths, list): + spec = importlib.machinery.PathFinder().find_spec(module, paths) + else: + spec = importlib.util.find_spec(module, paths) if not spec: raise ImportError("Can't find %s" % module) return module_from_spec(spec) -- cgit v1.2.1 From 7cfeee80514bbdcd91b5dfbb937a9848ba4b7cf3 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 19 Jan 2020 14:42:56 -0500 Subject: Extract 'find_spec' function to consolidate behavior. Ref #1905. --- setuptools/_imp.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'setuptools') diff --git a/setuptools/_imp.py b/setuptools/_imp.py index 6ccec579..451e45a8 100644 --- a/setuptools/_imp.py +++ b/setuptools/_imp.py @@ -17,12 +17,18 @@ C_BUILTIN = 6 PY_FROZEN = 7 +def find_spec(module, paths): + finder = ( + importlib.machinery.PathFinder().find_spec + if isinstance(paths, list) else + importlib.util.find_spec + ) + return finder(module, paths) + + def find_module(module, paths=None): """Just like 'imp.find_module()', but with package support""" - if isinstance(paths, list): - spec = importlib.machinery.PathFinder().find_spec(module, paths) - else: - spec = importlib.util.find_spec(module, paths) + spec = find_spec(module, paths) if spec is None: raise ImportError("Can't find %s" % module) if not spec.has_location and hasattr(spec, 'submodule_search_locations'): @@ -63,20 +69,14 @@ def find_module(module, paths=None): def get_frozen_object(module, paths=None): - if isinstance(paths, list): - spec = importlib.machinery.PathFinder().find_spec(module, paths) - else: - spec = importlib.util.find_spec(module, paths) + spec = find_spec(module, paths) if not spec: raise ImportError("Can't find %s" % module) return spec.loader.get_code(module) def get_module(module, paths, info): - if isinstance(paths, list): - spec = importlib.machinery.PathFinder().find_spec(module, paths) - else: - spec = importlib.util.find_spec(module, paths) + spec = find_spec(module, paths) if not spec: raise ImportError("Can't find %s" % module) return module_from_spec(spec) -- cgit v1.2.1 From 6b2490cd12e7e520b807ac35a084afd7bf3d912d Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 19 Jan 2020 14:55:21 -0500 Subject: Separate test for 'is_present' into its own test and add a TODO about this behavior being apparently unused. --- setuptools/tests/test_setuptools.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'setuptools') diff --git a/setuptools/tests/test_setuptools.py b/setuptools/tests/test_setuptools.py index 0da19b0e..bca69c30 100644 --- a/setuptools/tests/test_setuptools.py +++ b/setuptools/tests/test_setuptools.py @@ -108,6 +108,11 @@ class TestDepends: assert not req.is_present() assert not req.is_current() + @needs_bytecode + def test_require_present(self): + # In #1896, this test was failing for months with the only + # complaint coming from test runners (not end users). + # TODO: Evaluate if this code is needed at all. req = Require('Tests', None, 'tests', homepage="http://example.com") assert req.format is None assert req.attribute is None -- cgit v1.2.1