Skip to content

Commit b7d7b91

Browse files
committed
gh-145010: Fix Python.h compilation with -masm=intel
1 parent 0bbdb4e commit b7d7b91

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

Include/object.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@ _Py_ThreadId(void)
206206
#elif defined(__MINGW32__) && defined(_M_ARM64)
207207
tid = __getReg(18);
208208
#elif defined(__i386__)
209-
__asm__("movl %%gs:0, %0" : "=r" (tid)); // 32-bit always uses GS
209+
__asm__("{movl %%gs:0, %0|mov %0, dword ptr gs:[0]}" : "=r" (tid)); // 32-bit always uses GS
210210
#elif defined(__MACH__) && defined(__x86_64__)
211-
__asm__("movq %%gs:0, %0" : "=r" (tid)); // x86_64 macOSX uses GS
211+
__asm__("{movq %%gs:0, %0|mov %0, qword ptr gs:[0]}" : "=r" (tid)); // x86_64 macOSX uses GS
212212
#elif defined(__x86_64__)
213-
__asm__("movq %%fs:0, %0" : "=r" (tid)); // x86_64 Linux, BSD uses FS
213+
__asm__("{movq %%fs:0, %0|mov %0, qword ptr fs:[0]}" : "=r" (tid)); // x86_64 Linux, BSD uses FS
214214
#elif defined(__arm__) && __ARM_ARCH >= 7
215215
__asm__ ("mrc p15, 0, %0, c13, c0, 3\nbic %0, %0, #3" : "=r" (tid));
216216
#elif defined(__aarch64__) && defined(__APPLE__)

Lib/test/test_cppext/__init__.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# gh-91321: Build a basic C++ test extension to check that the Python C API is
22
# compatible with C++ and does not emit C++ compiler warnings.
33
import os.path
4+
import platform
45
import shlex
56
import shutil
67
import subprocess
@@ -28,13 +29,16 @@
2829
class BaseTests:
2930
TEST_INTERNAL_C_API = False
3031

31-
def check_build(self, extension_name, std=None, limited=False):
32+
def check_build(self, extension_name, std=None, limited=False,
33+
extra_cflags=None):
3234
venv_dir = 'env'
3335
with support.setup_venv_with_pip_setuptools(venv_dir) as python_exe:
3436
self._check_build(extension_name, python_exe,
35-
std=std, limited=limited)
37+
std=std, limited=limited,
38+
extra_cflags=extra_cflags)
3639

37-
def _check_build(self, extension_name, python_exe, std, limited):
40+
def _check_build(self, extension_name, python_exe, std, limited,
41+
extra_cflags=None):
3842
pkg_dir = 'pkg'
3943
os.mkdir(pkg_dir)
4044
shutil.copy(SETUP, os.path.join(pkg_dir, os.path.basename(SETUP)))
@@ -48,6 +52,8 @@ def run_cmd(operation, cmd):
4852
env['CPYTHON_TEST_LIMITED'] = '1'
4953
env['CPYTHON_TEST_EXT_NAME'] = extension_name
5054
env['TEST_INTERNAL_C_API'] = str(int(self.TEST_INTERNAL_C_API))
55+
if extra_cflags:
56+
env['CPYTHON_TEST_EXTRA_CFLAGS'] = extra_cflags
5157
if support.verbose:
5258
print('Run:', ' '.join(map(shlex.quote, cmd)))
5359
subprocess.run(cmd, check=True, env=env)
@@ -116,6 +122,14 @@ def test_build_cpp11(self):
116122
def test_build_cpp14(self):
117123
self.check_build('_testcpp14ext', std='c++14')
118124

125+
# Test that headers compile with Intel asm syntax, which may conflict
126+
# with inline assembly in free-threading headers that use AT&T syntax.
127+
@unittest.skipIf(support.MS_WINDOWS, "MSVC doesn't support -masm=intel")
128+
@unittest.skipUnless(platform.machine() in ('x86_64', 'i686', 'AMD64'),
129+
"x86-specific flag")
130+
def test_build_intel_asm(self):
131+
self.check_build('_testcppext_asm', extra_cflags='-masm=intel')
132+
119133

120134
class TestInteralCAPI(BaseTests, unittest.TestCase):
121135
TEST_INTERNAL_C_API = True

Lib/test/test_cppext/setup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ def main():
8686
if internal:
8787
cppflags.append('-DTEST_INTERNAL_C_API=1')
8888

89+
extra_cflags = os.environ.get("CPYTHON_TEST_EXTRA_CFLAGS", "")
90+
if extra_cflags:
91+
cppflags.extend(shlex.split(extra_cflags))
92+
8993
# On Windows, add PCbuild\amd64\ to include and library directories
9094
include_dirs = []
9195
library_dirs = []
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Use GCC dialect alternatives for inline assembly in ``object.h`` so that the
2+
Python headers compile correctly with ``-masm=intel``.

0 commit comments

Comments
 (0)