diff --git a/Lib/pickle.py b/Lib/pickle.py index beaefae0479d3c..a17c83b720d23a 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -1837,7 +1837,7 @@ def load_build(self): slotstate = None if isinstance(state, tuple) and len(state) == 2: state, slotstate = state - if state: + if state is not None: inst_dict = inst.__dict__ intern = sys.intern for k, v in state.items(): diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py index 9a3a26a8400844..8d4ca2dcf65e45 100644 --- a/Lib/test/pickletester.py +++ b/Lib/test/pickletester.py @@ -1024,6 +1024,14 @@ def test_zero_padded_integers(self): self.assertEqual(self.loads(b'L010L\n.'), 10) self.assertEqual(self.loads(b'L-010L\n.'), -10) + def test_load_build_falsy_state(self): + self.check_unpickling_error((pickle.UnpicklingError, AttributeError), b']]b.') + self.check_unpickling_error((pickle.UnpicklingError, AttributeError), b']K\x00b.') + self.check_unpickling_error((pickle.UnpicklingError, AttributeError), b'])b.') + self.check_unpickling_error((pickle.UnpicklingError, AttributeError), b']}b.') + self.check_unpickling_error((pickle.UnpicklingError, AttributeError), b']\x8fb.') + self.check_unpickling_error((pickle.UnpicklingError, AttributeError), b']\x89b.') + def test_nondecimal_integers(self): self.assertRaises(ValueError, self.loads, b'I0b10\n.') self.assertRaises(ValueError, self.loads, b'I0o10\n.') diff --git a/Misc/NEWS.d/next/Library/2025-01-22-00-53-23.gh-issue-128965._eQS3q.rst b/Misc/NEWS.d/next/Library/2025-01-22-00-53-23.gh-issue-128965._eQS3q.rst new file mode 100644 index 00000000000000..bac259c072e4ba --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-01-22-00-53-23.gh-issue-128965._eQS3q.rst @@ -0,0 +1 @@ +Fix :meth:`!pickle._Unpickler.load_build` to detect an invalid ``inst`` argument if ``state`` is falsey but not None.