Append a string to the MIME header. Optional charset, if given, should be a Charset instance or the name of a character set (which will be converted to a Charset instance). A value of None (the default) means that the charset given in the constructor is used.
(self, s, charset=None, errors='strict')
| 259 | return other == str(self) |
| 260 | |
| 261 | def append(self, s, charset=None, errors='strict'): |
| 262 | """Append a string to the MIME header. |
| 263 | |
| 264 | Optional charset, if given, should be a Charset instance or the name |
| 265 | of a character set (which will be converted to a Charset instance). A |
| 266 | value of None (the default) means that the charset given in the |
| 267 | constructor is used. |
| 268 | |
| 269 | s may be a byte string or a Unicode string. If it is a byte string |
| 270 | (i.e. isinstance(s, str) is false), then charset is the encoding of |
| 271 | that byte string, and a UnicodeError will be raised if the string |
| 272 | cannot be decoded with that charset. If s is a Unicode string, then |
| 273 | charset is a hint specifying the character set of the characters in |
| 274 | the string. In either case, when producing an RFC 2822 compliant |
| 275 | header using RFC 2047 rules, the string will be encoded using the |
| 276 | output codec of the charset. If the string cannot be encoded to the |
| 277 | output codec, a UnicodeError will be raised. |
| 278 | |
| 279 | Optional `errors' is passed as the errors argument to the decode |
| 280 | call if s is a byte string. |
| 281 | """ |
| 282 | if charset is None: |
| 283 | charset = self._charset |
| 284 | elif not isinstance(charset, Charset): |
| 285 | charset = Charset(charset) |
| 286 | if not isinstance(s, str): |
| 287 | input_charset = charset.input_codec or 'us-ascii' |
| 288 | if input_charset == _charset.UNKNOWN8BIT: |
| 289 | s = s.decode('us-ascii', 'surrogateescape') |
| 290 | else: |
| 291 | s = s.decode(input_charset, errors) |
| 292 | # Ensure that the bytes we're storing can be decoded to the output |
| 293 | # character set, otherwise an early error is raised. |
| 294 | output_charset = charset.output_codec or 'us-ascii' |
| 295 | if output_charset != _charset.UNKNOWN8BIT: |
| 296 | try: |
| 297 | s.encode(output_charset, errors) |
| 298 | except UnicodeEncodeError: |
| 299 | if output_charset!='us-ascii': |
| 300 | raise |
| 301 | charset = UTF8 |
| 302 | self._chunks.append((s, charset)) |
| 303 | |
| 304 | def _nonctext(self, s): |
| 305 | """True if string s is not a ctext character of RFC822. |