Skip to content

Commit c27e522

Browse files
committed
#14731: refactor email policy framework.
This patch primarily does two things: (1) it adds some internal-interface methods to Policy that allow for Policy to control the parsing and folding of headers in such a way that we can construct a backward compatibility policy that is 100% compatible with the 3.2 API, while allowing a new policy to implement the email6 API. (2) it adds that backward compatibility policy and refactors the test suite so that the only differences between the 3.2 test_email.py file and the 3.3 test_email.py file is some small changes in test framework and the addition of tests for bugs fixed that apply to the 3.2 API. There are some additional teaks, such as moving just the code needed for the compatibility policy into _policybase, so that the library code can import only _policybase. That way the new code that will be added for email6 will only get imported when a non-compatibility policy is imported.
1 parent 9242c13 commit c27e522

15 files changed

+1389
-517
lines changed

Doc/library/email.generator.rst

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ Here are the public methods of the :class:`Generator` class, imported from the
3232
:mod:`email.generator` module:
3333

3434

35-
.. class:: Generator(outfp, mangle_from_=True, maxheaderlen=78, *, \
36-
policy=policy.default)
35+
.. class:: Generator(outfp, mangle_from_=True, maxheaderlen=78, *, policy=None)
3736

3837
The constructor for the :class:`Generator` class takes a :term:`file-like object`
3938
called *outfp* for an argument. *outfp* must support the :meth:`write` method
@@ -55,8 +54,9 @@ Here are the public methods of the :class:`Generator` class, imported from the
5554
The default is 78, as recommended (but not required) by :rfc:`2822`.
5655

5756
The *policy* keyword specifies a :mod:`~email.policy` object that controls a
58-
number of aspects of the generator's operation. The default policy
59-
maintains backward compatibility.
57+
number of aspects of the generator's operation. If no *policy* is specified,
58+
then the *policy* attached to the message object passed to :attr:``flatten``
59+
is used.
6060

6161
.. versionchanged:: 3.3 Added the *policy* keyword.
6262

@@ -80,19 +80,19 @@ Here are the public methods of the :class:`Generator` class, imported from the
8080

8181
Optional *linesep* specifies the line separator character used to
8282
terminate lines in the output. If specified it overrides the value
83-
specified by the ``Generator``\'s ``policy``.
83+
specified by the *msg*\'s or ``Generator``\'s ``policy``.
8484

85-
Because strings cannot represent non-ASCII bytes, ``Generator`` ignores
86-
the value of the :attr:`~email.policy.Policy.must_be_7bit`
87-
:mod:`~email.policy` setting and operates as if it were set ``True``.
88-
This means that messages parsed with a Bytes parser that have a
89-
:mailheader:`Content-Transfer-Encoding` of 8bit will be converted to a
90-
use a 7bit Content-Transfer-Encoding. Non-ASCII bytes in the headers
91-
will be :rfc:`2047` encoded with a charset of `unknown-8bit`.
85+
Because strings cannot represent non-ASCII bytes, if the policy that
86+
applies when ``flatten`` is run has :attr:`~email.policy.Policy.cte_type`
87+
set to ``8bit``, ``Generator`` will operate as if it were set to
88+
``7bit``. This means that messages parsed with a Bytes parser that have
89+
a :mailheader:`Content-Transfer-Encoding` of ``8bit`` will be converted
90+
to a use a ``7bit`` Content-Transfer-Encoding. Non-ASCII bytes in the
91+
headers will be :rfc:`2047` encoded with a charset of ``unknown-8bit``.
9292

9393
.. versionchanged:: 3.2
94-
Added support for re-encoding 8bit message bodies, and the *linesep*
95-
argument.
94+
Added support for re-encoding ``8bit`` message bodies, and the
95+
*linesep* argument.
9696

9797
.. method:: clone(fp)
9898

@@ -149,13 +149,13 @@ formatted string representation of a message object. For more detail, see
149149
at *msg* to the output file specified when the :class:`BytesGenerator`
150150
instance was created. Subparts are visited depth-first and the resulting
151151
text will be properly MIME encoded. If the :mod:`~email.policy` option
152-
:attr:`~email.policy.Policy.must_be_7bit` is ``False`` (the default),
152+
:attr:`~email.policy.Policy.cte_type` is ``8bit`` (the default),
153153
then any bytes with the high bit set in the original parsed message that
154154
have not been modified will be copied faithfully to the output. If
155-
``must_be_7bit`` is true, the bytes will be converted as needed using an
156-
ASCII content-transfer-encoding. In particular, RFC-invalid non-ASCII
157-
bytes in headers will be encoded using the MIME ``unknown-8bit``
158-
character set, thus rendering them RFC-compliant.
155+
``cte_type`` is ``7bit``, the bytes will be converted as needed
156+
using an ASCII-compatible Content-Transfer-Encoding. In particular,
157+
RFC-invalid non-ASCII bytes in headers will be encoded using the MIME
158+
``unknown-8bit`` character set, thus rendering them RFC-compliant.
159159

160160
.. XXX: There should be a complementary option that just does the RFC
161161
compliance transformation but leaves CTE 8bit parts alone.

Doc/library/email.policy.rst

Lines changed: 207 additions & 83 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)