Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use __reduce_ex__ and __getnewargs_ex__
  • Loading branch information
Rémi Lapeyre committed Feb 21, 2019
commit 4657d7e06a334e7ceb9cffbd7139e1b403d3871a
29 changes: 17 additions & 12 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,16 +439,15 @@ def testAttributes(self):
value, expected[checkArgName]))

# test for pickling support
for p in [pickle]:
for protocol in range(p.HIGHEST_PROTOCOL + 1):
s = p.dumps(e, protocol)
new = p.loads(s)
for checkArgName in expected:
got = repr(getattr(new, checkArgName))
want = repr(expected[checkArgName])
self.assertEqual(got, want,
'pickled "%r", attribute "%s' %
(e, checkArgName))
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
s = pickle.dumps(e, protocol)
new = pickle.loads(s)
for checkArgName in expected:
got = repr(getattr(new, checkArgName))
want = repr(expected[checkArgName])
self.assertEqual(got, want,
'pickled "%r", attribute "%s"' %
(e, checkArgName))

def testWithTraceback(self):
try:
Expand Down Expand Up @@ -1417,8 +1416,14 @@ def test_pickle_overriden_init(self):
# Issue #27015
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
orig = NaiveException(x='foo')
exc = pickle.loads(pickle.dumps(orig, proto))
self.assertEqual(orig.x, exc.x)
if proto in (0, 1):
# Pickling excpetions keyword arguments is not supported for
# protocol 0 and 1
with self.assertRaises(TypeError):
pickle.loads(pickle.dumps(orig, proto))
else:
exc = pickle.loads(pickle.dumps(orig, proto))
self.assertEqual(orig.x, exc.x)


if __name__ == '__main__':
Expand Down
Loading