Repository navigation
Fall back to utf-8 when charset autodetection needs unread content - #3879
Open
qianeric-backup wants to merge 1 commit into
Open
qianeric-backup wants to merge 1 commit into
qianeric-backup wants to merge 1 commit into
Conversation
Response.encoding rejects a header charset that is not a Python codec, then tries to replace it with default_encoding. With a callable default_encoding and unread content neither the string branch nor the callable branch runs, so the rejected charset survives `or "utf-8"` (it is truthy) and is cached, making every later text access raise LookupError. The callable form is for autodetection, which needs the body; before that there is nothing to detect from. Fall back to utf-8, as the same line already does when no charset was given at all. Discussed in encode#3878.
qianeric-backup
marked this pull request as ready for review
November 28, 2026 02:58
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Discussed first, as the contributing guide asks: #3878.
Response.encodingcaches a charset Python has no codec for whendefault_encodingis a callable and the content has not been read yet:>>> response.encoding 'x-user-defined' >>> b"".join(response.iter_bytes()).decode(response.encoding) LookupError: unknown encoding: x-user-defined_is_known_encoding()correctly rejects the header'sx-user-defined, and the property then picks a replacement:A string
default_encodingreplaces the rejected charset. A callable one with unread content takes neither branch, soencodingis still the rejected value, andor "utf-8"does not catch it because a non-empty string is truthy. The same response viaclient.get()decodes fine, so whether a response is readable depends on how it was fetched.The callable form exists for autodetection, which by definition needs the body; before that point there is nothing to detect from, and the line above already falls back to
utf-8when there is no charset at all.Change
elif hasattr(self, "_content"): encoding = self.default_encoding(self._content) + else: + # The callable `default_encoding` requires the response content, + # which has not been read yet. Fall back to utf-8 rather than + # passing an unusable charset through. + encoding = NoneTests
Three cases in
tests/models/test_responses.py: the streaming call above, the same response read eagerly (unchanged behaviour), and a known charset with unread content (still honoured).master1417 passed, 1 skippedmaster+ the tests,_models.pyuntouched2 failed, 1418 passed, 1 skipped— bothLookupError: unknown encoding: x-user-defined1420 passed, 1 skippedcomm -3over the failure lists is empty againstmasterand shows exactly the two new tests against the red tree.18 legal responses compared byte for byte (known and unknown charsets, string and callable
default_encoding, eager and streaming reads, absent charset) produce identical output before and after.ruff format --diff,ruff check,mypy,scripts/sync-version, andcoverage --fail-under=100(100% after the change) are clean.Noting the guide's point that effort has moved to hip: this is small and contained, but I understand if it is not worth triaging now.