From: R David Murray Date: Sat, 10 Sep 2016 04:22:25 +0000 (-0400) Subject: #20476: Deal with the message_factory circular import differently. X-Git-Tag: v3.6.0b1~112 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b067c8fdd1e205bd0411417b6d5e4b832c3773fc;p=python #20476: Deal with the message_factory circular import differently. It turns out we can't depend on email.message getting imported every place message_factory is needed, so to avoid a circular import we need to special case Policy.message_factory=None in the parser instead of using monkey patching. I had a feeling that was a bad idea when I did it. --- diff --git a/Doc/library/email.policy.rst b/Doc/library/email.policy.rst index b345683520..8a418778b8 100644 --- a/Doc/library/email.policy.rst +++ b/Doc/library/email.policy.rst @@ -224,8 +224,8 @@ added matters. To illustrate:: .. attribute:: message_factory A factory function for constructing a new empty message object. Used - by the parser when building messages. Defaults to - :class:`~email.message.Message`. + by the parser when building messages. Defaults to ``None``, in + which case :class:`~email.message.Message` is used. .. versionadded:: 3.6 diff --git a/Lib/email/_policybase.py b/Lib/email/_policybase.py index d6994844e1..df4649676a 100644 --- a/Lib/email/_policybase.py +++ b/Lib/email/_policybase.py @@ -155,6 +155,7 @@ class Policy(_PolicyBase, metaclass=abc.ABCMeta): serialized by a generator. Default: True. message_factory -- the class to use to create new message objects. + If the value is None, the default is Message. """ @@ -163,7 +164,6 @@ class Policy(_PolicyBase, metaclass=abc.ABCMeta): cte_type = '8bit' max_line_length = 78 mangle_from_ = False - # XXX To avoid circular imports, this is set in email.message. message_factory = None def handle_defect(self, obj, defect): diff --git a/Lib/email/feedparser.py b/Lib/email/feedparser.py index 3d74978cdb..7c07ca8645 100644 --- a/Lib/email/feedparser.py +++ b/Lib/email/feedparser.py @@ -147,7 +147,11 @@ class FeedParser: self.policy = policy self._old_style_factory = False if _factory is None: - self._factory = policy.message_factory + if policy.message_factory is None: + from email.message import Message + self._factory = Message + else: + self._factory = policy.message_factory else: self._factory = _factory try: diff --git a/Lib/email/message.py b/Lib/email/message.py index f4380d931a..b6512f2198 100644 --- a/Lib/email/message.py +++ b/Lib/email/message.py @@ -1162,6 +1162,3 @@ class EmailMessage(MIMEPart): super().set_content(*args, **kw) if 'MIME-Version' not in self: self['MIME-Version'] = '1.0' - -# Set message_factory on Policy here to avoid a circular import. -Policy.message_factory = Message diff --git a/Lib/test/test_email/test_policy.py b/Lib/test/test_email/test_policy.py index 1d95d03bf5..8fecb8a5fc 100644 --- a/Lib/test/test_email/test_policy.py +++ b/Lib/test/test_email/test_policy.py @@ -24,7 +24,7 @@ class PolicyAPITests(unittest.TestCase): 'cte_type': '8bit', 'raise_on_defect': False, 'mangle_from_': True, - 'message_factory': email.message.Message, + 'message_factory': None, } # These default values are the ones set on email.policy.default. # If any of these defaults change, the docs must be updated.