Parses a query string like urlparse.parse_qs, but takes bytes and returns the values as byte strings. Keys still become type str (interpreted as latin1 in python3!) because it's too painful to keep them as byte strings in python3 and in practice they're nearly always ascii anyway.
(
qs: Union[str, bytes], keep_blank_values: bool = False, strict_parsing: bool = False
)
| 171 | |
| 172 | |
| 173 | def parse_qs_bytes( |
| 174 | qs: Union[str, bytes], keep_blank_values: bool = False, strict_parsing: bool = False |
| 175 | ) -> Dict[str, List[bytes]]: |
| 176 | """Parses a query string like urlparse.parse_qs, |
| 177 | but takes bytes and returns the values as byte strings. |
| 178 | |
| 179 | Keys still become type str (interpreted as latin1 in python3!) |
| 180 | because it's too painful to keep them as byte strings in |
| 181 | python3 and in practice they're nearly always ascii anyway. |
| 182 | """ |
| 183 | # This is gross, but python3 doesn't give us another way. |
| 184 | # Latin1 is the universal donor of character encodings. |
| 185 | if isinstance(qs, bytes): |
| 186 | qs = qs.decode("latin1") |
| 187 | result = urllib.parse.parse_qs( |
| 188 | qs, keep_blank_values, strict_parsing, encoding="latin1", errors="strict" |
| 189 | ) |
| 190 | encoded = {} |
| 191 | for k, v in result.items(): |
| 192 | encoded[k] = [i.encode("latin1") for i in v] |
| 193 | return encoded |
| 194 | |
| 195 | |
| 196 | _UTF8_TYPES = (bytes, type(None)) |
no test coverage detected