summaryrefslogtreecommitdiffstats
path: root/Lib/pickle.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2024-08-29 05:26:16 (GMT)
committerGitHub <noreply@github.com>2024-08-29 05:26:16 (GMT)
commit0c3ea3023878f5ad5ca4680d5510da1fe208cbfa (patch)
tree0c7a53b10e14015079acd427e32c2db480a73075 /Lib/pickle.py
parentc9930f5022f5e7a290896522280e47a1fecba38a (diff)
downloadcpython-0c3ea3023878f5ad5ca4680d5510da1fe208cbfa.zip
cpython-0c3ea3023878f5ad5ca4680d5510da1fe208cbfa.tar.gz
cpython-0c3ea3023878f5ad5ca4680d5510da1fe208cbfa.tar.bz2
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.
Diffstat (limited to 'Lib/pickle.py')
-rw-r--r--Lib/pickle.py18
1 files changed, 11 insertions, 7 deletions
diff --git a/Lib/pickle.py b/Lib/pickle.py
index b8e114a..ade0491 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -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)