cpython: 53287858e71f (original) (raw)

Mercurial > cpython

changeset 85076:53287858e71f

#18600: add policy to add_string, and as_bytes and __bytes__ methods. This was triggered by wanting to make the doctest in email.policy.rst pass; as_bytes and __bytes__ are clearly useful now that we have BytesGenerator. Also updated the Message docs to document the policy keyword that was added in 3.3. [#18600]

R David Murray rdmurray@bitdance.com
date Fri, 09 Aug 2013 16:15:28 -0400
parents a206f952668e
children b9a5b7e3b32f
files Doc/library/email.message.rst Doc/library/email.policy.rst Doc/whatsnew/3.4.rst Lib/email/message.py Lib/test/test_email/test_email.py Misc/NEWS
diffstat 6 files changed, 160 insertions(+), 21 deletions(-)[+] [-] Doc/library/email.message.rst 78 Doc/library/email.policy.rst 3 Doc/whatsnew/3.4.rst 20 Lib/email/message.py 44 Lib/test/test_email/test_email.py 33 Misc/NEWS 3

line wrap: on

line diff

--- a/Doc/library/email.message.rst +++ b/Doc/library/email.message.rst @@ -31,19 +31,32 @@ parameters, and for recursively walking Here are the methods of the :class:Message class: -.. class:: Message() +.. class:: Message(policy=compat32)

+

Note that this method is provided as a convenience and may not always format the message the way you want. For example, by default it does @@ -59,10 +72,57 @@ Here are the methods of the :class:`Mess g.flatten(msg) text = fp.getvalue()

+

+ .. method:: str()

+ +

+

+

+

+

+ +

+

.. method:: is_multipart()

--- a/Doc/library/email.policy.rst +++ b/Doc/library/email.policy.rst @@ -105,7 +105,8 @@ separators for the platform on which it >>> import os >>> with open('converted.txt', 'wb') as f:

Policy objects can also be combined using the addition operator, producing a policy object whose settings are a combination of the non-default values of the

--- a/Doc/whatsnew/3.4.rst +++ b/Doc/whatsnew/3.4.rst @@ -195,6 +195,26 @@ The :meth:~aifc.getparams method now r plain tuple. (Contributed by Claudiu Popa in :issue:17818.) +email +----- + +:meth:~email.message.Message.as_string now accepts a policy argument to +override the default policy of the message when generating a string +representation of it. This means that as_string can now be used in more +circumstances, instead of having to create and use a :mod:~email.generator in +order to pass formatting parameters to its flatten method. + +New method :meth:~email.message.Message.as_bytes added to produce a bytes +representation of the message in a fashion similar to how as_string +produces a string representation. It does not accept the maxheaderlen +argument, but does accept the unixfrom and policy arguments. The +:class:~email.message.Message :meth:~email.message.Message.__bytes__ method +calls it, meaning that bytes(mymsg) will now produce the intuitive +result: a bytes object containing the fully formatted message. + +(Contributed by R. David Murray in :issue:18600.) + + functools ---------

--- a/Lib/email/message.py +++ b/Lib/email/message.py @@ -132,22 +132,50 @@ class Message: def str(self): """Return the entire formatted message as a string.

+

+

+

+

--- a/Lib/test/test_email/test_email.py +++ b/Lib/test/test_email/test_email.py @@ -249,15 +249,42 @@ class TestMessageAPI(TestEmailBase): self.assertTrue('TO' in msg) def test_as_string(self):

+

+

+

# test_headerregistry.TestContentTypeHeader.bad_params def test_bad_param(self):

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -22,6 +22,9 @@ Core and Builtins Library ------- +- Issue #18600: Added policy argument to email.message.Message.as_string,