diff --git a/Include/internal/pycore_fileutils_windows.h b/Include/internal/pycore_fileutils_windows.h
index b79aa9fb4653765..41d22d6c84c651e 100644
--- a/Include/internal/pycore_fileutils_windows.h
+++ b/Include/internal/pycore_fileutils_windows.h
@@ -93,6 +93,58 @@ static inline BOOL _Py_GetFileInformationByName_ErrorIsTrustworthy(int error)
return FALSE;
}
+// Convert absolute paths to relative paths for paths within the installation path
+static const wchar_t* _Py_AbsolutePath_To_RelativePath(const wchar_t* abs_path)
+{
+ const 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;
+}
+
+static inline HANDLE _Py_WinCreateFile(
+ _In_ LPCWSTR lpFileName,
+ _In_ DWORD dwDesiredAccess,
+ _In_ DWORD dwShareMode,
+ _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,
+ _In_ DWORD dwCreationDisposition,
+ _In_ DWORD dwFlagsAndAttributes,
+ _In_opt_ HANDLE hTemplateFile
+)
+{
+#ifndef MS_WINDOWS_DESKTOP
+ if (dwShareMode == 0)
+ dwShareMode = FILE_SHARE_READ;
+
+ CREATEFILE2_EXTENDED_PARAMETERS ext;
+ ZeroMemory(&ext, sizeof(CREATEFILE2_EXTENDED_PARAMETERS));
+ ext.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
+ ext.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
+ ext.dwFileFlags = dwFlagsAndAttributes & 0xFFFF0000;
+
+ // UWP does not allow absolute paths due security restrictions and only has access to 'AppData'
+ // folder outside of the install path.
+ // If path is contained inside install path (sub folder), use the relative path instead.
+ if (wcschr(lpFileName, L':')) {
+ const wchar_t* rel_path = _Py_AbsolutePath_To_RelativePath(lpFileName);
+ if (rel_path) {
+ return CreateFile2(rel_path, dwDesiredAccess, dwShareMode, dwCreationDisposition, &ext);
+ }
+ }
+
+ return CreateFile2(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, &ext);
+#else
+ return CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
+ dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
+#endif
+}
+
#endif
#endif
diff --git a/Misc/NEWS.d/next/Windows/2026-07-04-13-33-04.gh-issue-152433.0EUfvN.rst b/Misc/NEWS.d/next/Windows/2026-07-04-13-33-04.gh-issue-152433.0EUfvN.rst
new file mode 100644
index 000000000000000..0eb90a73e78463e
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2026-07-04-13-33-04.gh-issue-152433.0EUfvN.rst
@@ -0,0 +1 @@
+Use ``CreateFile2`` on Windows UWP build.
diff --git a/Modules/_io/winconsoleio.c b/Modules/_io/winconsoleio.c
index 3da4f1df1708a78..2c990d9a6542494 100644
--- a/Modules/_io/winconsoleio.c
+++ b/Modules/_io/winconsoleio.c
@@ -30,6 +30,7 @@
#include
#include "_iomodule.h"
+#include "internal/pycore_fileutils_windows.h"
/* BUFSIZ determines how many characters can be typed at the console
before it starts blocking. */
@@ -435,10 +436,10 @@ _io__WindowsConsoleIO___init___impl(winconsoleio *self, PyObject *nameobj,
on the specific access. This is required for modern names
CONIN$ and CONOUT$, which allow reading/writing state as
well as reading/writing content. */
- handle = CreateFileW(name, GENERIC_READ | GENERIC_WRITE,
+ handle = _Py_WinCreateFile(name, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (handle == INVALID_HANDLE_VALUE)
- handle = CreateFileW(name, access,
+ handle = _Py_WinCreateFile(name, access,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
Py_END_ALLOW_THREADS
diff --git a/Modules/_winapi.c b/Modules/_winapi.c
index 3cee151b997dee3..8d55c5ad3b1e029 100644
--- a/Modules/_winapi.c
+++ b/Modules/_winapi.c
@@ -48,6 +48,7 @@
#include
#include
#include "winreparse.h"
+#include "internal/pycore_fileutils_windows.h"
// PSAPI_VERSION=2 redirects GetProcessMemoryInfo() to
// K32GetProcessMemoryInfo() in kernel32.dll, so we don't need to link
@@ -543,7 +544,7 @@ _winapi_CreateFile_impl(PyObject *module, LPCWSTR file_name,
}
Py_BEGIN_ALLOW_THREADS
- handle = CreateFileW(file_name, desired_access,
+ handle = _Py_WinCreateFile(file_name, desired_access,
share_mode, security_attributes,
creation_disposition,
flags_and_attributes, template_file);
@@ -718,7 +719,7 @@ _winapi_CreateJunction_impl(PyObject *module, LPCWSTR src_path,
if (!CreateDirectoryW(dst_path, NULL))
goto cleanup;
- junction = CreateFileW(dst_path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
+ junction = _Py_WinCreateFile(dst_path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING,
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (junction == INVALID_HANDLE_VALUE)
diff --git a/Modules/getpath.c b/Modules/getpath.c
index d1b0cf6bf902b96..98139a0e38e52a6 100644
--- a/Modules/getpath.c
+++ b/Modules/getpath.c
@@ -14,6 +14,7 @@
#ifdef MS_WINDOWS
# include // GetFullPathNameW(), MAX_PATH
# include
+# include "internal/pycore_fileutils_windows.h"
#endif
#ifdef __APPLE__
@@ -569,7 +570,7 @@ getpath_realpath_impl(PyObject *module, PyObject *pathobj)
}
Py_BEGIN_ALLOW_THREADS
- hFile = CreateFileW(path, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
+ hFile = _Py_WinCreateFile(path, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
len = GetFinalPathNameByHandleW(hFile, resolved, MAXPATHLEN, VOLUME_NAME_DOS);
err = len ? 0 : GetLastError();
diff --git a/Modules/overlapped.c b/Modules/overlapped.c
index f8b654594ebfc66..9db39bcb31a5664 100644
--- a/Modules/overlapped.c
+++ b/Modules/overlapped.c
@@ -14,7 +14,11 @@
#include "Python.h"
#include "pycore_tuple.h" // _PyTuple_FromPairSteal
-#define WINDOWS_LEAN_AND_MEAN
+#ifdef MS_WINDOWS
+# define WINDOWS_LEAN_AND_MEAN
+# include "internal/pycore_fileutils_windows.h"
+#endif
+
#include
#include
#include
@@ -1643,7 +1647,7 @@ _overlapped_Overlapped_ConnectPipe_impl(OverlappedObject *self,
HANDLE PipeHandle;
Py_BEGIN_ALLOW_THREADS
- PipeHandle = CreateFileW(Address,
+ PipeHandle = _Py_WinCreateFile(Address,
GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, NULL);
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 67461fdb3730860..eea2d3cc2313380 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -2150,7 +2150,7 @@ win32_xstat_slow_impl(const wchar_t *path, struct _Py_stat_struct *result,
flags |= FILE_FLAG_OPEN_REPARSE_POINT;
}
- hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL);
+ hFile = _Py_WinCreateFile(path, access, 0, NULL, OPEN_EXISTING, flags, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
/* Either the path doesn't exist, or the caller lacks access. */
error = GetLastError();
@@ -2185,7 +2185,7 @@ win32_xstat_slow_impl(const wchar_t *path, struct _Py_stat_struct *result,
case ERROR_INVALID_PARAMETER:
/* \\.\con requires read or write access. */
- hFile = CreateFileW(path, access | GENERIC_READ,
+ hFile = _Py_WinCreateFile(path, access | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, flags, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
@@ -2199,7 +2199,7 @@ win32_xstat_slow_impl(const wchar_t *path, struct _Py_stat_struct *result,
if (traverse) {
traverse = FALSE;
isUnhandledTag = TRUE;
- hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING,
+ hFile = _Py_WinCreateFile(path, access, 0, NULL, OPEN_EXISTING,
flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
}
if (hFile == INVALID_HANDLE_VALUE) {
@@ -4108,7 +4108,7 @@ os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
result = win32_fchmod(path->fd, mode);
}
else if (follow_symlinks) {
- HANDLE hfile = CreateFileW(path->wide,
+ HANDLE hfile = _Py_WinCreateFile(path->wide,
FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES,
0, NULL,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
@@ -5393,7 +5393,7 @@ os__path_isdevdrive_impl(PyObject *module, path_t *path)
/* only care about local dev drives */
r = Py_False;
} else {
- HANDLE hVolume = CreateFileW(
+ HANDLE hVolume = _Py_WinCreateFile(
volume,
FILE_READ_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE,
@@ -5541,7 +5541,7 @@ os__getfinalpathname_impl(PyObject *module, path_t *path)
PyObject *result;
Py_BEGIN_ALLOW_THREADS
- hFile = CreateFileW(
+ hFile = _Py_WinCreateFile(
path->wide,
0, /* desired access */
0, /* share mode */
@@ -5817,7 +5817,7 @@ _testFileTypeByName(LPCWSTR path, int testedType)
if (testedType != PY_IFREG && testedType != PY_IFDIR) {
flags |= FILE_FLAG_OPEN_REPARSE_POINT;
}
- HANDLE hfile = CreateFileW(path, FILE_READ_ATTRIBUTES, 0, NULL,
+ HANDLE hfile = _Py_WinCreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
OPEN_EXISTING, flags, NULL);
if (hfile != INVALID_HANDLE_VALUE) {
BOOL result = _testFileTypeByHandle(hfile, testedType, FALSE);
@@ -5873,7 +5873,7 @@ _testFileExistsByName(LPCWSTR path, BOOL followLinks)
if (!followLinks) {
flags |= FILE_FLAG_OPEN_REPARSE_POINT;
}
- HANDLE hfile = CreateFileW(path, FILE_READ_ATTRIBUTES, 0, NULL,
+ HANDLE hfile = _Py_WinCreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
OPEN_EXISTING, flags, NULL);
if (hfile != INVALID_HANDLE_VALUE) {
if (followLinks) {
@@ -5886,7 +5886,7 @@ _testFileExistsByName(LPCWSTR path, BOOL followLinks)
if (!result) {
return TRUE;
}
- hfile = CreateFileW(path, FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
+ hfile = _Py_WinCreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hfile != INVALID_HANDLE_VALUE) {
CloseHandle(hfile);
@@ -7182,7 +7182,7 @@ os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
#ifdef MS_WINDOWS
Py_BEGIN_ALLOW_THREADS
- hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
+ hFile = _Py_WinCreateFile(path->wide, FILE_WRITE_ATTRIBUTES, 0,
NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, NULL);
Py_END_ALLOW_THREADS
@@ -11095,7 +11095,7 @@ os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
/* First get a handle to the reparse point */
Py_BEGIN_ALLOW_THREADS
- reparse_point_handle = CreateFileW(
+ reparse_point_handle = _Py_WinCreateFile(
path->wide,
0,
0,
diff --git a/Python/dynload_win.c b/Python/dynload_win.c
index 423a2d2ef21e903..6b69241fd23e3fd 100644
--- a/Python/dynload_win.c
+++ b/Python/dynload_win.c
@@ -8,6 +8,7 @@
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "patchlevel.h" // PY_MAJOR_VERSION
+#include "pycore_fileutils_windows.h" // _Py_AbsolutePath_To_RelativePath()
#include
const char *_PyImport_DynLoadFiletab[] = {
@@ -278,20 +279,6 @@ _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)
@@ -323,7 +310,7 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
#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);
+ const wchar_t* rel_path = _Py_AbsolutePath_To_RelativePath(wpathname);
if (rel_path)
hDLL = LoadPackagedLibrary(rel_path, 0);
else