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: 2 additions & 0 deletions Lib/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -1735,6 +1735,8 @@ def load_reduce(self):
stack = self.stack
args = stack.pop()
func = stack[-1]
if not isinstance(args, tuple):
raise TypeError("argument list must be a tuple")
stack[-1] = func(*args)
dispatch[REDUCE[0]] = load_reduce

Expand Down
1 change: 1 addition & 0 deletions Lib/test/pickletester.py
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,7 @@ def test_bad_reduce(self):
self.assertEqual(self.loads(b'cbuiltins\nint\n)R.'), 0)
self.check_unpickling_error(TypeError, b'N)R.')
self.check_unpickling_error(TypeError, b'cbuiltins\nint\nNR.')
self.check_unpickling_error(TypeError, b'cbuiltins\nint\nNR.')
Comment on lines 1437 to +1438
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain what this test adds?

Copy link
Author

Choose a reason for hiding this comment

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

NR. case ensures that when REDUCE receives a non-tuple args, pickle.load_reduce() raises TypeError consistently (matching _pickle) and prevents regressions in this validation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you look carefully at the surrounding code and explain what this test adds?

Copy link
Author

Choose a reason for hiding this comment

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

Surrounding tests already cover an invalid callable (N)R.) and a valid REDUCE call with tuple args (int + ()).
cbuiltins\nint\nNR. is different because the callable is valid, but the REDUCE args object is not a tuple (None), so it reaches load_reduce() and hits the new gh-144412 tuple check.
This confirms we raise TypeError at the correct point, consistent with _pickle.
It also acts as a regression test to ensure non-tuple REDUCE args aren’t accidentally accepted again.


def test_bad_newobj(self):
error = (pickle.UnpicklingError, TypeError)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``pickle.load_reduce()`` now requires a tuple for ``REDUCE`` arguments to
match ``_pickle``; non-tuple argument lists now raise ``TypeError``.
Loading