summaryrefslogtreecommitdiffstats
path: root/Lib/email/header.py
diff options
context:
space:
mode:
authorR. David Murray <rdmurray@bitdance.com>2010-10-23 22:19:56 (GMT)
committerR. David Murray <rdmurray@bitdance.com>2010-10-23 22:19:56 (GMT)
commit8451c4b6e044f83efc2298a79af58c3e56d946a2 (patch)
treeedaad1a89627de27ad30b465b7a416c468850653 /Lib/email/header.py
parent29aad0005dd56634363dabd74cf6708c9a255b43 (diff)
downloadcpython-8451c4b6e044f83efc2298a79af58c3e56d946a2.zip
cpython-8451c4b6e044f83efc2298a79af58c3e56d946a2.tar.gz
cpython-8451c4b6e044f83efc2298a79af58c3e56d946a2.tar.bz2
#1349106: add linesep argument to generator.flatten and header.encode.
Diffstat (limited to 'Lib/email/header.py')
-rw-r--r--Lib/email/header.py16
1 files changed, 12 insertions, 4 deletions
diff --git a/Lib/email/header.py b/Lib/email/header.py
index 89c1391..88fa80f 100644
--- a/Lib/email/header.py
+++ b/Lib/email/header.py
@@ -272,7 +272,7 @@ class Header:
output_string = input_bytes.decode(output_charset, errors)
self._chunks.append((output_string, charset))
- def encode(self, splitchars=';, \t', maxlinelen=None):
+ def encode(self, splitchars=';, \t', maxlinelen=None, linesep='\n'):
"""Encode a message header into an RFC-compliant format.
There are many issues involved in converting a given string for use in
@@ -293,6 +293,11 @@ class Header:
Optional splitchars is a string containing characters to split long
ASCII lines on, in rough support of RFC 2822's `highest level
syntactic breaks'. This doesn't affect RFC 2047 encoded lines.
+
+ Optional linesep is a string to be used to separate the lines of
+ the value. The default value is the most useful for typical
+ Python applications, but it can be set to \r\n to produce RFC-compliant
+ line separators when needed.
"""
self._normalize()
if maxlinelen is None:
@@ -311,7 +316,7 @@ class Header:
if len(lines) > 1:
formatter.newline()
formatter.add_transition()
- return str(formatter)
+ return formatter._str(linesep)
def _normalize(self):
# Step 1: Normalize the chunks so that all runs of identical charsets
@@ -342,9 +347,12 @@ class _ValueFormatter:
self._lines = []
self._current_line = _Accumulator(headerlen)
- def __str__(self):
+ def _str(self, linesep):
self.newline()
- return NL.join(self._lines)
+ return linesep.join(self._lines)
+
+ def __str__(self):
+ return self._str(NL)
def newline(self):
end_of_line = self._current_line.pop()