def _fold(self, name, value, sanitize): parts = [] parts.append("%s: " % name) if isinstance(value, str): if _has_surrogates(value): if sanitize: h = header.Header( value, charset=_charset.UNKNOWN8BIT, header_name=name ) else: # If we have raw 8bit data in a byte string, we have no idea # what the encoding is. There is no safe way to split this # string. If it's ascii-subset, then we could do a normal # ascii split, but if it's multibyte then we could break the # string. There's no way to know so the least harm seems to # be to not split the string and risk it being too long. parts.append(value) h = None else: h = header.Header(value, header_name=name) else: # Assume it is a Header-like object. h = value if h is not None: parts.append( h.encode(linesep=self.linesep, maxlinelen=self.max_line_length) ) parts.append(self.linesep) return "".join(parts)
def _sanitize_header(self, name, value): # If the header value contains surrogates, return a Header using # the unknown-8bit charset to encode the bytes as encoded words. if not isinstance(value, str): # Assume it is already a header object return value if _has_surrogates(value): return header.Header(value, charset=_charset.UNKNOWN8BIT, header_name=name) else: return value