| OLD | NEW |
| 1 :mod:`email`: Policy Objects | 1 :mod:`email`: Policy Objects |
| 2 ---------------------------- | 2 ---------------------------- |
| 3 | 3 |
| 4 .. module:: email.policy | 4 .. module:: email.policy |
| 5 :synopsis: Controlling the parsing and generating of messages | 5 :synopsis: Controlling the parsing and generating of messages |
| 6 | 6 |
| 7 .. versionadded:: 3.3 | 7 .. versionadded:: 3.3 |
| 8 | 8 |
| 9 | 9 |
| 10 The :mod:`email` package's prime focus is the handling of email messages as | 10 The :mod:`email` package's prime focus is the handling of email messages as |
| 11 described by the various email and MIME RFCs. However, the general format of | 11 described by the various email and MIME RFCs. However, the general format of |
| 12 email messages (a block of header fields each consisting of a name followed by | 12 email messages (a block of header fields each consisting of a name followed by |
| 13 a colon followed by a value, the whole block followed by a blank line and an | 13 a colon followed by a value, the whole block followed by a blank line and an |
| 14 arbitrary 'body'), is a format that has found utility outside of the realm of | 14 arbitrary 'body'), is a format that has found utility outside of the realm of |
| 15 email. Some of these uses conform fairly closely to the main RFCs, some do | 15 email. Some of these uses conform fairly closely to the main RFCs, some do |
| 16 not. And even when working with email, there are times when it is desirable to | 16 not. And even when working with email, there are times when it is desirable to |
| 17 break strict compliance with the RFCs. | 17 break strict compliance with the RFCs. |
| 18 | 18 |
| 19 Policy objects give the email package the flexibility to handle all these | 19 Policy objects give the email package the flexibility to handle all these |
| 20 disparate use cases. | 20 disparate use cases. |
| 21 | 21 |
| 22 A :class:`Policy` object encapsulates a set of attributes and methods that | 22 A :class:`Policy` object encapsulates a set of attributes and methods that |
| 23 control the behavior of various components of the email package during use. | 23 control the behavior of various components of the email package during use. |
| 24 :class:`Policy` instances can be passed to various classes and methods in the | 24 :class:`Policy` instances can be passed to various classes and methods in the |
| 25 email package to alter the default behavior. The settable values and their | 25 email package to alter the default behavior. The settable values and their |
| 26 defaults are described below. The :mod:`policy` module also provides some | 26 defaults are described below. |
| 27 pre-created :class:`Policy` instances. In addition to a :const:`default` | |
| 28 instance, there are instances tailored for certain applications. For example | |
| 29 there is an :const:`SMTP` :class:`Policy` with defaults appropriate for | |
| 30 generating output to be sent to an SMTP server. These are listed `below | |
| 31 <Policy Instances>`. | |
| 32 | 27 |
| 33 In general an application will only need to deal with setting the policy at the | 28 There is a default policy used by all classes in the email package. This |
| 34 input and output boundaries. Once parsed, a message is represented by a | 29 policy is named :class:`Compat32`, with a corresponding pre-defined instance |
| 35 :class:`~email.message.Message` object, which is designed to be independent of | 30 named :const:`compat32`. It provides for complete backward compatibility (in |
| 36 the format that the message has "on the wire" when it is received, transmitted, | 31 some cases, including bug compatibility) with the pre-Python3.3 version of the |
| 37 or displayed. Thus, a :class:`Policy` can be specified when parsing a message | 32 email package. The first part of this documentation covers only those features |
| 38 to create a :class:`~email.message.Message`, and again when turning the | 33 of :class:`Policy`, an :term:`abstract base class` that defines the features |
| 39 :class:`~email.message.Message` into some other representation. While often a | 34 that are common to all policy objects, including :const:`compat32`. This |
| 40 program will use the same :class:`Policy` for both input and output, the two | 35 includes certain hook methods that are called internally by the email package, |
| 41 can be different. | 36 which a custom policy could override to obtain different behavior. |
| 37 |
| 38 When a :class:`~email.message.Message` object is created, it acquires a policy. |
| 39 By default this will be :const:`compat32`, but a different policy can be |
| 40 specified. If the ``Message`` is created by a :mod:`~email.parser`, a policy |
| 41 passed to the parser will be the policy used by the ``Message`` it creates. If |
| 42 the ``Message`` is created by the program, then the policy can be specified |
| 43 when it is created. When a ``Message`` is passed to a :mod:`~email.generator`, |
| 44 the generator uses the policy from the ``Message`` by default, but you can also |
| 45 pass a specific policy to the generator that will override the one stored on |
| 46 the ``Message`` object. |
| 47 |
| 48 :class:`Policy` instances are immutable, but they can be cloned, accepting the |
| 49 same keyword arguments as the class constructor and returning a new |
| 50 :class:`Policy` instance that is a copy of the original but with the specified |
| 51 attributes values changed. |
| 42 | 52 |
| 43 As an example, the following code could be used to read an email message from a | 53 As an example, the following code could be used to read an email message from a |
| 44 file on disk and pass it to the system ``sendmail`` program on a Unix system:: | 54 file on disk and pass it to the system ``sendmail`` program on a Unix system:: |
| 45 | 55 |
| 46 >>> from email import msg_from_binary_file | 56 >>> from email import msg_from_binary_file |
| 47 >>> from email.generator import BytesGenerator | 57 >>> from email.generator import BytesGenerator |
| 48 >>> import email.policy | |
| 49 >>> from subprocess import Popen, PIPE | 58 >>> from subprocess import Popen, PIPE |
| 50 >>> with open('mymsg.txt', 'b') as f: | 59 >>> with open('mymsg.txt', 'b') as f: |
| 51 ... msg = msg_from_binary_file(f, policy=email.policy.mbox) | 60 ... msg = msg_from_binary_file(f) |
| 52 >>> p = Popen(['sendmail', msg['To'][0].address], stdin=PIPE) | 61 >>> p = Popen(['sendmail', msg['To'][0].address], stdin=PIPE) |
| 53 >>> g = BytesGenerator(p.stdin, policy=email.policy.SMTP) | 62 >>> g = BytesGenerator(p.stdin, policy=msg.policy.clone(linesep='\r\n')) |
| 54 >>> g.flatten(msg) | 63 >>> g.flatten(msg) |
| 55 >>> p.stdin.close() | 64 >>> p.stdin.close() |
| 56 >>> rc = p.wait() | 65 >>> rc = p.wait() |
| 57 | 66 |
| 58 .. XXX email.policy.mbox/MBOX does not exist yet | 67 Here we are telling :class:`~email.generator.BytesGenerator` to use the RFC |
| 68 correct line separator characters when creating the binary string to feed into |
| 69 ``sendmail's`` ``stdin``, where the default policy would use ``\n`` line |
| 70 separators. |
| 59 | 71 |
| 60 Some email package methods accept a *policy* keyword argument, allowing the | 72 Some email package methods accept a *policy* keyword argument, allowing the |
| 61 policy to be overridden for that method. For example, the following code uses | 73 policy to be overridden for that method. For example, the following code uses |
| 62 the :meth:`~email.message.Message.as_string` method of the *msg* object from the | 74 the :meth:`~email.message.Message.as_string` method of the *msg* object from |
| 63 previous example and re-write it to a file using the native line separators for | 75 the previous example and writes the message to a file using the native line |
| 64 the platform on which it is running:: | 76 separators for the platform on which it is running:: |
| 65 | 77 |
| 66 >>> import os | 78 >>> import os |
| 67 >>> mypolicy = email.policy.Policy(linesep=os.linesep) | |
| 68 >>> with open('converted.txt', 'wb') as f: | 79 >>> with open('converted.txt', 'wb') as f: |
| 69 ... f.write(msg.as_string(policy=mypolicy)) | 80 ... f.write(msg.as_string(policy=msg.policy.clone(linesep=os.linesep)) |
| 70 | |
| 71 Policy instances are immutable, but they can be cloned, accepting the same | |
| 72 keyword arguments as the class constructor and returning a new :class:`Policy` | |
| 73 instance that is a copy of the original but with the specified attributes | |
| 74 values changed. For example, the following creates an SMTP policy that will | |
| 75 raise any defects detected as errors:: | |
| 76 | |
| 77 >>> strict_SMTP = email.policy.SMTP.clone(raise_on_defect=True) | |
| 78 | 81 |
| 79 Policy objects can also be combined using the addition operator, producing a | 82 Policy objects can also be combined using the addition operator, producing a |
| 80 policy object whose settings are a combination of the non-default values of the | 83 policy object whose settings are a combination of the non-default values of the |
| 81 summed objects:: | 84 summed objects:: |
| 82 | 85 |
| 83 >>> strict_SMTP = email.policy.SMTP + email.policy.strict | 86 >>> compat_SMTP = email.policy.clone(linesep='\r\n') |
| 87 >>> compat_strict = email.policy.clone(raise_on_defect=True) |
| 88 >>> compat_strict_SMTP = compat_SMTP + compat_strict |
| 84 | 89 |
| 85 This operation is not commutative; that is, the order in which the objects are | 90 This operation is not commutative; that is, the order in which the objects are |
| 86 added matters. To illustrate:: | 91 added matters. To illustrate:: |
| 87 | 92 |
| 88 >>> Policy = email.policy.Policy | 93 >>> policy100 = compat32.clone(max_line_length=100) |
| 89 >>> apolicy = Policy(max_line_length=100) + Policy(max_line_length=80) | 94 >>> policy80 = compat32.clone(max_line_length=80) |
| 95 >>> apolicy = policy100 + Policy80 |
| 90 >>> apolicy.max_line_length | 96 >>> apolicy.max_line_length |
| 91 80 | 97 80 |
| 92 >>> apolicy = Policy(max_line_length=80) + Policy(max_line_length=100) | 98 >>> apolicy = policy80 + policy100 |
| 93 >>> apolicy.max_line_length | 99 >>> apolicy.max_line_length |
| 94 100 | 100 100 |
| 95 | 101 |
| 96 | 102 |
| 97 .. class:: Policy(**kw) | 103 .. class:: Policy(**kw) |
| 98 | 104 |
| 99 The valid constructor keyword arguments are any of the attributes listed | 105 This is the :term:`abstract base class` for all policy classes. It provides |
| 100 below. | 106 default implementations for a couple of trivial methods, as well as the |
| 107 implementation of the immutability property, the :meth:`clone` method, and |
| 108 the constructor semantics. |
| 109 |
| 110 The constructor of a policy class can be passed various keyword arguments. |
| 111 The arguments that may be specified are any non-method properties on this |
| 112 class, plus any additional non-method properties on the concrete class. A |
| 113 value specified in the constructor will override the default value for the |
| 114 corresponding attribute. |
| 115 |
| 116 This class defines the following properties, and thus values for the |
| 117 following may be passed in the constructor of any policy class: |
| 101 | 118 |
| 102 .. attribute:: max_line_length | 119 .. attribute:: max_line_length |
| 103 | 120 |
| 104 The maximum length of any line in the serialized output, not counting the | 121 The maximum length of any line in the serialized output, not counting the |
| 105 end of line character(s). Default is 78, per :rfc:`5322`. A value of | 122 end of line character(s). Default is 78, per :rfc:`5322`. A value of |
| 106 ``0`` or :const:`None` indicates that no line wrapping should be | 123 ``0`` or :const:`None` indicates that no line wrapping should be |
| 107 done at all. | 124 done at all. |
| 108 | 125 |
| 109 .. attribute:: linesep | 126 .. attribute:: linesep |
| 110 | 127 |
| 111 The string to be used to terminate lines in serialized output. The | 128 The string to be used to terminate lines in serialized output. The |
| 112 default is ``\n`` because that's the internal end-of-line discipline used | 129 default is ``\n`` because that's the internal end-of-line discipline used |
| 113 by Python, though ``\r\n`` is required by the RFCs. See `Policy | 130 by Python, though ``\r\n`` is required by the RFCs. |
| 114 Instances`_ for policies that use an RFC conformant linesep. Setting it | |
| 115 to :attr:`os.linesep` may also be useful. | |
| 116 | 131 |
| 117 .. attribute:: must_be_7bit | 132 .. attribute:: cte_type |
| 118 | 133 |
| 119 If ``True``, data output by a bytes generator is limited to ASCII | 134 Controls the type of Content Transfer Encodings that may be or are |
| 120 characters. If :const:`False` (the default), then bytes with the high | 135 required to be used. The possible values are: |
| 121 bit set are preserved and/or allowed in certain contexts (for example, | 136 |
| 122 where possible a content transfer encoding of ``8bit`` will be used). | 137 ``7bit`` -- all data must be "7 bit clean" (ASCII-only). This means |
| 123 String generators act as if ``must_be_7bit`` is ``True`` regardless of | 138 that where necessary data will be encoded using either |
| 124 the policy in effect, since a string cannot represent non-ASCII bytes. | 139 quoted-printable or base64 encoding. |
| 140 |
| 141 ``8bit`` -- data is not constrained to be 7 bit clean. Data in headers |
| 142 is still required to be ASCII-only and so will be encoded |
| 143 (see 'binary_fold' below for an exception), but body parts |
| 144 may use the ``8bit`` CTE. |
| 145 |
| 146 A ``cte_type`` value of ``8bit`` only works with ``BytesGenerator``, not |
| 147 ``Generator``, because strings cannot contain binary data. If a |
| 148 ``Generator`` is operating under a policy that specifies ``cte_type`` |
| 149 ``8bit``, it will act as if *cte_type* is ``7bit``. |
| 125 | 150 |
| 126 .. attribute:: raise_on_defect | 151 .. attribute:: raise_on_defect |
| 127 | 152 |
| 128 If :const:`True`, any defects encountered will be raised as errors. If | 153 If :const:`True`, any defects encountered will be raised as errors. If |
| 129 :const:`False` (the default), defects will be passed to the | 154 :const:`False` (the default), defects will be passed to the |
| 130 :meth:`register_defect` method. | 155 :meth:`register_defect` method. |
| 131 | 156 |
| 132 :mod:`Policy` object also have the following methods: | 157 The following :class:`Policy` method is intended to be called by code using |
| 158 the email library to create policy instances with custom settings: |
| 133 | 159 |
| 134 .. method:: handle_defect(obj, defect) | 160 .. method:: clone(**kw) |
| 135 | |
| 136 *obj* is the object on which to register the defect. *defect* should be | |
| 137 an instance of a subclass of :class:`~email.errors.Defect`. | |
| 138 If :attr:`raise_on_defect` | |
| 139 is ``True`` the defect is raised as an exception. Otherwise *obj* and | |
| 140 *defect* are passed to :meth:`register_defect`. This method is intended | |
| 141 to be called by parsers when they encounter defects, and will not be | |
| 142 called by code that uses the email library unless that code is | |
| 143 implementing an alternate parser. | |
| 144 | |
| 145 .. method:: register_defect(obj, defect) | |
| 146 | |
| 147 *obj* is the object on which to register the defect. *defect* should be | |
| 148 a subclass of :class:`~email.errors.Defect`. This method is part of the | |
| 149 public API so that custom ``Policy`` subclasses can implement alternate | |
| 150 handling of defects. The default implementation calls the ``append`` | |
| 151 method of the ``defects`` attribute of *obj*. | |
| 152 | |
| 153 .. method:: clone(obj, *kw) | |
| 154 | 161 |
| 155 Return a new :class:`Policy` instance whose attributes have the same | 162 Return a new :class:`Policy` instance whose attributes have the same |
| 156 values as the current instance, except where those attributes are | 163 values as the current instance, except where those attributes are |
| 157 given new values by the keyword arguments. | 164 given new values by the keyword arguments. |
| 158 | 165 |
| 166 The remaining :class:`Policy` methods are called by the email package code, |
| 167 and are not intended to be called by an application using the email package. |
| 168 A custom policy must implement all of these methods. |
| 159 | 169 |
| 160 Policy Instances | 170 .. method:: handle_defect(obj, defect) |
| 161 ^^^^^^^^^^^^^^^^ | |
| 162 | 171 |
| 163 The following instances of :class:`Policy` provide defaults suitable for | 172 Handle a *defect* found on *obj*. When the email package calls this |
| 164 specific common application domains. | 173 method, *defect* will always be a subclass of |
| 174 :class:`~email.errors.Defect`. |
| 165 | 175 |
| 166 .. data:: default | 176 The default implementation checks the :attr:`raise_on_defect` flag. If |
| 177 it is ``True``, *defect* is raised as an exception. If it is ``False`` |
| 178 (the default), *obj* and *defect* are passed to :meth:`register_defect`. |
| 167 | 179 |
| 168 An instance of :class:`Policy` with all defaults unchanged. | 180 .. method:: register_defect(obj, defect) |
| 169 | 181 |
| 170 .. data:: SMTP | 182 Register a *defect* found on *obj*. In the email package, *defect* will |
| 183 always be a subclass of :class:`~email.errors.Defect`. |
| 171 | 184 |
| 172 Output serialized from a message will conform to the email and SMTP | 185 The default implementation calls the ``append`` method of the ``defects`` |
| 173 RFCs. The only changed attribute is :attr:`linesep`, which is set to | 186 attribute of *obj*. When the email package calls :attr:`handle_defect`, |
| 174 ``\r\n``. | 187 *obj* will normally have a ``defects`` attribute that has an ``append`` |
| 188 method. Custom object types used with the email package (for example, |
| 189 custom ``Message`` objects) should also provide such an attribute, |
| 190 otherwise defects in parsed messages will raise unexpected errors. |
| 175 | 191 |
| 176 .. data:: HTTP | 192 .. method:: header_source_parse(sourcelines) |
| 177 | 193 |
| 178 Suitable for use when serializing headers for use in HTTP traffic. | 194 The email package calls this method with a list of strings, each string |
| 179 :attr:`linesep` is set to ``\r\n``, and :attr:`max_line_length` is set to | 195 ending with the line separation characters found in the source being |
| 180 :const:`None` (unlimited). | 196 parsed. The first line includes the field header name and separator. |
| 197 All whitespace in the source is preserved. The method should return the |
| 198 ``(name, value)`` tuple that is to be stored in the ``Message`` to |
| 199 represent the parsed header. |
| 181 | 200 |
| 182 .. data:: strict | 201 If an implementation wishes to retain compatibility with the existing |
| 202 email package policies, *name* should be the case preserved name (all |
| 203 characters up to the '``:``' separator), while *value* should be the |
| 204 unfolded value (all line separator characters removed, but whitespace |
| 205 kept intact), stripped of leading whitespace. |
| 183 | 206 |
| 184 :attr:`raise_on_defect` is set to :const:`True`. | 207 *sourcelines* may contain surrogateescaped binary data. |
| 208 |
| 209 There is no default implementation |
| 210 |
| 211 .. method:: header_store_parse(name, value) |
| 212 |
| 213 The email package calls this method with the name and value provided by |
| 214 the application program when the application program is modifying a |
| 215 ``Message`` programmatically (as opposed to a ``Message`` created by a |
| 216 parser). The method should return the ``(name, value)`` tuple that is to |
| 217 be stored in the ``Message`` to represent the header. |
| 218 |
| 219 If an implementation wishes to retain compatibility with the existing |
| 220 email package policies, the *name* and *value* should be strings or |
| 221 string subclasses that do not change the content of the passed in |
| 222 arguments. |
| 223 |
| 224 There is no default implementation |
| 225 |
| 226 .. method:: header_fetch_parse(name, value) |
| 227 |
| 228 The email package calls this method with the *name* and *value* currently |
| 229 stored in the ``Message`` when that header is requested by the |
| 230 application program, and whatever the method returns is what is passed |
| 231 back to the application as the value of the header being retrieved. |
| 232 Note that there may be more than one header with the same name stored in |
| 233 the ``Message``; the method is passed the specific name and value of the |
| 234 header destined to be returned to the application. |
| 235 |
| 236 *value* may contain surrogateescaped binary data. There should be no |
| 237 surrogateescaped binary data in the value returned by the method. |
| 238 |
| 239 There is no default implementation |
| 240 |
| 241 .. method:: fold(name, value) |
| 242 |
| 243 The email package calls this method with the *name* and *value* currently |
| 244 stored in the ``Message`` for a given header. The method should return a |
| 245 string that represents that header "folded" correctly (according to the |
| 246 policy settings) by composing the *name* with the *value* and inserting |
| 247 :attr:`linesep` characters at the appropriate places. See :rfc:`5322` |
| 248 for a discussion of the rules for folding email headers. |
| 249 |
| 250 *value* may contain surrogateescaped binary data. There should be no |
| 251 surrogateescaped binary data in the string returned by the method. |
| 252 |
| 253 .. method:: fold_binary(name, value) |
| 254 |
| 255 The same as :meth:`fold`, except that the returned value should be a |
| 256 bytes object rather than a string. |
| 257 |
| 258 *value* may contain surrogateescaped binary data. These could be |
| 259 converted back into binary data in the returned bytes object. |
| 260 |
| 261 |
| 262 .. class:: Compat32(**kw) |
| 263 |
| 264 This concrete :class:`Policy` is the backward compatibility policy. It |
| 265 replicates the behavior of the email package in Python 3.2. The |
| 266 :mod:`policy` module also defines an instance of this class, |
| 267 :const:`compat32`, that is used as the default policy. Thus the default |
| 268 behavior of the email package is to maintain compatibility with Python 3.2. |
| 269 |
| 270 The class provides the following concrete implementations of the |
| 271 abstract methods of :class:`Policy`: |
| 272 |
| 273 .. method:: header_source_parse(sourcelines) |
| 274 |
| 275 The name is parsed as everything up to the '``:``' and returned |
| 276 unmodified. The value is determined by stripping leading whitespace off |
| 277 the remainder of the first line, joining all subsequent lines together, |
| 278 and stripping any trailing carriage return or linefeed characters. |
| 279 |
| 280 .. method:: header_store_parse(name, value) |
| 281 |
| 282 The name and value are returned unmodified. |
| 283 |
| 284 .. method:: header_fetch_parse(name, value) |
| 285 |
| 286 If the value contains binary data, it is converted into a |
| 287 :class:`~email.header.Header` object using the ``unknown-8bit`` charset. |
| 288 Otherwise it is returned unmodified. |
| 289 |
| 290 .. method:: fold(name, value) |
| 291 |
| 292 Headers are folded using the :class:`~email.header.Header` folding |
| 293 algorithm, which preserves existing line breaks in the value, and wraps |
| 294 each resulting line to the ``max_line_length``. Non-ASCII binary data are |
| 295 CTE encoded using the ``unknown-8bit`` charset. |
| 296 |
| 297 .. method:: fold_binary(name, value) |
| 298 |
| 299 Headers are folded using the :class:`~email.header.Header` folding |
| 300 algorithm, which preserves existing line breaks in the value, and wraps |
| 301 each resulting line to the ``max_line_length``. If ``cte_type`` is |
| 302 ``7bit``, non-ascii binary data is CTE encoded using the ``unknown-8bit`` |
| 303 charset. Otherwise the original source header is used, with its existing |
| 304 line breaks and and any (RFC invalid) binary data it may contain. |
| OLD | NEW |