Skip to content

Commit 23bace2

Browse files
authored
bpo-36635: Add _testinternalcapi module (GH-12841)
Add a new _testinternalcapi module to test the internal C API. Move _Py_GetConfigsAsDict() function to the internal C API: _testembed now uses _testinternalcapi to access the function.
1 parent 11efd79 commit 23bace2

File tree

13 files changed

+172
-49
lines changed

13 files changed

+172
-49
lines changed

Include/cpython/coreconfig.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,6 @@ typedef struct {
408408
._init_main = 1}
409409
/* Note: _PyCoreConfig_INIT sets other fields to 0/NULL */
410410

411-
412-
/* --- Function used for testing ---------------------------------- */
413-
414-
PyAPI_FUNC(PyObject*) _Py_GetConfigsAsDict(void);
415-
416411
#ifdef __cplusplus
417412
}
418413
#endif

Include/internal/pycore_coreconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *config,
110110
const _PyArgv *args);
111111
PyAPI_FUNC(void) _PyCoreConfig_Write(const _PyCoreConfig *config);
112112

113+
114+
/* --- Function used for testing ---------------------------------- */
115+
116+
PyAPI_FUNC(PyObject*) _Py_GetConfigsAsDict(void);
117+
113118
#ifdef __cplusplus
114119
}
115120
#endif

Lib/test/pythoninfo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ def collect_get_config(info_add):
598598
# Dump global configuration variables, _PyCoreConfig
599599
# and _PyMainInterpreterConfig
600600
try:
601-
from _testcapi import get_configs
601+
from _testinternalcapi import get_configs
602602
except ImportError:
603603
return
604604

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a new :mod:`_testinternalcapi` module to test the internal C API.

Modules/Setup

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ _symtable symtablemodule.c
173173
#_struct _struct.c # binary structure packing/unpacking
174174
#_weakref _weakref.c # basic weak reference support
175175
#_testcapi _testcapimodule.c # Python C API test module
176+
#_testinternalcapi _testinternalcapi.c -I$(srcdir)/Include/internal -DPy_BUILD_CORE_MODULE # Python internal C API test module
176177
#_random _randommodule.c # Random number generator
177178
#_elementtree -I$(srcdir)/Modules/expat -DHAVE_EXPAT_CONFIG_H -DUSE_PYEXPAT_CAPI _elementtree.c # elementtree accelerator
178179
#_pickle _pickle.c # pickle accelerator

Modules/_testcapimodule.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4736,13 +4736,6 @@ decode_locale_ex(PyObject *self, PyObject *args)
47364736
}
47374737

47384738

