[Python-checkins] bpo-46049: Fixes ._pth support on non-Windows (GH-30051)

zooba webhook-mailer at python.org
Sat Dec 11 10:06:21 EST 2021


https://github.com/python/cpython/commit/bfc59ed0a00106f5ba4a32a0c5b3dbe71d12665d
commit: bfc59ed0a00106f5ba4a32a0c5b3dbe71d12665d
branch: main
author: Steve Dower <steve.dower at python.org>
committer: zooba <steve.dower at microsoft.com>
date: 2021-12-11T15:06:17Z
summary:

bpo-46049: Fixes ._pth support on non-Windows (GH-30051)

files:
A Misc/NEWS.d/next/Core and Builtins/2021-12-11-13-49-19.bpo-46049.9dNto2.rst
M Lib/test/test_site.py
M Modules/getpath.c
M Modules/getpath.py

diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py
index 199022a1054f3..c54d868cb234f 100644
--- a/Lib/test/test_site.py
+++ b/Lib/test/test_site.py
@@ -564,26 +564,39 @@ def test_startup_interactivehook_isolated_explicit(self):
             'import site, sys; site.enablerlcompleter(); sys.exit(hasattr(sys, "__interactivehook__"))']).wait()
         self.assertTrue(r, "'__interactivehook__' not added by enablerlcompleter()")
 
- at unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
 class _pthFileTests(unittest.TestCase):
 
-    def _create_underpth_exe(self, lines, exe_pth=True):
-        import _winapi
-        temp_dir = tempfile.mkdtemp()
-        self.addCleanup(os_helper.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)
-        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)
-        return exe_file
+    if sys.platform == 'win32':
+        def _create_underpth_exe(self, lines, exe_pth=True):
+            import _winapi
+            temp_dir = tempfile.mkdtemp()
+            self.addCleanup(os_helper.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)
+            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)
+            return exe_file
+    else:
+        def _create_underpth_exe(self, lines, exe_pth=True):
+            if not exe_pth:
+                raise unittest.SkipTest("library ._pth file not supported on this platform")
+            temp_dir = tempfile.mkdtemp()
+            self.addCleanup(os_helper.rmtree, temp_dir)
+            exe_file = os.path.join(temp_dir, os.path.split(sys.executable)[1])
+            os.symlink(sys.executable, exe_file)
+            _pth_file = exe_file + '._pth'
+            with open(_pth_file, 'w') as f:
+                for line in lines:
+                    print(line, file=f)
+            return exe_file
 
     def _calc_sys_path_for_underpth_nosite(self, sys_prefix, lines):
         sys_path = []
@@ -605,7 +618,7 @@ def test_underpth_basic(self):
 
         output = subprocess.check_output([exe_file, '-c',
             'import sys; print("\\n".join(sys.path) if sys.flags.no_site else "")'
-        ], encoding='ansi')
+        ], encoding='utf-8', errors='surrogateescape')
         actual_sys_path = output.rstrip().split('\n')
         self.assertTrue(actual_sys_path, "sys.flags.no_site was False")
         self.assertEqual(
@@ -630,10 +643,10 @@ def test_underpth_nosite_file(self):
 
         env = os.environ.copy()
         env['PYTHONPATH'] = 'from-env'
-        env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
+        env['PATH'] = '{}{}{}'.format(exe_prefix, os.pathsep, os.getenv('PATH'))
         output = subprocess.check_output([exe_file, '-c',
             'import sys; print("\\n".join(sys.path) if sys.flags.no_site else "")'
-        ], env=env, encoding='ansi')
+        ], env=env, encoding='utf-8', errors='surrogateescape')
         actual_sys_path = output.rstrip().split('\n')
         self.assertTrue(actual_sys_path, "sys.flags.no_site was False")
         self.assertEqual(
@@ -666,7 +679,6 @@ def test_underpth_file(self):
             )], env=env)
         self.assertTrue(rc, "sys.path is incorrect")
 
-
     def test_underpth_dll_file(self):
         libpath = test.support.STDLIB_DIR
         exe_prefix = os.path.dirname(sys.executable)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-12-11-13-49-19.bpo-46049.9dNto2.rst b/Misc/NEWS.d/next/Core and Builtins/2021-12-11-13-49-19.bpo-46049.9dNto2.rst
new file mode 100644
index 0000000000000..07c6cb45614b6
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-12-11-13-49-19.bpo-46049.9dNto2.rst	
@@ -0,0 +1 @@
+Ensure :file:`._pth` files work as intended on platforms other than Windows.
diff --git a/Modules/getpath.c b/Modules/getpath.c
index 3adce4653342f..fdfe929514530 100644
--- a/Modules/getpath.c
+++ b/Modules/getpath.c
@@ -141,7 +141,7 @@ getpath_hassuffix(PyObject *Py_UNUSED(self), PyObject *args)
     if (path) {
         suffix = PyUnicode_AsWideCharString(suffixobj, &suffixLen);
         if (suffix) {
-            if (suffixLen < len ||
+            if (suffixLen > len ||
 #ifdef MS_WINDOWS
                 wcsicmp(&path[len - suffixLen], suffix) != 0
 #else
diff --git a/Modules/getpath.py b/Modules/getpath.py
index 77f951dbedef3..37d2ea03b0bbd 100644
--- a/Modules/getpath.py
+++ b/Modules/getpath.py
@@ -407,24 +407,22 @@ def search_up(prefix, *landmarks, test=isfile):
 # Calling Py_SetPythonHome() or Py_SetPath() will override ._pth search,
 # but environment variables and command-line options cannot.
 if not py_setpath and not home_was_set:
-    # Check adjacent to the main DLL/dylib/so
-    if library:
-        try:
-            pth = readlines(library.rpartition('.')[0] + '._pth')
-            pth_dir = dirname(library)
-        except FileNotFoundError:
-            pass
-
-    # Check adjacent to the original executable, even if we
-    # redirected to actually launch Python. This may allow a
-    # venv to override the base_executable's ._pth file, but
-    # it cannot override the library's one.
-    if not pth_dir:
-        try:
-            pth = readlines(executable.rpartition('.')[0] + '._pth')
-            pth_dir = dirname(executable)
-        except FileNotFoundError:
-            pass
+    # 1. Check adjacent to the main DLL/dylib/so (if set)
+    # 2. Check adjacent to the original executable
+    # 3. Check adjacent to our actual executable
+    # This may allow a venv to override the base_executable's
+    # ._pth file, but it cannot override the library's one.
+    for p in [library, executable, real_executable]:
+        if p:
+            if os_name == 'nt' and (hassuffix(p, 'exe') or hassuffix(p, 'dll')):
+                p = p.rpartition('.')[0]
+            p += '._pth'
+            try:
+                pth = readlines(p)
+                pth_dir = dirname(p)
+                break
+            except OSError:
+                pass
 
     # If we found a ._pth file, disable environment and home
     # detection now. Later, we will do the rest.



More information about the Python-checkins mailing list