diff --git a/Lib/test/test_pickle.py b/Lib/test/test_pickle.py index e2384b33345a45..63f231bf8a3935 100644 --- a/Lib/test/test_pickle.py +++ b/Lib/test/test_pickle.py @@ -376,6 +376,16 @@ class CUnpicklerTests(PyUnpicklerTests): bad_stack_errors = (pickle.UnpicklingError,) truncated_errors = (pickle.UnpicklingError,) + def test_issue135241(self): + # C implementation should check for hardcoded values 00 and 01 + # when getting booleans from the INT opcode. Doing a str comparison + # to bypass truthy/falsy comparisons. These payloads should return + # 0, not False. + out1 = self.loads(b'I+0\n.') + self.assertTrue(str(out1) == str(0)) + out2 = self.loads(b'I 0\n.') + self.assertTrue(str(out2) == str(0)) + class CPicklingErrorTests(PyPicklingErrorTests): pickler = _pickle.Pickler diff --git a/Misc/NEWS.d/next/Library/2025-06-08-01-10-34.gh-issue-135241.5j18IW.rst b/Misc/NEWS.d/next/Library/2025-06-08-01-10-34.gh-issue-135241.5j18IW.rst new file mode 100644 index 00000000000000..34217db5dfab19 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-06-08-01-10-34.gh-issue-135241.5j18IW.rst @@ -0,0 +1 @@ +The :code:`INT` opcode of the C accelerator ``_pickle`` module was updated to look only for "00" and "01" to push booleans onto the stack, aligning with the Python :mod:`pickle` module. diff --git a/Modules/_pickle.c b/Modules/_pickle.c index 86d8b38620cb7f..712154b2d53f3a 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -5255,7 +5255,7 @@ load_int(PickleState *state, UnpicklerObject *self) } } else { - if (len == 3 && (x == 0 || x == 1)) { + if (len == 3 && s[0] == '0' && (s[1] == '0' || s[1] == '1')) { if ((value = PyBool_FromLong(x)) == NULL) return -1; }