Skip to content

Commit 986c367

Browse files
authored
pythongh-133779: Revert Windows generation of pyconfig.h and go back to a static header. (pythonGH-133966)
Extension builders must specify Py_GIL_DISABLED if they want to link to the free-threaded builds. This was usually the case already, but this change guarantees it in all circumstances.
1 parent d55e11b commit 986c367

File tree

13 files changed

+63
-78
lines changed

13 files changed

+63
-78
lines changed

Doc/howto/free-threading-extensions.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ You can use it to enable code that only runs under the free-threaded build::
2323
/* code that only runs in the free-threaded build */
2424
#endif
2525

26+
.. note::
27+
28+
On Windows, this macro is not defined automatically, but must be specified
29+
to the compiler when building. The :func:`sysconfig.get_config_var` function
30+
can be used to determine whether the current running interpreter had the
31+
macro defined.
32+
33+
2634
Module Initialization
2735
=====================
2836

Doc/whatsnew/3.14.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,12 @@ Kumar Aditya, Edgar Margffoy, and many others.
829829
Some of these contributors are employed by Meta, which has continued to provide
830830
significant engineering resources to support this project.
831831

832+
From 3.14, when compiling extension modules for the free-threaded build of
833+
CPython on Windows, the preprocessor variable ``Py_GIL_DISABLED`` now needs to
834+
be specified by the build backend, as it will no longer be determined
835+
automatically by the C compiler. For a running interpreter, the setting that
836+
was used at compile time can be found using :func:`sysconfig.get_config_var`.
837+
832838

833839
.. _whatsnew314-pyrepl-highlighting:
834840

Include/Python.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@
5959
# include <intrin.h> // __readgsqword()
6060
#endif
6161

62+
// Suppress known warnings in Python header files.
63+
#if defined(_MSC_VER)
64+
// Warning that alignas behaviour has changed. Doesn't affect us, because we
65+
// never relied on the old behaviour.
66+
#pragma warning(push)
67+
#pragma warning(disable: 5274)
68+
#endif
69+
6270
// Include Python header files
6371
#include "pyport.h"
6472
#include "pymacro.h"
@@ -138,4 +146,9 @@
138146
#include "cpython/pyfpe.h"
139147
#include "cpython/tracemalloc.h"
140148

149+
// Restore warning filter
150+
#ifdef _MSC_VER
151+
#pragma warning(pop)
152+
#endif
153+
141154
#endif /* !Py_PYTHON_H */

Lib/sysconfig/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ def get_config_h_filename():
457457
"""Return the path of pyconfig.h."""
458458
if _PYTHON_BUILD:
459459
if os.name == "nt":
460-
inc_dir = os.path.dirname(sys._base_executable)
460+
inc_dir = os.path.join(_PROJECT_BASE, 'PC')
461461
else:
462462
inc_dir = _PROJECT_BASE
463463
else:

Lib/test/test_sysconfig.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,13 +531,10 @@ def test_srcdir(self):
531531
Python_h = os.path.join(srcdir, 'Include', 'Python.h')
532532
self.assertTrue(os.path.exists(Python_h), Python_h)
533533
# <srcdir>/PC/pyconfig.h.in always exists even if unused
534-
pyconfig_h = os.path.join(srcdir, 'PC', 'pyconfig.h.in')
535-
self.assertTrue(os.path.exists(pyconfig_h), pyconfig_h)
536534
pyconfig_h_in = os.path.join(srcdir, 'pyconfig.h.in')
537535
self.assertTrue(os.path.exists(pyconfig_h_in), pyconfig_h_in)
538536
if os.name == 'nt':
539-
# <executable dir>/pyconfig.h exists on Windows in a build tree
540-
pyconfig_h = os.path.join(sys.executable, '..', 'pyconfig.h')
537+
pyconfig_h = os.path.join(srcdir, 'PC', 'pyconfig.h')
541538
self.assertTrue(os.path.exists(pyconfig_h), pyconfig_h)
542539
elif os.name == 'posix':
543540
makefile_dir = os.path.dirname(sysconfig.get_makefile_filename())
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Reverts the change to generate different :file:`pyconfig.h` files based on
2+
compiler settings, as it was frequently causing extension builds to break.
3+
In particular, the ``Py_GIL_DISABLED`` preprocessor variable must now always
4+
be defined explicitly when compiling for the experimental free-threaded
5+
runtime. The :func:`sysconfig.get_config_var` function can be used to
6+
determine whether the current runtime was compiled with that flag or not.

