Skip to content

Commit 79dad03

Browse files
authored
gh-111650: Ensure pyconfig.h includes Py_GIL_DISABLED on Windows (GH-112778)
1 parent 498a096 commit 79dad03

25 files changed

+112
-44
lines changed

.github/workflows/reusable-windows.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
steps:
1717
- uses: actions/checkout@v4
1818
- name: Build CPython
19-
run: .\PCbuild\build.bat -e -d -p Win32 ${{ inputs.free-threaded && '--disable-gil' || '' }}
19+
run: .\PCbuild\build.bat -e -d -v -p Win32 ${{ inputs.free-threaded && '--disable-gil' || '' }}
2020
- name: Display build info
2121
run: .\python.bat -m test.pythoninfo
2222
- name: Tests
@@ -33,7 +33,7 @@ jobs:
3333
- name: Register MSVC problem matcher
3434
run: echo "::add-matcher::.github/problem-matchers/msvc.json"
3535
- name: Build CPython
36-
run: .\PCbuild\build.bat -e -d -p x64 ${{ inputs.free-threaded && '--disable-gil' || '' }}
36+
run: .\PCbuild\build.bat -e -d -v -p x64 ${{ inputs.free-threaded && '--disable-gil' || '' }}
3737
- name: Display build info
3838
run: .\python.bat -m test.pythoninfo
3939
- name: Tests
@@ -50,4 +50,4 @@ jobs:
5050
- name: Register MSVC problem matcher
5151
run: echo "::add-matcher::.github/problem-matchers/msvc.json"
5252
- name: Build CPython
53-
run: .\PCbuild\build.bat -e -d -p arm64 ${{ inputs.free-threaded && '--disable-gil' || '' }}
53+
run: .\PCbuild\build.bat -e -d -v -p arm64 ${{ inputs.free-threaded && '--disable-gil' || '' }}

Lib/sysconfig/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,16 @@ def get_config_h_filename():
404404
"""Return the path of pyconfig.h."""
405405
if _PYTHON_BUILD:
406406
if os.name == "nt":
407-
inc_dir = os.path.join(_PROJECT_BASE, "PC")
407+
# This ought to be as simple as dirname(sys._base_executable), but
408+
# if a venv uses symlinks to a build in the source tree, then this
409+
# fails. So instead we guess the subdirectory name from sys.winver
410+
if sys.winver.endswith('-32'):
411+
arch = 'win32'
412+
elif sys.winver.endswith('-arm64'):
413+
arch = 'arm64'
414+
else:
415+
arch = 'amd64'
416+
inc_dir = os.path.join(_PROJECT_BASE, 'PCbuild', arch)
408417
else:
409418
inc_dir = _PROJECT_BASE
410419
else:

Lib/test/test_sysconfig.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,11 +472,15 @@ def test_srcdir(self):
472472
# should be a full source checkout.
473473
Python_h = os.path.join(srcdir, 'Include', 'Python.h')
474474
self.assertTrue(os.path.exists(Python_h), Python_h)
475-
# <srcdir>/PC/pyconfig.h always exists even if unused on POSIX.
476-
pyconfig_h = os.path.join(srcdir, 'PC', 'pyconfig.h')
475+
# <srcdir>/PC/pyconfig.h.in always exists even if unused
476+
pyconfig_h = os.path.join(srcdir, 'PC', 'pyconfig.h.in')
477477
self.assertTrue(os.path.exists(pyconfig_h), pyconfig_h)
478478
pyconfig_h_in = os.path.join(srcdir, 'pyconfig.h.in')
479479
self.assertTrue(os.path.exists(pyconfig_h_in), pyconfig_h_in)
480+
if os.name == 'nt':
481+
# <executable dir>/pyconfig.h exists on Windows in a build tree
482+
pyconfig_h = os.path.join(sys.executable, '..', 'pyconfig.h')
483+
self.assertTrue(os.path.exists(pyconfig_h), pyconfig_h)
480484
elif os.name == 'posix':
481485
makefile_dir = os.path.dirname(sysconfig.get_makefile_filename())
482486
# Issue #19340: srcdir has been realpath'ed already

