22import email .charset
33import email .message
44import email .errors
5+ import sys
56from email import quoprimime
67
78class ContentManager :
@@ -142,22 +143,23 @@ def _encode_base64(data, max_line_length):
142143
143144
144145def _encode_text (string , charset , cte , policy ):
146+ # If max_line_length is 0 or None, there is no limit.
147+ maxlen = policy .max_line_length or sys .maxsize
145148 lines = string .encode (charset ).splitlines ()
146149 linesep = policy .linesep .encode ('ascii' )
147150 def embedded_body (lines ): return linesep .join (lines ) + linesep
148151 def normal_body (lines ): return b'\n ' .join (lines ) + b'\n '
149152 if cte is None :
150153 # Use heuristics to decide on the "best" encoding.
151- if max ((len ( x ) for x in lines ), default = 0 ) <= policy . max_line_length :
154+ if max (map (len , lines ), default = 0 ) <= maxlen :
152155 try :
153156 return '7bit' , normal_body (lines ).decode ('ascii' )
154157 except UnicodeDecodeError :
155158 pass
156159 if policy .cte_type == '8bit' :
157160 return '8bit' , normal_body (lines ).decode ('ascii' , 'surrogateescape' )
158161 sniff = embedded_body (lines [:10 ])
159- sniff_qp = quoprimime .body_encode (sniff .decode ('latin-1' ),
160- policy .max_line_length )
162+ sniff_qp = quoprimime .body_encode (sniff .decode ('latin-1' ), maxlen )
161163 sniff_base64 = binascii .b2a_base64 (sniff )
162164 # This is a little unfair to qp; it includes lineseps, base64 doesn't.
163165 if len (sniff_qp ) > len (sniff_base64 ):
@@ -172,9 +174,9 @@ def normal_body(lines): return b'\n'.join(lines) + b'\n'
172174 data = normal_body (lines ).decode ('ascii' , 'surrogateescape' )
173175 elif cte == 'quoted-printable' :
174176 data = quoprimime .body_encode (normal_body (lines ).decode ('latin-1' ),
175- policy . max_line_length )
177+ maxlen )
176178 elif cte == 'base64' :
177- data = _encode_base64 (embedded_body (lines ), policy . max_line_length )
179+ data = _encode_base64 (embedded_body (lines ), maxlen )
178180 else :
179181 raise ValueError ("Unknown content transfer encoding {}" .format (cte ))
180182 return cte , data
0 commit comments