Skip to content

Commit 28e93dd

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 4a02da4 commit 28e93dd

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

592-
def _create_underpth_exe(self, lines):
592+
def _create_underpth_exe(self, lines, exe_pth=True):
593+
import _winapi
593594
temp_dir = tempfile.mkdtemp()
594595
self.addCleanup(test.support.rmtree, temp_dir)
595596
exe_file = os.path.join(temp_dir, os.path.split(sys.executable)[1])
597+
dll_src_file = _winapi.GetModuleFileName(sys.dllhandle)
598+
dll_file = os.path.join(temp_dir, os.path.split(dll_src_file)[1])
596599
shutil.copy(sys.executable, exe_file)
597-
_pth_file = os.path.splitext(exe_file)[0] + '._pth'
600+
shutil.copy(dll_src_file, dll_file)
601+
if exe_pth:
602+
_pth_file = os.path.splitext(exe_file)[0] + '._pth'
603+
else:
604+
_pth_file = os.path.splitext(dll_file)[0] + '._pth'
598605
with open(_pth_file, 'w') as f:
599606
for line in lines:
600607
print(line, file=f)
@@ -662,5 +669,30 @@ def test_underpth_file(self):
662669
self.assertTrue(rc, "sys.path is incorrect")
663670

664671

672+
def test_underpth_dll_file(self):
673+
libpath = os.path.dirname(os.path.dirname(encodings.__file__))
674+
exe_prefix = os.path.dirname(sys.executable)
675+
exe_file = self._create_underpth_exe([
676+
'fake-path-name',
677+
*[libpath for _ in range(200)],
678+
'',
679+
'# comment',
680+
'import site'
681+
], exe_pth=False)
682+
sys_prefix = os.path.dirname(exe_file)
683+
env = os.environ.copy()
684+
env['PYTHONPATH'] = 'from-env'
685+
env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
686+
rc = subprocess.call([exe_file, '-c',
687+
'import sys; sys.exit(not sys.flags.no_site and '
688+
'%r in sys.path and %r in sys.path and %r not in sys.path and '
689+
'all("\\r" not in p and "\\n" not in p for p in sys.path))' % (
690+
os.path.join(sys_prefix, 'fake-path-name'),
691+
libpath,
692+
os.path.join(sys_prefix, 'from-env'),
693+
)], env=env)
694+
self.assertTrue(rc, "sys.path is incorrect")
695+
696+
665697
if __name__ == "__main__":
666698
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)