Return the string value of the header.
(self)
| 230 | self._headerlen = len(header_name) + 2 |
| 231 | |
| 232 | def __str__(self): |
| 233 | """Return the string value of the header.""" |
| 234 | self._normalize() |
| 235 | uchunks = [] |
| 236 | lastcs = None |
| 237 | lastspace = None |
| 238 | for string, charset in self._chunks: |
| 239 | # We must preserve spaces between encoded and non-encoded word |
| 240 | # boundaries, which means for us we need to add a space when we go |
| 241 | # from a charset to None/us-ascii, or from None/us-ascii to a |
| 242 | # charset. Only do this for the second and subsequent chunks. |
| 243 | # Don't add a space if the None/us-ascii string already has |
| 244 | # a space (trailing or leading depending on transition) |
| 245 | nextcs = charset |
| 246 | if nextcs == _charset.UNKNOWN8BIT: |
| 247 | original_bytes = string.encode('ascii', 'surrogateescape') |
| 248 | string = original_bytes.decode('ascii', 'replace') |
| 249 | if uchunks: |
| 250 | hasspace = string and self._nonctext(string[0]) |
| 251 | if lastcs not in (None, 'us-ascii'): |
| 252 | if nextcs in (None, 'us-ascii') and not hasspace: |
| 253 | uchunks.append(SPACE) |
| 254 | nextcs = None |
| 255 | elif nextcs not in (None, 'us-ascii') and not lastspace: |
| 256 | uchunks.append(SPACE) |
| 257 | lastspace = string and self._nonctext(string[-1]) |
| 258 | lastcs = nextcs |
| 259 | uchunks.append(string) |
| 260 | return EMPTYSTRING.join(uchunks) |
| 261 | |
| 262 | # Rich comparison operators for equality only. BAW: does it make sense to |
| 263 | # have or explicitly disable <, <=, >, >= operators? |