From b6d1531c182f0c8b5d1fda2319cee71779ba1326 Mon Sep 17 00:00:00 2001 From: Lysandros Nikolaou Date: Fri, 29 Nov 2024 16:19:06 +0100 Subject: [PATCH] Merge distutils fix for setting Py_GIL_DISABLED on Windows --- newsfragments/4662.bugfix.rst | 2 ++ setuptools/_distutils/command/build_ext.py | 9 ++++++++- setuptools/_distutils/util.py | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 newsfragments/4662.bugfix.rst diff --git a/newsfragments/4662.bugfix.rst b/newsfragments/4662.bugfix.rst new file mode 100644 index 0000000000..9284ba6363 --- /dev/null +++ b/newsfragments/4662.bugfix.rst @@ -0,0 +1,2 @@ +Merged with pypa/distutils@49e362c, including fix for setting Py_GIL_DISABLED +on Windows in pypa/distutils#310. diff --git a/setuptools/_distutils/command/build_ext.py b/setuptools/_distutils/command/build_ext.py index a7e3038be6..271378e580 100644 --- a/setuptools/_distutils/command/build_ext.py +++ b/setuptools/_distutils/command/build_ext.py @@ -23,7 +23,7 @@ ) from ..extension import Extension from ..sysconfig import customize_compiler, get_config_h_filename, get_python_version -from ..util import get_platform, is_mingw +from ..util import get_platform, is_mingw, is_freethreaded # An extension name is just a dot-separated list of Python NAMEs (ie. # the same as a fully-qualified module name). @@ -333,6 +333,13 @@ 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 + # (see pypa/setuptools#4662). + if os.name == 'nt' and 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 diff --git a/setuptools/_distutils/util.py b/setuptools/_distutils/util.py index 609c1a50cd..6ef2c9854a 100644 --- a/setuptools/_distutils/util.py +++ b/setuptools/_distutils/util.py @@ -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'))