Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit21ab20b

Browse files
[3.11]gh-113659: Skip hidden .pth files (GH-113660) (GH-114144)
Skip .pth files with names starting with a dot or hidden file attribute.(cherry picked from commit74208ed)Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parente7dcab3 commit21ab20b

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
importbuiltins
7575
import_sitebuiltins
7676
importio
77+
importstat
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+
exceptOSError:
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
exceptOSError:
223232
return
224-
names= [namefornameinnamesifname.endswith(".pth")]
233+
names= [namefornameinnames
234+
ifname.endswith(".pth")andnotname.startswith(".")]
225235
fornameinsorted(names):
226236
addpackage(sitedir,name,known_paths)
227237
ifreset:

‎Lib/test/test_site.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
importos
2020
importre
2121
importshutil
22+
importstat
2223
importsubprocess
2324
importsys
2425
importsysconfig
@@ -195,6 +196,45 @@ def test_addsitedir(self):
195196
finally:
196197
pth_file.cleanup()
197198

199+
deftest_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+
deftest_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+
deftest_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
deftest__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)

[8]ページ先頭

©2009-2025 Movatter.jp