Skip to content

Commit 185a6d7

Browse files
committed
Update tests that build extensions
1 parent de2da65 commit 185a6d7

6 files changed

Lines changed: 42 additions & 26 deletions

File tree

Lib/test/test_cext/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import shlex
99
import shutil
1010
import subprocess
11+
import sysconfig
12+
import sys
1113
import unittest
1214
from test import support
1315

@@ -62,6 +64,9 @@ def run_cmd(operation, cmd):
6264
env['CPYTHON_TEST_LIMITED'] = '1'
6365
if abi3t:
6466
env['CPYTHON_TEST_ABI3T'] = '1'
67+
if support.MS_WINDOWS and sysconfig.is_python_build():
68+
env['CPYTHON_EXTRA_INCDIRS'] = os.path.split(sysconfig.get_config_h_filename())[0]
69+
env['CPYTHON_EXTRA_LIBDIRS'] = os.path.split(sys.executable)[0]
6570
env['CPYTHON_TEST_EXT_NAME'] = extension_name
6671
env['TEST_INTERNAL_C_API'] = str(int(self.TEST_INTERNAL_C_API))
6772
if support.verbose:

Lib/test/test_cext/setup.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ def main():
6666
limited = bool(os.environ.get("CPYTHON_TEST_LIMITED", ""))
6767
abi3t = bool(os.environ.get("CPYTHON_TEST_ABI3T", ""))
6868
internal = bool(int(os.environ.get("TEST_INTERNAL_C_API", "0")))
69+
incdirs = os.environ.get("CPYTHON_EXTRA_INCDIRS", "")
70+
libdirs = os.environ.get("CPYTHON_EXTRA_LIBDIRS", "")
6971

7072
sources = [SOURCE]
7173

@@ -106,19 +108,16 @@ def main():
106108
if internal:
107109
cflags.append('-DTEST_INTERNAL_C_API=1')
108110

109-
# On Windows, add PCbuild\amd64\ to include and library directories
111+
# Add additional include and library directories, typically for in-tree
112+
# testing where not all directories are inferred
110113
include_dirs = []
111114
library_dirs = []
112-
if support.MS_WINDOWS:
113-
srcdir = sysconfig.get_config_var('srcdir')
114-
machine = platform.uname().machine
115-
pcbuild = os.path.join(srcdir, 'PCbuild', machine)
116-
if os.path.exists(pcbuild):
117-
# pyconfig.h is generated in PCbuild\amd64\
118-
include_dirs.append(pcbuild)
119-
# python313.lib is generated in PCbuild\amd64\
120-
library_dirs.append(pcbuild)
121-
print(f"Add PCbuild directory: {pcbuild}")
115+
if incdirs:
116+
print("Add incdirs:", incdirs)
117+
include_dirs.extend(incdirs.split(os.pathsep))
118+
if libdirs:
119+
print("Add libdirs:", libdirs)
120+
library_dirs.extend(libdirs.split(os.pathsep))
122121

123122
# Display information to help debugging
124123
for env_name in ('CC', 'CFLAGS', 'CPPFLAGS'):

Lib/test/test_cppext/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import shutil
77
import subprocess
88
import sys
9+
import sysconfig
910
import unittest
1011
from test import support
1112

@@ -50,6 +51,9 @@ def run_cmd(operation, cmd):
5051
env['CPYTHON_TEST_CPP_STD'] = std
5152
if limited:
5253
env['CPYTHON_TEST_LIMITED'] = '1'
54+
if support.MS_WINDOWS and sysconfig.is_python_build():
55+
env['CPYTHON_EXTRA_INCDIRS'] = os.path.split(sysconfig.get_config_h_filename())[0]
56+
env['CPYTHON_EXTRA_LIBDIRS'] = os.path.split(sys.executable)[0]
5357
env['CPYTHON_TEST_EXT_NAME'] = extension_name
5458
env['TEST_INTERNAL_C_API'] = str(int(self.TEST_INTERNAL_C_API))
5559
if extra_cflags:

Lib/test/test_cppext/setup.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def main():
4848
module_name = os.environ["CPYTHON_TEST_EXT_NAME"]
4949
limited = bool(os.environ.get("CPYTHON_TEST_LIMITED", ""))
5050
internal = bool(int(os.environ.get("TEST_INTERNAL_C_API", "0")))
51+
incdirs = os.environ.get("CPYTHON_EXTRA_INCDIRS", "")
52+
libdirs = os.environ.get("CPYTHON_EXTRA_LIBDIRS", "")
5153

