diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-03-31 13:49:26 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-03-31 13:49:26 (GMT) |
commit | b9100e5d9db64c727f2de579f143853eee4b0ba4 (patch) | |
tree | 9d6b74b8c6a026a3ee107512fb3caa7f933f4cc4 | |
parent | bfe1824d08fb107768ce17dd4de6c7f879935098 (diff) | |
download | cpython-b9100e5d9db64c727f2de579f143853eee4b0ba4.zip cpython-b9100e5d9db64c727f2de579f143853eee4b0ba4.tar.gz cpython-b9100e5d9db64c727f2de579f143853eee4b0ba4.tar.bz2 |
Issue #18473: Fixed pickle compatibility tests for optional modules.
Added WindowsError to compatibility mappings.
-rw-r--r-- | Lib/_compat_pickle.py | 7 | ||||
-rw-r--r-- | Lib/test/test_pickle.py | 35 |
2 files changed, 32 insertions, 10 deletions
diff --git a/Lib/_compat_pickle.py b/Lib/_compat_pickle.py index b1f15a7..6e39d4a 100644 --- a/Lib/_compat_pickle.py +++ b/Lib/_compat_pickle.py @@ -141,6 +141,13 @@ PYTHON2_EXCEPTIONS = ( "ZeroDivisionError", ) +try: + WindowsError +except NameError: + pass +else: + PYTHON2_EXCEPTIONS += ("WindowsError",) + for excname in PYTHON2_EXCEPTIONS: NAME_MAPPING[("exceptions", excname)] = ("builtins", excname) diff --git a/Lib/test/test_pickle.py b/Lib/test/test_pickle.py index 0159b18..f5add27 100644 --- a/Lib/test/test_pickle.py +++ b/Lib/test/test_pickle.py @@ -240,7 +240,16 @@ def getmodule(module): try: return sys.modules[module] except KeyError: - __import__(module) + try: + __import__(module) + except AttributeError as exc: + if support.verbose: + print("Can't import module %r: %s" % (module, exc)) + raise ImportError + except ImportError as exc: + if support.verbose: + print(exc) + raise return sys.modules[module] def getattribute(module, name): @@ -264,18 +273,16 @@ class CompatPickleTests(unittest.TestCase): for module in modules: try: getmodule(module) - except ImportError as exc: - if support.verbose: - print(exc) + except ImportError: + pass def test_import_mapping(self): for module3, module2 in REVERSE_IMPORT_MAPPING.items(): with self.subTest((module3, module2)): try: getmodule(module3) - except ImportError as exc: - if support.verbose: - print(exc) + except ImportError: + pass if module3[:1] != '_': self.assertIn(module2, IMPORT_MAPPING) self.assertEqual(IMPORT_MAPPING[module2], module3) @@ -283,14 +290,19 @@ class CompatPickleTests(unittest.TestCase): def test_name_mapping(self): for (module3, name3), (module2, name2) in REVERSE_NAME_MAPPING.items(): with self.subTest(((module3, name3), (module2, name2))): - attr = getattribute(module3, name3) if (module2, name2) == ('exceptions', 'OSError'): + attr = getattribute(module3, name3) self.assertTrue(issubclass(attr, OSError)) else: module, name = mapping(module2, name2) if module3[:1] != '_': self.assertEqual((module, name), (module3, name3)) - self.assertEqual(getattribute(module, name), attr) + try: + attr = getattribute(module3, name3) + except ImportError: + pass + else: + self.assertEqual(getattribute(module, name), attr) def test_reverse_import_mapping(self): for module2, module3 in IMPORT_MAPPING.items(): @@ -315,7 +327,10 @@ class CompatPickleTests(unittest.TestCase): def test_reverse_name_mapping(self): for (module2, name2), (module3, name3) in NAME_MAPPING.items(): with self.subTest(((module2, name2), (module3, name3))): - attr = getattribute(module3, name3) + try: + attr = getattribute(module3, name3) + except ImportError: + pass module, name = reverse_mapping(module3, name3) if (module2, name2, module3, name3) not in ALT_NAME_MAPPING: self.assertEqual((module, name), (module2, name2)) |