gh-128965: pickle load_build function checks if state is None, not False#128966
gh-128965: pickle load_build function checks if state is None, not False#128966Legoclones wants to merge 8 commits intopython:mainfrom
load_build function checks if state is None, not False#128966Conversation
picnixz
left a comment
There was a problem hiding this comment.
Can we have tests that check this code path? Namely check that non-dict objects raise a TypeError and that falsey dicts do not raise exceptions? In addition, we need a NEWS entry for the bugfix, e.g:
Fix :meth:`!pickle._Pickler.load_build` for non-dictionary states.(My NEWS entry is definitely poorly worded but I don't have a better suggestion for now. Maybe you can come up with a better formulation).
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Added 6 tests where the `state` is falsey but not None, and the `inst` value is invalid - these should throw an AttributeError because the `__dict__` attribute doesn't exist for `inst`. In `pickle.py`, if `state` is falsey but not None code is never run to check that `inst` is a valid object (but it does happen in the C accelerator).
|
@picnixz I went ahead and added some tests and a NEWS entry. The problem here isn't that |
|
Okay, I'm not sure how to specify in the test that I want the Python version of the |
|
Okay I now understand that |
|
This PR is stale because it has been open for 30 days with no activity. |
Inside of the
load_build()function for pickle'sBUILDopcode, the C accelerator at one point checks ifstateisPy_None, while the Python version only checksif state.cpython/Modules/_pickle.c
Line 6638 in 34ded1a
cpython/Lib/pickle.py
Line 1765 in 34ded1a
This means if
stateis something like an empty dictionary or tuple, the code block under theifstatement WILL be run in_pickle.c, but NOT inpickle.py.As an example, the bytestream
b']]b.'has the following disassembly:This will do nothing in
pickle.pybut error out in_pickle.cwith the messagestate is not a dictionary. The easy solution is to changeif statetoif state != None, and it shouldn't break any existing functionality. I've attached a pull request.load_buildfunction checks ifstateis None, not False #128965