xref: /trunk/main/python/python-3.11.15-win7.patch (revision d2cc2296e66478e5593190d691337190565aa006)
1diff -ruN misc/Python-3.11.15/PC/aoo_win7_compat.h misc/build/Python-3.11.15/PC/aoo_win7_compat.h
2--- misc/Python-3.11.15/PC/aoo_win7_compat.h    1970-01-01 00:00:00.000000000 +0000
3+++ misc/build/Python-3.11.15/PC/aoo_win7_compat.h  2025-01-01 00:00:00.000000000 +0000
4@@ -0,0 +1,48 @@
5+/*
6+ * Licensed to the Apache Software Foundation (ASF) under one
7+ * or more contributor license agreements.  See the NOTICE file
8+ * distributed with this work for additional information
9+ * regarding copyright ownership.  The ASF licenses this file
10+ * to you under the Apache License, Version 2.0 (the
11+ * "License"); you may not use this file except in compliance
12+ * with the License.  You may obtain a copy of the License at
13+ *
14+ *   http://www.apache.org/licenses/LICENSE-2.0
15+ *
16+ * Unless required by applicable law or agreed to in writing,
17+ * software distributed under the License is distributed on an
18+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19+ * KIND, either express or implied.  See the License for the
20+ * specific language governing permissions and limitations
21+ * under the License.
22+ */
23+
24+/*
25+ * Force-included into every CPython translation unit (PCbuild/pyproject.props,
26+ * ForcedIncludeFiles).
27+ *
28+ * CPython 3.11 is compiled here at the Windows 7 API level (Py_WINVER 0x0601
29+ * in PC/pyconfig.h) instead of upstream's Windows 8 one, so the SDK hides what it
30+ * gates on Windows 8.  Most of that would be a real dependency and must stay a
31+ * compile error.  What is defined here is only what CPython uses WITHOUT
32+ * depending on Windows 8 -- plain constants, never functions -- each with the
33+ * use that makes it safe.  The values are the SDK's.
34+ *
35+ * Found by matching every name the 10.0.19041.0 SDK declares under a Windows 8
36+ * or later guard against the sources this module compiles.
37+ */
38+
39+#ifndef AOO_PYTHON_WIN7_COMPAT_H
40+#define AOO_PYTHON_WIN7_COMPAT_H
41+
42+/* WinNls.h, WINVER >= _WIN32_WINNT_WIN8.  Modules/_winapi.c
43+   (_winapi.LCMapStringEx) only uses them to reject these flags with
44+   ValueError before calling LCMapStringEx, which itself exists from Vista. */
45+#ifndef LCMAP_SORTHANDLE
46+#define LCMAP_SORTHANDLE 0x20000000
47+#endif
48+#ifndef LCMAP_HASH
49+#define LCMAP_HASH       0x00040000
50+#endif
51+
52+#endif /* AOO_PYTHON_WIN7_COMPAT_H */
53diff -ruN misc/Python-3.11.15/PC/pathcch.h misc/build/Python-3.11.15/PC/pathcch.h
54--- misc/Python-3.11.15/PC/pathcch.h    1970-01-01 00:00:00.000000000 +0000
55+++ misc/build/Python-3.11.15/PC/pathcch.h  2025-01-01 00:00:00.000000000 +0000
56@@ -0,0 +1,63 @@
57+/*
58+ * Licensed to the Apache Software Foundation (ASF) under one
59+ * or more contributor license agreements.  See the NOTICE file
60+ * distributed with this work for additional information
61+ * regarding copyright ownership.  The ASF licenses this file
62+ * to you under the Apache License, Version 2.0 (the
63+ * "License"); you may not use this file except in compliance
64+ * with the License.  You may obtain a copy of the License at
65+ *
66+ *   http://www.apache.org/licenses/LICENSE-2.0
67+ *
68+ * Unless required by applicable law or agreed to in writing,
69+ * software distributed under the License is distributed on an
70+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
71+ * KIND, either express or implied.  See the License for the
72+ * specific language governing permissions and limitations
73+ * under the License.
74+ */
75+
76+/*
77+ * Stand-in for the Windows SDK's <pathcch.h>, for a Python that must load on
78+ * Windows 7 SP1.
79+ *
80+ * The SDK header declares the PathCch* functions as imports from
81+ * api-ms-win-core-path-l1-1-0.dll, an API set that exists from Windows 8 on.
82+ * A python311.dll importing it statically cannot be loaded on Windows 7 at
83+ * all.  This file lives in PC/, which is on the include path ahead of the SDK,
84+ * so `#include <pathcch.h>` in getpath.c, posixmodule.c, fileutils.c and
85+ * pathconfig.c resolves here instead and the SDK header is never seen.
86+ *
87+ * Only what those four files use is declared.  The definitions in
88+ * PC/pathcch_win7.c call the real functions from KernelBase.dll when it
89+ * exports them (Windows 8 and later), so behaviour there is the system's own;
90+ * the fallbacks only ever run on Windows 7.
91+ */
92+
93+#ifndef AOO_PYTHON_PATHCCH_H
94+#define AOO_PYTHON_PATHCCH_H
95+
96+#include <windows.h>
97+
98+#ifdef __cplusplus
99+extern "C" {
100+#endif
101+
102+#define PATHCCH_NONE             0x00000000
103+#define PATHCCH_ALLOW_LONG_PATHS 0x00000001
104+
105+/* Largest buffer, in characters, the PathCch functions accept. */
106+#define PATHCCH_MAX_CCH          0x8000
107+
108+HRESULT WINAPI PathCchSkipRoot(PCWSTR pszPath, PCWSTR *ppszRootEnd);
109+HRESULT WINAPI PathCchFindExtension(PCWSTR pszPath, size_t cchPath,
110+                                    PCWSTR *ppszExt);
111+HRESULT WINAPI PathCchCombineEx(PWSTR pszPathOut, size_t cchPathOut,
112+                                PCWSTR pszPathIn, PCWSTR pszMore,
113+                                ULONG dwFlags);
114+
115+#ifdef __cplusplus
116+}
117+#endif
118+
119+#endif /* AOO_PYTHON_PATHCCH_H */
120diff -ruN misc/Python-3.11.15/PC/pathcch_win7.c misc/build/Python-3.11.15/PC/pathcch_win7.c
121--- misc/Python-3.11.15/PC/pathcch_win7.c   1970-01-01 00:00:00.000000000 +0000
122+++ misc/build/Python-3.11.15/PC/pathcch_win7.c 2025-01-01 00:00:00.000000000 +0000
123@@ -0,0 +1,241 @@
124+/*
125+ * Licensed to the Apache Software Foundation (ASF) under one
126+ * or more contributor license agreements.  See the NOTICE file
127+ * distributed with this work for additional information
128+ * regarding copyright ownership.  The ASF licenses this file
129+ * to you under the Apache License, Version 2.0 (the
130+ * "License"); you may not use this file except in compliance
131+ * with the License.  You may obtain a copy of the License at
132+ *
133+ *   http://www.apache.org/licenses/LICENSE-2.0
134+ *
135+ * Unless required by applicable law or agreed to in writing,
136+ * software distributed under the License is distributed on an
137+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
138+ * KIND, either express or implied.  See the License for the
139+ * specific language governing permissions and limitations
140+ * under the License.
141+ */
142+
143+/*
144+ * PathCch* for a python311.dll that must load on Windows 7 SP1.  See
145+ * PC/pathcch.h for why this exists.
146+ *
147+ * Each function first asks KernelBase.dll for the real implementation.
148+ * KernelBase is mapped into every process from Windows 7 on, so this loads
149+ * nothing and searches no directory; from Windows 8 on it exports the PathCch
150+ * family and the system's own code runs.  Only when the export is missing --
151+ * i.e. on Windows 7 -- does the fallback below run.
152+ *
153+ * The fallbacks are written from the documented contract of each function,
154+ * and cover what Python 3.11 asks of them:
155+ *   PathCchSkipRoot      os._path_splitroot (posixmodule.c), _Py_isabs
156+ *                        (fileutils.c)
157+ *   PathCchFindExtension the ".exe" test in getpath.c's isxfile
158+ *   PathCchCombineEx     _Py_join_relfile (fileutils.c), used by getpath
159+ * PathCchCombineEx falls back to shlwapi's PathCombineW and so is limited to
160+ * MAX_PATH there -- which is the limit Windows 7 itself imposes on the Win32
161+ * file API without a \\?\ prefix.
162+ */
163+
164+#include "pathcch.h"
165+
166+#include <shlwapi.h>
167+#include <string.h>
168+#include <wchar.h>
169+
170+typedef HRESULT (WINAPI *skip_root_fn)(PCWSTR, PCWSTR *);
171+typedef HRESULT (WINAPI *find_extension_fn)(PCWSTR, size_t, PCWSTR *);
172+typedef HRESULT (WINAPI *combine_ex_fn)(PWSTR, size_t, PCWSTR, PCWSTR, ULONG);
173+
174+/* Looks NAME up in KernelBase.dll once and remembers the answer in *SLOT:
175+   0 = not looked up yet, -1 = not exported, otherwise the address.  Two
176+   threads racing here store the same value, and a pointer-sized aligned store
177+   is atomic on every Windows target, so no lock is needed. */
178+static FARPROC
179+kernelbase_export(volatile LONG_PTR *slot, const char *name)
180+{
181+    LONG_PTR value = *slot;
182+    if (value == 0) {
183+        HMODULE kernelbase = GetModuleHandleW(L"kernelbase.dll");
184+        FARPROC proc = kernelbase ? GetProcAddress(kernelbase, name) : NULL;
185+        value = proc ? (LONG_PTR)proc : (LONG_PTR)-1;
186+        *slot = value;
187+    }
188+    return value == (LONG_PTR)-1 ? NULL : (FARPROC)value;
189+}
190+
191+/* Advances past one path segment and the backslash ending it, if any. */
192+static PCWSTR
193+skip_segment(PCWSTR p)
194+{
195+    while (*p != L'\0' && *p != L'\\') {
196+        ++p;
197+    }
198+    if (*p == L'\\') {
199+        ++p;
200+    }
201+    return p;
202+}
203+
204+static int
205+is_drive_letter(WCHAR c)
206+{
207+    return (c >= L'A' && c <= L'Z') || (c >= L'a' && c <= L'z');
208+}
209+
210+/* "X:" or "X:\" -> the character after it. */
211+static PCWSTR
212+skip_drive(PCWSTR p)
213+{
214+    p += 2;
215+    if (*p == L'\\') {
216+        ++p;
217+    }
218+    return p;
219+}
220+
221+/* Root forms, as documented for PathCchSkipRoot:
222+     C:\path        drive-absolute       root "C:\"
223+     C:path         drive-relative       root "C:"
224+     \path          rooted               root "\"
225+     \\server\share\path                 root "\\server\share\"
226+     \\?\C:\path                         root "\\?\C:\"
227+     \\?\UNC\server\share\path           root "\\?\UNC\server\share\"
228+     \\?\Volume{guid}\path               root "\\?\Volume{guid}\"
229+   Anything else -- a relative path -- has no root and is E_INVALIDARG. */
230+static HRESULT
231+skip_root_fallback(PCWSTR path, PCWSTR *root_end)
232+{
233+    PCWSTR p;
234+
235+    if (path == NULL || root_end == NULL || path[0] == L'\0') {
236+        return E_INVALIDARG;
237+    }
238+    *root_end = NULL;
239+
240+    if (path[0] == L'\\' && path[1] == L'\\') {
241+        if (path[2] == L'?') {
242+            if (path[3] != L'\\') {
243+                return E_INVALIDARG;
244+            }
245+            p = path + 4;
246+            if (_wcsnicmp(p, L"UNC\\", 4) == 0) {
247+                p = skip_segment(p + 4);        /* server */
248+                p = skip_segment(p);            /* share */
249+            }
250+            else if (is_drive_letter(p[0]) && p[1] == L':') {
251+                p = skip_drive(p);
252+            }
253+            else if (_wcsnicmp(p, L"Volume{", 7) == 0) {
254+                p = skip_segment(p);
255+            }
256+            else {
257+                return E_INVALIDARG;
258+            }
259+        }
260+        else {
261+            p = skip_segment(path + 2);         /* server */
262+            p = skip_segment(p);                /* share */
263+        }
264+    }
265+    else if (is_drive_letter(path[0]) && path[1] == L':') {
266+        p = skip_drive(path);
267+    }
268+    else if (path[0] == L'\\') {
269+        p = path + 1;
270+    }
271+    else {
272+        return E_INVALIDARG;
273+    }
274+
275+    *root_end = p;
276+    return S_OK;
277+}
278+
279+/* The extension is the last '.' in the final path segment; with none,
280+   *ext is the terminating NUL.  The string must end within cch characters. */
281+static HRESULT
282+find_extension_fallback(PCWSTR path, size_t cch, PCWSTR *ext)
283+{
284+    PCWSTR p;
285+    PCWSTR dot = NULL;
286+
287+    if (ext == NULL) {
288+        return E_INVALIDARG;
289+    }
290+    *ext = NULL;
291+    if (path == NULL || cch == 0 || cch > PATHCCH_MAX_CCH) {
292+        return E_INVALIDARG;
293+    }
294+    for (p = path; p < path + cch && *p != L'\0'; ++p) {
295+        if (*p == L'.') {
296+            dot = p;
297+        }
298+        else if (*p == L'\\') {
299+            dot = NULL;
300+        }
301+    }
302+    if (p == path + cch) {
303+        return E_INVALIDARG;                    /* not terminated */
304+    }
305+    *ext = dot != NULL ? dot : p;
306+    return S_OK;
307+}
308+
309+static HRESULT
310+combine_ex_fallback(PWSTR out, size_t cch, PCWSTR in, PCWSTR more,
311+                    ULONG flags)
312+{
313+    WCHAR combined[MAX_PATH];
314+    size_t len;
315+
316+    (void)flags;        /* long paths do not exist on Windows 7 */
317+    if (out == NULL || cch == 0 || cch > PATHCCH_MAX_CCH) {
318+        return E_INVALIDARG;
319+    }
320+    out[0] = L'\0';
321+    if (PathCombineW(combined, in, more) == NULL) {
322+        return HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
323+    }
324+    len = wcslen(combined);
325+    if (len >= cch) {
326+        return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
327+    }
328+    memcpy(out, combined, (len + 1) * sizeof(WCHAR));
329+    return S_OK;
330+}
331+
332+static volatile LONG_PTR skip_root_slot;
333+static volatile LONG_PTR find_extension_slot;
334+static volatile LONG_PTR combine_ex_slot;
335+
336+HRESULT WINAPI
337+PathCchSkipRoot(PCWSTR pszPath, PCWSTR *ppszRootEnd)
338+{
339+    skip_root_fn real = (skip_root_fn)kernelbase_export(
340+        &skip_root_slot, "PathCchSkipRoot");
341+    return real != NULL ? real(pszPath, ppszRootEnd)
342+                        : skip_root_fallback(pszPath, ppszRootEnd);
343+}
344+
345+HRESULT WINAPI
346+PathCchFindExtension(PCWSTR pszPath, size_t cchPath, PCWSTR *ppszExt)
347+{
348+    find_extension_fn real = (find_extension_fn)kernelbase_export(
349+        &find_extension_slot, "PathCchFindExtension");
350+    return real != NULL ? real(pszPath, cchPath, ppszExt)
351+                        : find_extension_fallback(pszPath, cchPath, ppszExt);
352+}
353+
354+HRESULT WINAPI
355+PathCchCombineEx(PWSTR pszPathOut, size_t cchPathOut, PCWSTR pszPathIn,
356+                 PCWSTR pszMore, ULONG dwFlags)
357+{
358+    combine_ex_fn real = (combine_ex_fn)kernelbase_export(
359+        &combine_ex_slot, "PathCchCombineEx");
360+    return real != NULL
361+        ? real(pszPathOut, cchPathOut, pszPathIn, pszMore, dwFlags)
362+        : combine_ex_fallback(pszPathOut, cchPathOut, pszPathIn, pszMore,
363+                              dwFlags);
364+}
365diff -ruN misc/Python-3.11.15/PC/pyconfig.h misc/build/Python-3.11.15/PC/pyconfig.h
366--- misc/Python-3.11.15/PC/pyconfig.h   2025-01-01 00:00:00.000000000 +0000
367+++ misc/build/Python-3.11.15/PC/pyconfig.h 2025-01-01 00:00:00.000000000 +0000
368@@ -141,9 +141,12 @@
369 #endif /* MS_WIN64 */
370
371 /* set the version macros for the windows headers */
372-/* Python 3.9+ requires Windows 8 or greater */
373-#define Py_WINVER 0x0602 /* _WIN32_WINNT_WIN8 */
374-#define Py_NTDDI NTDDI_WIN8
375+/* Upstream Python 3.9+ requires Windows 8 or greater.  Apache OpenOffice
376+   builds it for Windows 7 SP1, its own floor: PC/pathcch_win7.c stands in for
377+   the PathCch* functions Windows 7 lacks, and PC/aoo_win7_compat.h supplies
378+   the few Windows 8 constants used without depending on Windows 8. */
379+#define Py_WINVER 0x0601 /* _WIN32_WINNT_WIN7 */
380+#define Py_NTDDI NTDDI_WIN7
381
382 /* We only set these values when building Python - we don't want to force
383    these values on extensions, as that will affect the prototypes and
384diff -ruN misc/Python-3.11.15/PCbuild/pyproject.props misc/build/Python-3.11.15/PCbuild/pyproject.props
385--- misc/Python-3.11.15/PCbuild/pyproject.props 2025-01-01 00:00:00.000000000 +0000
386+++ misc/build/Python-3.11.15/PCbuild/pyproject.props   2025-01-01 00:00:00.000000000 +0000
387@@ -39,6 +39,7 @@
388     <ClCompile>
389       <AdditionalIncludeDirectories>$(PySourcePath)Include;$(PySourcePath)Include\internal;$(PySourcePath)PC;$(IntDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
390       <PreprocessorDefinitions>WIN32;$(_Py3NamePreprocessorDefinition);$(_PlatformPreprocessorDefinition)$(_DebugPreprocessorDefinition)$(_PydPreprocessorDefinition)%(PreprocessorDefinitions)</PreprocessorDefinitions>
391+      <ForcedIncludeFiles>aoo_win7_compat.h;%(ForcedIncludeFiles)</ForcedIncludeFiles>
392
393       <Optimization>MaxSpeed</Optimization>
394       <IntrinsicFunctions>true</IntrinsicFunctions>
395diff -ruN misc/Python-3.11.15/PCbuild/pythoncore.vcxproj misc/build/Python-3.11.15/PCbuild/pythoncore.vcxproj
396--- misc/Python-3.11.15/PCbuild/pythoncore.vcxproj  2025-01-01 00:00:00.000000000 +0000
397+++ misc/build/Python-3.11.15/PCbuild/pythoncore.vcxproj    2025-01-01 00:00:00.000000000 +0000
398@@ -106,7 +106,7 @@
399       <PreprocessorDefinitions Condition="$(IncludeExternals)">_Py_HAVE_ZLIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
400     </ClCompile>
401     <Link>
402-      <AdditionalDependencies>version.lib;ws2_32.lib;pathcch.lib;bcrypt.lib;%(AdditionalDependencies)</AdditionalDependencies>
403+      <AdditionalDependencies>version.lib;ws2_32.lib;shlwapi.lib;bcrypt.lib;%(AdditionalDependencies)</AdditionalDependencies>
404     </Link>
405   </ItemDefinitionGroup>
406   <ItemGroup>
407@@ -473,6 +473,7 @@
408     <ClCompile Include="..\PC\invalid_parameter_handler.c" />
409     <ClCompile Include="..\PC\winreg.c" />
410     <ClCompile Include="..\PC\config.c" />
411+    <ClCompile Include="..\PC\pathcch_win7.c" />
412     <ClCompile Include="..\PC\msvcrtmodule.c" />
413     <ClCompile Include="..\Python\pyhash.c" />
414     <ClCompile Include="..\Python\_warnings.c" />
415