PC/pyconfig.h.in renamed to PC/pyconfig.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,17 @@ WIN32 is still required for the locale module.
9999
# define Py_DEBUG 1
100100
#endif
101101

102-
/* Define to 1 if you want to disable the GIL */
103-
/* Uncomment the definition for free-threaded builds, or define it manually
104-
* when compiling extension modules. Note that we test with #ifdef, so
105-
* defining as 0 will still disable the GIL. */
106-
#ifndef Py_GIL_DISABLED
107-
/* #define Py_GIL_DISABLED 1 */
102+
/* Define to 1 when compiling for experimental free-threaded builds */
103+
#ifdef Py_GIL_DISABLED
104+
/* We undefine if it was set to zero because all later checks are #ifdef.
105+
* Note that non-Windows builds do not do this, and so every effort should
106+
* be made to avoid defining the variable at all when not desired. However,
107+
* sysconfig.get_config_var always returns a 1 or a 0, and so it seems likely
108+
* that a build backend will define it with the value.
109+
*/
110+
#if Py_GIL_DISABLED == 0
111+
#undef Py_GIL_DISABLED
112+
#endif
108113
#endif
109114

110115
/* Compiler specific defines */

PCbuild/_freeze_module.vcxproj

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@
276276
<ClCompile Include="..\Python\uniqueid.c" />
277277
</ItemGroup>
278278
<ItemGroup>
279-
<ClInclude Include="..\PC\pyconfig.h.in" />
279+
<ClInclude Include="..\PC\pyconfig.h" />
280280
</ItemGroup>
281281
<ItemGroup>
282282
<!-- BEGIN frozen modules -->
@@ -436,31 +436,6 @@
436436
<ImportGroup Label="ExtensionTargets">
437437
</ImportGroup>
438438

439-
<!-- Direct copy from pythoncore.vcxproj, but this one is only used for our
440-
own build. All other extension modules will use the copy that pythoncore
441-
generates. -->
442-
<Target Name="_UpdatePyconfig" BeforeTargets="PrepareForBuild">
443-
<MakeDir Directories="$(IntDir)" Condition="!Exists($(IntDir))" />
444-
<ItemGroup>
445-
<PyConfigH Remove="@(PyConfigH)" />
446-
<PyConfigH Include="@(ClInclude)" Condition="'%(Filename)%(Extension)' == 'pyconfig.h.in'" />
447-
</ItemGroup>
448-
<Error Text="Did not find pyconfig.h" Condition="@(ClInclude) == ''" />
449-
<PropertyGroup>
450-
<PyConfigH>@(PyConfigH->'%(FullPath)', ';')</PyConfigH>
451-
<PyConfigHText>$([System.IO.File]::ReadAllText($(PyConfigH)))</PyConfigHText>
452-
<OldPyConfigH Condition="Exists('$(IntDir)pyconfig.h')">$([System.IO.File]::ReadAllText('$(IntDir)pyconfig.h'))</OldPyConfigH>
453-
</PropertyGroup>
454-
<PropertyGroup Condition="$(DisableGil) == 'true'">
455-
<PyConfigHText>$(PyConfigHText.Replace('/* #define Py_GIL_DISABLED 1 */', '#define Py_GIL_DISABLED 1'))</PyConfigHText>
456-
</PropertyGroup>
457-
<Message Text="Updating pyconfig.h" Condition="$(PyConfigHText.TrimEnd()) != $(OldPyConfigH.TrimEnd())" />
458-
<WriteLinesToFile File="$(IntDir)pyconfig.h"
459-
Lines="$(PyConfigHText)"
460-
Overwrite="true"
461-
Condition="$(PyConfigHText.TrimEnd()) != $(OldPyConfigH.TrimEnd())" />
462-
</Target>
463-
464439
<Target Name="_RebuildGetPath" AfterTargets="_RebuildFrozen" Condition="$(Configuration) != 'PGUpdate'">
465440
<Exec Command='"$(TargetPath)" "%(GetPath.ModName)" "%(GetPath.FullPath)" "%(GetPath.IntFile)"' />
466441

