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