Repository navigation
Headers.encoding is cached from the raw bytes and never invalidated, so non-ASCII writes and reads fail after a read #3808
Unanswered
simpleqt
asked this question in
Potential Issue
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Headersusesself._encodingfor two different things — the explicitly configured encoding, and the cache for the encoding it lazily determines from the raw header bytes — and never invalidates that cache when the headers change. That makes two valid operations fail depending on access history:Byte keys and values are a supported
HeaderTypes, and the utf-8/iso-8859-1 fallback in theencodingproperty exists exactly for them, so an object whose ownrawis correct should not be unable to read it back.copy()also pins the determined encoding onto the copy, so the same gets inherited.Where it comes from, in the current code:
httpx/_models.py:183— theencodingproperty writes the determined encoding back intoself._encoding.httpx/_models.py:309-310—__setitem__encodes withself._encoding or "utf-8", i.e. the fallback only works while_encodingis stillNone.httpx/_models.py:279—update()first caches the pre-update encoding of the existing headers throughkey in self→__contains__→self.encoding.httpx/_models.py:282—copy()carries the determined value over.What I'd expect:
_encodingkeeps only an explicitly configured encoding, the determined one is cached separately, and the determined value is re-computed afterupdate(),__setitem__and__delitem__.I have a fix for this with a regression test for both cases, and can open a pull request if you'd consider it.
All reactions