gooderp18绿色标准版
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

935 linhas
85KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="email.policy: Policy Objects" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/email.policy.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/email/policy.py The email package’s prime focus is the handling of email messages as described by the various email and MIME RFCs. However, the general format of email messages (a ..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/email/policy.py The email package’s prime focus is the handling of email messages as described by the various email and MIME RFCs. However, the general format of email messages (a ..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>email.policy: Policy Objects &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="email.errors: Exception and Defect classes" href="email.errors.html" />
  33. <link rel="prev" title="email.generator: Generating MIME documents" href="email.generator.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/email.policy.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h4>Previous topic</h4>
  84. <p class="topless"><a href="email.generator.html"
  85. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.generator</span></code>: Generating MIME documents</a></p>
  86. </div>
  87. <div>
  88. <h4>Next topic</h4>
  89. <p class="topless"><a href="email.errors.html"
  90. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.errors</span></code>: Exception and Defect classes</a></p>
  91. </div>
  92. <div role="note" aria-label="source link">
  93. <h3>This Page</h3>
  94. <ul class="this-page-menu">
  95. <li><a href="../bugs.html">Report a Bug</a></li>
  96. <li>
  97. <a href="https://github.com/python/cpython/blob/main/Doc/library/email.policy.rst"
  98. rel="nofollow">Show Source
  99. </a>
  100. </li>
  101. </ul>
  102. </div>
  103. </nav>
  104. </div>
  105. </div>
  106. <div class="related" role="navigation" aria-label="related navigation">
  107. <h3>Navigation</h3>
  108. <ul>
  109. <li class="right" style="margin-right: 10px">
  110. <a href="../genindex.html" title="General Index"
  111. accesskey="I">index</a></li>
  112. <li class="right" >
  113. <a href="../py-modindex.html" title="Python Module Index"
  114. >modules</a> |</li>
  115. <li class="right" >
  116. <a href="email.errors.html" title="email.errors: Exception and Defect classes"
  117. accesskey="N">next</a> |</li>
  118. <li class="right" >
  119. <a href="email.generator.html" title="email.generator: Generating MIME documents"
  120. accesskey="P">previous</a> |</li>
  121. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  122. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  123. <li class="switchers">
  124. <div class="language_switcher_placeholder"></div>
  125. <div class="version_switcher_placeholder"></div>
  126. </li>
  127. <li>
  128. </li>
  129. <li id="cpython-language-and-version">
  130. <a href="../index.html">3.12.3 Documentation</a> &#187;
  131. </li>
  132. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  133. <li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> &#187;</li>
  134. <li class="nav-item nav-item-3"><a href="email.html" accesskey="U"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code> — An email and MIME handling package</a> &#187;</li>
  135. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.policy</span></code>: Policy Objects</a></li>
  136. <li class="right">
  137. <div class="inline-search" role="search">
  138. <form class="inline-search" action="../search.html" method="get">
  139. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  140. <input type="submit" value="Go" />
  141. </form>
  142. </div>
  143. |
  144. </li>
  145. <li class="right">
  146. <label class="theme-selector-label">
  147. Theme
  148. <select class="theme-selector" oninput="activateTheme(this.value)">
  149. <option value="auto" selected>Auto</option>
  150. <option value="light">Light</option>
  151. <option value="dark">Dark</option>
  152. </select>
  153. </label> |</li>
  154. </ul>
  155. </div>
  156. <div class="document">
  157. <div class="documentwrapper">
  158. <div class="bodywrapper">
  159. <div class="body" role="main">
  160. <section id="module-email.policy">
  161. <span id="email-policy-policy-objects"></span><h1><a class="reference internal" href="#module-email.policy" title="email.policy: Controlling the parsing and generating of messages"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.policy</span></code></a>: Policy Objects<a class="headerlink" href="#module-email.policy" title="Link to this heading">¶</a></h1>
  162. <div class="versionadded">
  163. <p><span class="versionmodified added">New in version 3.3.</span></p>
  164. </div>
  165. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/email/policy.py">Lib/email/policy.py</a></p>
  166. <hr class="docutils" />
  167. <p>The <a class="reference internal" href="email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code></a> package’s prime focus is the handling of email messages as
  168. described by the various email and MIME RFCs. However, the general format of
  169. email messages (a block of header fields each consisting of a name followed by
  170. a colon followed by a value, the whole block followed by a blank line and an
  171. arbitrary ‘body’), is a format that has found utility outside of the realm of
  172. email. Some of these uses conform fairly closely to the main email RFCs, some
  173. do not. Even when working with email, there are times when it is desirable to
  174. break strict compliance with the RFCs, such as generating emails that
  175. interoperate with email servers that do not themselves follow the standards, or
  176. that implement extensions you want to use in ways that violate the
  177. standards.</p>
  178. <p>Policy objects give the email package the flexibility to handle all these
  179. disparate use cases.</p>
  180. <p>A <a class="reference internal" href="#email.policy.Policy" title="email.policy.Policy"><code class="xref py py-class docutils literal notranslate"><span class="pre">Policy</span></code></a> object encapsulates a set of attributes and methods that
  181. control the behavior of various components of the email package during use.
  182. <a class="reference internal" href="#email.policy.Policy" title="email.policy.Policy"><code class="xref py py-class docutils literal notranslate"><span class="pre">Policy</span></code></a> instances can be passed to various classes and methods in the
  183. email package to alter the default behavior. The settable values and their
  184. defaults are described below.</p>
  185. <p>There is a default policy used by all classes in the email package. For all of
  186. the <a class="reference internal" href="email.parser.html#module-email.parser" title="email.parser: Parse flat text email messages to produce a message object structure."><code class="xref py py-mod docutils literal notranslate"><span class="pre">parser</span></code></a> classes and the related convenience functions, and for
  187. the <a class="reference internal" href="email.compat32-message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> class, this is the <a class="reference internal" href="#email.policy.Compat32" title="email.policy.Compat32"><code class="xref py py-class docutils literal notranslate"><span class="pre">Compat32</span></code></a>
  188. policy, via its corresponding pre-defined instance <a class="reference internal" href="#email.policy.compat32" title="email.policy.compat32"><code class="xref py py-const docutils literal notranslate"><span class="pre">compat32</span></code></a>. This
  189. policy provides for complete backward compatibility (in some cases, including
  190. bug compatibility) with the pre-Python3.3 version of the email package.</p>
  191. <p>This default value for the <em>policy</em> keyword to
  192. <a class="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code></a> is the <a class="reference internal" href="#email.policy.EmailPolicy" title="email.policy.EmailPolicy"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailPolicy</span></code></a> policy, via
  193. its pre-defined instance <a class="reference internal" href="#email.policy.default" title="email.policy.default"><code class="xref py py-data docutils literal notranslate"><span class="pre">default</span></code></a>.</p>
  194. <p>When a <a class="reference internal" href="email.compat32-message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> or <a class="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code></a>
  195. object is created, it acquires a policy. If the message is created by a
  196. <a class="reference internal" href="email.parser.html#module-email.parser" title="email.parser: Parse flat text email messages to produce a message object structure."><code class="xref py py-mod docutils literal notranslate"><span class="pre">parser</span></code></a>, a policy passed to the parser will be the policy used by
  197. the message it creates. If the message is created by the program, then the
  198. policy can be specified when it is created. When a message is passed to a
  199. <a class="reference internal" href="email.generator.html#module-email.generator" title="email.generator: Generate flat text email messages from a message structure."><code class="xref py py-mod docutils literal notranslate"><span class="pre">generator</span></code></a>, the generator uses the policy from the message by
  200. default, but you can also pass a specific policy to the generator that will
  201. override the one stored on the message object.</p>
  202. <p>The default value for the <em>policy</em> keyword for the <a class="reference internal" href="email.parser.html#module-email.parser" title="email.parser: Parse flat text email messages to produce a message object structure."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.parser</span></code></a> classes
  203. and the parser convenience functions <strong>will be changing</strong> in a future version of
  204. Python. Therefore you should <strong>always specify explicitly which policy you want
  205. to use</strong> when calling any of the classes and functions described in the
  206. <a class="reference internal" href="email.parser.html#module-email.parser" title="email.parser: Parse flat text email messages to produce a message object structure."><code class="xref py py-mod docutils literal notranslate"><span class="pre">parser</span></code></a> module.</p>
  207. <p>The first part of this documentation covers the features of <a class="reference internal" href="#email.policy.Policy" title="email.policy.Policy"><code class="xref py py-class docutils literal notranslate"><span class="pre">Policy</span></code></a>, an
  208. <a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">abstract base class</span></a> that defines the features that are common to all
  209. policy objects, including <a class="reference internal" href="#email.policy.compat32" title="email.policy.compat32"><code class="xref py py-const docutils literal notranslate"><span class="pre">compat32</span></code></a>. This includes certain hook
  210. methods that are called internally by the email package, which a custom policy
  211. could override to obtain different behavior. The second part describes the
  212. concrete classes <a class="reference internal" href="#email.policy.EmailPolicy" title="email.policy.EmailPolicy"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailPolicy</span></code></a> and <a class="reference internal" href="#email.policy.Compat32" title="email.policy.Compat32"><code class="xref py py-class docutils literal notranslate"><span class="pre">Compat32</span></code></a>, which implement
  213. the hooks that provide the standard behavior and the backward compatible
  214. behavior and features, respectively.</p>
  215. <p><a class="reference internal" href="#email.policy.Policy" title="email.policy.Policy"><code class="xref py py-class docutils literal notranslate"><span class="pre">Policy</span></code></a> instances are immutable, but they can be cloned, accepting the
  216. same keyword arguments as the class constructor and returning a new
  217. <a class="reference internal" href="#email.policy.Policy" title="email.policy.Policy"><code class="xref py py-class docutils literal notranslate"><span class="pre">Policy</span></code></a> instance that is a copy of the original but with the specified
  218. attributes values changed.</p>
  219. <p>As an example, the following code could be used to read an email message from a
  220. file on disk and pass it to the system <code class="docutils literal notranslate"><span class="pre">sendmail</span></code> program on a Unix system:</p>
  221. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">email</span> <span class="kn">import</span> <span class="n">message_from_binary_file</span>
  222. <span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">email.generator</span> <span class="kn">import</span> <span class="n">BytesGenerator</span>
  223. <span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">email</span> <span class="kn">import</span> <span class="n">policy</span>
  224. <span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">subprocess</span> <span class="kn">import</span> <span class="n">Popen</span><span class="p">,</span> <span class="n">PIPE</span>
  225. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;mymsg.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  226. <span class="gp">... </span> <span class="n">msg</span> <span class="o">=</span> <span class="n">message_from_binary_file</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">policy</span><span class="o">=</span><span class="n">policy</span><span class="o">.</span><span class="n">default</span><span class="p">)</span>
  227. <span class="gp">...</span>
  228. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">([</span><span class="s1">&#39;sendmail&#39;</span><span class="p">,</span> <span class="n">msg</span><span class="p">[</span><span class="s1">&#39;To&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">addresses</span><span class="p">[</span><span class="mi">0</span><span class="p">]],</span> <span class="n">stdin</span><span class="o">=</span><span class="n">PIPE</span><span class="p">)</span>
  229. <span class="gp">&gt;&gt;&gt; </span><span class="n">g</span> <span class="o">=</span> <span class="n">BytesGenerator</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">stdin</span><span class="p">,</span> <span class="n">policy</span><span class="o">=</span><span class="n">msg</span><span class="o">.</span><span class="n">policy</span><span class="o">.</span><span class="n">clone</span><span class="p">(</span><span class="n">linesep</span><span class="o">=</span><span class="s1">&#39;</span><span class="se">\r\n</span><span class="s1">&#39;</span><span class="p">))</span>
  230. <span class="gp">&gt;&gt;&gt; </span><span class="n">g</span><span class="o">.</span><span class="n">flatten</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
  231. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  232. <span class="gp">&gt;&gt;&gt; </span><span class="n">rc</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
  233. </pre></div>
  234. </div>
  235. <p>Here we are telling <a class="reference internal" href="email.generator.html#email.generator.BytesGenerator" title="email.generator.BytesGenerator"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesGenerator</span></code></a> to use the RFC
  236. correct line separator characters when creating the binary string to feed into
  237. <code class="docutils literal notranslate"><span class="pre">sendmail's</span></code> <code class="docutils literal notranslate"><span class="pre">stdin</span></code>, where the default policy would use <code class="docutils literal notranslate"><span class="pre">\n</span></code> line
  238. separators.</p>
  239. <p>Some email package methods accept a <em>policy</em> keyword argument, allowing the
  240. policy to be overridden for that method. For example, the following code uses
  241. the <a class="reference internal" href="email.compat32-message.html#email.message.Message.as_bytes" title="email.message.Message.as_bytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">as_bytes()</span></code></a> method of the <em>msg</em> object from
  242. the previous example and writes the message to a file using the native line
  243. separators for the platform on which it is running:</p>
  244. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">os</span>
  245. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;converted.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;wb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  246. <span class="gp">... </span> <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">msg</span><span class="o">.</span><span class="n">as_bytes</span><span class="p">(</span><span class="n">policy</span><span class="o">=</span><span class="n">msg</span><span class="o">.</span><span class="n">policy</span><span class="o">.</span><span class="n">clone</span><span class="p">(</span><span class="n">linesep</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">linesep</span><span class="p">)))</span>
  247. <span class="go">17</span>
  248. </pre></div>
  249. </div>
  250. <p>Policy objects can also be combined using the addition operator, producing a
  251. policy object whose settings are a combination of the non-default values of the
  252. summed objects:</p>
  253. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compat_SMTP</span> <span class="o">=</span> <span class="n">policy</span><span class="o">.</span><span class="n">compat32</span><span class="o">.</span><span class="n">clone</span><span class="p">(</span><span class="n">linesep</span><span class="o">=</span><span class="s1">&#39;</span><span class="se">\r\n</span><span class="s1">&#39;</span><span class="p">)</span>
  254. <span class="gp">&gt;&gt;&gt; </span><span class="n">compat_strict</span> <span class="o">=</span> <span class="n">policy</span><span class="o">.</span><span class="n">compat32</span><span class="o">.</span><span class="n">clone</span><span class="p">(</span><span class="n">raise_on_defect</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  255. <span class="gp">&gt;&gt;&gt; </span><span class="n">compat_strict_SMTP</span> <span class="o">=</span> <span class="n">compat_SMTP</span> <span class="o">+</span> <span class="n">compat_strict</span>
  256. </pre></div>
  257. </div>
  258. <p>This operation is not commutative; that is, the order in which the objects are
  259. added matters. To illustrate:</p>
  260. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">policy100</span> <span class="o">=</span> <span class="n">policy</span><span class="o">.</span><span class="n">compat32</span><span class="o">.</span><span class="n">clone</span><span class="p">(</span><span class="n">max_line_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
  261. <span class="gp">&gt;&gt;&gt; </span><span class="n">policy80</span> <span class="o">=</span> <span class="n">policy</span><span class="o">.</span><span class="n">compat32</span><span class="o">.</span><span class="n">clone</span><span class="p">(</span><span class="n">max_line_length</span><span class="o">=</span><span class="mi">80</span><span class="p">)</span>
  262. <span class="gp">&gt;&gt;&gt; </span><span class="n">apolicy</span> <span class="o">=</span> <span class="n">policy100</span> <span class="o">+</span> <span class="n">policy80</span>
  263. <span class="gp">&gt;&gt;&gt; </span><span class="n">apolicy</span><span class="o">.</span><span class="n">max_line_length</span>
  264. <span class="go">80</span>
  265. <span class="gp">&gt;&gt;&gt; </span><span class="n">apolicy</span> <span class="o">=</span> <span class="n">policy80</span> <span class="o">+</span> <span class="n">policy100</span>
  266. <span class="gp">&gt;&gt;&gt; </span><span class="n">apolicy</span><span class="o">.</span><span class="n">max_line_length</span>
  267. <span class="go">100</span>
  268. </pre></div>
  269. </div>
  270. <dl class="py class">
  271. <dt class="sig sig-object py" id="email.policy.Policy">
  272. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.policy.</span></span><span class="sig-name descname"><span class="pre">Policy</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kw</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.Policy" title="Link to this definition">¶</a></dt>
  273. <dd><p>This is the <a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">abstract base class</span></a> for all policy classes. It provides
  274. default implementations for a couple of trivial methods, as well as the
  275. implementation of the immutability property, the <a class="reference internal" href="#email.policy.Policy.clone" title="email.policy.Policy.clone"><code class="xref py py-meth docutils literal notranslate"><span class="pre">clone()</span></code></a> method, and
  276. the constructor semantics.</p>
  277. <p>The constructor of a policy class can be passed various keyword arguments.
  278. The arguments that may be specified are any non-method properties on this
  279. class, plus any additional non-method properties on the concrete class. A
  280. value specified in the constructor will override the default value for the
  281. corresponding attribute.</p>
  282. <p>This class defines the following properties, and thus values for the
  283. following may be passed in the constructor of any policy class:</p>
  284. <dl class="py attribute">
  285. <dt class="sig sig-object py" id="email.policy.Policy.max_line_length">
  286. <span class="sig-name descname"><span class="pre">max_line_length</span></span><a class="headerlink" href="#email.policy.Policy.max_line_length" title="Link to this definition">¶</a></dt>
  287. <dd><p>The maximum length of any line in the serialized output, not counting the
  288. end of line character(s). Default is 78, per <span class="target" id="index-0"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5322.html"><strong>RFC 5322</strong></a>. A value of
  289. <code class="docutils literal notranslate"><span class="pre">0</span></code> or <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> indicates that no line wrapping should be
  290. done at all.</p>
  291. </dd></dl>
  292. <dl class="py attribute">
  293. <dt class="sig sig-object py" id="email.policy.Policy.linesep">
  294. <span class="sig-name descname"><span class="pre">linesep</span></span><a class="headerlink" href="#email.policy.Policy.linesep" title="Link to this definition">¶</a></dt>
  295. <dd><p>The string to be used to terminate lines in serialized output. The
  296. default is <code class="docutils literal notranslate"><span class="pre">\n</span></code> because that’s the internal end-of-line discipline used
  297. by Python, though <code class="docutils literal notranslate"><span class="pre">\r\n</span></code> is required by the RFCs.</p>
  298. </dd></dl>
  299. <dl class="py attribute">
  300. <dt class="sig sig-object py" id="email.policy.Policy.cte_type">
  301. <span class="sig-name descname"><span class="pre">cte_type</span></span><a class="headerlink" href="#email.policy.Policy.cte_type" title="Link to this definition">¶</a></dt>
  302. <dd><p>Controls the type of Content Transfer Encodings that may be or are
  303. required to be used. The possible values are:</p>
  304. <table class="docutils align-default">
  305. <tbody>
  306. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">7bit</span></code></p></td>
  307. <td><p>all data must be “7 bit clean” (ASCII-only). This means that
  308. where necessary data will be encoded using either
  309. quoted-printable or base64 encoding.</p></td>
  310. </tr>
  311. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">8bit</span></code></p></td>
  312. <td><p>data is not constrained to be 7 bit clean. Data in headers is
  313. still required to be ASCII-only and so will be encoded (see
  314. <a class="reference internal" href="#email.policy.Policy.fold_binary" title="email.policy.Policy.fold_binary"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fold_binary()</span></code></a> and <a class="reference internal" href="#email.policy.EmailPolicy.utf8" title="email.policy.EmailPolicy.utf8"><code class="xref py py-attr docutils literal notranslate"><span class="pre">utf8</span></code></a> below for
  315. exceptions), but body parts may use the <code class="docutils literal notranslate"><span class="pre">8bit</span></code> CTE.</p></td>
  316. </tr>
  317. </tbody>
  318. </table>
  319. <p>A <code class="docutils literal notranslate"><span class="pre">cte_type</span></code> value of <code class="docutils literal notranslate"><span class="pre">8bit</span></code> only works with <code class="docutils literal notranslate"><span class="pre">BytesGenerator</span></code>, not
  320. <code class="docutils literal notranslate"><span class="pre">Generator</span></code>, because strings cannot contain binary data. If a
  321. <code class="docutils literal notranslate"><span class="pre">Generator</span></code> is operating under a policy that specifies
  322. <code class="docutils literal notranslate"><span class="pre">cte_type=8bit</span></code>, it will act as if <code class="docutils literal notranslate"><span class="pre">cte_type</span></code> is <code class="docutils literal notranslate"><span class="pre">7bit</span></code>.</p>
  323. </dd></dl>
  324. <dl class="py attribute">
  325. <dt class="sig sig-object py" id="email.policy.Policy.raise_on_defect">
  326. <span class="sig-name descname"><span class="pre">raise_on_defect</span></span><a class="headerlink" href="#email.policy.Policy.raise_on_defect" title="Link to this definition">¶</a></dt>
  327. <dd><p>If <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>, any defects encountered will be raised as errors. If
  328. <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> (the default), defects will be passed to the
  329. <a class="reference internal" href="#email.policy.Policy.register_defect" title="email.policy.Policy.register_defect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">register_defect()</span></code></a> method.</p>
  330. </dd></dl>
  331. <dl class="py attribute">
  332. <dt class="sig sig-object py" id="email.policy.Policy.mangle_from_">
  333. <span class="sig-name descname"><span class="pre">mangle_from_</span></span><a class="headerlink" href="#email.policy.Policy.mangle_from_" title="Link to this definition">¶</a></dt>
  334. <dd><p>If <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>, lines starting with <em>“From “</em> in the body are
  335. escaped by putting a <code class="docutils literal notranslate"><span class="pre">&gt;</span></code> in front of them. This parameter is used when
  336. the message is being serialized by a generator.
  337. Default: <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a>.</p>
  338. <div class="versionadded">
  339. <p><span class="versionmodified added">New in version 3.5.</span></p>
  340. </div>
  341. </dd></dl>
  342. <dl class="py attribute">
  343. <dt class="sig sig-object py" id="email.policy.Policy.message_factory">
  344. <span class="sig-name descname"><span class="pre">message_factory</span></span><a class="headerlink" href="#email.policy.Policy.message_factory" title="Link to this definition">¶</a></dt>
  345. <dd><p>A factory function for constructing a new empty message object. Used
  346. by the parser when building messages. Defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code>, in
  347. which case <a class="reference internal" href="email.compat32-message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> is used.</p>
  348. <div class="versionadded">
  349. <p><span class="versionmodified added">New in version 3.6.</span></p>
  350. </div>
  351. </dd></dl>
  352. <p>The following <a class="reference internal" href="#email.policy.Policy" title="email.policy.Policy"><code class="xref py py-class docutils literal notranslate"><span class="pre">Policy</span></code></a> method is intended to be called by code using
  353. the email library to create policy instances with custom settings:</p>
  354. <dl class="py method">
  355. <dt class="sig sig-object py" id="email.policy.Policy.clone">
  356. <span class="sig-name descname"><span class="pre">clone</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kw</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.Policy.clone" title="Link to this definition">¶</a></dt>
  357. <dd><p>Return a new <a class="reference internal" href="#email.policy.Policy" title="email.policy.Policy"><code class="xref py py-class docutils literal notranslate"><span class="pre">Policy</span></code></a> instance whose attributes have the same
  358. values as the current instance, except where those attributes are
  359. given new values by the keyword arguments.</p>
  360. </dd></dl>
  361. <p>The remaining <a class="reference internal" href="#email.policy.Policy" title="email.policy.Policy"><code class="xref py py-class docutils literal notranslate"><span class="pre">Policy</span></code></a> methods are called by the email package code,
  362. and are not intended to be called by an application using the email package.
  363. A custom policy must implement all of these methods.</p>
  364. <dl class="py method">
  365. <dt class="sig sig-object py" id="email.policy.Policy.handle_defect">
  366. <span class="sig-name descname"><span class="pre">handle_defect</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">defect</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.Policy.handle_defect" title="Link to this definition">¶</a></dt>
  367. <dd><p>Handle a <em>defect</em> found on <em>obj</em>. When the email package calls this
  368. method, <em>defect</em> will always be a subclass of
  369. <code class="xref py py-class docutils literal notranslate"><span class="pre">Defect</span></code>.</p>
  370. <p>The default implementation checks the <a class="reference internal" href="#email.policy.Policy.raise_on_defect" title="email.policy.Policy.raise_on_defect"><code class="xref py py-attr docutils literal notranslate"><span class="pre">raise_on_defect</span></code></a> flag. If
  371. it is <code class="docutils literal notranslate"><span class="pre">True</span></code>, <em>defect</em> is raised as an exception. If it is <code class="docutils literal notranslate"><span class="pre">False</span></code>
  372. (the default), <em>obj</em> and <em>defect</em> are passed to <a class="reference internal" href="#email.policy.Policy.register_defect" title="email.policy.Policy.register_defect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">register_defect()</span></code></a>.</p>
  373. </dd></dl>
  374. <dl class="py method">
  375. <dt class="sig sig-object py" id="email.policy.Policy.register_defect">
  376. <span class="sig-name descname"><span class="pre">register_defect</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">defect</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.Policy.register_defect" title="Link to this definition">¶</a></dt>
  377. <dd><p>Register a <em>defect</em> on <em>obj</em>. In the email package, <em>defect</em> will always
  378. be a subclass of <code class="xref py py-class docutils literal notranslate"><span class="pre">Defect</span></code>.</p>
  379. <p>The default implementation calls the <code class="docutils literal notranslate"><span class="pre">append</span></code> method of the <code class="docutils literal notranslate"><span class="pre">defects</span></code>
  380. attribute of <em>obj</em>. When the email package calls <a class="reference internal" href="#email.policy.Policy.handle_defect" title="email.policy.Policy.handle_defect"><code class="xref py py-attr docutils literal notranslate"><span class="pre">handle_defect</span></code></a>,
  381. <em>obj</em> will normally have a <code class="docutils literal notranslate"><span class="pre">defects</span></code> attribute that has an <code class="docutils literal notranslate"><span class="pre">append</span></code>
  382. method. Custom object types used with the email package (for example,
  383. custom <code class="docutils literal notranslate"><span class="pre">Message</span></code> objects) should also provide such an attribute,
  384. otherwise defects in parsed messages will raise unexpected errors.</p>
  385. </dd></dl>
  386. <dl class="py method">
  387. <dt class="sig sig-object py" id="email.policy.Policy.header_max_count">
  388. <span class="sig-name descname"><span class="pre">header_max_count</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.Policy.header_max_count" title="Link to this definition">¶</a></dt>
  389. <dd><p>Return the maximum allowed number of headers named <em>name</em>.</p>
  390. <p>Called when a header is added to an <a class="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code></a>
  391. or <a class="reference internal" href="email.compat32-message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> object. If the returned value is not
  392. <code class="docutils literal notranslate"><span class="pre">0</span></code> or <code class="docutils literal notranslate"><span class="pre">None</span></code>, and there are already a number of headers with the
  393. name <em>name</em> greater than or equal to the value returned, a
  394. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p>
  395. <p>Because the default behavior of <code class="docutils literal notranslate"><span class="pre">Message.__setitem__</span></code> is to append the
  396. value to the list of headers, it is easy to create duplicate headers
  397. without realizing it. This method allows certain headers to be limited
  398. in the number of instances of that header that may be added to a
  399. <code class="docutils literal notranslate"><span class="pre">Message</span></code> programmatically. (The limit is not observed by the parser,
  400. which will faithfully produce as many headers as exist in the message
  401. being parsed.)</p>
  402. <p>The default implementation returns <code class="docutils literal notranslate"><span class="pre">None</span></code> for all header names.</p>
  403. </dd></dl>
  404. <dl class="py method">
  405. <dt class="sig sig-object py" id="email.policy.Policy.header_source_parse">
  406. <span class="sig-name descname"><span class="pre">header_source_parse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sourcelines</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.Policy.header_source_parse" title="Link to this definition">¶</a></dt>
  407. <dd><p>The email package calls this method with a list of strings, each string
  408. ending with the line separation characters found in the source being
  409. parsed. The first line includes the field header name and separator.
  410. All whitespace in the source is preserved. The method should return the
  411. <code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">value)</span></code> tuple that is to be stored in the <code class="docutils literal notranslate"><span class="pre">Message</span></code> to
  412. represent the parsed header.</p>
  413. <p>If an implementation wishes to retain compatibility with the existing
  414. email package policies, <em>name</em> should be the case preserved name (all
  415. characters up to the ‘<code class="docutils literal notranslate"><span class="pre">:</span></code>’ separator), while <em>value</em> should be the
  416. unfolded value (all line separator characters removed, but whitespace
  417. kept intact), stripped of leading whitespace.</p>
  418. <p><em>sourcelines</em> may contain surrogateescaped binary data.</p>
  419. <p>There is no default implementation</p>
  420. </dd></dl>
  421. <dl class="py method">
  422. <dt class="sig sig-object py" id="email.policy.Policy.header_store_parse">
  423. <span class="sig-name descname"><span class="pre">header_store_parse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.Policy.header_store_parse" title="Link to this definition">¶</a></dt>
  424. <dd><p>The email package calls this method with the name and value provided by
  425. the application program when the application program is modifying a
  426. <code class="docutils literal notranslate"><span class="pre">Message</span></code> programmatically (as opposed to a <code class="docutils literal notranslate"><span class="pre">Message</span></code> created by a
  427. parser). The method should return the <code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">value)</span></code> tuple that is to
  428. be stored in the <code class="docutils literal notranslate"><span class="pre">Message</span></code> to represent the header.</p>
  429. <p>If an implementation wishes to retain compatibility with the existing
  430. email package policies, the <em>name</em> and <em>value</em> should be strings or
  431. string subclasses that do not change the content of the passed in
  432. arguments.</p>
  433. <p>There is no default implementation</p>
  434. </dd></dl>
  435. <dl class="py method">
  436. <dt class="sig sig-object py" id="email.policy.Policy.header_fetch_parse">
  437. <span class="sig-name descname"><span class="pre">header_fetch_parse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.Policy.header_fetch_parse" title="Link to this definition">¶</a></dt>
  438. <dd><p>The email package calls this method with the <em>name</em> and <em>value</em> currently
  439. stored in the <code class="docutils literal notranslate"><span class="pre">Message</span></code> when that header is requested by the
  440. application program, and whatever the method returns is what is passed
  441. back to the application as the value of the header being retrieved.
  442. Note that there may be more than one header with the same name stored in
  443. the <code class="docutils literal notranslate"><span class="pre">Message</span></code>; the method is passed the specific name and value of the
  444. header destined to be returned to the application.</p>
  445. <p><em>value</em> may contain surrogateescaped binary data. There should be no
  446. surrogateescaped binary data in the value returned by the method.</p>
  447. <p>There is no default implementation</p>
  448. </dd></dl>
  449. <dl class="py method">
  450. <dt class="sig sig-object py" id="email.policy.Policy.fold">
  451. <span class="sig-name descname"><span class="pre">fold</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.Policy.fold" title="Link to this definition">¶</a></dt>
  452. <dd><p>The email package calls this method with the <em>name</em> and <em>value</em> currently
  453. stored in the <code class="docutils literal notranslate"><span class="pre">Message</span></code> for a given header. The method should return a
  454. string that represents that header “folded” correctly (according to the
  455. policy settings) by composing the <em>name</em> with the <em>value</em> and inserting
  456. <a class="reference internal" href="#email.policy.Policy.linesep" title="email.policy.Policy.linesep"><code class="xref py py-attr docutils literal notranslate"><span class="pre">linesep</span></code></a> characters at the appropriate places. See <span class="target" id="index-1"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5322.html"><strong>RFC 5322</strong></a>
  457. for a discussion of the rules for folding email headers.</p>
  458. <p><em>value</em> may contain surrogateescaped binary data. There should be no
  459. surrogateescaped binary data in the string returned by the method.</p>
  460. </dd></dl>
  461. <dl class="py method">
  462. <dt class="sig sig-object py" id="email.policy.Policy.fold_binary">
  463. <span class="sig-name descname"><span class="pre">fold_binary</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.Policy.fold_binary" title="Link to this definition">¶</a></dt>
  464. <dd><p>The same as <a class="reference internal" href="#email.policy.Policy.fold" title="email.policy.Policy.fold"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fold()</span></code></a>, except that the returned value should be a
  465. bytes object rather than a string.</p>
  466. <p><em>value</em> may contain surrogateescaped binary data. These could be
  467. converted back into binary data in the returned bytes object.</p>
  468. </dd></dl>
  469. </dd></dl>
  470. <dl class="py class">
  471. <dt class="sig sig-object py" id="email.policy.EmailPolicy">
  472. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.policy.</span></span><span class="sig-name descname"><span class="pre">EmailPolicy</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kw</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.EmailPolicy" title="Link to this definition">¶</a></dt>
  473. <dd><p>This concrete <a class="reference internal" href="#email.policy.Policy" title="email.policy.Policy"><code class="xref py py-class docutils literal notranslate"><span class="pre">Policy</span></code></a> provides behavior that is intended to be fully
  474. compliant with the current email RFCs. These include (but are not limited
  475. to) <span class="target" id="index-2"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5322.html"><strong>RFC 5322</strong></a>, <span class="target" id="index-3"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2047.html"><strong>RFC 2047</strong></a>, and the current MIME RFCs.</p>
  476. <p>This policy adds new header parsing and folding algorithms. Instead of
  477. simple strings, headers are <code class="docutils literal notranslate"><span class="pre">str</span></code> subclasses with attributes that depend
  478. on the type of the field. The parsing and folding algorithm fully implement
  479. <span class="target" id="index-4"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2047.html"><strong>RFC 2047</strong></a> and <span class="target" id="index-5"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5322.html"><strong>RFC 5322</strong></a>.</p>
  480. <p>The default value for the <a class="reference internal" href="#email.policy.Policy.message_factory" title="email.policy.Policy.message_factory"><code class="xref py py-attr docutils literal notranslate"><span class="pre">message_factory</span></code></a>
  481. attribute is <a class="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code></a>.</p>
  482. <p>In addition to the settable attributes listed above that apply to all
  483. policies, this policy adds the following additional attributes:</p>
  484. <div class="versionadded">
  485. <p><span class="versionmodified added">New in version 3.6: </span><a class="footnote-reference brackets" href="#id2" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p>
  486. </div>
  487. <dl class="py attribute">
  488. <dt class="sig sig-object py" id="email.policy.EmailPolicy.utf8">
  489. <span class="sig-name descname"><span class="pre">utf8</span></span><a class="headerlink" href="#email.policy.EmailPolicy.utf8" title="Link to this definition">¶</a></dt>
  490. <dd><p>If <code class="docutils literal notranslate"><span class="pre">False</span></code>, follow <span class="target" id="index-6"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5322.html"><strong>RFC 5322</strong></a>, supporting non-ASCII characters in
  491. headers by encoding them as “encoded words”. If <code class="docutils literal notranslate"><span class="pre">True</span></code>, follow
  492. <span class="target" id="index-7"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc6532.html"><strong>RFC 6532</strong></a> and use <code class="docutils literal notranslate"><span class="pre">utf-8</span></code> encoding for headers. Messages
  493. formatted in this way may be passed to SMTP servers that support
  494. the <code class="docutils literal notranslate"><span class="pre">SMTPUTF8</span></code> extension (<span class="target" id="index-8"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc6531.html"><strong>RFC 6531</strong></a>).</p>
  495. </dd></dl>
  496. <dl class="py attribute">
  497. <dt class="sig sig-object py" id="email.policy.EmailPolicy.refold_source">
  498. <span class="sig-name descname"><span class="pre">refold_source</span></span><a class="headerlink" href="#email.policy.EmailPolicy.refold_source" title="Link to this definition">¶</a></dt>
  499. <dd><p>If the value for a header in the <code class="docutils literal notranslate"><span class="pre">Message</span></code> object originated from a
  500. <a class="reference internal" href="email.parser.html#module-email.parser" title="email.parser: Parse flat text email messages to produce a message object structure."><code class="xref py py-mod docutils literal notranslate"><span class="pre">parser</span></code></a> (as opposed to being set by a program), this
  501. attribute indicates whether or not a generator should refold that value
  502. when transforming the message back into serialized form. The possible
  503. values are:</p>
  504. <table class="docutils align-default">
  505. <tbody>
  506. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">none</span></code></p></td>
  507. <td><p>all source values use original folding</p></td>
  508. </tr>
  509. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">long</span></code></p></td>
  510. <td><p>source values that have any line that is longer than
  511. <code class="docutils literal notranslate"><span class="pre">max_line_length</span></code> will be refolded</p></td>
  512. </tr>
  513. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">all</span></code></p></td>
  514. <td><p>all values are refolded.</p></td>
  515. </tr>
  516. </tbody>
  517. </table>
  518. <p>The default is <code class="docutils literal notranslate"><span class="pre">long</span></code>.</p>
  519. </dd></dl>
  520. <dl class="py attribute">
  521. <dt class="sig sig-object py" id="email.policy.EmailPolicy.header_factory">
  522. <span class="sig-name descname"><span class="pre">header_factory</span></span><a class="headerlink" href="#email.policy.EmailPolicy.header_factory" title="Link to this definition">¶</a></dt>
  523. <dd><p>A callable that takes two arguments, <code class="docutils literal notranslate"><span class="pre">name</span></code> and <code class="docutils literal notranslate"><span class="pre">value</span></code>, where
  524. <code class="docutils literal notranslate"><span class="pre">name</span></code> is a header field name and <code class="docutils literal notranslate"><span class="pre">value</span></code> is an unfolded header field
  525. value, and returns a string subclass that represents that header. A
  526. default <code class="docutils literal notranslate"><span class="pre">header_factory</span></code> (see <a class="reference internal" href="email.headerregistry.html#module-email.headerregistry" title="email.headerregistry: Automatic Parsing of headers based on the field name"><code class="xref py py-mod docutils literal notranslate"><span class="pre">headerregistry</span></code></a>) is provided
  527. that supports custom parsing for the various address and date <span class="target" id="index-9"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5322.html"><strong>RFC 5322</strong></a>
  528. header field types, and the major MIME header field stypes. Support for
  529. additional custom parsing will be added in the future.</p>
  530. </dd></dl>
  531. <dl class="py attribute">
  532. <dt class="sig sig-object py" id="email.policy.EmailPolicy.content_manager">
  533. <span class="sig-name descname"><span class="pre">content_manager</span></span><a class="headerlink" href="#email.policy.EmailPolicy.content_manager" title="Link to this definition">¶</a></dt>
  534. <dd><p>An object with at least two methods: get_content and set_content. When
  535. the <a class="reference internal" href="email.message.html#email.message.EmailMessage.get_content" title="email.message.EmailMessage.get_content"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_content()</span></code></a> or
  536. <a class="reference internal" href="email.message.html#email.message.EmailMessage.set_content" title="email.message.EmailMessage.set_content"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_content()</span></code></a> method of an
  537. <a class="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code></a> object is called, it calls the
  538. corresponding method of this object, passing it the message object as its
  539. first argument, and any arguments or keywords that were passed to it as
  540. additional arguments. By default <code class="docutils literal notranslate"><span class="pre">content_manager</span></code> is set to
  541. <a class="reference internal" href="email.contentmanager.html#email.contentmanager.raw_data_manager" title="email.contentmanager.raw_data_manager"><code class="xref py py-data docutils literal notranslate"><span class="pre">raw_data_manager</span></code></a>.</p>
  542. <div class="versionadded">
  543. <p><span class="versionmodified added">New in version 3.4.</span></p>
  544. </div>
  545. </dd></dl>
  546. <p>The class provides the following concrete implementations of the abstract
  547. methods of <a class="reference internal" href="#email.policy.Policy" title="email.policy.Policy"><code class="xref py py-class docutils literal notranslate"><span class="pre">Policy</span></code></a>:</p>
  548. <dl class="py method">
  549. <dt class="sig sig-object py" id="email.policy.EmailPolicy.header_max_count">
  550. <span class="sig-name descname"><span class="pre">header_max_count</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.EmailPolicy.header_max_count" title="Link to this definition">¶</a></dt>
  551. <dd><p>Returns the value of the
  552. <a class="reference internal" href="email.headerregistry.html#email.headerregistry.BaseHeader.max_count" title="email.headerregistry.BaseHeader.max_count"><code class="xref py py-attr docutils literal notranslate"><span class="pre">max_count</span></code></a> attribute of the
  553. specialized class used to represent the header with the given name.</p>
  554. </dd></dl>
  555. <dl class="py method">
  556. <dt class="sig sig-object py" id="email.policy.EmailPolicy.header_source_parse">
  557. <span class="sig-name descname"><span class="pre">header_source_parse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sourcelines</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.EmailPolicy.header_source_parse" title="Link to this definition">¶</a></dt>
  558. <dd><p>The name is parsed as everything up to the ‘<code class="docutils literal notranslate"><span class="pre">:</span></code>’ and returned
  559. unmodified. The value is determined by stripping leading whitespace off
  560. the remainder of the first line, joining all subsequent lines together,
  561. and stripping any trailing carriage return or linefeed characters.</p>
  562. </dd></dl>
  563. <dl class="py method">
  564. <dt class="sig sig-object py" id="email.policy.EmailPolicy.header_store_parse">
  565. <span class="sig-name descname"><span class="pre">header_store_parse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.EmailPolicy.header_store_parse" title="Link to this definition">¶</a></dt>
  566. <dd><p>The name is returned unchanged. If the input value has a <code class="docutils literal notranslate"><span class="pre">name</span></code>
  567. attribute and it matches <em>name</em> ignoring case, the value is returned
  568. unchanged. Otherwise the <em>name</em> and <em>value</em> are passed to
  569. <code class="docutils literal notranslate"><span class="pre">header_factory</span></code>, and the resulting header object is returned as
  570. the value. In this case a <code class="docutils literal notranslate"><span class="pre">ValueError</span></code> is raised if the input value
  571. contains CR or LF characters.</p>
  572. </dd></dl>
  573. <dl class="py method">
  574. <dt class="sig sig-object py" id="email.policy.EmailPolicy.header_fetch_parse">
  575. <span class="sig-name descname"><span class="pre">header_fetch_parse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.EmailPolicy.header_fetch_parse" title="Link to this definition">¶</a></dt>
  576. <dd><p>If the value has a <code class="docutils literal notranslate"><span class="pre">name</span></code> attribute, it is returned to unmodified.
  577. Otherwise the <em>name</em>, and the <em>value</em> with any CR or LF characters
  578. removed, are passed to the <code class="docutils literal notranslate"><span class="pre">header_factory</span></code>, and the resulting
  579. header object is returned. Any surrogateescaped bytes get turned into
  580. the unicode unknown-character glyph.</p>
  581. </dd></dl>
  582. <dl class="py method">
  583. <dt class="sig sig-object py" id="email.policy.EmailPolicy.fold">
  584. <span class="sig-name descname"><span class="pre">fold</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.EmailPolicy.fold" title="Link to this definition">¶</a></dt>
  585. <dd><p>Header folding is controlled by the <a class="reference internal" href="#email.policy.EmailPolicy.refold_source" title="email.policy.EmailPolicy.refold_source"><code class="xref py py-attr docutils literal notranslate"><span class="pre">refold_source</span></code></a> policy setting.
  586. A value is considered to be a ‘source value’ if and only if it does not
  587. have a <code class="docutils literal notranslate"><span class="pre">name</span></code> attribute (having a <code class="docutils literal notranslate"><span class="pre">name</span></code> attribute means it is a
  588. header object of some sort). If a source value needs to be refolded
  589. according to the policy, it is converted into a header object by
  590. passing the <em>name</em> and the <em>value</em> with any CR and LF characters removed
  591. to the <code class="docutils literal notranslate"><span class="pre">header_factory</span></code>. Folding of a header object is done by
  592. calling its <code class="docutils literal notranslate"><span class="pre">fold</span></code> method with the current policy.</p>
  593. <p>Source values are split into lines using <a class="reference internal" href="stdtypes.html#str.splitlines" title="str.splitlines"><code class="xref py py-meth docutils literal notranslate"><span class="pre">splitlines()</span></code></a>. If
  594. the value is not to be refolded, the lines are rejoined using the
  595. <code class="docutils literal notranslate"><span class="pre">linesep</span></code> from the policy and returned. The exception is lines
  596. containing non-ascii binary data. In that case the value is refolded
  597. regardless of the <code class="docutils literal notranslate"><span class="pre">refold_source</span></code> setting, which causes the binary data
  598. to be CTE encoded using the <code class="docutils literal notranslate"><span class="pre">unknown-8bit</span></code> charset.</p>
  599. </dd></dl>
  600. <dl class="py method">
  601. <dt class="sig sig-object py" id="email.policy.EmailPolicy.fold_binary">
  602. <span class="sig-name descname"><span class="pre">fold_binary</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.EmailPolicy.fold_binary" title="Link to this definition">¶</a></dt>
  603. <dd><p>The same as <a class="reference internal" href="#email.policy.EmailPolicy.fold" title="email.policy.EmailPolicy.fold"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fold()</span></code></a> if <a class="reference internal" href="#email.policy.Policy.cte_type" title="email.policy.Policy.cte_type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">cte_type</span></code></a> is <code class="docutils literal notranslate"><span class="pre">7bit</span></code>, except
  604. that the returned value is bytes.</p>
  605. <p>If <a class="reference internal" href="#email.policy.Policy.cte_type" title="email.policy.Policy.cte_type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">cte_type</span></code></a> is <code class="docutils literal notranslate"><span class="pre">8bit</span></code>, non-ASCII binary data is
  606. converted back
  607. into bytes. Headers with binary data are not refolded, regardless of the
  608. <code class="docutils literal notranslate"><span class="pre">refold_header</span></code> setting, since there is no way to know whether the
  609. binary data consists of single byte characters or multibyte characters.</p>
  610. </dd></dl>
  611. </dd></dl>
  612. <p>The following instances of <a class="reference internal" href="#email.policy.EmailPolicy" title="email.policy.EmailPolicy"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailPolicy</span></code></a> provide defaults suitable for
  613. specific application domains. Note that in the future the behavior of these
  614. instances (in particular the <code class="docutils literal notranslate"><span class="pre">HTTP</span></code> instance) may be adjusted to conform even
  615. more closely to the RFCs relevant to their domains.</p>
  616. <dl class="py data">
  617. <dt class="sig sig-object py" id="email.policy.default">
  618. <span class="sig-prename descclassname"><span class="pre">email.policy.</span></span><span class="sig-name descname"><span class="pre">default</span></span><a class="headerlink" href="#email.policy.default" title="Link to this definition">¶</a></dt>
  619. <dd><p>An instance of <code class="docutils literal notranslate"><span class="pre">EmailPolicy</span></code> with all defaults unchanged. This policy
  620. uses the standard Python <code class="docutils literal notranslate"><span class="pre">\n</span></code> line endings rather than the RFC-correct
  621. <code class="docutils literal notranslate"><span class="pre">\r\n</span></code>.</p>
  622. </dd></dl>
  623. <dl class="py data">
  624. <dt class="sig sig-object py" id="email.policy.SMTP">
  625. <span class="sig-prename descclassname"><span class="pre">email.policy.</span></span><span class="sig-name descname"><span class="pre">SMTP</span></span><a class="headerlink" href="#email.policy.SMTP" title="Link to this definition">¶</a></dt>
  626. <dd><p>Suitable for serializing messages in conformance with the email RFCs.
  627. Like <code class="docutils literal notranslate"><span class="pre">default</span></code>, but with <code class="docutils literal notranslate"><span class="pre">linesep</span></code> set to <code class="docutils literal notranslate"><span class="pre">\r\n</span></code>, which is RFC
  628. compliant.</p>
  629. </dd></dl>
  630. <dl class="py data">
  631. <dt class="sig sig-object py" id="email.policy.SMTPUTF8">
  632. <span class="sig-prename descclassname"><span class="pre">email.policy.</span></span><span class="sig-name descname"><span class="pre">SMTPUTF8</span></span><a class="headerlink" href="#email.policy.SMTPUTF8" title="Link to this definition">¶</a></dt>
  633. <dd><p>The same as <code class="docutils literal notranslate"><span class="pre">SMTP</span></code> except that <a class="reference internal" href="#email.policy.EmailPolicy.utf8" title="email.policy.EmailPolicy.utf8"><code class="xref py py-attr docutils literal notranslate"><span class="pre">utf8</span></code></a> is <code class="docutils literal notranslate"><span class="pre">True</span></code>.
  634. Useful for serializing messages to a message store without using encoded
  635. words in the headers. Should only be used for SMTP transmission if the
  636. sender or recipient addresses have non-ASCII characters (the
  637. <a class="reference internal" href="smtplib.html#smtplib.SMTP.send_message" title="smtplib.SMTP.send_message"><code class="xref py py-meth docutils literal notranslate"><span class="pre">smtplib.SMTP.send_message()</span></code></a> method handles this automatically).</p>
  638. </dd></dl>
  639. <dl class="py data">
  640. <dt class="sig sig-object py" id="email.policy.HTTP">
  641. <span class="sig-prename descclassname"><span class="pre">email.policy.</span></span><span class="sig-name descname"><span class="pre">HTTP</span></span><a class="headerlink" href="#email.policy.HTTP" title="Link to this definition">¶</a></dt>
  642. <dd><p>Suitable for serializing headers with for use in HTTP traffic. Like
  643. <code class="docutils literal notranslate"><span class="pre">SMTP</span></code> except that <code class="docutils literal notranslate"><span class="pre">max_line_length</span></code> is set to <code class="docutils literal notranslate"><span class="pre">None</span></code> (unlimited).</p>
  644. </dd></dl>
  645. <dl class="py data">
  646. <dt class="sig sig-object py" id="email.policy.strict">
  647. <span class="sig-prename descclassname"><span class="pre">email.policy.</span></span><span class="sig-name descname"><span class="pre">strict</span></span><a class="headerlink" href="#email.policy.strict" title="Link to this definition">¶</a></dt>
  648. <dd><p>Convenience instance. The same as <code class="docutils literal notranslate"><span class="pre">default</span></code> except that
  649. <code class="docutils literal notranslate"><span class="pre">raise_on_defect</span></code> is set to <code class="docutils literal notranslate"><span class="pre">True</span></code>. This allows any policy to be made
  650. strict by writing:</p>
  651. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">somepolicy</span> <span class="o">+</span> <span class="n">policy</span><span class="o">.</span><span class="n">strict</span>
  652. </pre></div>
  653. </div>
  654. </dd></dl>
  655. <p>With all of these <a class="reference internal" href="#email.policy.EmailPolicy" title="email.policy.EmailPolicy"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailPolicies</span></code></a>, the effective API of
  656. the email package is changed from the Python 3.2 API in the following ways:</p>
  657. <ul class="simple">
  658. <li><p>Setting a header on a <a class="reference internal" href="email.compat32-message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> results in that
  659. header being parsed and a header object created.</p></li>
  660. <li><p>Fetching a header value from a <a class="reference internal" href="email.compat32-message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> results
  661. in that header being parsed and a header object created and
  662. returned.</p></li>
  663. <li><p>Any header object, or any header that is refolded due to the
  664. policy settings, is folded using an algorithm that fully implements the
  665. RFC folding algorithms, including knowing where encoded words are required
  666. and allowed.</p></li>
  667. </ul>
  668. <p>From the application view, this means that any header obtained through the
  669. <a class="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code></a> is a header object with extra
  670. attributes, whose string value is the fully decoded unicode value of the
  671. header. Likewise, a header may be assigned a new value, or a new header
  672. created, using a unicode string, and the policy will take care of converting
  673. the unicode string into the correct RFC encoded form.</p>
  674. <p>The header objects and their attributes are described in
  675. <a class="reference internal" href="email.headerregistry.html#module-email.headerregistry" title="email.headerregistry: Automatic Parsing of headers based on the field name"><code class="xref py py-mod docutils literal notranslate"><span class="pre">headerregistry</span></code></a>.</p>
  676. <dl class="py class">
  677. <dt class="sig sig-object py" id="email.policy.Compat32">
  678. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.policy.</span></span><span class="sig-name descname"><span class="pre">Compat32</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kw</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.Compat32" title="Link to this definition">¶</a></dt>
  679. <dd><p>This concrete <a class="reference internal" href="#email.policy.Policy" title="email.policy.Policy"><code class="xref py py-class docutils literal notranslate"><span class="pre">Policy</span></code></a> is the backward compatibility policy. It
  680. replicates the behavior of the email package in Python 3.2. The
  681. <a class="reference internal" href="#module-email.policy" title="email.policy: Controlling the parsing and generating of messages"><code class="xref py py-mod docutils literal notranslate"><span class="pre">policy</span></code></a> module also defines an instance of this class,
  682. <a class="reference internal" href="#email.policy.compat32" title="email.policy.compat32"><code class="xref py py-const docutils literal notranslate"><span class="pre">compat32</span></code></a>, that is used as the default policy. Thus the default
  683. behavior of the email package is to maintain compatibility with Python 3.2.</p>
  684. <p>The following attributes have values that are different from the
  685. <a class="reference internal" href="#email.policy.Policy" title="email.policy.Policy"><code class="xref py py-class docutils literal notranslate"><span class="pre">Policy</span></code></a> default:</p>
  686. <dl class="py attribute">
  687. <dt class="sig sig-object py" id="email.policy.Compat32.mangle_from_">
  688. <span class="sig-name descname"><span class="pre">mangle_from_</span></span><a class="headerlink" href="#email.policy.Compat32.mangle_from_" title="Link to this definition">¶</a></dt>
  689. <dd><p>The default is <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  690. </dd></dl>
  691. <p>The class provides the following concrete implementations of the
  692. abstract methods of <a class="reference internal" href="#email.policy.Policy" title="email.policy.Policy"><code class="xref py py-class docutils literal notranslate"><span class="pre">Policy</span></code></a>:</p>
  693. <dl class="py method">
  694. <dt class="sig sig-object py" id="email.policy.Compat32.header_source_parse">
  695. <span class="sig-name descname"><span class="pre">header_source_parse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sourcelines</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.Compat32.header_source_parse" title="Link to this definition">¶</a></dt>
  696. <dd><p>The name is parsed as everything up to the ‘<code class="docutils literal notranslate"><span class="pre">:</span></code>’ and returned
  697. unmodified. The value is determined by stripping leading whitespace off
  698. the remainder of the first line, joining all subsequent lines together,
  699. and stripping any trailing carriage return or linefeed characters.</p>
  700. </dd></dl>
  701. <dl class="py method">
  702. <dt class="sig sig-object py" id="email.policy.Compat32.header_store_parse">
  703. <span class="sig-name descname"><span class="pre">header_store_parse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.Compat32.header_store_parse" title="Link to this definition">¶</a></dt>
  704. <dd><p>The name and value are returned unmodified.</p>
  705. </dd></dl>
  706. <dl class="py method">
  707. <dt class="sig sig-object py" id="email.policy.Compat32.header_fetch_parse">
  708. <span class="sig-name descname"><span class="pre">header_fetch_parse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.Compat32.header_fetch_parse" title="Link to this definition">¶</a></dt>
  709. <dd><p>If the value contains binary data, it is converted into a
  710. <a class="reference internal" href="email.header.html#email.header.Header" title="email.header.Header"><code class="xref py py-class docutils literal notranslate"><span class="pre">Header</span></code></a> object using the <code class="docutils literal notranslate"><span class="pre">unknown-8bit</span></code> charset.
  711. Otherwise it is returned unmodified.</p>
  712. </dd></dl>
  713. <dl class="py method">
  714. <dt class="sig sig-object py" id="email.policy.Compat32.fold">
  715. <span class="sig-name descname"><span class="pre">fold</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.Compat32.fold" title="Link to this definition">¶</a></dt>
  716. <dd><p>Headers are folded using the <a class="reference internal" href="email.header.html#email.header.Header" title="email.header.Header"><code class="xref py py-class docutils literal notranslate"><span class="pre">Header</span></code></a> folding
  717. algorithm, which preserves existing line breaks in the value, and wraps
  718. each resulting line to the <code class="docutils literal notranslate"><span class="pre">max_line_length</span></code>. Non-ASCII binary data are
  719. CTE encoded using the <code class="docutils literal notranslate"><span class="pre">unknown-8bit</span></code> charset.</p>
  720. </dd></dl>
  721. <dl class="py method">
  722. <dt class="sig sig-object py" id="email.policy.Compat32.fold_binary">
  723. <span class="sig-name descname"><span class="pre">fold_binary</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.policy.Compat32.fold_binary" title="Link to this definition">¶</a></dt>
  724. <dd><p>Headers are folded using the <a class="reference internal" href="email.header.html#email.header.Header" title="email.header.Header"><code class="xref py py-class docutils literal notranslate"><span class="pre">Header</span></code></a> folding
  725. algorithm, which preserves existing line breaks in the value, and wraps
  726. each resulting line to the <code class="docutils literal notranslate"><span class="pre">max_line_length</span></code>. If <code class="docutils literal notranslate"><span class="pre">cte_type</span></code> is
  727. <code class="docutils literal notranslate"><span class="pre">7bit</span></code>, non-ascii binary data is CTE encoded using the <code class="docutils literal notranslate"><span class="pre">unknown-8bit</span></code>
  728. charset. Otherwise the original source header is used, with its existing
  729. line breaks and any (RFC invalid) binary data it may contain.</p>
  730. </dd></dl>
  731. </dd></dl>
  732. <dl class="py data">
  733. <dt class="sig sig-object py" id="email.policy.compat32">
  734. <span class="sig-prename descclassname"><span class="pre">email.policy.</span></span><span class="sig-name descname"><span class="pre">compat32</span></span><a class="headerlink" href="#email.policy.compat32" title="Link to this definition">¶</a></dt>
  735. <dd><p>An instance of <a class="reference internal" href="#email.policy.Compat32" title="email.policy.Compat32"><code class="xref py py-class docutils literal notranslate"><span class="pre">Compat32</span></code></a>, providing backward compatibility with the
  736. behavior of the email package in Python 3.2.</p>
  737. </dd></dl>
  738. <p class="rubric">Footnotes</p>
  739. <aside class="footnote-list brackets">
  740. <aside class="footnote brackets" id="id2" role="doc-footnote">
  741. <span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id1">1</a><span class="fn-bracket">]</span></span>
  742. <p>Originally added in 3.3 as a <a class="reference internal" href="../glossary.html#term-provisional-package"><span class="xref std std-term">provisional feature</span></a>.</p>
  743. </aside>
  744. </aside>
  745. </section>
  746. <div class="clearer"></div>
  747. </div>
  748. </div>
  749. </div>
  750. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  751. <div class="sphinxsidebarwrapper">
  752. <div>
  753. <h4>Previous topic</h4>
  754. <p class="topless"><a href="email.generator.html"
  755. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.generator</span></code>: Generating MIME documents</a></p>
  756. </div>
  757. <div>
  758. <h4>Next topic</h4>
  759. <p class="topless"><a href="email.errors.html"
  760. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.errors</span></code>: Exception and Defect classes</a></p>
  761. </div>
  762. <div role="note" aria-label="source link">
  763. <h3>This Page</h3>
  764. <ul class="this-page-menu">
  765. <li><a href="../bugs.html">Report a Bug</a></li>
  766. <li>
  767. <a href="https://github.com/python/cpython/blob/main/Doc/library/email.policy.rst"
  768. rel="nofollow">Show Source
  769. </a>
  770. </li>
  771. </ul>
  772. </div>
  773. </div>
  774. <div id="sidebarbutton" title="Collapse sidebar">
  775. <span>«</span>
  776. </div>
  777. </div>
  778. <div class="clearer"></div>
  779. </div>
  780. <div class="related" role="navigation" aria-label="related navigation">
  781. <h3>Navigation</h3>
  782. <ul>
  783. <li class="right" style="margin-right: 10px">
  784. <a href="../genindex.html" title="General Index"
  785. >index</a></li>
  786. <li class="right" >
  787. <a href="../py-modindex.html" title="Python Module Index"
  788. >modules</a> |</li>
  789. <li class="right" >
  790. <a href="email.errors.html" title="email.errors: Exception and Defect classes"
  791. >next</a> |</li>
  792. <li class="right" >
  793. <a href="email.generator.html" title="email.generator: Generating MIME documents"
  794. >previous</a> |</li>
  795. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  796. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  797. <li class="switchers">
  798. <div class="language_switcher_placeholder"></div>
  799. <div class="version_switcher_placeholder"></div>
  800. </li>
  801. <li>
  802. </li>
  803. <li id="cpython-language-and-version">
  804. <a href="../index.html">3.12.3 Documentation</a> &#187;
  805. </li>
  806. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  807. <li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> &#187;</li>
  808. <li class="nav-item nav-item-3"><a href="email.html" ><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code> — An email and MIME handling package</a> &#187;</li>
  809. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.policy</span></code>: Policy Objects</a></li>
  810. <li class="right">
  811. <div class="inline-search" role="search">
  812. <form class="inline-search" action="../search.html" method="get">
  813. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  814. <input type="submit" value="Go" />
  815. </form>
  816. </div>
  817. |
  818. </li>
  819. <li class="right">
  820. <label class="theme-selector-label">
  821. Theme
  822. <select class="theme-selector" oninput="activateTheme(this.value)">
  823. <option value="auto" selected>Auto</option>
  824. <option value="light">Light</option>
  825. <option value="dark">Dark</option>
  826. </select>
  827. </label> |</li>
  828. </ul>
  829. </div>
  830. <div class="footer">
  831. &copy;
  832. <a href="../copyright.html">
  833. Copyright
  834. </a>
  835. 2001-2024, Python Software Foundation.
  836. <br />
  837. This page is licensed under the Python Software Foundation License Version 2.
  838. <br />
  839. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  840. <br />
  841. See <a href="/license.html">History and License</a> for more information.<br />
  842. <br />
  843. The Python Software Foundation is a non-profit corporation.
  844. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  845. <br />
  846. <br />
  847. Last updated on Apr 09, 2024 (13:47 UTC).
  848. <a href="/bugs.html">Found a bug</a>?
  849. <br />
  850. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  851. </div>
  852. </body>
  853. </html>
上海开阖软件有限公司 沪ICP备12045867号-1