Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-41304: Ensure python3x._pth is loaded on Windows #21495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,12 +598,19 @@ def test_startup_interactivehook_isolated_explicit(self):
@unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
class _pthFileTests(unittest.TestCase):

def _create_underpth_exe(self, lines):
def _create_underpth_exe(self, lines, exe_pth=True):
import _winapi
temp_dir = tempfile.mkdtemp()
self.addCleanup(test.support.rmtree, temp_dir)
exe_file = os.path.join(temp_dir, os.path.split(sys.executable)[1])
dll_src_file = _winapi.GetModuleFileName(sys.dllhandle)
dll_file = os.path.join(temp_dir, os.path.split(dll_src_file)[1])
shutil.copy(sys.executable, exe_file)
_pth_file = os.path.splitext(exe_file)[0] + '._pth'
shutil.copy(dll_src_file, dll_file)
if exe_pth:
_pth_file = os.path.splitext(exe_file)[0] + '._pth'
else:
_pth_file = os.path.splitext(dll_file)[0] + '._pth'
with open(_pth_file, 'w') as f:
for line in lines:
print(line, file=f)
Expand Down Expand Up @@ -671,5 +678,30 @@ def test_underpth_file(self):
self.assertTrue(rc, "sys.path is incorrect")


def test_underpth_dll_file(self):
libpath = os.path.dirname(os.path.dirname(encodings.__file__))
exe_prefix = os.path.dirname(sys.executable)
exe_file = self._create_underpth_exe([
'fake-path-name',
*[libpath for _ in range(200)],
'',
'# comment',
'import site'
], exe_pth=False)
sys_prefix = os.path.dirname(exe_file)
env = os.environ.copy()
env['PYTHONPATH'] = 'from-env'
env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
rc = subprocess.call([exe_file, '-c',
'import sys; sys.exit(not sys.flags.no_site and '
'%r in sys.path and %r in sys.path and %r not in sys.path and '
'all("\\r" not in p and "\\n" not in p for p in sys.path))' % (
os.path.join(sys_prefix, 'fake-path-name'),
libpath,
os.path.join(sys_prefix, 'from-env'),
)], env=env)
self.assertTrue(rc, "sys.path is incorrect")


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes `python3x._pth` being ignored on Windows
2 changes: 1 addition & 1 deletion PC/getpathp.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ static int
get_pth_filename(PyCalculatePath *calculate, wchar_t *filename,
const _PyPathConfig *pathconfig)
{
if (get_dllpath(filename) &&
if (!get_dllpath(filename) &&
!change_ext(filename, filename, L"._pth") &&
exists(filename))
{
Expand Down