Skip to content

Commit 4657d7e

Browse files
author
Rémi Lapeyre
committed
Use __reduce_ex__ and __getnewargs_ex__
1 parent 6c906da commit 4657d7e

File tree

2 files changed

+369
-36
lines changed

2 files changed

+369
-36
lines changed

Lib/test/test_exceptions.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -439,16 +439,15 @@ def testAttributes(self):
439439
value, expected[checkArgName]))
440440

441441
# test for pickling support
442-
for p in [pickle]:
443-
for protocol in range(p.HIGHEST_PROTOCOL + 1):
444-
s = p.dumps(e, protocol)
445-
new = p.loads(s)
446-
for checkArgName in expected:
447-
got = repr(getattr(new, checkArgName))
448-
want = repr(expected[checkArgName])
449-
self.assertEqual(got, want,
450-
'pickled "%r", attribute "%s' %
451-
(e, checkArgName))
442+
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
443+
s = pickle.dumps(e, protocol)
444+
new = pickle.loads(s)
445+
for checkArgName in expected:
446+
got = repr(getattr(new, checkArgName))
447+
want = repr(expected[checkArgName])
448+
self.assertEqual(got, want,
449+
'pickled "%r", attribute "%s"' %
450+
(e, checkArgName))
452451

453452
def testWithTraceback(self):
454453
try:
@@ -1417,8 +1416,14 @@ def test_pickle_overriden_init(self):
14171416
# Issue #27015
14181417
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
14191418
orig = NaiveException(x='foo')
1420-
exc = pickle.loads(pickle.dumps(orig, proto))
1421-
self.assertEqual(orig.x, exc.x)
1419+
if proto in (0, 1):
1420+
# Pickling excpetions keyword arguments is not supported for
1421+
# protocol 0 and 1
1422+
with self.assertRaises(TypeError):
1423+
pickle.loads(pickle.dumps(orig, proto))
1424+
else:
1425+
exc = pickle.loads(pickle.dumps(orig, proto))
1426+
self.assertEqual(orig.x, exc.x)
14221427

14231428

14241429
if __name__ == '__main__':

0 commit comments

Comments
 (0)