FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-131031: Fix separated running of `pickle` tests by donbarbos · Pull Request #133218 · python/cpython · GitHub

/ cpython Public
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
31 changes: 24 additions & 7 deletions Lib/test/pickletester.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import types
import unittest
import weakref
from contextlib import contextmanager
from textwrap import dedent
from http.cookies import SimpleCookie

Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

You can use support.swap_item(). It is already used in other tests to fake a module.

try:
yield
finally:
sys.modules["__main__"] = real_main

# Simple mutable object.
class Object:
pass
Expand Down Expand Up @@ -2287,10 +2299,12 @@ 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)
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")
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'")

Expand All @@ -2304,16 +2318,19 @@ 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)
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")
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'")

Expand Down

Back | FazBrowse Home | New Git URL