Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ccb9d05

Browse files
committed
Merged revisions 87217 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ........ r87217 | r.david.murray | 2010-12-13 18:51:19 -0500 (Mon, 13 Dec 2010) | 5 lines #1078919: make add_header automatically do RFC2231 encoding when needed. Also document the use of three-tuples if control of the charset and language is desired. ........
1 parent fa66d58 commit ccb9d05

4 files changed

Lines changed: 66 additions & 5 deletions

File tree

Doc/library/email.message.rst

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,15 @@ Here are the methods of the :class:`Message` class:
257257
taken as the parameter name, with underscores converted to dashes (since
258258
dashes are illegal in Python identifiers). Normally, the parameter will
259259
be added as ``key="value"`` unless the value is ``None``, in which case
260-
only the key will be added.
260+
only the key will be added. If the value contains non-ASCII characters,
261+
it can be specified as a three tuple in the format
262+
``(CHARSET, LANGUAGE, VALUE)``, where ``CHARSET`` is a string naming the
263+
charset to be used to encode the value, ``LANGUAGE`` can usually be set
264+
to ``None`` or the empty string (see :RFC:`2231` for other possibilities),
265+
and ``VALUE`` is the string value containing non-ASCII code points. If
266+
a three tuple is not passed and the value contains non-ASCII characters,
267+
it is automatically encoded in :RFC`2231` format using a ``CHARSET``
268+
of ``utf-8`` and a ``LANGUAGE`` of ``None``.
261269

262270
Here's an example::
263271

@@ -267,6 +275,15 @@ Here are the methods of the :class:`Message` class:
267275

268276
Content-Disposition: attachment; filename="bud.gif"
269277

278+
An example with with non-ASCII characters::
279+
280+
msg.add_header('Content-Disposition', 'attachment',
281+
filename=('iso-8859-1', '', 'Fußballer.ppt'))
282+
283+
Which produces ::
284+
285+
Content-Disposition: attachment; filename*="iso-8859-1''Fu%DFballer.ppt"
286+
270287

271288
.. method:: replace_header(_name, _value)
272289

@@ -356,7 +373,7 @@ Here are the methods of the :class:`Message` class:
356373
:rfc:`2231`, you can collapse the parameter value by calling
357374
:func:`email.utils.collapse_rfc2231_value`, passing in the return value
358375
from :meth:`get_param`. This will return a suitably decoded Unicode
359-
string whn the value is a tuple, or the original string unquoted if it
376+
string when the value is a tuple, or the original string unquoted if it
360377
isn't. For example::
361378

362379
rawparam = msg.get_param('foo')

Lib/email/message.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ def _splitparam(param):
3939
def _formatparam(param, value=None, quote=True):
4040
"""Convenience function to format and return a key=value pair.
4141
42-
This will quote the value if needed or if quote is true.
42+
This will quote the value if needed or if quote is true. If value is a
43+
three tuple (charset, language, value), it will be encoded according
44+
to RFC2231 rules. If it contains non-ascii characters it will likewise
45+
be encoded according to RFC2231 rules, using the utf-8 charset and
46+
a null language.
4347
"""
4448
if value is not None and len(value) > 0:
4549
# A tuple is used for RFC 2231 encoded parameter values where items
@@ -49,6 +53,12 @@ def _formatparam(param, value=None, quote=True):
4953
# Encode as per RFC 2231
5054
param += '*'
5155
value = utils.encode_rfc2231(value[2], value[0], value[1])
56+
else:
57+
try:
58+
value.encode('ascii')
59+
except UnicodeEncodeError:
60+
param += '*'
61+
value = utils.encode_rfc2231(value, 'utf-8', '')
5262
# BAW: Please check this. I think that if quote is set it should
5363
# force quoting even if not necessary.
5464
if quote or tspecials.search(value):
@@ -391,11 +401,19 @@ def add_header(self, _name, _value, **_params):
391401
name is the header field to add. keyword arguments can be used to set
392402
additional parameters for the header field, with underscores converted
393403
to dashes. Normally the parameter will be added as key="value" unless
394-
value is None, in which case only the key will be added.
404+
value is None, in which case only the key will be added. If a
405+
parameter value contains non-ASCII characters it can be specified as a
406+
three-tuple of (charset, language, value), in which case it will be
407+
encoded according to RFC2231 rules. Otherwise it will be encoded using
408+
the utf-8 charset and a language of ''.
395409
396-
Example:
410+
Examples:
397411
398412
msg.add_header('content-disposition', 'attachment', filename='bud.gif')
413+
msg.add_header('content-disposition', 'attachment',
414+
filename=('utf-8', '', Fußballer.ppt'))
415+
msg.add_header('content-disposition', 'attachment',
416+
filename='Fußballer.ppt'))
399417
"""
400418
parts = []
401419
for k, v in _params.items():

Lib/email/test/test_email.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,29 @@ def test_broken_base64_payload(self):
504504
self.assertEqual(msg.get_payload(decode=True),
505505
bytes(x, 'raw-unicode-escape'))
506506

507+
# Issue 1078919
508+
def test_ascii_add_header(self):
509+
msg = Message()
510+
msg.add_header('Content-Disposition', 'attachment',
511+
filename='bud.gif')
512+
self.assertEqual('attachment; filename="bud.gif"',
513+
msg['Content-Disposition'])
514+
515+
def test_noascii_add_header(self):
516+
msg = Message()
517+
msg.add_header('Content-Disposition', 'attachment',
518+
filename="Fußballer.ppt")
519+
self.assertEqual(
520+
'attachment; filename*="utf-8\'\'Fu%C3%9Fballer.ppt"',
521+
msg['Content-Disposition'])
522+
523+
def test_nonascii_add_header_via_triple(self):
524+
msg = Message()
525+
msg.add_header('Content-Disposition', 'attachment',
526+
filename=('iso-8859-1', '', 'Fußballer.ppt'))
527+
self.assertEqual(
528+
'attachment; filename*="iso-8859-1\'\'Fu%DFballer.ppt"',
529+
msg['Content-Disposition'])
507530

508531

509532
# Test the email.encoders module

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ Core and Builtins
1717
Library
1818
-------
1919

20+
- Issue #1078919: add_header now automatically RFC2231 encodes parameters
21+
that contain non-ascii values.
22+
2023
- Issue #10107: Warn about unsaved files in IDLE on OSX.
2124

2225
- Issue #7904: Changes to urllib.parse.urlsplit to handle schemes as defined by

0 commit comments

Comments
 (0)