4739-
static PyObject *
4740-
get_configs(PyObject *self, PyObject *Py_UNUSED(args))
4741-
{
4742-
return _Py_GetConfigsAsDict();
4743-
}
4744-
4745-
47464739
#ifdef Py_REF_DEBUG
47474740
static PyObject *
47484741
negative_refcount(PyObject *self, PyObject *Py_UNUSED(args))
@@ -4990,7 +4983,6 @@ static PyMethodDef TestMethods[] = {
49904983
{"bad_get", (PyCFunction)(void(*)(void))bad_get, METH_FASTCALL},
49914984
{"EncodeLocaleEx", encode_locale_ex, METH_VARARGS},
49924985
{"DecodeLocaleEx", decode_locale_ex, METH_VARARGS},
4993-
{"get_configs", get_configs, METH_NOARGS},
49944986
#ifdef Py_REF_DEBUG
49954987
{"negative_refcount", negative_refcount, METH_NOARGS},
49964988
#endif

Modules/_testinternalcapi.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* C Extension module to test Python internal C APIs (Include/internal).
3+
*/
4+
5+
#if !defined(Py_BUILD_CORE_BUILTIN) && !defined(Py_BUILD_CORE_MODULE)
6+
# error "Py_BUILD_CORE_BUILTIN or Py_BUILD_CORE_MODULE must be defined"
7+
#endif
8+
9+
#define PY_SSIZE_T_CLEAN
10+
11+
#include "Python.h"
12+
#include "pycore_coreconfig.h"
13+
14+
15+
static PyObject *
16+
get_configs(PyObject *self, PyObject *Py_UNUSED(args))
17+
{
18+
return _Py_GetConfigsAsDict();
19+
}
20+
21+
22+
static PyMethodDef TestMethods[] = {
23+
{"get_configs", get_configs, METH_NOARGS},
24+
{NULL, NULL} /* sentinel */
25+
};
26+
27+
28+
static struct PyModuleDef _testcapimodule = {
29+
PyModuleDef_HEAD_INIT,
30+
"_testinternalcapi",
31+
NULL,
32+
-1,
33+
TestMethods,
34+
NULL,
35+
NULL,
36+
NULL,
37+
NULL
38+
};
39+
40+
41+
PyMODINIT_FUNC
42+
PyInit__testinternalcapi(void)
43+
{
44+
return PyModule_Create(&_testcapimodule);
45+
}

PCbuild/_testinternalcapi.vcxproj

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|ARM">
5+
<Configuration>Debug</Configuration>
6+
<Platform>ARM</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Debug|Win32">
9+
<Configuration>Debug</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="PGInstrument|ARM">
17+
<Configuration>PGInstrument</Configuration>
18+
<Platform>ARM</Platform>
19+
</ProjectConfiguration>
20+
<ProjectConfiguration Include="PGInstrument|Win32">
21+
<Configuration>PGInstrument</Configuration>
22+
<Platform>Win32</Platform>
23+
</ProjectConfiguration>
24+
<ProjectConfiguration Include="PGInstrument|x64">
25+
<Configuration>PGInstrument</Configuration>
26+
<Platform>x64</Platform>
27+
</ProjectConfiguration>
28+
<ProjectConfiguration Include="PGUpdate|ARM">
29+
<Configuration>PGUpdate</Configuration>
30+
<Platform>ARM</Platform>
31+
</ProjectConfiguration>
32+
<ProjectConfiguration Include="PGUpdate|Win32">
33+
<Configuration>PGUpdate</Configuration>
34+
<Platform>Win32</Platform>
35+
</ProjectConfiguration>
36+
<ProjectConfiguration Include="PGUpdate|x64">
37+
<Configuration>PGUpdate</Configuration>
38+
<Platform>x64</Platform>
39+
</ProjectConfiguration>
40+
<ProjectConfiguration Include="Release|ARM">
41+
<Configuration>Release</Configuration>
42+
<Platform>ARM</Platform>
43+
</ProjectConfiguration>
44+
<ProjectConfiguration Include="Release|Win32">
45+
<Configuration>Release</Configuration>
46+
<Platform>Win32</Platform>
47+
</ProjectConfiguration>
48+
<ProjectConfiguration Include="Release|x64">
49+
<Configuration>Release</Configuration>
50+
<Platform>x64</Platform>
51+
</ProjectConfiguration>
52+
</ItemGroup>
53+
<PropertyGroup Label="Globals">
54+
<ProjectGuid>{900342D7-516A-4469-B1AD-59A66E49A25F}</ProjectGuid>
55+
<RootNamespace>_testinternalcapi</RootNamespace>
56+
<Keyword>Win32Proj</Keyword>
57+
<SupportPGO>false</SupportPGO>
58+
</PropertyGroup>
59+
<Import Project="python.props" />
60+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
61+
<PropertyGroup Label="Configuration">
62+
<ConfigurationType>DynamicLibrary</ConfigurationType>
63+
<CharacterSet>NotSet</CharacterSet>
64+
</PropertyGroup>
65+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
66+
<PropertyGroup>
67+
<TargetExt>.pyd</TargetExt>
68+
</PropertyGroup>
69+
<ImportGroup Label="ExtensionSettings">
70+
</ImportGroup>
71+
<ImportGroup Label="PropertySheets">
72+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
73+
<Import Project="pyproject.props" />
74+
</ImportGroup>
75+
<PropertyGroup Label="UserMacros" />
76+
<PropertyGroup>
77+
<_ProjectFileVersion>10.0.30319.1</_ProjectFileVersion>
78+
</PropertyGroup>
79+
<ItemGroup>
80+
<ClCompile Include="..\Modules\_testinternalcapi.c" />
81+
</ItemGroup>
82+
<ItemGroup>
83+
<ResourceCompile Include="..\PC\python_nt.rc" />
84+
</ItemGroup>
85+
<ItemGroup>
86+
<ProjectReference Include="pythoncore.vcxproj">
87+
<Project>{cf7ac3d1-e2df-41d2-bea6-1e2556cdea26}</Project>
88+
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
89+
</ProjectReference>
90+
</ItemGroup>
91+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
92+
<ImportGroup Label="ExtensionTargets">
93+
</ImportGroup>
94+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{136fc5eb-7fe4-4486-8c6d-b49f37a00199}</UniqueIdentifier>
6+
</Filter>
7+
</ItemGroup>
8+
<ItemGroup>
9+
<ClCompile Include="..\Modules\_testinternalcapi.c">
10+
<Filter>Source Files</Filter>
11+
</ClCompile>
12+
</ItemGroup>
13+
</Project>

PCbuild/pcbuild.proj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@
6262
<ExtensionModules Include="@(ExternalModules->'%(Identity)')" Condition="$(IncludeExternals)" />
6363
<Projects Include="@(ExtensionModules->'%(Identity).vcxproj')" Condition="$(IncludeExtensions)" />
6464
<!-- Test modules -->
65-
<TestModules Include="_ctypes_test;_testbuffer;_testcapi;_testembed;_testimportmultiple;_testmultiphase;_testconsole" />
65+
<TestModules Include="_ctypes_test;_testbuffer;_testcapi;_testinternalcapi;_testembed;_testimportmultiple;_testmultiphase;_testconsole" />
6666
<TestModules Include="xxlimited" Condition="'$(Configuration)' == 'Release'" />
6767
<Projects Include="@(TestModules->'%(Identity).vcxproj')" Condition="$(IncludeTests)">
6868
<!-- Disable parallel build for test modules -->
6969
<BuildInParallel>false</BuildInParallel>
7070
</Projects>
71-
71+
7272
<!-- _freeze_importlib -->
7373
<Projects2 Condition="$(Platform) != 'ARM'" Include="_freeze_importlib.vcxproj" />
7474
<!-- python[w].exe -->

0 commit comments

Comments
 (0)