| 209 | return folded.encode(charset, 'surrogateescape') |
| 210 | |
| 211 | def _fold(self, name, value, refold_binary=False): |
| 212 | if hasattr(value, 'name'): |
| 213 | return value.fold(policy=self) |
| 214 | maxlen = self.max_line_length if self.max_line_length else sys.maxsize |
| 215 | # We can't use splitlines here because it splits on more than \r and \n. |
| 216 | lines = linesep_splitter.split(value) |
| 217 | refold = (self.refold_source == 'all' or |
| 218 | self.refold_source == 'long' and |
| 219 | (lines and len(lines[0])+len(name)+2 > maxlen or |
| 220 | any(len(x) > maxlen for x in lines[1:]))) |
| 221 | |
| 222 | if not refold: |
| 223 | if not self.utf8: |
| 224 | refold = not value.isascii() |
| 225 | elif refold_binary: |
| 226 | refold = _has_surrogates(value) |
| 227 | if refold: |
| 228 | return self.header_factory(name, ''.join(lines)).fold(policy=self) |
| 229 | |
| 230 | return name + ': ' + self.linesep.join(lines) + self.linesep |
| 231 | |
| 232 | |
| 233 | default = EmailPolicy() |