Skip to content

Commit 85c03d4

Browse files
authored
gh-158810: Use _Py_TYPEOF in Py_ARRAY_LENGTH() (#158838)
Fix the compiler warning "extension used" when building test_cext with clang. * test_cext no longer overrides the CC environment variable if it's already defined. * test_cext now also logs __STRICT_ANSI__ macro.
1 parent 047157b commit 85c03d4

4 files changed

Lines changed: 15 additions & 5 deletions

File tree

‎Include/pymacro.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,12 @@
214214
//
215215
// gh-158810: Do not use __builtin_types_compatible_p() in strict C ANSI mode
216216
// and on C++.
217-
#if (defined(__GNUC__) && __GNUC__ >= 4 \
217+
#if (defined(__GNUC__) && __GNUC__ >= 4 && defined(_Py_TYPEOF) \
218218
&& !defined(__STRICT_ANSI__) && !defined(__cplusplus))
219219
#define Py_ARRAY_LENGTH(array) \
220220
(sizeof(array) / sizeof((array)[0]) \
221-
+ Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \
222-
typeof(&(array)[0]))))
221+
+ Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(_Py_TYPEOF(array), \
222+
_Py_TYPEOF(&(array)[0]))))
223223
#else
224224
#define Py_ARRAY_LENGTH(array) \
225225
(sizeof(array) / sizeof((array)[0]))

‎Lib/test/test_cext/extension.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ module_exec(PyObject *module)
388388
return -1;
389389
}
390390
#endif
391+
#ifdef __STRICT_ANSI__
392+
if (PyModule_AddIntConstant(module, "__STRICT_ANSI__", 1) < 0) {
393+
return -1;
394+
}
395+
#endif
391396
#ifdef __cplusplus
392397
if (PyModule_AddIntMacro(module, __cplusplus) < 0) {
393398
return -1;

‎Lib/test/test_cext/runtests.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ def main():
2828
testmod = importlib.import_module(module_name)
2929

3030
newline = False
31-
for name in ('__STDC_VERSION__', '__cplusplus', '_MSVC_LANG'):
31+
for name in (
32+
'__STDC_VERSION__',
33+
'__STRICT_ANSI__',
34+
'__cplusplus',
35+
'_MSVC_LANG',
36+
):
3237
try:
3338
value = getattr(testmod, name)
3439
except AttributeError:

‎Lib/test/test_cext/setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def main():
135135
# option emits a C++ compiler warning. Remove "-std11" option from the
136136
# CC command.
137137
cmd = (sysconfig.get_config_var('CC') or '')
138-
if cmd is not None:
138+
if cmd is not None and 'CC' not in os.environ:
139139
if support.MS_WINDOWS:
140140
std_prefix = '/std'
141141
else:

0 commit comments

Comments
 (0)