aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/email/message.py
diff options
context:
space:
mode:
authorSidney Markowitz <sidney@sidney.com>2023-12-12 05:21:18 +1300
committerGitHub <noreply@github.com>2023-12-11 18:21:18 +0200
commit27a5fd8cb8c88537216d7a498eba9d9177951d76 (patch)
tree4bb61ba73e6039abd3947528596f08ef9671a645 /Lib/email/message.py
parent3251ba8f1af535bf28e31a6832511ba19e96b262 (diff)
downloadcpython-27a5fd8cb8c88537216d7a498eba9d9177951d76.tar.gz
cpython-27a5fd8cb8c88537216d7a498eba9d9177951d76.zip
gh-94606: Fix error when message with Unicode surrogate not surrogateescaped string (GH-94641)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib/email/message.py')
-rw-r--r--Lib/email/message.py29
1 files changed, 15 insertions, 14 deletions
diff --git a/Lib/email/message.py b/Lib/email/message.py
index 411118c74da..fe769580fed 100644
--- a/Lib/email/message.py
+++ b/Lib/email/message.py
@@ -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':