Lib/test/test_venv.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,18 @@
4646
def check_output(cmd, encoding=None):
4747
p = subprocess.Popen(cmd,
4848
stdout=subprocess.PIPE,
49-
stderr=subprocess.PIPE,
50-
encoding=encoding)
49+
stderr=subprocess.PIPE)
5150
out, err = p.communicate()
5251
if p.returncode:
5352
if verbose and err:
54-
print(err.decode('utf-8', 'backslashreplace'))
53+
print(err.decode(encoding or 'utf-8', 'backslashreplace'))
5554
raise subprocess.CalledProcessError(
5655
p.returncode, cmd, out, err)
56+
if encoding:
57+
return (
58+
out.decode(encoding, 'backslashreplace'),
59+
err.decode(encoding, 'backslashreplace'),
60+
)
5761
return out, err
5862

5963
class BaseTest(unittest.TestCase):
@@ -281,8 +285,8 @@ def test_sysconfig(self):
281285
('get_config_h_filename()', sysconfig.get_config_h_filename())):
282286
with self.subTest(call):
283287
cmd[2] = 'import sysconfig; print(sysconfig.%s)' % call
284-
out, err = check_output(cmd)
285-
self.assertEqual(out.strip(), expected.encode(), err)
288+
out, err = check_output(cmd, encoding='utf-8')
289+
self.assertEqual(out.strip(), expected, err)
286290

287291
@requireVenvCreate
288292
@unittest.skipUnless(can_symlink(), 'Needs symlinks')
@@ -303,8 +307,8 @@ def test_sysconfig_symlinks(self):
303307
('get_config_h_filename()', sysconfig.get_config_h_filename())):
304308
with self.subTest(call):
305309
cmd[2] = 'import sysconfig; print(sysconfig.%s)' % call
306-
out, err = check_output(cmd)
307-
self.assertEqual(out.strip(), expected.encode(), err)
310+
out, err = check_output(cmd, encoding='utf-8')
311+
self.assertEqual(out.strip(), expected, err)
308312

309313
if sys.platform == 'win32':
310314
ENV_SUBDIRS = (
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Ensures the ``Py_GIL_DISABLED`` preprocessor variable is defined in
2+
:file:`pyconfig.h` so that extension modules written in C are able to use
3+
it.

Modules/_ctypes/_ctypes_test.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#ifndef _MSC_VER
21
#include "pyconfig.h" // Py_GIL_DISABLED
3-
#endif
42

53
#ifndef Py_GIL_DISABLED
64
// Need limited C API version 3.12 for Py_MOD_PER_INTERPRETER_GIL_SUPPORTED

Modules/_scproxy.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
* using the SystemConfiguration framework.
44
*/
55

6-
#ifndef _MSC_VER
76
#include "pyconfig.h" // Py_GIL_DISABLED
8-
#endif
97

108
#ifndef Py_GIL_DISABLED
119
// Need limited C API version 3.12 for Py_MOD_PER_INTERPRETER_GIL_SUPPORTED

Modules/_stat.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
*
1212
*/
1313

14-
#ifndef _MSC_VER
1514
#include "pyconfig.h" // Py_GIL_DISABLED
16-
#endif
1715

1816
#ifndef Py_GIL_DISABLED
1917
// Need limited C API version 3.13 for PyModule_Add() on Windows

Modules/_testcapi/heaptype_relative.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#ifndef _MSC_VER
21
#include "pyconfig.h" // Py_GIL_DISABLED
3-
#endif
42

53
#ifndef Py_GIL_DISABLED
64
#define Py_LIMITED_API 0x030c0000 // 3.12

Modules/_testcapi/vectorcall_limited.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* Test Vectorcall in the limited API */
22

3-
#ifndef _MSC_VER
43
#include "pyconfig.h" // Py_GIL_DISABLED
5-
#endif
64

75
#ifndef Py_GIL_DISABLED
86
#define Py_LIMITED_API 0x030c0000 // 3.12

0 commit comments

Comments
 (0)