Skip to content
Myr

Fixed detection of utf-32-be by BOM.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Герасименко Евгений · committed by Герасименко Евгений · parent 8e0a532 · Browse files · 2 files+12 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+12 added−1 removed

  • tests:+11 added−0 removed
  • other:+1 added−1 removed

2 files+12 added−1 removed

modified:requests/utils.py+1 added-1 removed
712 unmodified lines
713 # JSON always starts with two ASCII characters, so detection is as714 # easy as counting the nulls and from their location and count715 # determine the encoding. Also detect a BOM, if present.716 sample = data[:4]717 if sample in (codecs.BOM_UTF32_LE, codecs.BOM32_BE):717 if sample in (codecs.BOM_UTF32_LE, codecs.BOM_UTF32_BE):718 return 'utf-32' # BOM included719 if sample[:3] == codecs.BOM_UTF8:720 return 'utf-8-sig' # BOM included, MS style (discouraged)721 if sample[:2] in (codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE):
106 unmodified lines
modified:tests/test_utils.py+11 added
272 unmodified lines
273274 def test_bad_utf_like_encoding(self):275 assert guess_json_utf(b'\x00\x00\x00\x00') is None276277 @pytest.mark.parametrize(278 ('encoding', 'expected'), (279 ('utf-16-be', 'utf-16'),280 ('utf-16-le', 'utf-16'),281 ('utf-32-be', 'utf-32'),282 ('utf-32-le', 'utf-32')283 ))284 def test_guess_by_bom(self, encoding, expected):285 data = '\ufeff{}'.encode(encoding)286 assert guess_json_utf(data) == expected287288289USER = PASSWORD = "%!*'();:@&=+$,/?#[] "290ENCODED_USER = compat.quote(USER, '')291ENCODED_PASSWORD = compat.quote(PASSWORD, '')292
263 unmodified lines