https://github.com/python/cpython/commit/5172fa4cd9f7063b2d3b774dc4a5c0e9eceda612
commit: 5172fa4cd9f7063b2d3b774dc4a5c0e9eceda612
branch: main
author: thexai <[email protected]>
committer: chris-eibl <[email protected]>
date: 2026-09-11T15:35:34+02:00
summary:

gh-152433: Windows: make ``dynload_win.c`` UWP compatible (#152797)

files:
A Misc/NEWS.d/next/Windows/2026-07-01-17-31-05.gh-issue-152433.dod-DF.rst
M Python/dynload_win.c

diff --git 
a/Misc/NEWS.d/next/Windows/2026-07-01-17-31-05.gh-issue-152433.dod-DF.rst 
b/Misc/NEWS.d/next/Windows/2026-07-01-17-31-05.gh-issue-152433.dod-DF.rst
new file mode 100644
index 00000000000000..e948f5508396ea
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2026-07-01-17-31-05.gh-issue-152433.dod-DF.rst
@@ -0,0 +1 @@
+make ``dynload_win.c`` UWP compatible.
diff --git a/Python/dynload_win.c b/Python/dynload_win.c
index 1c2544e94160ac..423a2d2ef21e90 100644
--- a/Python/dynload_win.c
+++ b/Python/dynload_win.c
@@ -164,12 +164,16 @@ _Py_CheckPython3(void)
     static int python3_checked = 0;
     static HANDLE hPython3;
     #define MAXPATHLEN 512
-    wchar_t py3path[MAXPATHLEN+1];
     if (python3_checked) {
         return hPython3 != NULL;
     }
     python3_checked = 1;
 
+#ifndef MS_WINDOWS_DESKTOP
+    // LoadPackagedLibrary doesn't accept absolute paths so load dll name from 
current app dir
+    hPython3 = LoadPackagedLibrary(PY3_DLLNAME, 0);
+#else
+    wchar_t py3path[MAXPATHLEN + 1];
     /* If there is a python3.dll next to the python3y.dll,
        use that DLL */
     if (PyWin_DLLhModule && GetModuleFileNameW(PyWin_DLLhModule, py3path, 
MAXPATHLEN)) {
@@ -202,6 +206,8 @@ _Py_CheckPython3(void)
             hPython3 = LoadLibraryExW(py3path, NULL, 
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
         }
     }
+#endif
+
     return hPython3 != NULL;
     #undef MAXPATHLEN
 #endif /* PY3_DLLNAME */
@@ -272,6 +278,20 @@ _Py_CheckPython3t(void)
 
 #endif /* Py_ENABLE_SHARED */
 
+static wchar_t* _Py_AbsolutePath_To_RelativePath(wchar_t* abs_path)
+{
+    wchar_t* rel_path = NULL;
+    wchar_t process_path[512] = { 0 };
+    if (GetModuleFileNameW(NULL, process_path, 512))
+    {
+        wchar_t* path = wcsrchr(process_path, L'\\');
+        path[1] = L'\0'; // strip process name
+        if (wcsstr(abs_path, process_path))
+          rel_path = &abs_path[wcslen(process_path)];
+    }
+    return rel_path;
+}
+
 dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
                                               const char *shortname,
                                               PyObject *pathname, FILE *fp)
@@ -299,14 +319,24 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char 
*prefix,
         old_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
 #endif
 
+        Py_BEGIN_ALLOW_THREADS
+#ifndef MS_WINDOWS_DESKTOP
+        // UWP does not allow absolute paths due security restrictions.
+        // If path is contained inside process path (sub folder), use the 
relative path instead.
+        wchar_t* rel_path = _Py_AbsolutePath_To_RelativePath(wpathname);
+        if (rel_path)
+            hDLL = LoadPackagedLibrary(rel_path, 0);
+        else
+            hDLL = LoadPackagedLibrary(wpathname, 0);
+#else
         /* bpo-36085: We use LoadLibraryEx with restricted search paths
            to avoid DLL preloading attacks and enable use of the
            AddDllDirectory function. We add SEARCH_DLL_LOAD_DIR to
            ensure DLLs adjacent to the PYD are preferred. */
-        Py_BEGIN_ALLOW_THREADS
         hDLL = LoadLibraryExW(wpathname, NULL,
                               LOAD_LIBRARY_SEARCH_DEFAULT_DIRS |
                               LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR);
+#endif
         Py_END_ALLOW_THREADS
         PyMem_Free(wpathname);
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to