summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_pkgutil.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2024-04-30 14:23:44 (GMT)
committerGitHub <noreply@github.com>2024-04-30 14:23:44 (GMT)
commitc0eaa232f63a62e0e0408911ab5f118dca2af607 (patch)
tree106f5a6167409cecf7fb0c165b9517ba194c1786 /Lib/test/test_pkgutil.py
parent3b268f4edc02b22257d745363b5cae199b6e5720 (diff)
downloadcpython-c0eaa232f63a62e0e0408911ab5f118dca2af607.zip
cpython-c0eaa232f63a62e0e0408911ab5f118dca2af607.tar.gz
cpython-c0eaa232f63a62e0e0408911ab5f118dca2af607.tar.bz2
gh-117860: Add tests for resolving names when import rebind names (GH-118176)
Add tests for "import", pkgutil.resolve_name() and unittest.mock.path() for cases when "import a.b as x" and "from a import b as x" give different results.
Diffstat (limited to 'Lib/test/test_pkgutil.py')
-rw-r--r--Lib/test/test_pkgutil.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py
index 6fcd726..e19dce1 100644
--- a/Lib/test/test_pkgutil.py
+++ b/Lib/test/test_pkgutil.py
@@ -12,6 +12,9 @@ import tempfile
import shutil
import zipfile
+from test.support.import_helper import DirsOnSysPath
+from test.test_importlib.util import uncache
+
# Note: pkgutil.walk_packages is currently tested in test_runpy. This is
# a hack to get a major issue resolved for 3.3b2. Longer term, it should
# be moved back here, perhaps by factoring out the helper code for
@@ -318,6 +321,38 @@ class PkgutilTests(unittest.TestCase):
with self.assertRaises(exc):
pkgutil.resolve_name(s)
+ def test_name_resolution_import_rebinding(self):
+ # The same data is also used for testing import in test_import and
+ # mock.patch in test_unittest.
+ path = os.path.join(os.path.dirname(__file__), 'test_import', 'data')
+ with uncache('package3', 'package3.submodule'), DirsOnSysPath(path):
+ self.assertEqual(pkgutil.resolve_name('package3.submodule.attr'), 'submodule')
+ with uncache('package3', 'package3.submodule'), DirsOnSysPath(path):
+ self.assertEqual(pkgutil.resolve_name('package3.submodule:attr'), 'submodule')
+ with uncache('package3', 'package3.submodule'), DirsOnSysPath(path):
+ self.assertEqual(pkgutil.resolve_name('package3:submodule.attr'), 'rebound')
+ self.assertEqual(pkgutil.resolve_name('package3.submodule.attr'), 'submodule')
+ self.assertEqual(pkgutil.resolve_name('package3:submodule.attr'), 'rebound')
+ with uncache('package3', 'package3.submodule'), DirsOnSysPath(path):
+ self.assertEqual(pkgutil.resolve_name('package3:submodule.attr'), 'rebound')
+ self.assertEqual(pkgutil.resolve_name('package3.submodule:attr'), 'submodule')
+ self.assertEqual(pkgutil.resolve_name('package3:submodule.attr'), 'rebound')
+
+ def test_name_resolution_import_rebinding2(self):
+ path = os.path.join(os.path.dirname(__file__), 'test_import', 'data')
+ with uncache('package4', 'package4.submodule'), DirsOnSysPath(path):
+ self.assertEqual(pkgutil.resolve_name('package4.submodule.attr'), 'submodule')
+ with uncache('package4', 'package4.submodule'), DirsOnSysPath(path):
+ self.assertEqual(pkgutil.resolve_name('package4.submodule:attr'), 'submodule')
+ with uncache('package4', 'package4.submodule'), DirsOnSysPath(path):
+ self.assertEqual(pkgutil.resolve_name('package4:submodule.attr'), 'origin')
+ self.assertEqual(pkgutil.resolve_name('package4.submodule.attr'), 'submodule')
+ self.assertEqual(pkgutil.resolve_name('package4:submodule.attr'), 'submodule')
+ with uncache('package4', 'package4.submodule'), DirsOnSysPath(path):
+ self.assertEqual(pkgutil.resolve_name('package4:submodule.attr'), 'origin')
+ self.assertEqual(pkgutil.resolve_name('package4.submodule:attr'), 'submodule')
+ self.assertEqual(pkgutil.resolve_name('package4:submodule.attr'), 'submodule')
+
class PkgutilPEP302Tests(unittest.TestCase):