Skip to content

Commit 8936f8a

Browse files
committed
gh-87888: Make FileCookieJar use latin-1 instead of locale encoding
* Byte transparent reading and writing. * Consistent with WSGI. * Avoid UnicodeDecodeError when reading comments.
1 parent 0729b31 commit 8936f8a

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

Lib/http/cookiejar.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,7 +1798,9 @@ def load(self, filename=None, ignore_discard=False, ignore_expires=False):
17981798
if self.filename is not None: filename = self.filename
17991799
else: raise ValueError(MISSING_FILENAME_TEXT)
18001800

1801-
with open(filename) as f:
1801+
# We use latin-1 here because WSGI uses latin-1 for HTTP headers too.
1802+
# See gh-87888 for more info.
1803+
with open(filename, encoding="latin1") as f:
18021804
self._really_load(f, filename, ignore_discard, ignore_expires)
18031805

18041806
def revert(self, filename=None,
@@ -1890,7 +1892,7 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
18901892
if self.filename is not None: filename = self.filename
18911893
else: raise ValueError(MISSING_FILENAME_TEXT)
18921894

1893-
with open(filename, "w") as f:
1895+
with open(filename, "w", encoding="latin1") as f:
18941896
# There really isn't an LWP Cookies 2.0 format, but this indicates
18951897
# that there is extra information in here (domain_dot and
18961898
# port_spec) while still being compatible with libwww-perl, I hope.
@@ -2086,7 +2088,7 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
20862088
if self.filename is not None: filename = self.filename
20872089
else: raise ValueError(MISSING_FILENAME_TEXT)
20882090

2089-
with open(filename, "w") as f:
2091+
with open(filename, "w", encoding="latin1") as f:
20902092
f.write(NETSCAPE_HEADER_TEXT)
20912093
now = time.time()
20922094
for cookie in self:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Changed encoding used by :class:`http.cookiejar.FileCookieJar` and its
2+
subclasses from locale encoding to "latin-1".

0 commit comments

Comments
 (0)