]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46015: Fixes calculation of sys.path in a venv on Windows (GH-29992)
authorSteve Dower <steve.dower@python.org>
Wed, 8 Dec 2021 19:25:58 +0000 (19:25 +0000)
committerGitHub <noreply@github.com>
Wed, 8 Dec 2021 19:25:58 +0000 (19:25 +0000)
Also ensures that pybuilddir.txt is written early enough in the build to be picked up by later steps.

Lib/test/test_embed.py
Lib/test/test_getpath.py
Misc/NEWS.d/next/Windows/2021-12-08-16-36-20.bpo-46105.t1mJ6Q.rst [new file with mode: 0644]
Modules/getpath.c
Modules/getpath.py
PCbuild/python.vcxproj

index 94161b651ff86c014c49ff661704ec876d30bbb0..8012d80bf3e55e1ce8bd86cf96e36e15cfffd1fc 100644 (file)
@@ -1362,6 +1362,8 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
             if not MS_WINDOWS:
                 paths[-1] = lib_dynload
             else:
+                # Include DLLs directory as well
+                paths.insert(1, '.\\DLLs')
                 for index, path in enumerate(paths):
                     if index == 0:
                         # Because we copy the DLLs into tmpdir as well, the zip file
index 9dd167b9975c02a97f4fec523bb15a42f826f3c8..3fb1b280031c8a456caba894c255a191e7f313f8 100644 (file)
@@ -100,6 +100,7 @@ class MockGetPathTests(unittest.TestCase):
             module_search_paths_set=1,
             module_search_paths=[
                 r"C:\Python\python98.zip",
+                r"C:\Python\DLLs",
                 r"C:\Python\Lib",
                 r"C:\Python",
             ],
diff --git a/Misc/NEWS.d/next/Windows/2021-12-08-16-36-20.bpo-46105.t1mJ6Q.rst b/Misc/NEWS.d/next/Windows/2021-12-08-16-36-20.bpo-46105.t1mJ6Q.rst
new file mode 100644 (file)
index 0000000..bfb6d31
--- /dev/null
@@ -0,0 +1 @@
+Fixed calculation of :data:`sys.path` in a venv on Windows.
index 0b982f10e418b293f2bdc66341ab1f5c4e3a7187..fedb41cdb37888a517dae7672f8b64db0a606691 100644 (file)
@@ -390,7 +390,7 @@ getpath_readlines(PyObject *Py_UNUSED(self), PyObject *args)
         while (cb && (p1[cb] == L'\n' || p1[cb] == L'\r')) {
             --cb;
         }
-        PyObject *u = PyUnicode_FromWideChar(p1, cb + 1);
+        PyObject *u = PyUnicode_FromWideChar(p1, cb ? cb + 1 : 0);
         if (!u || PyList_Append(r, u) < 0) {
             Py_XDECREF(u);
             Py_CLEAR(r);
index 6a13e232ec3e889415a97683f7808698fb2206a8..84c9760b112488a7feeb0e59f2ee4980ca93a6f7 100644 (file)
@@ -668,14 +668,17 @@ elif not pythonpath:
             pythonpath.append(joinpath(prefix, p))
 
     # Then add stdlib_dir and platstdlib_dir
-    if stdlib_dir:
-        pythonpath.append(stdlib_dir)
-    if platstdlib_dir:
-        if os_name == 'nt' and venv_prefix:
-            # QUIRK: Windows appends executable_dir instead of platstdlib_dir
-            # when in a venv
-            pythonpath.append(executable_dir)
-        else:
+    if os_name == 'nt' and venv_prefix:
+        # QUIRK: Windows generates paths differently in a venv
+        if platstdlib_dir:
+            pythonpath.append(platstdlib_dir)
+        if stdlib_dir:
+            pythonpath.append(stdlib_dir)
+        pythonpath.append(executable_dir)
+    else:
+        if stdlib_dir:
+            pythonpath.append(stdlib_dir)
+        if platstdlib_dir:
             pythonpath.append(platstdlib_dir)
 
     config['module_search_paths'] = pythonpath
index f44513702879e5931f98c9af932ffc2bc56f665b..77bccde69e3ba3baca585e0f193ab8538da8f532 100644 (file)
     <Import Project="regen.targets" />
   </ImportGroup>
   <Target Name="_TriggerPostRegen" AfterTargets="Build" DependsOnTargets="PostBuildRegen" />
+  <Target Name="GeneratePyBuildDirTxt" AfterTargets="Link">
+    <Message Text="Generating $(OutDir)pybuilddir.txt" />
+    <WriteLinesToFile File="$(OutDir)pybuilddir.txt" Lines="%0D%0A" Overwrite="true" />
+  </Target>
   <Target Name="ValidateUcrtbase" AfterTargets="AfterBuild" Condition="$(Configuration) != 'PGInstrument' and $(Platform) != 'ARM' and $(Platform) != 'ARM64'">
     <PropertyGroup>
       <UcrtName>ucrtbase</UcrtName>
@@ -147,7 +151,4 @@ $(_PGOPath)
     </PropertyGroup>
     <WriteLinesToFile File="$(PySourcePath)python.bat" Lines="$(_Content)" Overwrite="true" Condition="'$(_Content)' != '$(_ExistingContent)'" />
   </Target>
-  <Target Name="GeneratePyBuildDirTxt" BeforeTargets="AfterBuild">
-    <WriteLinesToFile File="$(OutDir)pybuilddir.txt" Lines="" Overwrite="true" />
-  </Target>
 </Project>