From: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Date: Fri, 21 Jun 2024 12:22:38 +0000 (+0200) Subject: gh-120380: fix Python implementation of `pickle.Pickler` for `bytes` and `bytearray... X-Git-Tag: v3.14.0a1~1383 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7595e6743ac78ac0dd19418176f66d251668fafc;p=thirdparty%2FPython%2Fcpython.git gh-120380: fix Python implementation of `pickle.Pickler` for `bytes` and `bytearray` objects in protocol version 5. (GH-120422) --- diff --git a/Lib/pickle.py b/Lib/pickle.py index 33c97c8c5efb..d719ceb7a0b8 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -782,14 +782,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("