From 2732a2221f2940be93db95104ad08bd8ab0114c0 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Wed, 30 Apr 2025 21:11:26 +0400 Subject: [PATCH 1/2] Fix separated running of `pickle` tests --- Lib/test/pickletester.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py index bdc7ef62943a28..0293411b067161 100644 --- a/Lib/test/pickletester.py +++ b/Lib/test/pickletester.py @@ -2289,10 +2289,10 @@ def test_nested_lookup_error(self): with self.subTest(proto=proto): with self.assertRaises(pickle.PicklingError) as cm: self.dumps(obj, proto) - self.assertEqual(str(cm.exception), - f"Can't pickle {obj!r}: it's not found as __main__.AbstractPickleTests.spam") - self.assertEqual(str(cm.exception.__context__), - "module '__main__' has no attribute 'AbstractPickleTests'") + self.assertEqual(str(cm.exception), + f"Can't pickle {obj!r}: it's not found as __main__.AbstractPickleTests.spam") + self.assertEqual(str(cm.exception.__context__), + "module '__main__' has no attribute 'AbstractPickleTests'") def test_wrong_object_lookup_error(self): # Name is bound to different object @@ -2312,10 +2312,10 @@ def test_wrong_object_lookup_error(self): with self.subTest(proto=proto): with self.assertRaises(pickle.PicklingError) as cm: self.dumps(obj, proto) - self.assertEqual(str(cm.exception), - f"Can't pickle {obj!r}: it's not found as __main__.AbstractPickleTests") - self.assertEqual(str(cm.exception.__context__), - "module '__main__' has no attribute 'AbstractPickleTests'") + self.assertEqual(str(cm.exception), + f"Can't pickle {obj!r}: it's not found as __main__.AbstractPickleTests") + self.assertEqual(str(cm.exception.__context__), + "module '__main__' has no attribute 'AbstractPickleTests'") def test_local_lookup_error(self): # Test that whichmodule() errors out cleanly when looking up From 45d670cee4d4bb7b12bd4cd086575c8c48b23220 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Sat, 3 May 2025 20:39:19 +0400 Subject: [PATCH 2/2] Correct fix --- Lib/test/pickletester.py | 43 ++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py index 0293411b067161..4609ebc10672d2 100644 --- a/Lib/test/pickletester.py +++ b/Lib/test/pickletester.py @@ -15,6 +15,7 @@ import types import unittest import weakref +from contextlib import contextmanager from textwrap import dedent from http.cookies import SimpleCookie @@ -152,6 +153,17 @@ def __getinitargs__(self): __main__.E = E E.__module__ = "__main__" +@contextmanager +def fake_main_module(): + real_main = sys.modules.get("__main__") + fake_main = types.ModuleType("__main__") + + sys.modules["__main__"] = fake_main + try: + yield + finally: + sys.modules["__main__"] = real_main + # Simple mutable object. class Object: pass @@ -2287,12 +2299,14 @@ def test_nested_lookup_error(self): obj.__module__ = None for proto in protocols: with self.subTest(proto=proto): - with self.assertRaises(pickle.PicklingError) as cm: - self.dumps(obj, proto) - self.assertEqual(str(cm.exception), - f"Can't pickle {obj!r}: it's not found as __main__.AbstractPickleTests.spam") - self.assertEqual(str(cm.exception.__context__), - "module '__main__' has no attribute 'AbstractPickleTests'") + with fake_main_module(): + with self.assertRaises(pickle.PicklingError) as cm: + self.dumps(obj, proto) + self.assertEqual(str(cm.exception), + f"Can't pickle {obj!r}: " + "it's not found as __main__.AbstractPickleTests.spam") + self.assertEqual(str(cm.exception.__context__), + "module '__main__' has no attribute 'AbstractPickleTests'") def test_wrong_object_lookup_error(self): # Name is bound to different object @@ -2304,18 +2318,21 @@ def test_wrong_object_lookup_error(self): with self.assertRaises(pickle.PicklingError) as cm: self.dumps(obj, proto) 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__}.AbstractPickleTests") self.assertIsNone(cm.exception.__context__) obj.__module__ = None for proto in protocols: with self.subTest(proto=proto): - with self.assertRaises(pickle.PicklingError) as cm: - self.dumps(obj, proto) - self.assertEqual(str(cm.exception), - f"Can't pickle {obj!r}: it's not found as __main__.AbstractPickleTests") - self.assertEqual(str(cm.exception.__context__), - "module '__main__' has no attribute 'AbstractPickleTests'") + with fake_main_module(): + with self.assertRaises(pickle.PicklingError) as cm: + self.dumps(obj, proto) + self.assertEqual(str(cm.exception), + f"Can't pickle {obj!r}: " + "it's not found as __main__.AbstractPickleTests") + self.assertEqual(str(cm.exception.__context__), + "module '__main__' has no attribute 'AbstractPickleTests'") def test_local_lookup_error(self): # Test that whichmodule() errors out cleanly when looking up