Skip to content

Commit 3b6a8d2

Browse files
bpo-41304: Ensure python3x._pth is loaded on Windows (GH-21495)
(cherry picked from commit 936a660) Co-authored-by: Steve Dower <[email protected]>
1 parent 16eea45 commit 3b6a8d2

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

584-
def _create_underpth_exe(self, lines):
584+
def _create_underpth_exe(self, lines, exe_pth=True):
585+
import _winapi
585586
temp_dir = tempfile.mkdtemp()
586587
self.addCleanup(test.support.rmtree, temp_dir)
587588
exe_file = os.path.join(temp_dir, os.path.split(sys.executable)[1])
589+
dll_src_file = _winapi.GetModuleFileName(sys.dllhandle)
590+
dll_file = os.path.join(temp_dir, os.path.split(dll_src_file)[1])
588591
shutil.copy(sys.executable, exe_file)
589-
_pth_file = os.path.splitext(exe_file)[0] + '._pth'
592+
shutil.copy(dll_src_file, dll_file)
593+
if exe_pth:
594+
_pth_file = os.path.splitext(exe_file)[0] + '._pth'
595+
else:
596+
_pth_file = os.path.splitext(dll_file)[0] + '._pth'
590597
with open(_pth_file, 'w') as f:
591598
for line in lines:
592599
print(line, file=f)
@@ -654,5 +661,30 @@ def test_underpth_file(self):
654661
self.assertTrue(rc, "sys.path is incorrect")
655662

656663

664+
def test_underpth_dll_file(self):
665+
libpath = os.path.dirname(os.path.dirname(encodings.__file__))
666+
exe_prefix = os.path.dirname(sys.executable)
667+
exe_file = self._create_underpth_exe([
668+
'fake-path-name',
669+
*[libpath for _ in range(200)],
670+
'',
671+
'# comment',
672+
'import site'
673+
], exe_pth=False)
674+
sys_prefix = os.path.dirname(exe_file)
675+
env = os.environ.copy()
676+
env['PYTHONPATH'] = 'from-env'
677+
env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
678+
rc = subprocess.call([exe_file, '-c',
679+
'import sys; sys.exit(not sys.flags.no_site and '
680+
'%r in sys.path and %r in sys.path and %r not in sys.path and '
681+
'all("\\r" not in p and "\\n" not in p for p in sys.path))' % (
682+
os.path.join(sys_prefix, 'fake-path-name'),
683+
libpath,
684+
os.path.join(sys_prefix, 'from-env'),
685+
)], env=env)
686+
self.assertTrue(rc, "sys.path is incorrect")
687+
688+
657689
if __name__ == "__main__":
658690
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
@@ -717,7 +717,7 @@ static int
717717
get_pth_filename(PyCalculatePath *calculate, wchar_t *filename,
718718
const _PyPathConfig *pathconfig)
719719
{
720-
if (get_dllpath(filename) &&
720+
if (!get_dllpath(filename) &&
721721
!change_ext(filename, filename, L"._pth") &&
722722
exists(filename))
723723
{

0 commit comments

Comments
 (0)