| 447 | return new_params |
| 448 | |
| 449 | def collapse_rfc2231_value(value, errors='replace', |
| 450 | fallback_charset='us-ascii'): |
| 451 | if not isinstance(value, tuple) or len(value) != 3: |
| 452 | return unquote(value) |
| 453 | # While value comes to us as a unicode string, we need it to be a bytes |
| 454 | # object. We do not want bytes() normal utf-8 decoder, we want a straight |
| 455 | # interpretation of the string as character bytes. |
| 456 | charset, language, text = value |
| 457 | if charset is None: |
| 458 | # Issue 17369: if charset/lang is None, decode_rfc2231 couldn't parse |
| 459 | # the value, so use the fallback_charset. |
| 460 | charset = fallback_charset |
| 461 | rawbytes = bytes(text, 'raw-unicode-escape') |
| 462 | try: |
| 463 | # Explicitly look up the codec for warning generation, see gh-140030 |
| 464 | # Can be removed in 3.17 |
| 465 | import codecs |
| 466 | codecs.lookup(charset) |
| 467 | return str(rawbytes, charset, errors) |
| 468 | except LookupError: |
| 469 | # charset is not a known codec. |
| 470 | return unquote(text) |
| 471 | |
| 472 | |
| 473 | # |