@@ -179,35 +179,44 @@ def addpackage(sitedir, name, known_paths):
179179 return
180180 _trace (f"Processing .pth file: { fullname !r} " )
181181 try :
182- # locale encoding is not ideal especially on Windows. But we have used
183- # it for a long time. setuptools uses the locale encoding too.
184- f = io .TextIOWrapper (io .open_code (fullname ), encoding = "locale" )
182+ with io .open_code (fullname ) as f :
183+ pth_content = f .read ()
185184 except OSError :
186185 return
187- with f :
188- for n , line in enumerate (f ):
189- if line .startswith ("#" ):
190- continue
191- if line .strip () == "" :
186+
187+ try :
188+ pth_content = pth_content .decode ()
189+ except UnicodeDecodeError :
190+ # Fallback to locale encoding for backward compatibility.
191+ # We will deprecate this fallback in the future.
192+ import locale
193+ pth_content = pth_content .decode (locale .getencoding ())
194+ _trace (f"Cannot read { fullname !r} as UTF-8. "
195+ f"Using fallback encoding { locale .getencoding ()!r} " )
196+
197+ for n , line in enumerate (pth_content .splitlines (), 1 ):
198+ if line .startswith ("#" ):
199+ continue
200+ if line .strip () == "" :
201+ continue
202+ try :
203+ if line .startswith (("import " , "import\t " )):
204+ exec (line )
192205 continue
193- try :
194- if line .startswith (("import " , "import\t " )):
195- exec (line )
196- continue
197- line = line .rstrip ()
198- dir , dircase = makepath (sitedir , line )
199- if not dircase in known_paths and os .path .exists (dir ):
200- sys .path .append (dir )
201- known_paths .add (dircase )
202- except Exception as exc :
203- print ("Error processing line {:d} of {}:\n " .format (n + 1 , fullname ),
204- file = sys .stderr )
205- import traceback
206- for record in traceback .format_exception (exc ):
207- for line in record .splitlines ():
208- print (' ' + line , file = sys .stderr )
209- print ("\n Remainder of file ignored" , file = sys .stderr )
210- break
206+ line = line .rstrip ()
207+ dir , dircase = makepath (sitedir , line )
208+ if dircase not in known_paths and os .path .exists (dir ):
209+ sys .path .append (dir )
210+ known_paths .add (dircase )
211+ except Exception as exc :
212+ print (f"Error processing line { n :d} of { fullname } :\n " ,
213+ file = sys .stderr )
214+ import traceback
215+ for record in traceback .format_exception (exc ):
216+ for line in record .splitlines ():
217+ print (' ' + line , file = sys .stderr )
218+ print ("\n Remainder of file ignored" , file = sys .stderr )
219+ break
211220 if reset :
212221 known_paths = None
213222 return known_paths
0 commit comments