Skip to content

Commit

Permalink
Merge branch '3.12' into backport-e59da0c-3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
ambv authored Jul 23, 2023
2 parents 7baeaa3 + bd72fb1 commit 40f7fdb
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Lib/email/feedparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def close(self):
assert not self._msgstack
# Look for final set of defects
if root.get_content_maintype() == 'multipart' \
and not root.is_multipart():
and not root.is_multipart() and not self._headersonly:
defect = errors.MultipartInvariantViolationDefect()
self.policy.handle_defect(root, defect)
return root
Expand Down
10 changes: 6 additions & 4 deletions Lib/gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,12 @@ def gettext(self, message):
missing = object()
tmsg = self._catalog.get(message, missing)
if tmsg is missing:
if self._fallback:
return self._fallback.gettext(message)
return message
return tmsg
tmsg = self._catalog.get((message, self.plural(1)), missing)
if tmsg is not missing:
return tmsg
if self._fallback:
return self._fallback.gettext(message)
return message

def ngettext(self, msgid1, msgid2, n):
try:
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_email/data/msg_47.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Date: 01 Jan 2001 00:01+0000
From: arthur@example.example
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=foo

--foo
Content-Type: text/plain
bar

--foo
Content-Type: text/html
<html><body><p>baz</p></body></html>

--foo--
10 changes: 10 additions & 0 deletions Lib/test/test_email/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -3712,6 +3712,16 @@ def test_bytes_header_parser(self):
self.assertIsInstance(msg.get_payload(), str)
self.assertIsInstance(msg.get_payload(decode=True), bytes)

def test_header_parser_multipart_is_valid(self):
# Don't flag valid multipart emails as having defects
with openfile('msg_47.txt', encoding="utf-8") as fp:
msgdata = fp.read()

parser = email.parser.Parser(policy=email.policy.default)
parsed_msg = parser.parsestr(msgdata, headersonly=True)

self.assertEqual(parsed_msg.defects, [])

def test_bytes_parser_does_not_close_file(self):
with openfile('msg_02.txt', 'rb') as fp:
email.parser.BytesParser().parse(fp)
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ def test_plural_forms1(self):
eq(x, 'Hay %s fichero')
x = gettext.ngettext('There is %s file', 'There are %s files', 2)
eq(x, 'Hay %s ficheros')
x = gettext.gettext('There is %s file')
eq(x, 'Hay %s fichero')

def test_plural_context_forms1(self):
eq = self.assertEqual
Expand All @@ -338,6 +340,8 @@ def test_plural_forms2(self):
eq(x, 'Hay %s fichero')
x = t.ngettext('There is %s file', 'There are %s files', 2)
eq(x, 'Hay %s ficheros')
x = t.gettext('There is %s file')
eq(x, 'Hay %s fichero')

def test_plural_context_forms2(self):
eq = self.assertEqual
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ensure `gettext(msg)` retrieve translations even if a plural form exists. In
other words: `gettext(msg) == ngettext(msg, '', 1)`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Do not report ``MultipartInvariantViolationDefect`` defect
when the :class:`email.parser.Parser` class is used
to parse emails with ``headersonly=True``.

0 comments on commit 40f7fdb

Please sign in to comment.