mirror of
https://github.com/python/cpython.git
synced 2025-10-10 00:43:41 +00:00
#20476: use EmailMessage as factory if non-compat32 policy is used.
In 3.5 I will fix this right by adding a message_factory attribute to the policy.
This commit is contained in:
parent
11c5afd138
commit
aa21297457
4 changed files with 37 additions and 10 deletions
|
@ -126,7 +126,7 @@ class BufferedSubFile(object):
|
||||||
class FeedParser:
|
class FeedParser:
|
||||||
"""A feed-style parser of email."""
|
"""A feed-style parser of email."""
|
||||||
|
|
||||||
def __init__(self, _factory=message.Message, *, policy=compat32):
|
def __init__(self, _factory=None, *, policy=compat32):
|
||||||
"""_factory is called with no arguments to create a new message obj
|
"""_factory is called with no arguments to create a new message obj
|
||||||
|
|
||||||
The policy keyword specifies a policy object that controls a number of
|
The policy keyword specifies a policy object that controls a number of
|
||||||
|
@ -134,14 +134,23 @@ class FeedParser:
|
||||||
backward compatibility.
|
backward compatibility.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self._factory = _factory
|
|
||||||
self.policy = policy
|
self.policy = policy
|
||||||
try:
|
self._factory_kwds = lambda: {'policy': self.policy}
|
||||||
_factory(policy=self.policy)
|
if _factory is None:
|
||||||
self._factory_kwds = lambda: {'policy': self.policy}
|
# What this should be:
|
||||||
except TypeError:
|
#self._factory = policy.default_message_factory
|
||||||
# Assume this is an old-style factory
|
# but, because we are post 3.4 feature freeze, fix with temp hack:
|
||||||
self._factory_kwds = lambda: {}
|
if self.policy is compat32:
|
||||||
|
self._factory = message.Message
|
||||||
|
else:
|
||||||
|
self._factory = message.EmailMessage
|
||||||
|
else:
|
||||||
|
self._factory = _factory
|
||||||
|
try:
|
||||||
|
_factory(policy=self.policy)
|
||||||
|
except TypeError:
|
||||||
|
# Assume this is an old-style factory
|
||||||
|
self._factory_kwds = lambda: {}
|
||||||
self._input = BufferedSubFile()
|
self._input = BufferedSubFile()
|
||||||
self._msgstack = []
|
self._msgstack = []
|
||||||
self._parse = self._parsegen().__next__
|
self._parse = self._parsegen().__next__
|
||||||
|
|
|
@ -17,7 +17,7 @@ from email._policybase import compat32
|
||||||
|
|
||||||
|
|
||||||
class Parser:
|
class Parser:
|
||||||
def __init__(self, _class=Message, *, policy=compat32):
|
def __init__(self, _class=None, *, policy=compat32):
|
||||||
"""Parser of RFC 2822 and MIME email messages.
|
"""Parser of RFC 2822 and MIME email messages.
|
||||||
|
|
||||||
Creates an in-memory object tree representing the email message, which
|
Creates an in-memory object tree representing the email message, which
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import unittest
|
import unittest
|
||||||
import textwrap
|
import textwrap
|
||||||
from email import policy
|
from email import policy, message_from_string
|
||||||
from email.message import EmailMessage, MIMEPart
|
from email.message import EmailMessage, MIMEPart
|
||||||
from test.test_email import TestEmailBase, parameterize
|
from test.test_email import TestEmailBase, parameterize
|
||||||
|
|
||||||
|
@ -20,6 +20,20 @@ class Test(TestEmailBase):
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
m['To'] = 'xyz@abc'
|
m['To'] = 'xyz@abc'
|
||||||
|
|
||||||
|
def test_rfc2043_auto_decoded_and_emailmessage_used(self):
|
||||||
|
m = message_from_string(textwrap.dedent("""\
|
||||||
|
Subject: Ayons asperges pour le =?utf-8?q?d=C3=A9jeuner?=
|
||||||
|
From: =?utf-8?q?Pep=C3=A9?= Le Pew <pepe@example.com>
|
||||||
|
To: "Penelope Pussycat" <"penelope@example.com">
|
||||||
|
MIME-Version: 1.0
|
||||||
|
Content-Type: text/plain; charset="utf-8"
|
||||||
|
|
||||||
|
sample text
|
||||||
|
"""), policy=policy.default)
|
||||||
|
self.assertEqual(m['subject'], "Ayons asperges pour le déjeuner")
|
||||||
|
self.assertEqual(m['from'], "Pepé Le Pew <pepe@example.com>")
|
||||||
|
self.assertIsInstance(m, EmailMessage)
|
||||||
|
|
||||||
|
|
||||||
@parameterize
|
@parameterize
|
||||||
class TestEmailMessageBase:
|
class TestEmailMessageBase:
|
||||||
|
|
|
@ -24,6 +24,10 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #20476: If a non-compat32 policy is used with any of the email parsers,
|
||||||
|
EmailMessage is now used as the factory class. The factory class should
|
||||||
|
really come from the policy; that will get fixed in 3.5.
|
||||||
|
|
||||||
- Issue #19920: TarFile.list() no longer fails when outputs a listing
|
- Issue #19920: TarFile.list() no longer fails when outputs a listing
|
||||||
containing non-encodable characters. Based on patch by Vajrasky Kok.
|
containing non-encodable characters. Based on patch by Vajrasky Kok.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue