Header encoding is mandated as ascii, but we allow fallbacks to utf-8 or iso-8859-1.
(self)
| 165 | |
| 166 | @property |
| 167 | def encoding(self) -> str: |
| 168 | """ |
| 169 | Header encoding is mandated as ascii, but we allow fallbacks to utf-8 |
| 170 | or iso-8859-1. |
| 171 | """ |
| 172 | if self._encoding is None: |
| 173 | for encoding in ["ascii", "utf-8"]: |
| 174 | for key, value in self.raw: |
| 175 | try: |
| 176 | key.decode(encoding) |
| 177 | value.decode(encoding) |
| 178 | except UnicodeDecodeError: |
| 179 | break |
| 180 | else: |
| 181 | # The else block runs if 'break' did not occur, meaning |
| 182 | # all values fitted the encoding. |
| 183 | self._encoding = encoding |
| 184 | break |
| 185 | else: |
| 186 | # The ISO-8859-1 encoding covers all 256 code points in a byte, |
| 187 | # so will never raise decode errors. |
| 188 | self._encoding = "iso-8859-1" |
| 189 | return self._encoding |
| 190 | |
| 191 | @encoding.setter |
| 192 | def encoding(self, value: str) -> None: |