FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-159858: Fix memoryview payloads with 7bit, 8bit and binary email CTEs by NikitaMikhailov · Pull Request #159859 · python/cpython · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 2 additions & 2 deletions Lib/email/contentmanager.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ def set_bytes_content(msg, data, maintype, subtype, cte='base64',
data = binascii.b2a_qp(data, istext=False, header=False, quotetabs=True)
data = data.decode('ascii')
elif cte == '7bit':
data = data.decode('ascii')
data = str(data, 'ascii')
elif cte in ('8bit', 'binary'):
data = data.decode('ascii', 'surrogateescape')
data = str(data, 'ascii', 'surrogateescape')
msg.set_payload(data)
msg['Content-Transfer-Encoding'] = cte
_finalize_set(msg, disposition, filename, cid, params)
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_email/test_contentmanager.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,29 @@ def test_set_content_bytes_cte_7bit(self):
ASCII-only message.
"""))

def test_set_byteslike_content_with_unencoded_cte(self):
for cte in ('7bit', '8bit', 'binary'):
content = (b'ASCII-only message.\n' if cte == '7bit' else
b'non-ASCII \xa7\xff message.\r\n')
for buffer_type in (bytes, bytearray, memoryview):
with self.subTest(cte=cte, buffer_type=buffer_type):
m = self._make_message()
m.set_content(buffer_type(content),
maintype='application',
subtype='octet-stream', cte=cte)
self.assertEqual(m['Content-Transfer-Encoding'], cte)
self.assertEqual(m.get_payload(decode=True), content)
self.assertEqual(m.get_content(), content)

def test_set_byteslike_non_ascii_with_cte_7bit_raises(self):
for buffer_type in (bytes, bytearray, memoryview):
with self.subTest(buffer_type=buffer_type):
m = self._make_message()
with self.assertRaises(UnicodeDecodeError):
m.set_content(buffer_type(b'non-ASCII \xa7\xff message'),
maintype='application',
subtype='octet-stream', cte='7bit')

content_object_params = {
'text_plain': ('content', ()),
'text_html': ('content', ('html',)),
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :meth:`email.message.EmailMessage.set_content` raising
:exc:`AttributeError` for :class:`memoryview` payloads with the ``7bit``,
``8bit``, or ``binary`` content transfer encoding.
Loading

Back | FazBrowse Home | New Git URL