Skip to content

Commit 4bfcffe

Browse files
authored
bpo-41304: Ensure python3x._pth is loaded on Windows (GH-21495) (#21499)
1 parent 79c6b60 commit 4bfcffe

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

Lib/test/test_site.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,12 +573,19 @@ def test_startup_interactivehook_isolated_explicit(self):
573573
@unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
574574
class _pthFileTests(unittest.TestCase):
575575

576-
def _create_underpth_exe(self, lines):
576+
def _create_underpth_exe(self, lines, exe_pth=True):
577+
import _winapi
577578
temp_dir = tempfile.mkdtemp()
578579
self.addCleanup(test.support.rmtree, temp_dir)
579580
exe_file = os.path.join(temp_dir, os.path.split(sys.executable)[1])
581+
dll_src_file = _winapi.GetModuleFileName(sys.dllhandle)
582+
dll_file = os.path.join(temp_dir, os.path.split(dll_src_file)[1])
580583
shutil.copy(sys.executable, exe_file)
581-
_pth_file = os.path.splitext(exe_file)[0] + '._pth'
584+
shutil.copy(dll_src_file, dll_file)
585+
if exe_pth:
586+
_pth_file = os.path.splitext(exe_file)[0] + '._pth'
587+
else:
588+
_pth_file = os.path.splitext(dll_file)[0] + '._pth'
582589
with open(_pth_file, 'w') as f:
583590
for line in lines:
584591
print(line, file=f)
@@ -646,5 +653,30 @@ def test_underpth_file(self):
646653
self.assertTrue(rc, "sys.path is incorrect")
647654

648655

656+
def test_underpth_dll_file(self):
657+
libpath = os.path.dirname(os.path.dirname(encodings.__file__))
658+
exe_prefix = os.path.dirname(sys.executable)
659+
exe_file = self._create_underpth_exe([
660+
'fake-path-name',
661+
*[libpath for _ in range(200)],
662+
'',
663+
'# comment',
664+
'import site'
665+
], exe_pth=False)
666+
sys_prefix = os.path.dirname(exe_file)
667+
env = os.environ.copy()
668+
env['PYTHONPATH'] = 'from-env'
669+
env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
670+
rc = subprocess.call([exe_file, '-c',
671+
'import sys; sys.exit(not sys.flags.no_site and '
672+
'%r in sys.path and %r in sys.path and %r not in sys.path and '
673+
'all("\\r" not in p and "\\n" not in p for p in sys.path))' % (
674+
os.path.join(sys_prefix, 'fake-path-name'),
675+
libpath,
676+
os.path.join(sys_prefix, 'from-env'),
677+
)], env=env)
678+
self.assertTrue(rc, "sys.path is incorrect")
679+
680+
649681
if __name__ == "__main__":
650682
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes `python3x._pth` being ignored on Windows

PC/getpathp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ calculate_init(PyCalculatePath *calculate,
673673
static int
674674
get_pth_filename(wchar_t *spbuffer, _PyPathConfig *config)
675675
{
676-
if (get_dllpath(spbuffer) &&
676+
if (!get_dllpath(spbuffer) &&
677677
!change_ext(spbuffer, spbuffer, L"._pth") &&
678678
exists(spbuffer))
679679
{

0 commit comments

Comments
 (0)