Skip to content

Commit 59da126

Browse files
[3.12] gh-113659: Skip hidden .pth files (GH-113660) (GH-114143)
Skip .pth files with names starting with a dot or hidden file attribute. (cherry picked from commit 74208ed) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 60f5f75 commit 59da126

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

Lib/site.py

+11-1
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

+40
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import os
1919
import re
2020
import shutil
21+
import stat
2122
import subprocess
2223
import sys
2324
import sysconfig
@@ -194,6 +195,45 @@ def test_addsitedir(self):
194195
finally:
195196
pth_file.cleanup()
196197

198+
def test_addsitedir_dotfile(self):
199+
pth_file = PthFile('.dotfile')
200+
pth_file.cleanup(prep=True)
201+
try:
202+
pth_file.create()
203+
site.addsitedir(pth_file.base_dir, set())
204+
self.assertNotIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
205+
self.assertIn(pth_file.base_dir, sys.path)
206+
finally:
207+
pth_file.cleanup()
208+
209+
@unittest.skipUnless(hasattr(os, 'chflags'), 'test needs os.chflags()')
210+
def test_addsitedir_hidden_flags(self):
211+
pth_file = PthFile()
212+
pth_file.cleanup(prep=True)
213+
try:
214+
pth_file.create()
215+
st = os.stat(pth_file.file_path)
216+
os.chflags(pth_file.file_path, st.st_flags | stat.UF_HIDDEN)
217+
site.addsitedir(pth_file.base_dir, set())
218+
self.assertNotIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
219+
self.assertIn(pth_file.base_dir, sys.path)
220+
finally:
221+
pth_file.cleanup()
222+
223+
@unittest.skipUnless(sys.platform == 'win32', 'test needs Windows')
224+
@support.requires_subprocess()
225+
def test_addsitedir_hidden_file_attribute(self):
226+
pth_file = PthFile()
227+
pth_file.cleanup(prep=True)
228+
try:
229+
pth_file.create()
230+
subprocess.check_call(['attrib', '+H', pth_file.file_path])
231+
site.addsitedir(pth_file.base_dir, set())
232+
self.assertNotIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
233+
self.assertIn(pth_file.base_dir, sys.path)
234+
finally:
235+
pth_file.cleanup()
236+
197237
# This tests _getuserbase, hence the double underline
198238
# to distinguish from a test for getuserbase
199239
def test__getuserbase(self):
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)