Skip to content

Commit a831abb

Browse files
authored
gh-158445: Add C API tests on build information functions (#158603)
Add tests on functions: * Py_GetBuildInfo() * Py_GetCompiler() * Py_GetCopyright() * Py_GetPlatform() * Py_GetVersion() Changes: * Add Modules/_testlimitedcapi.c and Lib/test/test_capi/test_build.py files. * Add internal _Py_GetBuiltWithAssert() function. * Reimplemenet support.built_with_c_assertions() using _Py_GetBuiltWithAssert() to cover more cases like buildbots using CFLAGS=-UNDEBUG in release mode.
1 parent f610e8c commit a831abb

10 files changed

Lines changed: 149 additions & 11 deletions

File tree

‎Lib/test/support/__init__.py‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3525,17 +3525,12 @@ def check_immutable_type(testcase, type):
35253525

35263526
def built_with_c_assertions():
35273527
"""Check if Python was built with C assertions (assert())."""
3528-
3529-
if MS_WINDOWS:
3530-
# On Windows, rely on the Py_DEBUG macro to check for assertions
3528+
try:
3529+
import _testlimitedcapi
3530+
except ImportError:
35313531
return Py_DEBUG
3532-
3533-
# Check if the NDEBUG macro is defined in C compiler flags
3534-
PY_CFLAGS = (sysconfig.get_config_var('PY_CFLAGS') or '')
3535-
if '-DNDEBUG' in PY_CFLAGS:
3536-
return False
3537-
3538-
return True
3532+
else:
3533+
return bool(_testlimitedcapi._py_getbuiltwithassert())
35393534

35403535

35413536
def inject_memory_error(start=0, stop=0):

‎Lib/test/test_capi/test_build.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import sys
2+
import unittest
3+
from test.support import import_helper
4+
5+
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
6+
7+
8+
class CAPITest(unittest.TestCase):
9+
# Test Py_GetVersion()
10+
def test_getversion(self):
11+
self.assertEqual(_testlimitedcapi.py_getversion(), sys.version)
12+
13+
# Test Py_GetPlatform()
14+
def test_getplatform(self):
15+
self.assertEqual(_testlimitedcapi.py_getplatform(), sys.platform)
16+
17+
# Test Py_GetCopyright()
18+
def test_getcopyright(self):
19+
self.assertEqual(_testlimitedcapi.py_getcopyright(), sys.copyright)
20+
21+
# Test Py_GetCompiler()
22+
def test_getcompiler(self):
23+
compiler = _testlimitedcapi.py_getcompiler()
24+
self.assertIsInstance(compiler, str)
25+
self.assertNotEqual(compiler, '')
26+
27+
# Test Py_GetBuildInfo()
28+
def test_getbuildinfo(self):
29+
buildinfo = _testlimitedcapi.py_getbuildinfo()
30+
self.assertIsInstance(buildinfo, str)
31+
self.assertNotEqual(buildinfo, '')
32+
33+
# Test internal _Py_GetBuiltWithAssert()
34+
def test__getbuiltwithassert(self):
35+
self.assertIn(_testlimitedcapi._py_getbuiltwithassert(), (0, 1))
36+
37+
38+
if __name__ == "__main__":
39+
unittest.main()

‎Lib/test/test_support.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,10 @@ def test_linked_to_musl(self):
838838
for v in linked:
839839
self.assertIsInstance(v, int)
840840

841+
def test_built_with_c_assertions(self):
842+
# Check that calling the function doesn't raise an exception
843+
result = support.built_with_c_assertions()
844+
self.assertIsInstance(result, bool)
841845

842846
# XXX -follows a list of untested API
843847
# make_legacy_pyc

‎Modules/Setup.stdlib.in‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
175175
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c _testinternalcapi/complex.c _testinternalcapi/interpreter.c _testinternalcapi/tokenizer.c _testinternalcapi/tuple.c _testinternalcapi/typecache.c
176176
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/run.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/hash.c _testcapi/time.c _testcapi/bytes.c _testcapi/object.c _testcapi/modsupport.c _testcapi/monitoring.c _testcapi/config.c _testcapi/import.c _testcapi/frame.c _testcapi/type.c _testcapi/function.c _testcapi/module.c _testcapi/weakref.c _testcapi/marshal.c
177-
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/abstract.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/capsule.c _testlimitedcapi/codec.c _testlimitedcapi/complex.c _testlimitedcapi/dict.c _testlimitedcapi/eval.c _testlimitedcapi/float.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/import.c _testlimitedcapi/list.c _testlimitedcapi/long.c _testlimitedcapi/object.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/slice.c _testlimitedcapi/slots.c _testlimitedcapi/sys.c _testlimitedcapi/threadstate.c _testlimitedcapi/tuple.c _testlimitedcapi/unicode.c _testlimitedcapi/vectorcall_limited.c _testlimitedcapi/version.c _testlimitedcapi/file.c _testlimitedcapi/weakref.c _testlimitedcapi/run.c _testlimitedcapi/type.c _testlimitedcapi/hash.c
177+
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/abstract.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/capsule.c _testlimitedcapi/codec.c _testlimitedcapi/complex.c _testlimitedcapi/dict.c _testlimitedcapi/eval.c _testlimitedcapi/float.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/import.c _testlimitedcapi/list.c _testlimitedcapi/long.c _testlimitedcapi/object.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/slice.c _testlimitedcapi/slots.c _testlimitedcapi/sys.c _testlimitedcapi/threadstate.c _testlimitedcapi/tuple.c _testlimitedcapi/unicode.c _testlimitedcapi/vectorcall_limited.c _testlimitedcapi/version.c _testlimitedcapi/file.c _testlimitedcapi/weakref.c _testlimitedcapi/run.c _testlimitedcapi/type.c _testlimitedcapi/hash.c _testlimitedcapi/build.c
178178
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
179179
@MODULE__TESTCLINIC_LIMITED_TRUE@_testclinic_limited _testclinic_limited.c
180180

‎Modules/_testlimitedcapi.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ module_exec(PyObject *mod)
9797
if (_PyTestLimitedCAPI_Init_Hash(mod) < 0) {
9898
return -1;
9999
}
100+
if (_PyTestLimitedCAPI_Init_Build(mod) < 0) {
101+
return -1;
102+
}
100103
return 0;
101104
}
102105

‎Modules/_testlimitedcapi/build.c‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include "parts.h"
2+
#include "util.h"
3+
4+
5+
static PyObject*
6+
return_string(const char *str)
7+
{
8+
assert(str != NULL);
9+
return PyUnicode_FromString(str);
10+
}
11+
12+
13+
/* Test Py_GetVersion() */
14+
static PyObject*
15+
py_getversion(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
16+
{
17+
return return_string(Py_GetVersion());
18+
}
19+
20+
21+
/* Test Py_GetPlatform() */
22+
static PyObject*
23+
py_getplatform(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
24+
{
25+
return return_string(Py_GetPlatform());
26+
}
27+
28+
29+
/* Test Py_GetCopyright() */
30+
static PyObject*
31+
py_getcopyright(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
32+
{
33+
return return_string(Py_GetCopyright());
34+
}
35+
36+
37+
/* Test Py_GetCompiler() */
38+
static PyObject*
39+
py_getcompiler(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
40+
{
41+
return return_string(Py_GetCompiler());
42+
}
43+
44+
45+
/* Test Py_GetBuildInfo() */
46+
static PyObject*
47+
py_getbuildinfo(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
48+
{
49+
return return_string(Py_GetBuildInfo());
50+
}
51+
52+
53+
/* Test _Py_GetBuiltWithAssert() */
54+
static PyObject*
55+
_py_getbuiltwithassert(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
56+
{
57+
// Function only exported for _testlimitedcapi
58+
PyAPI_FUNC(int) _Py_GetBuiltWithAssert(void);
59+
60+
return PyLong_FromLong(_Py_GetBuiltWithAssert());
61+
}
62+
63+
64+
static PyMethodDef test_methods[] = {
65+
{"py_getversion", py_getversion, METH_NOARGS},
66+
{"py_getplatform", py_getplatform, METH_NOARGS},
67+
{"py_getcopyright", py_getcopyright, METH_NOARGS},
68+
{"py_getcompiler", py_getcompiler, METH_NOARGS},
69+
{"py_getbuildinfo", py_getbuildinfo, METH_NOARGS},
70+
{"_py_getbuiltwithassert", _py_getbuiltwithassert, METH_NOARGS},
71+
{NULL},
72+
};
73+
74+
int
75+
_PyTestLimitedCAPI_Init_Build(PyObject *m)
76+
{
77+
return PyModule_AddFunctions(m, test_methods);
78+
}

‎Modules/_testlimitedcapi/parts.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ int _PyTestLimitedCAPI_Init_Weakref(PyObject *module);
5353
int _PyTestLimitedCAPI_Init_Run(PyObject *module);
5454
int _PyTestLimitedCAPI_Init_Type(PyObject *module);
5555
int _PyTestLimitedCAPI_Init_Hash(PyObject *module);
56+
int _PyTestLimitedCAPI_Init_Build(PyObject *module);
5657

5758
#endif // Py_TESTLIMITEDCAPI_PARTS_H

‎Modules/getbuildinfo.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,19 @@ _Py_gitidentifier(void)
7777
gitid = GITBRANCH;
7878
return gitid;
7979
}
80+
81+
82+
// Check if Python was built with NDEBUG macro defined or not. Implement the
83+
// function in Modules/getbuildinfo.c so it's built with the same compiler
84+
// flags than the Python core C code.
85+
//
86+
// Export the function for '_testlimitedcapi' shared extension.
87+
PyAPI_FUNC(int)
88+
_Py_GetBuiltWithAssert(void)
89+
{
90+
#ifdef NDEBUG
91+
return 0;
92+
#else
93+
return 1;
94+
#endif
95+
}

‎PCbuild/_testlimitedcapi.vcxproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
<ClCompile Include="..\Modules\_testlimitedcapi\run.c" />
124124
<ClCompile Include="..\Modules\_testlimitedcapi\type.c" />
125125
<ClCompile Include="..\Modules\_testlimitedcapi\hash.c" />
126+
<ClCompile Include="..\Modules\_testlimitedcapi\build.c" />
126127
</ItemGroup>
127128
<ItemGroup>
128129
<ResourceCompile Include="..\PC\python_nt.rc" />

‎PCbuild/_testlimitedcapi.vcxproj.filters‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<ClCompile Include="..\Modules\_testlimitedcapi\run.c" />
4040
<ClCompile Include="..\Modules\_testlimitedcapi\type.c" />
4141
<ClCompile Include="..\Modules\_testlimitedcapi\hash.c" />
42+
<ClCompile Include="..\Modules\_testlimitedcapi\build.c" />
4243
<ClCompile Include="..\Modules\_testlimitedcapi.c" />
4344
</ItemGroup>
4445
<ItemGroup>

0 commit comments

Comments
 (0)