mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
[3.11] gh-94606: Fix error when message with Unicode surrogate not surrogateescaped string (GH-94641) (GH-112972)
(cherry picked from commit 27a5fd8cb8
)
Co-authored-by: Sidney Markowitz <sidney@sidney.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
a37e1473da
commit
5aec2d2452
4 changed files with 49 additions and 16 deletions
|
@ -289,25 +289,26 @@ class Message:
|
|||
# cte might be a Header, so for now stringify it.
|
||||
cte = str(self.get('content-transfer-encoding', '')).lower()
|
||||
# payload may be bytes here.
|
||||
if isinstance(payload, str):
|
||||
if utils._has_surrogates(payload):
|
||||
bpayload = payload.encode('ascii', 'surrogateescape')
|
||||
if not decode:
|
||||
if not decode:
|
||||
if isinstance(payload, str) and utils._has_surrogates(payload):
|
||||
try:
|
||||
bpayload = payload.encode('ascii', 'surrogateescape')
|
||||
try:
|
||||
payload = bpayload.decode(self.get_param('charset', 'ascii'), 'replace')
|
||||
except LookupError:
|
||||
payload = bpayload.decode('ascii', 'replace')
|
||||
elif decode:
|
||||
try:
|
||||
bpayload = payload.encode('ascii')
|
||||
except UnicodeError:
|
||||
# This won't happen for RFC compliant messages (messages
|
||||
# containing only ASCII code points in the unicode input).
|
||||
# If it does happen, turn the string into bytes in a way
|
||||
# guaranteed not to fail.
|
||||
bpayload = payload.encode('raw-unicode-escape')
|
||||
if not decode:
|
||||
except UnicodeEncodeError:
|
||||
pass
|
||||
return payload
|
||||
if isinstance(payload, str):
|
||||
try:
|
||||
bpayload = payload.encode('ascii', 'surrogateescape')
|
||||
except UnicodeEncodeError:
|
||||
# This won't happen for RFC compliant messages (messages
|
||||
# containing only ASCII code points in the unicode input).
|
||||
# If it does happen, turn the string into bytes in a way
|
||||
# guaranteed not to fail.
|
||||
bpayload = payload.encode('raw-unicode-escape')
|
||||
if cte == 'quoted-printable':
|
||||
return quopri.decodestring(bpayload)
|
||||
elif cte == 'base64':
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue