Skip to content

Commit 003e6d2

Browse files
authored
gh-129666: Add C11/C++11 to docs and -pedantic-errors to GCC/clang test_c[pp]ext tests (GH-130686)
1 parent cc17307 commit 003e6d2

File tree

7 files changed

+62
-0
lines changed

7 files changed

+62
-0
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

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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ 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

3437
@unittest.skipIf(support.MS_WINDOWS, "MSVC doesn't support /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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
# a C++ extension using the Python C API does not emit C++ compiler
1919
# warnings
2020
'-Werror',
21+
22+
# Ask for strict(er) compliance with the standard.
23+
'-pedantic-errors',
24+
25+
# But allow C++11 features for -std=C++03. We use:
26+
# - `long long` (-Wno-c++11-long-long)
27+
# - comma at end of `enum` lists (no narrower GCC option exists)
28+
'-Wno-c++11-extensions',
2129
]
2230
else:
2331
# MSVC compiler flags

0 commit comments

Comments
 (0)