Skip to content

Commit

Permalink
Set Py_GIL_DISABLED=1 for free threading on Windows
Browse files Browse the repository at this point in the history
When free threaded CPython is installed from the official Windows
installer it doesn't have the macro `Py_GIL_DISABLED` properly set
becuase its `pyconfig.h` file is shared across the co-installed default
build.

Define the macro when building free threaded Python extensions on
Windows so that each individual C API extension doesn't have to work
around this limitation.

See pypa/setuptools#4662
  • Loading branch information
colesbury committed Nov 1, 2024
1 parent 5cc8304 commit 533a649
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions distutils/command/build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ def run(self): # noqa: C901
if os.name == 'nt' and self.plat_name != get_platform():
self.compiler.initialize(self.plat_name)

# The official Windows free threaded Python installer doesn't set
# Py_GIL_DISABLED because its pyconfig.h is shared with the
# default build, so we need to define it here.
if os.name == 'nt' and util.is_freethreaded():
self.compiler.define_macro('Py_GIL_DISABLED', '1')

# And make sure that any compile/link-related options (which might
# come from the command-line or from the setup script) are set in
# that CCompiler object -- that way, they automatically apply to
Expand Down
4 changes: 4 additions & 0 deletions distutils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,3 +503,7 @@ def is_mingw():
get_platform() starts with 'mingw'.
"""
return sys.platform == 'win32' and get_platform().startswith('mingw')

def is_freethreaded():
"""Return True if the Python interpreter is built with free threading support."""
return bool(sysconfig.get_config_var('Py_GIL_DISABLED'))

0 comments on commit 533a649

Please sign in to comment.