From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Wed, 26 Jun 2024 10:12:04 +0000 (+0200) Subject: [3.12] gh-120380: fix Python implementation of `pickle.Pickler` for `bytes` and ... X-Git-Tag: v3.12.5~165 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6de373d7508ef621854a08a4bc770431b46f8f82;p=thirdparty%2FPython%2Fcpython.git [3.12] gh-120380: fix Python implementation of `pickle.Pickler` for `bytes` and `bytearray` objects in protocol version 5. (GH-120422) (GH-120833) gh-120380: fix Python implementation of `pickle.Pickler` for `bytes` and `bytearray` objects in protocol version 5. (GH-120422) (cherry picked from commit 7595e6743ac78ac0dd19418176f66d251668fafc) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- diff --git a/Lib/pickle.py b/Lib/pickle.py index 6e3c61fd0b2c..81098d0685a0 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -780,14 +780,10 @@ class _Pickler: self.write(FLOAT + repr(obj).encode("ascii") + b'\n') dispatch[float] = save_float - def save_bytes(self, obj): - if self.proto < 3: - if not obj: # bytes object is empty - self.save_reduce(bytes, (), obj=obj) - else: - self.save_reduce(codecs.encode, - (str(obj, 'latin1'), 'latin1'), obj=obj) - return + def _save_bytes_no_memo(self, obj): + # helper for writing bytes objects for protocol >= 3 + # without memoizing them + assert self.proto >= 3 n = len(obj) if n <= 0xff: self.write(SHORT_BINBYTES + pack("= 5 + # without memoizing them + assert self.proto >= 5 + n = len(obj) + if n >= self.framer._FRAME_SIZE_TARGET: + self._write_large_bytes(BYTEARRAY8 + pack("= self.framer._FRAME_SIZE_TARGET: - self._write_large_bytes(BYTEARRAY8 + pack("