mirror of
https://github.com/python/cpython.git
synced 2025-08-22 09:45:06 +00:00
gh-123431: Harmonize extension code checks in pickle (GH-123434)
This checks are redundant in normal circumstances and can only work if the extension registry was intentionally broken. * The Python implementation now raises exception for the extension code with false boolean value. * Simplify the C code. RuntimeError is now raised in explicit checks. * Add many tests.
This commit is contained in:
parent
c9930f5022
commit
0c3ea30238
3 changed files with 73 additions and 28 deletions
|
@ -1086,11 +1086,16 @@ class _Pickler:
|
|||
|
||||
module_name = whichmodule(obj, name)
|
||||
if self.proto >= 2:
|
||||
code = _extension_registry.get((module_name, name))
|
||||
if code:
|
||||
assert code > 0
|
||||
code = _extension_registry.get((module_name, name), _NoValue)
|
||||
if code is not _NoValue:
|
||||
if code <= 0xff:
|
||||
write(EXT1 + pack("<B", code))
|
||||
data = pack("<B", code)
|
||||
if data == b'\0':
|
||||
# Should never happen in normal circumstances,
|
||||
# since the type and the value of the code are
|
||||
# checked in copyreg.add_extension().
|
||||
raise RuntimeError("extension code 0 is out of range")
|
||||
write(EXT1 + data)
|
||||
elif code <= 0xffff:
|
||||
write(EXT2 + pack("<H", code))
|
||||
else:
|
||||
|
@ -1581,9 +1586,8 @@ class _Unpickler:
|
|||
dispatch[EXT4[0]] = load_ext4
|
||||
|
||||
def get_extension(self, code):
|
||||
nil = []
|
||||
obj = _extension_cache.get(code, nil)
|
||||
if obj is not nil:
|
||||
obj = _extension_cache.get(code, _NoValue)
|
||||
if obj is not _NoValue:
|
||||
self.append(obj)
|
||||
return
|
||||
key = _inverted_registry.get(code)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue