Skip to content

Commit 936a660

Browse files
authored
bpo-41304: Ensure python3x._pth is loaded on Windows (GH-21495)
1 parent af4eda4 commit 936a660

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
@@ -598,12 +598,19 @@ def test_startup_interactivehook_isolated_explicit(self):
598598
@unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
599599
class _pthFileTests(unittest.TestCase):
600600

601-
def _create_underpth_exe(self, lines):
601+
def _create_underpth_exe(self, lines, exe_pth=True):
602+
import _winapi
602603
temp_dir = tempfile.mkdtemp()
603604
self.addCleanup(test.support.rmtree, temp_dir)
604605
exe_file = os.path.join(temp_dir, os.path.split(sys.executable)[1])
606+
dll_src_file = _winapi.GetModuleFileName(sys.dllhandle)
607+
dll_file = os.path.join(temp_dir, os.path.split(dll_src_file)[1])
605608
shutil.copy(sys.executable, exe_file)
606-
_pth_file = os.path.splitext(exe_file)[0] + '._pth'
609+
shutil.copy(dll_src_file, dll_file)
610+
if exe_pth:
611+
_pth_file = os.path.splitext(exe_file)[0] + '._pth'
612+
else:
613+
_pth_file = os.path.splitext(dll_file)[0] + '._pth'
607614
with open(_pth_file, 'w') as f:
608615
for line in lines:
609616
print(line, file=f)
@@ -671,5 +678,30 @@ def test_underpth_file(self):
671678
self.assertTrue(rc, "sys.path is incorrect")
672679

673680

681+
def test_underpth_dll_file(self):
682+
libpath = os.path.dirname(os.path.dirname(encodings.__file__))
683+
exe_prefix = os.path.dirname(sys.executable)
684+
exe_file = self._create_underpth_exe([
685+
'fake-path-name',
686+
*[libpath for _ in range(200)],
687+
'',
688+
'# comment',
689+
'import site'
690+
], exe_pth=False)
691+
sys_prefix = os.path.dirname(exe_file)
692+
env = os.environ.copy()
693+
env['PYTHONPATH'] = 'from-env'
694+
env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
695+
rc = subprocess.call([exe_file, '-c',
696+
'import sys; sys.exit(not sys.flags.no_site and '
697+
'%r in sys.path and %r in sys.path and %r not in sys.path and '
698+
'all("\\r" not in p and "\\n" not in p for p in sys.path))' % (
699+
os.path.join(sys_prefix, 'fake-path-name'),
700+
libpath,
701+
os.path.join(sys_prefix, 'from-env'),
702+
)], env=env)
703+
self.assertTrue(rc, "sys.path is incorrect")
704+
705+
674706
if __name__ == "__main__":
675707
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
@@ -669,7 +669,7 @@ static int
669669
get_pth_filename(PyCalculatePath *calculate, wchar_t *filename,
670670
const _PyPathConfig *pathconfig)
671671
{
672-
if (get_dllpath(filename) &&
672+
if (!get_dllpath(filename) &&
673673
!change_ext(filename, filename, L"._pth") &&
674674
exists(filename))
675675
{

0 commit comments

Comments
 (0)