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
6 changes: 1 addition & 5 deletions Lib/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -1623,11 +1623,7 @@ def load_dict(self):
def _instantiate(self, klass, args):
if (args or not isinstance(klass, type) or
hasattr(klass, "__getinitargs__")):
try:
value = klass(*args)
except TypeError as err:
raise TypeError("in constructor for %s: %s" %
(klass.__name__, str(err)), err.__traceback__)
value = klass(*args)
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
14 changes: 14 additions & 0 deletions Lib/test/pickletester.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,20 @@ 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 propagates unchanged. The pure-Python
# unpickler used to replace it with one that carried the traceback
# object in its args.
# 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)
self.assertEqual(cm.exception.args, ("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 @@
The pure-Python :mod:`pickle` unpickler no longer replaces a :exc:`TypeError`
raised by an old-style instance constructor with a new one carrying the
traceback object in its ``args``. The original error now propagates, as it
already did in the C implementation.
Loading