(original) (raw)

changeset: 103547:8375b8d54bf7 user: R David Murray rdmurray@bitdance.com date: Sat Sep 10 00:22:25 2016 -0400 files: Doc/library/email.policy.rst Lib/email/_policybase.py Lib/email/feedparser.py Lib/email/message.py Lib/test/test_email/test_policy.py description: #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 -r 3934e070c9db -r 8375b8d54bf7 Doc/library/email.policy.rst --- a/Doc/library/email.policy.rst Fri Sep 09 20:56:52 2016 -0700 +++ b/Doc/library/email.policy.rst Sat Sep 10 00:22:25 2016 -0400 @@ -224,8 +224,8 @@ .. 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 -r 3934e070c9db -r 8375b8d54bf7 Lib/email/_policybase.py --- a/Lib/email/_policybase.py Fri Sep 09 20:56:52 2016 -0700 +++ b/Lib/email/_policybase.py Sat Sep 10 00:22:25 2016 -0400 @@ -155,6 +155,7 @@ 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 @@ 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 -r 3934e070c9db -r 8375b8d54bf7 Lib/email/feedparser.py --- a/Lib/email/feedparser.py Fri Sep 09 20:56:52 2016 -0700 +++ b/Lib/email/feedparser.py Sat Sep 10 00:22:25 2016 -0400 @@ -147,7 +147,11 @@ 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 -r 3934e070c9db -r 8375b8d54bf7 Lib/email/message.py --- a/Lib/email/message.py Fri Sep 09 20:56:52 2016 -0700 +++ b/Lib/email/message.py Sat Sep 10 00:22:25 2016 -0400 @@ -1162,6 +1162,3 @@ 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 -r 3934e070c9db -r 8375b8d54bf7 Lib/test/test_email/test_policy.py --- a/Lib/test/test_email/test_policy.py Fri Sep 09 20:56:52 2016 -0700 +++ b/Lib/test/test_email/test_policy.py Sat Sep 10 00:22:25 2016 -0400 @@ -24,7 +24,7 @@ '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. /rdmurray@bitdance.com