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

Commit59da126

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 commit74208ed)Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent60f5f75 commit59da126

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
@@ -18,6 +18,7 @@
1818
importos
1919
importre
2020
importshutil
21+
importstat
2122
importsubprocess
2223
importsys
2324
importsysconfig
@@ -194,6 +195,45 @@ def test_addsitedir(self):
194195
finally:
195196
pth_file.cleanup()
196197

198+
deftest_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+
deftest_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+
deftest_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
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