cpython: 77082b818676 (original) (raw)
Mercurial > cpython
changeset 89013:77082b818676
#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. [#20476]
R David Murray rdmurray@bitdance.com | |
---|---|
date | Fri, 07 Feb 2014 10:44:16 -0500 |
parents | f3f92d55f942 |
children | a9d7d53d5fbd |
files | Lib/email/feedparser.py Lib/email/parser.py Lib/test/test_email/test_message.py Misc/NEWS |
diffstat | 4 files changed, 37 insertions(+), 10 deletions(-)[+] [-] Lib/email/feedparser.py 25 Lib/email/parser.py 2 Lib/test/test_email/test_message.py 16 Misc/NEWS 4 |
line wrap: on
line diff
--- a/Lib/email/feedparser.py +++ b/Lib/email/feedparser.py @@ -126,7 +126,7 @@ class BufferedSubFile(object): class FeedParser: """A feed-style parser of email."""
- def init(self, _factory=None, *, policy=compat32): """_factory is called with no arguments to create a new message obj
The policy keyword specifies a policy object that controls a number of @@ -134,14 +134,23 @@ class FeedParser: backward compatibility. """
self._factory = _factory[](#l1.16) self.policy = policy[](#l1.17)
try:[](#l1.18)
_factory(policy=self.policy)[](#l1.19)
self._factory_kwds = lambda: {'policy': self.policy}[](#l1.20)
except TypeError:[](#l1.21)
# Assume this is an old-style factory[](#l1.22)
self._factory_kwds = lambda: {}[](#l1.23)
self._factory_kwds = lambda: {'policy': self.policy}[](#l1.24)
if _factory is None:[](#l1.25)
# What this should be:[](#l1.26)
#self._factory = policy.default_message_factory[](#l1.27)
# but, because we are post 3.4 feature freeze, fix with temp hack:[](#l1.28)
if self.policy is compat32:[](#l1.29)
self._factory = message.Message[](#l1.30)
else:[](#l1.31)
self._factory = message.EmailMessage[](#l1.32)
else:[](#l1.33)
self._factory = _factory[](#l1.34)
try:[](#l1.35)
_factory(policy=self.policy)[](#l1.36)
except TypeError:[](#l1.37)
# Assume this is an old-style factory[](#l1.38)
self._factory_kwds = lambda: {}[](#l1.39) self._input = BufferedSubFile()[](#l1.40) self._msgstack = [][](#l1.41) self._parse = self._parsegen().__next__[](#l1.42)
--- a/Lib/email/parser.py +++ b/Lib/email/parser.py @@ -17,7 +17,7 @@ from email._policybase import compat32 class Parser:
Creates an in-memory object tree representing the email message, which
--- a/Lib/test/test_email/test_message.py +++ b/Lib/test/test_email/test_message.py @@ -1,6 +1,6 @@ import unittest import textwrap -from email import policy +from email import policy, message_from_string from email.message import EmailMessage, MIMEPart from test.test_email import TestEmailBase, parameterize @@ -20,6 +20,20 @@ class Test(TestEmailBase): with self.assertRaises(ValueError): m['To'] = 'xyz@abc'
- def test_rfc2043_auto_decoded_and_emailmessage_used(self):
m = message_from_string(textwrap.dedent("""\[](#l3.16)
Subject: Ayons asperges pour le =?utf-8?q?d=C3=A9jeuner?=[](#l3.17)
From: =?utf-8?q?Pep=C3=A9?= Le Pew <pepe@example.com>[](#l3.18)
To: "Penelope Pussycat" <"penelope@example.com">[](#l3.19)
MIME-Version: 1.0[](#l3.20)
Content-Type: text/plain; charset="utf-8"[](#l3.21)
sample text[](#l3.23)
"""), policy=policy.default)[](#l3.24)
self.assertEqual(m['subject'], "Ayons asperges pour le déjeuner")[](#l3.25)
self.assertEqual(m['from'], "Pepé Le Pew <pepe@example.com>")[](#l3.26)
self.assertIsInstance(m, EmailMessage)[](#l3.27)
+ @parameterize class TestEmailMessageBase:
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -24,6 +24,10 @@ Core and Builtins Library ------- +- Issue #20476: If a non-compat32 policy is used with any of the email parsers,