diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index fb0fd2e97999af..8a808f42e00a47 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -1798,7 +1798,10 @@ def load(self, filename=None, ignore_discard=False, ignore_expires=False): if self.filename is not None: filename = self.filename else: raise ValueError(MISSING_FILENAME_TEXT) - with open(filename) as f: + # cookie value should be ASCII, but cookiejar file may contain + # non-ASCII comments or invalid cookies. + # We use "surrogateescape" error handler to read them. + with open(filename, encoding="ascii", errors="surrogateescape") as f: self._really_load(f, filename, ignore_discard, ignore_expires) def revert(self, filename=None, @@ -1892,7 +1895,7 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False): with os.fdopen( os.open(filename, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, 0o600), - 'w', + 'w', encoding="ascii", errors="surrogateescape", ) as f: # There really isn't an LWP Cookies 2.0 format, but this indicates # that there is extra information in here (domain_dot and @@ -2086,7 +2089,7 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False): with os.fdopen( os.open(filename, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, 0o600), - 'w', + 'w', encoding="ascii", errors="surrogateescape", ) as f: f.write(NETSCAPE_HEADER_TEXT) now = time.time() diff --git a/Misc/NEWS.d/next/Library/2022-05-06-20-32-41.gh-issue-87888.2_R3zS.rst b/Misc/NEWS.d/next/Library/2022-05-06-20-32-41.gh-issue-87888.2_R3zS.rst new file mode 100644 index 00000000000000..d20ef5635c61e5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-05-06-20-32-41.gh-issue-87888.2_R3zS.rst @@ -0,0 +1 @@ +Changed the encoding and error handler used by :class:`http.cookiejar.FileCookieJar` and its subclasses when reading and writing Cookie files to "ASCII" and "surrogateescape".