You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To preserve backwards compatibility with older versions of pickle where there were no opcodes to push bools onto the stack, the INT opcode has hardcoded logic to turn the payload I00\n into False and I01\n into True:
However, there's a slight difference in logic here. In pickle, it looks for the hardcoded values 00\n and 01\n. In _pickle (the accelerated version), it just checks that 3 bytes were provided and strtol() parses it out to 0 and 1. Therefore, payloads like I 0\n and I+0\n will be False in _pickle and 0 in pickle.
While 1/True and 0/False have identical behavior in most scenarios in Python, they are still technically different values and in some situations (like when using the type() function, or 1 is True) they will return different results. I know the situations in which this difference would matter is unlikely (probably why no one has said anything about it yet), but it can't hurt to align the two implementations.
I have attached a PR here that changes the logic in _pickle.c to check for the specific values 00 and 01.
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
To preserve backwards compatibility with older versions of pickle where there were no opcodes to push bools onto the stack, the
INT
opcode has hardcoded logic to turn the payloadI00\n
intoFalse
andI01\n
intoTrue
:cpython/Lib/pickle.py
Lines 1383 to 1389 in 8fdbbf8
cpython/Modules/_pickle.c
Lines 5258 to 5261 in 8fdbbf8
However, there's a slight difference in logic here. In
pickle
, it looks for the hardcoded values00\n
and01\n
. In_pickle
(the accelerated version), it just checks that 3 bytes were provided andstrtol()
parses it out to 0 and 1. Therefore, payloads likeI 0\n
andI+0\n
will beFalse
in_pickle
and0
inpickle
.While
1
/True
and0
/False
have identical behavior in most scenarios in Python, they are still technically different values and in some situations (like when using thetype()
function, or1 is True
) they will return different results. I know the situations in which this difference would matter is unlikely (probably why no one has said anything about it yet), but it can't hurt to align the two implementations.I have attached a PR here that changes the logic in
_pickle.c
to check for the specific values00
and01
.CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
The text was updated successfully, but these errors were encountered: