@@ -1798,9 +1798,10 @@ 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-         # 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 :
1801+         # cookie value should be ASCII, but cookiejar file may contain 
1802+         # non-ASCII comments or invalid cookies. 
1803+         # We use "surrogateescape" error handler to read them. 
1804+         with  open (filename , encoding = "ascii" , errors = "surrogateescape" ) as  f :
18041805            self ._really_load (f , filename , ignore_discard , ignore_expires )
18051806
18061807    def  revert (self , filename = None ,
@@ -1894,7 +1895,7 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
18941895
18951896        with  os .fdopen (
18961897            os .open (filename , os .O_CREAT  |  os .O_WRONLY  |  os .O_TRUNC , 0o600 ),
1897-             'w' , encoding = "latin1 " ,
1898+             'w' , encoding = "ascii"  ,  errors = "surrogateescape "
18981899        ) as  f :
18991900            # There really isn't an LWP Cookies 2.0 format, but this indicates 
19001901            # that there is extra information in here (domain_dot and 
@@ -2088,7 +2089,7 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
20882089
20892090        with  os .fdopen (
20902091            os .open (filename , os .O_CREAT  |  os .O_WRONLY  |  os .O_TRUNC , 0o600 ),
2091-             'w' , encoding = "latin1 " ,
2092+             'w' , encoding = "ascii"  ,  errors = "surrogateescape "
20922093        ) as  f :
20932094            f .write (NETSCAPE_HEADER_TEXT )
20942095            now  =  time .time ()
0 commit comments