5254
cppflags = list(CPPFLAGS)
5355
cppflags.append(f'-DMODULE_NAME={module_name}')
@@ -90,19 +92,16 @@ def main():
9092
if extra_cflags:
9193
cppflags.extend(shlex.split(extra_cflags))
9294

93-
# On Windows, add PCbuild\amd64\ to include and library directories
95+
# Add additional include and library directories, typically for in-tree
96+
# testing where not all directories are inferred
9497
include_dirs = []
9598
library_dirs = []
96-
if support.MS_WINDOWS:
97-
srcdir = sysconfig.get_config_var('srcdir')
98-
machine = platform.uname().machine
99-
pcbuild = os.path.join(srcdir, 'PCbuild', machine)
100-
if os.path.exists(pcbuild):
101-
# pyconfig.h is generated in PCbuild\amd64\
102-
include_dirs.append(pcbuild)
103-
# python313.lib is generated in PCbuild\amd64\
104-
library_dirs.append(pcbuild)
105-
print(f"Add PCbuild directory: {pcbuild}")
99+
if incdirs:
100+
print("Add incdirs:", incdirs)
101+
include_dirs.extend(incdirs.split(os.pathsep))
102+
if libdirs:
103+
print("Add libdirs:", libdirs)
104+
library_dirs.extend(libdirs.split(os.pathsep))
106105

107106
# Display information to help debugging
108107
for env_name in ('CC', 'CXX', 'CFLAGS', 'CPPFLAGS', 'CXXFLAGS'):

Python/dynload_win.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ static char *GetPythonImport (HINSTANCE hModule)
151151
to this python DLL is loaded, not a python3.dll that might be on the path
152152
by chance.
153153
Return whether the DLL was found.
154+
On free-threaded builds, PY3_DLLNAME is undefined and this is a no-op.
155+
_Py_CheckPython3t will check for python3t.dll in that case.
154156
*/
155157
extern HMODULE PyWin_DLLhModule;
156158
static int
@@ -210,16 +212,17 @@ _Py_CheckPython3(void)
210212
* check is a bit more complicated on that version as we need to try loading
211213
* from a subdirectory first in case the adjacent python3t.dll is meant for
212214
* python315t.dll (and we are python315.dll).
213-
* ABI3T_DLLNAME is undefined for free-threaded builds, and so this function is
214-
* a no-op (we assume that _Py_CheckPython3 has already been called).
215215
*/
216216
static int
217217
_Py_CheckPython3t(void)
218218
{
219219
#ifndef ABI3T_DLLNAME
220220
return 1;
221221
#else
222-
#if PY_MAJOR_VERSION==3 && PY_MINOR_VERSION==15
222+
#if defined(PY3_DLLNAME) && PY_MAJOR_VERSION==3 && PY_MINOR_VERSION==15
223+
/* GIL-enabled builds of 3.15 might have a python3t.dll adjacent that is for
224+
a free-threaded build. So we first check in a subdirectory in that case.
225+
If it's not there, we'll look adjacent. */
223226
#define ABI3T_COMPAT_DLLNAME L"abi3t-compat\\" ABI3T_DLLNAME L".dll"
224227
#endif
225228
static int python3t_checked = 0;

Tools/peg_generator/pegen/build.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,15 @@ def compile_c_extension(
145145
str(MOD_DIR.parent.parent.parent / "Parser" / "lexer"),
146146
str(MOD_DIR.parent.parent.parent / "Parser" / "tokenizer"),
147147
]
148+
library_dirs = []
148149
if sys.platform == "win32":
149150
# HACK: The location of pyconfig.h has moved within our build, and
150151
# setuptools hasn't updated for it yet. So add the path manually for now
151152
include_dirs.append(pathlib.Path(sysconfig.get_config_h_filename()).parent)
153+
if sysconfig.is_python_build():
154+
# HACK: Our output directory for free-threaded builds has moved, and so
155+
# tests running in-tree require our sys.executable directory for libs
156+
library_dirs.append(pathlib.Path(sys.executable).parent)
152157
extension = Extension(
153158
extension_name,
154159
sources=[generated_source_path],
@@ -160,7 +165,8 @@ def compile_c_extension(
160165
assert isinstance(cmd, setuptools.command.build_ext.build_ext)
161166
fixup_build_ext(cmd)
162167
cmd.build_lib = str(source_file_path.parent)
163-
cmd.include_dirs = include_dirs
168+
cmd.include_dirs = [str(p) for p in include_dirs]
169+
cmd.library_dirs = [str(p) for p in library_dirs]
164170
if build_dir:
165171
cmd.build_temp = build_dir
166172
cmd.ensure_finalized()

0 commit comments

Comments
 (0)