Skip to content

Commit e30a560

Browse files
committed
restore support for windows 7
this backports the windows 7 compatible fix for bpo-39401 from pythongh-18234, originally authored by Steve Dower, and removes explicit dependencies on pathcch.
1 parent 1331118 commit e30a560

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

PC/getpathp.c

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,43 @@ ismodule(wchar_t *filename, int update_filename)
250250
stuff as fits will be appended.
251251
*/
252252

253+
254+
static int _PathCchCombineEx_Initialized = 0;
255+
typedef HRESULT(__stdcall *PPathCchCombineEx) (PWSTR pszPathOut, size_t cchPathOut,
256+
PCWSTR pszPathIn, PCWSTR pszMore,
257+
unsigned long dwFlags);
258+
static PPathCchCombineEx _PathCchCombineEx;
259+
253260
static void
254261
join(wchar_t *buffer, const wchar_t *stuff)
255262
{
256-
if (FAILED(PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
257-
Py_FatalError("buffer overflow in getpathp.c's join()");
263+
if (_PathCchCombineEx_Initialized == 0) {
264+
HMODULE pathapi = LoadLibraryExW(L"api-ms-win-core-path-l1-1-0.dll", NULL,
265+
LOAD_LIBRARY_SEARCH_SYSTEM32);
266+
if (pathapi) {
267+
_PathCchCombineEx = (PPathCchCombineEx)GetProcAddress(pathapi, "PathCchCombineEx");
268+
}
269+
else {
270+
_PathCchCombineEx = NULL;
271+
}
272+
_PathCchCombineEx_Initialized = 1;
273+
}
274+
if (_PathCchCombineEx) {
275+
if (FAILED(_PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
276+
Py_FatalError("buffer overflow in getpathp.c's join()");
277+
}
278+
} else {
279+
if (!PathCombineW(buffer, buffer, stuff)) {
280+
Py_FatalError("buffer overflow in getpathp.c's join()");
281+
}
258282
}
259283
}
260284

285+
static int _PathCchCanonicalizeEx_Initialized = 0;
286+
typedef HRESULT(__stdcall *PPathCchCanonicalizeEx) (PWSTR pszPathOut, size_t cchPathOut,
287+
PCWSTR pszPathIn, unsigned long dwFlags);
288+
static PPathCchCanonicalizeEx _PathCchCanonicalizeEx;
289+
261290
/* Call PathCchCanonicalizeEx(path): remove navigation elements such as "."
262291
and ".." to produce a direct, well-formed path. */
263292
static PyStatus
@@ -267,8 +296,26 @@ canonicalize(wchar_t *buffer, const wchar_t *path)
267296
return _PyStatus_NO_MEMORY();
268297
}
269298

270-
if (FAILED(PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
271-
return INIT_ERR_BUFFER_OVERFLOW();
299+
if (_PathCchCanonicalizeEx_Initialized == 0) {
300+
HMODULE pathapi = LoadLibraryExW(L"api-ms-win-core-path-l1-1-0.dll", NULL,
301+
LOAD_LIBRARY_SEARCH_SYSTEM32);
302+
if (pathapi) {
303+
_PathCchCanonicalizeEx = (PPathCchCanonicalizeEx)GetProcAddress(pathapi, "PathCchCanonicalizeEx");
304+
}
305+
else {
306+
_PathCchCanonicalizeEx = NULL;
307+
}
308+
_PathCchCanonicalizeEx_Initialized = 1;
309+
}
310+
if (_PathCchCanonicalizeEx) {
311+
if (FAILED(_PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
312+
return INIT_ERR_BUFFER_OVERFLOW();
313+
}
314+
}
315+
else {
316+
if (!PathCanonicalizeW(buffer, path)) {
317+
return INIT_ERR_BUFFER_OVERFLOW();
318+
}
272319
}
273320
return _PyStatus_OK();
274321
}

PC/pyconfig.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ WIN32 is still required for the locale module.
136136

137137
/* set the version macros for the windows headers */
138138
/* Python 3.9+ requires Windows 8 or greater */
139-
#define Py_WINVER 0x0602 /* _WIN32_WINNT_WIN8 */
140-
#define Py_NTDDI NTDDI_WIN8
139+
#define Py_WINVER 0x0601 /* _WIN32_WINNT_WIN7 */
140+
#define Py_NTDDI NTDDI_WIN7
141141

142142
/* We only set these values when building Python - we don't want to force
143143
these values on extensions, as that will affect the prototypes and

PCbuild/pythoncore.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
<PreprocessorDefinitions Condition="$(IncludeExternals)">_Py_HAVE_ZLIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
107107
</ClCompile>
108108
<Link>
109-
<AdditionalDependencies>version.lib;shlwapi.lib;ws2_32.lib;pathcch.lib;%(AdditionalDependencies)</AdditionalDependencies>
109+
<AdditionalDependencies>version.lib;shlwapi.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
110110
</Link>
111111
</ItemDefinitionGroup>
112112
<ItemGroup>

0 commit comments

Comments
 (0)