44.. module :: email.policy
55 :synopsis: Controlling the parsing and generating of messages
66
7+ .. versionadded: 3.3
8+
79
810 The :mod: `email ` package's prime focus is the handling of email messages as
911described by the various email and MIME RFCs. However, the general format of
@@ -14,8 +16,8 @@ email. Some of these uses conform fairly closely to the main RFCs, some do
1416not. And even when working with email, there are times when it is desirable to
1517break strict compliance with the RFCs.
1618
17- Policy objects are the mechanism used to provide the email package with the
18- flexibility to handle all these disparate use cases,
19+ Policy objects give the email package the flexibility to handle all these
20+ disparate use cases.
1921
2022A :class: `Policy ` object encapsulates a set of attributes and methods that
2123control the behavior of various components of the email package during use.
@@ -39,24 +41,23 @@ program will use the same :class:`Policy` for both input and output, the two
3941can be different.
4042
4143As an example, the following code could be used to read an email message from a
42- file on disk and pass it to the system ``sendmail `` program on a ``unix ``
43- system::
44+ file on disk and pass it to the system ``sendmail `` program on a Unix system::
4445
4546 >>> from email import msg_from_binary_file
4647 >>> from email.generator import BytesGenerator
4748 >>> import email.policy
4849 >>> from subprocess import Popen, PIPE
4950 >>> with open('mymsg.txt', 'b') as f:
50- >>> msg = msg_from_binary_file(f, policy=email.policy.mbox)
51+ ... Msg = msg_from_binary_file(f, policy=email.policy.mbox)
5152 >>> p = Popen(['sendmail', msg['To'][0].address], stdin=PIPE)
52- >>> g = BytesGenerator(p.stdin, email.policy.policy= SMTP)
53+ >>> g = BytesGenerator(p.stdin, policy= email.policy.SMTP)
5354 >>> g.flatten(msg)
5455 >>> p.stdin.close()
5556 >>> rc = p.wait()
5657
5758Some email package methods accept a *policy * keyword argument, allowing the
58- policy to be overridden for that method. For example, the following code use
59- the :meth: `email.message.Message.as_string ` method to the *msg * object from the
59+ policy to be overridden for that method. For example, the following code uses
60+ the :meth: `email.message.Message.as_string ` method of the *msg * object from the
6061previous example and re-write it to a file using the native line separators for
6162the platform on which it is running::
6263
@@ -106,26 +107,28 @@ added matters. To illustrate::
106107 .. attribute :: linesep
107108
108109 The string to be used to terminate lines in serialized output. The
109- default is ' \\ n' because that's the internal end-of-line discipline used
110- by Python, though ' \\ r \\ n' is required by the RFCs. See `Policy
110+ default is `` \n `` because that's the internal end-of-line discipline used
111+ by Python, though `` \r\n `` is required by the RFCs. See `Policy
111112 Instances `_ for policies that use an RFC conformant linesep. Setting it
112113 to :attr: `os.linesep ` may also be useful.
113114
114115 .. attribute :: must_be_7bit
115116
116- If :const: ` True `, data output by a bytes generator is limited to ASCII
117+ If `` True ` `, data output by a bytes generator is limited to ASCII
117118 characters. If :const: `False ` (the default), then bytes with the high
118119 bit set are preserved and/or allowed in certain contexts (for example,
119120 where possible a content transfer encoding of ``8bit `` will be used).
120- String generators act as if ``must_be_7bit `` is `True ` regardless of the
121- policy in effect, since a string cannot represent non-ASCII bytes.
121+ String generators act as if ``must_be_7bit `` is `` True `` regardless of
122+ the policy in effect, since a string cannot represent non-ASCII bytes.
122123
123124 .. attribute :: raise_on_defect
124125
125126 If :const: `True `, any defects encountered will be raised as errors. If
126127 :const: `False ` (the default), defects will be passed to the
127128 :meth: `register_defect ` method.
128129
130+ :mod: `Policy ` object also have the following methods:
131+
129132 .. method :: handle_defect(obj, defect)
130133
131134 *obj * is the object on which to register the defect. *defect * should be
@@ -145,35 +148,35 @@ added matters. To illustrate::
145148 handling of defects. The default implementation calls the ``append ``
146149 method of the ``defects `` attribute of *obj *.
147150
148- .. method :: clone(obj, *kw):
151+ .. method :: clone(obj, *kw)
149152
150153 Return a new :class: `Policy ` instance whose attributes have the same
151154 values as the current instance, except where those attributes are
152155 given new values by the keyword arguments.
153156
154157
155158Policy Instances
156- ................
159+ ^^^^^^^^^^^^^^^^
157160
158161The following instances of :class: `Policy ` provide defaults suitable for
159162specific common application domains.
160163
161164.. data :: default
162165
163- An instance of :class: `Policy ` with all defaults unchanged.
166+ An instance of :class: `Policy ` with all defaults unchanged.
164167
165168.. data :: SMTP
166169
167- Output serialized from a message will conform to the email and SMTP
168- RFCs. The only changed attribute is :attr: `linesep `, which is set to
169- ``\r\n ``.
170+ Output serialized from a message will conform to the email and SMTP
171+ RFCs. The only changed attribute is :attr: `linesep `, which is set to
172+ ``\r\n ``.
170173
171174.. data :: HTTP
172175
173- Suitable for use when serializing headers for use in HTTP traffic.
174- :attr: `linesep ` is set to ``\r\n ``, and :attr: `max_line_length ` is set to
175- :const: `None ` (unlimited).
176+ Suitable for use when serializing headers for use in HTTP traffic.
177+ :attr: `linesep ` is set to ``\r\n ``, and :attr: `max_line_length ` is set to
178+ :const: `None ` (unlimited).
176179
177180.. data :: strict
178181
179- :attr: `raise_on_defect ` is set to :const: `True `.
182+ :attr: `raise_on_defect ` is set to :const: `True `.
0 commit comments