Skip to content

Commit 74208ed

Browse files
gh-113659: Skip hidden .pth files (GH-113660)
Skip .pth files with names starting with a dot or hidden file attribute.
1 parent 7a24ecc commit 74208ed

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

Lib/site.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import builtins
7575
import _sitebuiltins
7676
import io
77+
import stat
7778

7879
# Prefixes for site-packages; add additional prefixes like /usr/local here
7980
PREFIXES = [sys.prefix, sys.exec_prefix]
@@ -168,6 +169,14 @@ def addpackage(sitedir, name, known_paths):
168169
else:
169170
reset = False
170171
fullname = os.path.join(sitedir, name)
172+
try:
173+
st = os.lstat(fullname)
174+
except OSError:
175+
return
176+
if ((getattr(st, 'st_flags', 0) & stat.UF_HIDDEN) or
177+
(getattr(st, 'st_file_attributes', 0) & stat.FILE_ATTRIBUTE_HIDDEN)):
178+
_trace(f"Skipping hidden .pth file: {fullname!r}")
179+
return
171180
_trace(f"Processing .pth file: {fullname!r}")
172181
try:
173182
# locale encoding is not ideal especially on Windows. But we have used
@@ -221,7 +230,8 @@ def addsitedir(sitedir, known_paths=None):
221230
names = os.listdir(sitedir)
222231
except OSError:
223232
return
224-
names = [name for name in names if name.endswith(".pth")]
233+
names = [name for name in names
234+
if name.endswith(".pth") and not name.startswith(".")]
225235
for name in sorted(names):
226236
addpackage(sitedir, name, known_paths)
227237
if reset:

Lib/test/test_site.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import os
2020
import re
2121
import shutil
22+
import stat
2223
import subprocess
2324
import sys
2425
import sysconfig
@@ -195,6 +196,45 @@ def test_addsitedir(self):
195196
finally:
196197
pth_file.cleanup()
197198

199+
def test_addsitedir_dotfile(self):
200+
pth_file = PthFile('.dotfile')
201+
pth_file.cleanup(prep=True)
202+
try:
203+
pth_file.create()
204+
site.addsitedir(pth_file.base_dir, set())
205+
self.assertNotIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
206+
self.assertIn(pth_file.base_dir, sys.path)
207+
finally:
208+
pth_file.cleanup()
209+
210+
@unittest.skipUnless(hasattr(os, 'chflags'), 'test needs os.chflags()')
211+
def test_addsitedir_hidden_flags(self):
212+
pth_file = PthFile()
213+
pth_file.cleanup(prep=True)
214+
try:
215+
pth_file.create()
216+
st = os.stat(pth_file.file_path)
217+
os.chflags(pth_file.file_path, st.st_flags | stat.UF_HIDDEN)
218+
site.addsitedir(pth_file.base_dir, set())
219+
self.assertNotIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
220+
self.assertIn(pth_file.base_dir, sys.path)
221+
finally:
222+
pth_file.cleanup()
223+
224+
@unittest.skipUnless(sys.platform == 'win32', 'test needs Windows')
225+
@support.requires_subprocess()
226+
def test_addsitedir_hidden_file_attribute(self):
227+
pth_file = PthFile()
228+
pth_file.cleanup(prep=True)
229+
try:
230+
pth_file.create()
231+
subprocess.check_call(['attrib', '+H', pth_file.file_path])
232+
site.addsitedir(pth_file.base_dir, set())
233+
self.assertNotIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
234+
self.assertIn(pth_file.base_dir, sys.path)
235+
finally:
236+
pth_file.cleanup()
237+
198238
# This tests _getuserbase, hence the double underline
199239
# to distinguish from a test for getuserbase
200240
def test__getuserbase(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Skip ``.pth`` files with names starting with a dot or hidden file attribute.

0 commit comments

Comments
 (0)