Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Lib/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,7 @@ def _instantiate(self, klass, args):
value = klass(*args)
except TypeError as err:
raise TypeError("in constructor for %s: %s" %
(klass.__name__, str(err)), err.__traceback__)
(klass.__name__, str(err))) from err
else:
value = klass.__new__(klass)
self.append(value)
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/picklecommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ class E(C):
def __getinitargs__(self):
return ()

# For test_load_bad_constructor
class BadConstructor:
def __init__(self, *args):
raise TypeError("bad constructor")

import __main__
__main__.C = C
C.__module__ = "__main__"
__main__.D = D
D.__module__ = "__main__"
__main__.E = E
E.__module__ = "__main__"
__main__.BadConstructor = BadConstructor
BadConstructor.__module__ = "__main__"

# Simple mutable object.
class Object(object):
Expand Down
24 changes: 24 additions & 0 deletions Lib/test/pickletester.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,30 @@ def test_load_classic_instance(self):
b'q\x00oq\x01}q\x02b.').replace(b'X', xname)
self.assert_is_copy(X(*args), self.loads(pickle2))

def test_load_bad_constructor(self):
# gh-154002: a TypeError raised by an old-style instance constructor
# during INST/OBJ unpickling must be reported with proper exception
# chaining, not with the traceback object stored in the exception's
# args (which used to happen in the pure-Python unpickler).
# 0: ( MARK
# 1: I INT 1
# 4: i INST '__main__ BadConstructor' (MARK at 0)
# 28: . STOP
data = b'(I1\ni__main__\nBadConstructor\n.'
with self.assertRaises(TypeError) as cm:
self.loads(data)
exc = cm.exception
# A traceback object must never leak into the exception's args.
self.assertNotIn(types.TracebackType, [type(a) for a in exc.args])
# Only the pure-Python unpickler wraps the failure; the C one lets the
# original TypeError propagate. When it wraps, it must chain the cause.
if str(exc).startswith("in constructor for "):
self.assertEqual(
exc.args,
("in constructor for BadConstructor: bad constructor",))
self.assertIsInstance(exc.__cause__, TypeError)
self.assertEqual(str(exc.__cause__), "bad constructor")

def test_maxint64(self):
maxint64 = (1 << 63) - 1
data = b'I' + str(maxint64).encode("ascii") + b'\n.'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix exception handling in the pure-Python :mod:`pickle` unpickler when a
class constructor raises :exc:`TypeError` while unpickling an old-style
instance. The original error is now chained with ``from`` instead of having
its traceback object stored in the raised exception's ``args``.
Loading