Skip to content

Commit d91cc9d

Browse files
authored
gh-129666: Add C11/C++11 to docs and -pedantic-errors to GCC/clang test_c[pp]ext tests (GH-130692)
Disable pedantic check for c++03 (unlimited API) Also add a check for c++03 *limited* API, which passes in pedantic mode after removing a comma in the `PySendResult` declaration, and allowing `long long`.
1 parent 6c48ed7 commit d91cc9d

File tree

8 files changed

+75
-1
lines changed

8 files changed

+75
-1
lines changed

Doc/c-api/intro.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ familiar with writing an extension before attempting to embed Python in a real
3030
application.
3131

3232

33+
Language version compatibility
34+
==============================
35+
36+
Python's C API is compatible with C11 and C++11 versions of C and C++.
37+
38+
This is a lower limit: the C API does not require features from later
39+
C/C++ versions.
40+
You do *not* need to enable your compiler's "c11 mode".
41+
42+
3343
Coding standards
3444
================
3545

Include/object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
683683
typedef enum {
684684
PYGEN_RETURN = 0,
685685
PYGEN_ERROR = -1,
686-
PYGEN_NEXT = 1,
686+
PYGEN_NEXT = 1
687687
} PySendResult;
688688
#endif
689689

Lib/test/test_cext/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ def test_build_c11(self):
3838

3939
@unittest.skipIf(support.MS_WINDOWS, "MSVC doesn't support /std:c99")
4040
def test_build_c99(self):
41+
# In public docs, we say C API is compatible with C11. However,
42+
# in practice we do maintain C99 compatibility in public headers.
43+
# Please ask the C API WG before adding a new C11-only feature.
4144
self.check_build('_test_c99_cext', std='c99')
4245

4346
@support.requires_gil_enabled('incompatible with Free Threading')

Lib/test/test_cext/extension.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,24 @@ _testcext_exec(
5858
return 0;
5959
}
6060

61+
// Converting from function pointer to void* has undefined behavior, but
62+
// works on all known platforms, and CPython's module and type slots currently
63+
// need it.
64+
// (GCC doesn't have a narrower category for this than -Wpedantic.)
65+
_Py_COMP_DIAG_PUSH
66+
#if defined(__GNUC__)
67+
#pragma GCC diagnostic ignored "-Wpedantic"
68+
#elif defined(__clang__)
69+
#pragma clang diagnostic ignored "-Wpedantic"
70+
#endif
71+
6172
static PyModuleDef_Slot _testcext_slots[] = {
6273
{Py_mod_exec, (void*)_testcext_exec},
6374
{0, NULL}
6475
};
6576

77+
_Py_COMP_DIAG_POP
78+
6679

6780
PyDoc_STRVAR(_testcext_doc, "C test extension.");
6881

Lib/test/test_cext/setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
# gh-120593: Check the 'const' qualifier
2323
'-Wcast-qual',
24+
25+
# Ask for strict(er) compliance with the standard
26+
'-pedantic-errors',
2427
]
2528
if not support.Py_GIL_DISABLED:
2629
CFLAGS.append(

Lib/test/test_cppext/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,15 @@ def test_build(self):
2929
self.check_build('_testcppext')
3030

3131
def test_build_cpp03(self):
32+
# In public docs, we say C API is compatible with C++11. However,
33+
# in practice we do maintain C++03 compatibility in public headers.
34+
# Please ask the C API WG before adding a new C++11-only feature.
3235
self.check_build('_testcpp03ext', std='c++03')
3336

37+
@support.requires_gil_enabled('incompatible with Free Threading')
38+
def test_build_limited_cpp03(self):
39+
self.check_build('_test_limited_cpp03ext', std='c++03', limited=True)
40+
3441
@unittest.skipIf(support.MS_WINDOWS, "MSVC doesn't support /std:c++11")
3542
def test_build_cpp11(self):
3643
self.check_build('_testcpp11ext', std='c++11')

Lib/test/test_cppext/extension.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,24 @@ class VirtualPyObject : public PyObject {
161161

162162
int VirtualPyObject::instance_count = 0;
163163

164+
// Converting from function pointer to void* has undefined behavior, but
165+
// works on all known platforms, and CPython's module and type slots currently
166+
// need it.
167+
// (GCC doesn't have a narrower category for this than -Wpedantic.)
168+
_Py_COMP_DIAG_PUSH
169+
#if defined(__GNUC__)
170+
#pragma GCC diagnostic ignored "-Wpedantic"
171+
#elif defined(__clang__)
172+
#pragma clang diagnostic ignored "-Wpedantic"
173+
#endif
174+
164175
PyType_Slot VirtualPyObject_Slots[] = {
165176
{Py_tp_free, (void*)VirtualPyObject::dealloc},
166177
{0, _Py_NULL},
167178
};
168179

180+
_Py_COMP_DIAG_POP
181+
169182
PyType_Spec VirtualPyObject_Spec = {
170183
/* .name */ STR(MODULE_NAME) ".VirtualPyObject",
171184
/* .basicsize */ sizeof(VirtualPyObject),
@@ -241,11 +254,20 @@ _testcppext_exec(PyObject *module)
241254
return 0;
242255
}
243256

257+
// Need to ignore "-Wpedantic" warnings; see VirtualPyObject_Slots above
258+
_Py_COMP_DIAG_PUSH
259+
#if defined(__GNUC__)
260+
#pragma GCC diagnostic ignored "-Wpedantic"
261+
#elif defined(__clang__)
262+
#pragma clang diagnostic ignored "-Wpedantic"
263+
#endif
264+
244265
static PyModuleDef_Slot _testcppext_slots[] = {
245266
{Py_mod_exec, reinterpret_cast<void*>(_testcppext_exec)},
246267
{0, _Py_NULL}
247268
};
248269

270+
_Py_COMP_DIAG_POP
249271

250272
PyDoc_STRVAR(_testcppext_doc, "C++ test extension.");
251273

Lib/test/test_cppext/setup.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@
1919
# warnings
2020
'-Werror',
2121
]
22+
23+
CPPFLAGS_PEDANTIC = [
24+
# Ask for strict(er) compliance with the standard.
25+
# We cannot do this for c++03 unlimited API, since several headers in
26+
# Include/cpython/ use commas at end of `enum` declarations, a C++11
27+
# feature for which GCC has no narrower option than -Wpedantic itself.
28+
'-pedantic-errors',
29+
30+
# We also use `long long`, a C++11 feature we can enable individually.
31+
'-Wno-long-long',
32+
]
2233
else:
2334
# MSVC compiler flags
2435
CPPFLAGS = [
@@ -27,6 +38,7 @@
2738
# Treat all compiler warnings as compiler errors
2839
'/WX',
2940
]
41+
CPPFLAGS_PEDANTIC = []
3042

3143

3244
def main():
@@ -45,6 +57,10 @@ def main():
4557
else:
4658
cppflags.append(f'-std={std}')
4759

60+
if limited or (std != 'c++03'):
61+
# See CPPFLAGS_PEDANTIC docstring
62+
cppflags.extend(CPPFLAGS_PEDANTIC)
63+
4864
# gh-105776: When "gcc -std=11" is used as the C++ compiler, -std=c11
4965
# option emits a C++ compiler warning. Remove "-std11" option from the
5066
# CC command.

0 commit comments

Comments
 (0)