Skip to content

Commit 0fa2edd

Browse files
committed
#14731: add preliminary What's New entry for policy framework.
1 parent c27e522 commit 0fa2edd

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Doc/whatsnew/3.3.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,61 @@ consideration when updating code for Python 3.3, and thus should be read about
555555
in the `Porting Python code`_ section of this document.
556556

557557

558+
New Email Package Features
559+
==========================
560+
561+
The email package now has a :mod:`~email.policy` framework. A
562+
:class:`~email.policy.Policy` is an object with several methods and properties
563+
that control how the email package behaves. The primary policy for Python 3.3
564+
is the :class:`~email.policy.Compat32` policy, which provides backward
565+
compatibility with the email package in Python 3.2. A ``policy`` can be
566+
specified when an email message is parsed by a :mod:`~email.parser`, or when a
567+
:class:`~email.message.Message` object is created, or when an email is
568+
serialized using a :mod:`~email.generator`. Unless overridden, a policy passed
569+
to a ``parser`` is inherited by all the ``Message`` object and sub-objects
570+
created by the ``parser``. By default a ``generator`` will use the policy of
571+
the ``Message`` object it is serializing. The default policy is
572+
:data:`~email.policy.compat32`.
573+
574+
The minimum set of controls implemented by all ``policy`` objects are:
575+
576+
=============== =======================================================
577+
max_line_length The maximum length, excluding the linesep character(s),
578+
individual lines may have when a ``Message`` is
579+
serialized. Defaults to 78.
580+
581+
linesep The character used to separate individual lines when a
582+
``Message`` is serialized. Defaults to ``\n``.
583+
584+
cte_type ``7bit`` or ``8bit``. ``8bit`` applies only to a
585+
``Bytes`` ``generator``, and means that non-ASCII may
586+
be used where allowed by the protocol (or where it
587+
exists in the original input).
588+
589+
raise_on_defect Causes a ``parser`` to raise error when defects are
590+
encountered instead of adding them to the ``Message``
591+
object's ``defects`` list.
592+
=============== =======================================================
593+
594+
A new policy instance, with new settings, is created using the
595+
:meth:`~email.policy.Policy.clone` method of policy objects. ``clone`` takes
596+
any of the above controls as keyword arguments. Any control not specified in
597+
the call retains its default value. Thus you can create a policy that uses
598+
``\r\n`` linesep characters like this::
599+
600+
mypolicy = compat32.clone(linesep=`\r\n`)
601+
602+
Policies can be used to make the generation of messages in the format needed by
603+
your application simpler. Instead of having to remember to specify
604+
``linesep='\r\n'`` in all the places you call a ``generator``, you can specify
605+
it once, when you set the policy used by the ``parser`` or the ``Message``,
606+
whichever your program uses to create ``Message`` objects. On the other hand,
607+
if you need to generate messages in multiple forms, you can still specify the
608+
parameters in the appropriate ``generator`` call. Or you can have custom
609+
policy instances for your different cases, and pass those in when you create
610+
the ``generator``.
611+
612+
558613
Other Language Changes
559614
======================
560615

0 commit comments

Comments
 (0)