PCbuild/pyproject.props

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
<Py_IntDir Condition="'$(Py_IntDir)' == ''">$(MSBuildThisFileDirectory)obj\</Py_IntDir>
1111
<IntDir>$(Py_IntDir)\$(MajorVersionNumber)$(MinorVersionNumber)$(ArchName)_$(Configuration)\$(ProjectName)\</IntDir>
1212
<IntDir>$(IntDir.Replace(`\\`, `\`))</IntDir>
13-
<!-- pyconfig.h is updated by pythoncore.vcxproj, so it's always in pythoncore's IntDir -->
14-
<GeneratedPyConfigDir>$(Py_IntDir)\$(MajorVersionNumber)$(MinorVersionNumber)$(ArchName)_$(Configuration)\pythoncore\</GeneratedPyConfigDir>
1513
<GeneratedFrozenModulesDir>$(Py_IntDir)\$(MajorVersionNumber)$(MinorVersionNumber)_frozen\</GeneratedFrozenModulesDir>
1614
<GeneratedZlibNgDir>$(Py_IntDir)\$(MajorVersionNumber)$(MinorVersionNumber)$(ArchName)_$(Configuration)\zlib-ng\</GeneratedZlibNgDir>
15+
<GeneratedJitStencilsDir>$(Py_IntDir)\$(MajorVersionNumber)$(MinorVersionNumber)_$(Configuration)</GeneratedJitStencilsDir>
1716
<TargetName Condition="'$(TargetName)' == ''">$(ProjectName)</TargetName>
1817
<TargetName>$(TargetName)$(PyDebugExt)</TargetName>
1918
<GenerateManifest>false</GenerateManifest>
@@ -49,11 +48,12 @@
4948
<_PlatformPreprocessorDefinition Condition="$(Platform) == 'x64'">_WIN64;</_PlatformPreprocessorDefinition>
5049
<_PlatformPreprocessorDefinition Condition="$(Platform) == 'x64' and $(PlatformToolset) != 'ClangCL'">_M_X64;$(_PlatformPreprocessorDefinition)</_PlatformPreprocessorDefinition>
5150
<_Py3NamePreprocessorDefinition>PY3_DLLNAME=L"$(Py3DllName)$(PyDebugExt)";</_Py3NamePreprocessorDefinition>
51+
<_FreeThreadedPreprocessorDefinition Condition="$(DisableGil) == 'true'">Py_GIL_DISABLED=1;</_FreeThreadedPreprocessorDefinition>
5252
</PropertyGroup>
5353
<ItemDefinitionGroup>
5454
<ClCompile>
55-
<AdditionalIncludeDirectories>$(PySourcePath)Include;$(PySourcePath)Include\internal;$(PySourcePath)Include\internal\mimalloc;$(GeneratedPyConfigDir);$(PySourcePath)PC;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
56-
<PreprocessorDefinitions>WIN32;$(_Py3NamePreprocessorDefinition);$(_PlatformPreprocessorDefinition)$(_DebugPreprocessorDefinition)$(_PyStatsPreprocessorDefinition)$(_PydPreprocessorDefinition)%(PreprocessorDefinitions)</PreprocessorDefinitions>
55+
<AdditionalIncludeDirectories>$(PySourcePath)Include;$(PySourcePath)Include\internal;$(PySourcePath)Include\internal\mimalloc;$(PySourcePath)PC;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
56+
<PreprocessorDefinitions>WIN32;$(_Py3NamePreprocessorDefinition)$(_PlatformPreprocessorDefinition)$(_DebugPreprocessorDefinition)$(_PyStatsPreprocessorDefinition)$(_PydPreprocessorDefinition)$(_FreeThreadedPreprocessorDefinition)%(PreprocessorDefinitions)</PreprocessorDefinitions>
5757
<PreprocessorDefinitions Condition="'$(SupportPGO)' and ($(Configuration) == 'PGInstrument' or $(Configuration) == 'PGUpdate')">_Py_USING_PGO=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
5858

5959
<Optimization>MaxSpeed</Optimization>

PCbuild/pythoncore.vcxproj

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
<AdditionalOptions>/Zm200 %(AdditionalOptions)</AdditionalOptions>
103103
<AdditionalIncludeDirectories>$(PySourcePath)Modules\_hacl;$(PySourcePath)Modules\_hacl\include;$(PySourcePath)Python;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
104104
<AdditionalIncludeDirectories Condition="$(IncludeExternals)">$(zlibNgDir);$(GeneratedZlibNgDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
105+
<AdditionalIncludeDirectories Condition="'$(UseJIT)' == 'true'">$(GeneratedJitStencilsDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
105106
<PreprocessorDefinitions>_USRDLL;Py_BUILD_CORE;Py_BUILD_CORE_BUILTIN;Py_ENABLE_SHARED;MS_DLL_ID="$(SysWinVer)";ZLIB_COMPAT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
106107
<PreprocessorDefinitions Condition="$(IncludeExternals)">_Py_HAVE_ZLIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
107108
<PreprocessorDefinitions Condition="'$(UseJIT)' == 'true'">_Py_JIT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
@@ -409,7 +410,7 @@
409410
<ClInclude Include="..\Parser\string_parser.h" />
410411
<ClInclude Include="..\Parser\pegen.h" />
411412
<ClInclude Include="..\PC\errmap.h" />
412-
<ClInclude Include="..\PC\pyconfig.h.in" />
413+
<ClInclude Include="..\PC\pyconfig.h" />
413414
<ClInclude Include="..\Python\condvar.h" />
414415
<ClInclude Include="..\Python\stdlib_module_names.h" />
415416
<ClInclude Include="..\Python\thread_nt.h" />
@@ -688,34 +689,6 @@
688689
</ImportGroup>
689690
<Target Name="_TriggerRegen" BeforeTargets="PrepareForBuild" DependsOnTargets="Regen" />
690691

691-
<Target Name="_UpdatePyconfig" BeforeTargets="PrepareForBuild">
692-
<MakeDir Directories="$(IntDir)" Condition="!Exists($(IntDir))" />
693-
<ItemGroup>
694-
<PyConfigH Remove="@(PyConfigH)" />
695-
<PyConfigH Include="@(ClInclude)" Condition="'%(Filename)%(Extension)' == 'pyconfig.h.in'" />
696-
</ItemGroup>
697-
<Error Text="Did not find pyconfig.h" Condition="@(ClInclude) == ''" />
698-
<PropertyGroup>
699-
<PyConfigH>@(PyConfigH->'%(FullPath)', ';')</PyConfigH>
700-
<PyConfigHText>$([System.IO.File]::ReadAllText($(PyConfigH)))</PyConfigHText>
701-
<OldPyConfigH Condition="Exists('$(IntDir)pyconfig.h')">$([System.IO.File]::ReadAllText('$(IntDir)pyconfig.h'))</OldPyConfigH>
702-
</PropertyGroup>
703-
<PropertyGroup Condition="$(DisableGil) == 'true'">
704-
<PyConfigHText>$(PyConfigHText.Replace('/* #define Py_GIL_DISABLED 1 */', '#define Py_GIL_DISABLED 1'))</PyConfigHText>
705-
</PropertyGroup>
706-
<Message Text="Updating pyconfig.h" Condition="$(PyConfigHText.TrimEnd()) != $(OldPyConfigH.TrimEnd())" />
707-
<WriteLinesToFile File="$(IntDir)pyconfig.h"
708-
Lines="$(PyConfigHText)"
709-
Overwrite="true"
710-
Condition="$(PyConfigHText.TrimEnd()) != $(OldPyConfigH.TrimEnd())" />
711-
</Target>
712-
<Target Name="_CopyPyconfig" Inputs="$(IntDir)pyconfig.h" Outputs="$(OutDir)pyconfig.h" AfterTargets="Build" DependsOnTargets="_UpdatePyconfig">
713-
<Copy SourceFiles="$(IntDir)pyconfig.h" DestinationFolder="$(OutDir)" />
714-
</Target>
715-
<Target Name="_CleanPyconfig" AfterTargets="Clean">
716-
<Delete Files="$(IntDir)pyconfig.h;$(OutDir)pyconfig.h" />
717-
</Target>
718-
719692
<Target Name="_GetBuildInfo" BeforeTargets="PrepareForBuild">
720693
<PropertyGroup>
721694
<GIT Condition="$(GIT) == ''">git</GIT>

PCbuild/regen.targets

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
<_KeywordSources Include="$(PySourcePath)Grammar\python.gram;$(PySourcePath)Grammar\Tokens" />
3030
<_KeywordOutputs Include="$(PySourcePath)Lib\keyword.py" />
3131
<!-- Taken from _Target._compute_digest in Tools\jit\_targets.py: -->
32-
<_JITSources Include="$(PySourcePath)Python\executor_cases.c.h;$(GeneratedPyConfigDir)pyconfig.h;$(PySourcePath)Tools\jit\**"/>
32+
<_JITSources Include="$(PySourcePath)Python\executor_cases.c.h;$(PySourcePath)PC\pyconfig.h;$(PySourcePath)Tools\jit\**"/>
3333
<!-- Need to explicitly enumerate these, since globbing doesn't work for missing outputs: -->
34-
<_JITOutputs Include="$(GeneratedPyConfigDir)jit_stencils.h"/>
35-
<_JITOutputs Include="$(GeneratedPyConfigDir)jit_stencils-aarch64-pc-windows-msvc.h" Condition="$(Platform) == 'ARM64'"/>
36-
<_JITOutputs Include="$(GeneratedPyConfigDir)jit_stencils-i686-pc-windows-msvc.h" Condition="$(Platform) == 'Win32'"/>
37-
<_JITOutputs Include="$(GeneratedPyConfigDir)jit_stencils-x86_64-pc-windows-msvc.h" Condition="$(Platform) == 'x64'"/>
34+
<_JITOutputs Include="$(GeneratedJitStencilsDir)jit_stencils.h"/>
35+
<_JITOutputs Include="$(GeneratedJitStencilsDir)jit_stencils-aarch64-pc-windows-msvc.h" Condition="$(Platform) == 'ARM64'"/>
36+
<_JITOutputs Include="$(GeneratedJitStencilsDir)jit_stencils-i686-pc-windows-msvc.h" Condition="$(Platform) == 'Win32'"/>
37+
<_JITOutputs Include="$(GeneratedJitStencilsDir)jit_stencils-x86_64-pc-windows-msvc.h" Condition="$(Platform) == 'x64'"/>
3838
<_CasesSources Include="$(PySourcePath)Python\bytecodes.c;$(PySourcePath)Python\optimizer_bytecodes.c;"/>
3939
<_CasesOutputs Include="$(PySourcePath)Python\generated_cases.c.h;$(PySourcePath)Include\opcode_ids.h;$(PySourcePath)Include\internal\pycore_uop_ids.h;$(PySourcePath)Python\opcode_targets.h;$(PySourcePath)Include\internal\pycore_opcode_metadata.h;$(PySourcePath)Include\internal\pycore_uop_metadata.h;$(PySourcePath)Python\optimizer_cases.c.h;$(PySourcePath)Lib\_opcode_metadata.py"/>
4040
<_SbomSources Include="$(PySourcePath)PCbuild\get_externals.bat" />
@@ -116,7 +116,7 @@
116116

117117
<Target Name="_RegenJIT"
118118
Condition="'$(UseJIT)' == 'true'"
119-
DependsOnTargets="_UpdatePyconfig;FindPythonForBuild"
119+
DependsOnTargets="FindPythonForBuild"
120120
Inputs="@(_JITSources)"
121121
Outputs="@(_JITOutputs)">
122122
<PropertyGroup>
@@ -126,7 +126,7 @@
126126
<JITArgs Condition="$(Configuration) == 'Debug'">$(JITArgs) --debug</JITArgs>
127127
</PropertyGroup>
128128
<Exec Command='$(PythonForBuild) "$(PySourcePath)Tools\jit\build.py" $(JITArgs)'
129-
WorkingDirectory="$(GeneratedPyConfigDir)"/>
129+
WorkingDirectory="$(GeneratedJitStencilsDir)"/>
130130
</Target>
131131
<Target Name="_CleanJIT" AfterTargets="Clean">
132132
<Delete Files="@(_JITOutputs)"/>

Tools/msi/dev/dev_files.wxs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<Fragment>
44
<ComponentGroup Id="dev_pyconfig">
55
<Component Id="include_pyconfig.h" Directory="include" Guid="*">
6-
<File Id="include_pyconfig.h" Name="pyconfig.h" Source="pyconfig.h" KeyPath="yes" />
6+
<File Id="include_pyconfig.h" Name="pyconfig.h" Source="!(bindpath.src)PC\pyconfig.h" KeyPath="yes" />
77
</Component>
88
</ComponentGroup>
99
</Fragment>

Tools/peg_generator/pegen/build.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ def compile_c_extension(
108108
extra_compile_args.append("-DPy_BUILD_CORE_MODULE")
109109
# Define _Py_TEST_PEGEN to not call PyAST_Validate() in Parser/pegen.c
110110
extra_compile_args.append("-D_Py_TEST_PEGEN")
111+
if sys.platform == "win32" and sysconfig.get_config_var("Py_GIL_DISABLED"):
112+
extra_compile_args.append("-DPy_GIL_DISABLED")
111113
extra_link_args = get_extra_flags("LDFLAGS", "PY_LDFLAGS_NODIST")
112114
if keep_asserts:
113115
extra_compile_args.append("-UNDEBUG")

0 commit comments

Comments
 (0)