@@ -32,7 +32,8 @@ 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)
35+ .. class :: Generator(outfp, mangle_from_=True, maxheaderlen=78, *, \
36+ policy=policy.default)
3637
3738 The constructor for the :class: `Generator ` class takes a :term: `file-like object `
3839 called *outfp * for an argument. *outfp * must support the :meth: `write ` method
@@ -53,10 +54,16 @@ Here are the public methods of the :class:`Generator` class, imported from the
5354 :class: `~email.header.Header ` class. Set to zero to disable header wrapping.
5455 The default is 78, as recommended (but not required) by :rfc: `2822 `.
5556
57+ 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.
60+
61+ .. versionchanged :: 3.3 Added the *policy* keyword.
62+
5663 The other public :class: `Generator ` methods are:
5764
5865
59- .. method :: flatten(msg, unixfrom=False, linesep='\\n' )
66+ .. method :: flatten(msg, unixfrom=False, linesep=None )
6067
6168 Print the textual representation of the message object structure rooted at
6269 *msg * to the output file specified when the :class: `Generator ` instance
@@ -72,12 +79,13 @@ Here are the public methods of the :class:`Generator` class, imported from the
7279 Note that for subparts, no envelope header is ever printed.
7380
7481 Optional *linesep * specifies the line separator character used to
75- terminate lines in the output. It defaults to ``\n `` because that is
76- the most useful value for Python application code (other library packages
77- expect ``\n `` separated lines). ``linesep=\r\n `` can be used to
78- generate output with RFC-compliant line separators.
82+ terminate lines in the output. If specified it overrides the value
83+ specified by the ``Generator ``\' s ``policy ``.
7984
80- Messages parsed with a Bytes parser that have a
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
8189 :mailheader: `Content-Transfer-Encoding ` of 8bit will be converted to a
8290 use a 7bit Content-Transfer-Encoding. Non-ASCII bytes in the headers
8391 will be :rfc: `2047 ` encoded with a charset of `unknown-8bit `.
@@ -103,7 +111,8 @@ As a convenience, see the :class:`~email.message.Message` methods
103111formatted string representation of a message object. For more detail, see
104112:mod: `email.message `.
105113
106- .. class :: BytesGenerator(outfp, mangle_from_=True, maxheaderlen=78)
114+ .. class :: BytesGenerator(outfp, mangle_from_=True, maxheaderlen=78, *, \
115+ policy=policy.default)
107116
108117 The constructor for the :class: `BytesGenerator ` class takes a binary
109118 :term: `file-like object ` called *outfp * for an argument. *outfp * must
@@ -125,19 +134,31 @@ formatted string representation of a message object. For more detail, see
125134 wrapping. The default is 78, as recommended (but not required) by
126135 :rfc: `2822 `.
127136
137+ The *policy * keyword specifies a :mod: `~email.policy ` object that controls a
138+ number of aspects of the generator's operation. The default policy
139+ maintains backward compatibility.
140+
141+ .. versionchanged :: 3.3 Added the *policy* keyword.
142+
128143 The other public :class: `BytesGenerator ` methods are:
129144
130145
131- .. method :: flatten(msg, unixfrom=False, linesep='\n' )
146+ .. method :: flatten(msg, unixfrom=False, linesep=None )
132147
133148 Print the textual representation of the message object structure rooted
134149 at *msg * to the output file specified when the :class: `BytesGenerator `
135150 instance was created. Subparts are visited depth-first and the resulting
136- text will be properly MIME encoded. If the input that created the *msg *
137- contained bytes with the high bit set and those bytes have not been
138- modified, they will be copied faithfully to the output, even if doing so
139- is not strictly RFC compliant. (To produce strictly RFC compliant
140- output, use the :class: `Generator ` class.)
151+ text will be properly MIME encoded. If the :mod: `~email.policy ` option
152+ :attr: `~email.policy.Policy.must_be_7bit ` is ``False `` (the default),
153+ then any bytes with the high bit set in the original parsed message that
154+ 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.
159+
160+ .. XXX: There should be a complementary option that just does the RFC
161+ compliance transformation but leaves CTE 8bit parts alone.
141162
142163 Messages parsed with a Bytes parser that have a
143164 :mailheader: `Content-Transfer-Encoding ` of 8bit will be reconstructed
@@ -152,10 +173,8 @@ formatted string representation of a message object. For more detail, see
152173 Note that for subparts, no envelope header is ever printed.
153174
154175 Optional *linesep * specifies the line separator character used to
155- terminate lines in the output. It defaults to ``\n `` because that is
156- the most useful value for Python application code (other library packages
157- expect ``\n `` separated lines). ``linesep=\r\n `` can be used to
158- generate output with RFC-compliant line separators.
176+ terminate lines in the output. If specified it overrides the value
177+ specified by the ``Generator ``\ 's ``policy ``.
159178
160179 .. method :: clone(fp)
161180
0 commit comments