summaryrefslogtreecommitdiffstats
path: root/Lib/email/message.py
diff options
context:
space:
mode:
authorR. David Murray <rdmurray@bitdance.com>2010-12-13 23:57:01 (GMT)
committerR. David Murray <rdmurray@bitdance.com>2010-12-13 23:57:01 (GMT)
commitccb9d05b6c6b32ac9429f90cf24a1da477fde79e (patch)
treea8ca264f47d0a9a985968c5397389315bd444c57 /Lib/email/message.py
parentfa66d583f6eb79c95ff07a84cbae1d44c81f5b8e (diff)
downloadcpython-ccb9d05b6c6b32ac9429f90cf24a1da477fde79e.zip
cpython-ccb9d05b6c6b32ac9429f90cf24a1da477fde79e.tar.gz
cpython-ccb9d05b6c6b32ac9429f90cf24a1da477fde79e.tar.bz2
Merged revisions 87217 via svnmerge from
svn+ssh://pythondev@svn.python.org/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. ........
Diffstat (limited to 'Lib/email/message.py')
-rw-r--r--Lib/email/message.py24
1 files changed, 21 insertions, 3 deletions
diff --git a/Lib/email/message.py b/Lib/email/message.py
index a6bf146..9ef2363 100644
--- a/Lib/email/message.py
+++ b/Lib/email/message.py
@@ -39,7 +39,11 @@ def _splitparam(param):
def _formatparam(param, value=None, quote=True):
"""Convenience function to format and return a key=value pair.
- This will quote the value if needed or if quote is true.
+ This will quote the value if needed or if quote is true. If value is a
+ three tuple (charset, language, value), it will be encoded according
+ to RFC2231 rules. If it contains non-ascii characters it will likewise
+ be encoded according to RFC2231 rules, using the utf-8 charset and
+ a null language.
"""
if value is not None and len(value) > 0:
# A tuple is used for RFC 2231 encoded parameter values where items
@@ -49,6 +53,12 @@ def _formatparam(param, value=None, quote=True):
# Encode as per RFC 2231
param += '*'
value = utils.encode_rfc2231(value[2], value[0], value[1])
+ else:
+ try:
+ value.encode('ascii')
+ except UnicodeEncodeError:
+ param += '*'
+ value = utils.encode_rfc2231(value, 'utf-8', '')
# BAW: Please check this. I think that if quote is set it should
# force quoting even if not necessary.
if quote or tspecials.search(value):
@@ -391,11 +401,19 @@ class Message:
name is the header field to add. keyword arguments can be used to set
additional parameters for the header field, with underscores converted
to dashes. Normally the parameter will be added as key="value" unless
- value is None, in which case only the key will be added.
+ value is None, in which case only the key will be added. If a
+ parameter value contains non-ASCII characters it can be specified as a
+ three-tuple of (charset, language, value), in which case it will be
+ encoded according to RFC2231 rules. Otherwise it will be encoded using
+ the utf-8 charset and a language of ''.
- Example:
+ Examples:
msg.add_header('content-disposition', 'attachment', filename='bud.gif')
+ msg.add_header('content-disposition', 'attachment',
+ filename=('utf-8', '', Fußballer.ppt'))
+ msg.add_header('content-disposition', 'attachment',
+ filename='Fußballer.ppt'))
"""
parts = []
for k, v in _params.items():