mirror of https://github.com/python/cpython.git
gh-131031: Fix test_pickle when invoked directly (GH-133356)
This commit is contained in:
parent
bfac7d2edc
commit
e15bbfafbc
|
@ -2272,7 +2272,11 @@ def test_nonencodable_module_name_error(self):
|
||||||
|
|
||||||
def test_nested_lookup_error(self):
|
def test_nested_lookup_error(self):
|
||||||
# Nested name does not exist
|
# Nested name does not exist
|
||||||
obj = REX('AbstractPickleTests.spam')
|
global TestGlobal
|
||||||
|
class TestGlobal:
|
||||||
|
class A:
|
||||||
|
pass
|
||||||
|
obj = REX('TestGlobal.A.B.C')
|
||||||
obj.__module__ = __name__
|
obj.__module__ = __name__
|
||||||
for proto in protocols:
|
for proto in protocols:
|
||||||
with self.subTest(proto=proto):
|
with self.subTest(proto=proto):
|
||||||
|
@ -2280,9 +2284,9 @@ def test_nested_lookup_error(self):
|
||||||
self.dumps(obj, proto)
|
self.dumps(obj, proto)
|
||||||
self.assertEqual(str(cm.exception),
|
self.assertEqual(str(cm.exception),
|
||||||
f"Can't pickle {obj!r}: "
|
f"Can't pickle {obj!r}: "
|
||||||
f"it's not found as {__name__}.AbstractPickleTests.spam")
|
f"it's not found as {__name__}.TestGlobal.A.B.C")
|
||||||
self.assertEqual(str(cm.exception.__context__),
|
self.assertEqual(str(cm.exception.__context__),
|
||||||
"type object 'AbstractPickleTests' has no attribute 'spam'")
|
"type object 'A' has no attribute 'B'")
|
||||||
|
|
||||||
obj.__module__ = None
|
obj.__module__ = None
|
||||||
for proto in protocols:
|
for proto in protocols:
|
||||||
|
@ -2290,21 +2294,25 @@ def test_nested_lookup_error(self):
|
||||||
with self.assertRaises(pickle.PicklingError) as cm:
|
with self.assertRaises(pickle.PicklingError) as cm:
|
||||||
self.dumps(obj, proto)
|
self.dumps(obj, proto)
|
||||||
self.assertEqual(str(cm.exception),
|
self.assertEqual(str(cm.exception),
|
||||||
f"Can't pickle {obj!r}: it's not found as __main__.AbstractPickleTests.spam")
|
f"Can't pickle {obj!r}: "
|
||||||
|
f"it's not found as __main__.TestGlobal.A.B.C")
|
||||||
self.assertEqual(str(cm.exception.__context__),
|
self.assertEqual(str(cm.exception.__context__),
|
||||||
"module '__main__' has no attribute 'AbstractPickleTests'")
|
"module '__main__' has no attribute 'TestGlobal'")
|
||||||
|
|
||||||
def test_wrong_object_lookup_error(self):
|
def test_wrong_object_lookup_error(self):
|
||||||
# Name is bound to different object
|
# Name is bound to different object
|
||||||
obj = REX('AbstractPickleTests')
|
global TestGlobal
|
||||||
|
class TestGlobal:
|
||||||
|
pass
|
||||||
|
obj = REX('TestGlobal')
|
||||||
obj.__module__ = __name__
|
obj.__module__ = __name__
|
||||||
AbstractPickleTests.ham = []
|
|
||||||
for proto in protocols:
|
for proto in protocols:
|
||||||
with self.subTest(proto=proto):
|
with self.subTest(proto=proto):
|
||||||
with self.assertRaises(pickle.PicklingError) as cm:
|
with self.assertRaises(pickle.PicklingError) as cm:
|
||||||
self.dumps(obj, proto)
|
self.dumps(obj, proto)
|
||||||
self.assertEqual(str(cm.exception),
|
self.assertEqual(str(cm.exception),
|
||||||
f"Can't pickle {obj!r}: it's not the same object as {__name__}.AbstractPickleTests")
|
f"Can't pickle {obj!r}: "
|
||||||
|
f"it's not the same object as {__name__}.TestGlobal")
|
||||||
self.assertIsNone(cm.exception.__context__)
|
self.assertIsNone(cm.exception.__context__)
|
||||||
|
|
||||||
obj.__module__ = None
|
obj.__module__ = None
|
||||||
|
@ -2313,9 +2321,10 @@ def test_wrong_object_lookup_error(self):
|
||||||
with self.assertRaises(pickle.PicklingError) as cm:
|
with self.assertRaises(pickle.PicklingError) as cm:
|
||||||
self.dumps(obj, proto)
|
self.dumps(obj, proto)
|
||||||
self.assertEqual(str(cm.exception),
|
self.assertEqual(str(cm.exception),
|
||||||
f"Can't pickle {obj!r}: it's not found as __main__.AbstractPickleTests")
|
f"Can't pickle {obj!r}: "
|
||||||
|
f"it's not found as __main__.TestGlobal")
|
||||||
self.assertEqual(str(cm.exception.__context__),
|
self.assertEqual(str(cm.exception.__context__),
|
||||||
"module '__main__' has no attribute 'AbstractPickleTests'")
|
"module '__main__' has no attribute 'TestGlobal'")
|
||||||
|
|
||||||
def test_local_lookup_error(self):
|
def test_local_lookup_error(self):
|
||||||
# Test that whichmodule() errors out cleanly when looking up
|
# Test that whichmodule() errors out cleanly when looking up
|
||||||
|
|
Loading…
Reference in New Issue