Issue #18473: Fixed pickle compatibility tests for optional modules.

Added WindowsError to compatibility mappings.
This commit is contained in:
Serhiy Storchaka 2015-03-31 16:49:26 +03:00
parent bfe1824d08
commit b9100e5d9d
2 changed files with 32 additions and 10 deletions

View File

@ -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)

View File

@ -240,7 +240,16 @@ def getmodule(module):
try:
return sys.modules[module]
except KeyError:
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,13 +290,18 @@ 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))
try:
attr = getattribute(module3, name3)
except ImportError:
pass
else:
self.assertEqual(getattribute(module, name), attr)
def test_reverse_import_mapping(self):
@ -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))):
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))