Skip to content

Commit 1f885df

Browse files
gh-107782: Use _testcapi to test non-representable signatures (GH-109325)
Builtin functions and methods that have non-representable signatures today will have representable signatures yesterday, and they will become unusable for testing this feature. So we need to add special functions and methods to the _testcapi module that always have non-representable signatures.
1 parent a806e92 commit 1f885df

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

Lib/test/test_pydoc.py

+37-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from io import StringIO
2525
from collections import namedtuple
2626
from urllib.request import urlopen, urlcleanup
27+
from test import support
2728
from test.support import import_helper
2829
from test.support import os_helper
2930
from test.support.script_helper import (assert_python_ok,
@@ -1236,22 +1237,56 @@ def test_bound_builtin_classmethod_o(self):
12361237
self.assertEqual(self._get_summary_line(dict.__class_getitem__),
12371238
"__class_getitem__(object, /) method of builtins.type instance")
12381239

1240+
@support.cpython_only
12391241
def test_module_level_callable_unrepresentable_default(self):
1240-
self.assertEqual(self._get_summary_line(getattr),
1241-
"getattr(...)")
1242+
import _testcapi
1243+
builtin = _testcapi.func_with_unrepresentable_signature
1244+
self.assertEqual(self._get_summary_line(builtin),
1245+
"func_with_unrepresentable_signature(a, b=<x>)")
12421246

1247+
@support.cpython_only
12431248
def test_builtin_staticmethod_unrepresentable_default(self):
12441249
self.assertEqual(self._get_summary_line(str.maketrans),
12451250
"maketrans(x, y=<unrepresentable>, z=<unrepresentable>, /)")
1251+
import _testcapi
1252+
cls = _testcapi.DocStringUnrepresentableSignatureTest
1253+
self.assertEqual(self._get_summary_line(cls.staticmeth),
1254+
"staticmeth(a, b=<x>)")
12461255

1256+
@support.cpython_only
12471257
def test_unbound_builtin_method_unrepresentable_default(self):
12481258
self.assertEqual(self._get_summary_line(dict.pop),
12491259
"pop(self, key, default=<unrepresentable>, /)")
1260+
import _testcapi
1261+
cls = _testcapi.DocStringUnrepresentableSignatureTest
1262+
self.assertEqual(self._get_summary_line(cls.meth),
1263+
"meth(self, /, a, b=<x>)")
12501264

1265+
@support.cpython_only
12511266
def test_bound_builtin_method_unrepresentable_default(self):
12521267
self.assertEqual(self._get_summary_line({}.pop),
12531268
"pop(key, default=<unrepresentable>, /) "
12541269
"method of builtins.dict instance")
1270+
import _testcapi
1271+
obj = _testcapi.DocStringUnrepresentableSignatureTest()
1272+
self.assertEqual(self._get_summary_line(obj.meth),
1273+
"meth(a, b=<x>) "
1274+
"method of _testcapi.DocStringUnrepresentableSignatureTest instance")
1275+
1276+
@support.cpython_only
1277+
def test_unbound_builtin_classmethod_unrepresentable_default(self):
1278+
import _testcapi
1279+
cls = _testcapi.DocStringUnrepresentableSignatureTest
1280+
descr = cls.__dict__['classmeth']
1281+
self.assertEqual(self._get_summary_line(descr),
1282+
"classmeth(type, /, a, b=<x>)")
1283+
1284+
@support.cpython_only
1285+
def test_bound_builtin_classmethod_unrepresentable_default(self):
1286+
import _testcapi
1287+
cls = _testcapi.DocStringUnrepresentableSignatureTest
1288+
self.assertEqual(self._get_summary_line(cls.classmeth),
1289+
"classmeth(a, b=<x>) method of builtins.type instance")
12551290

12561291
def test_overridden_text_signature(self):
12571292
class C:

Modules/_testcapi/docstring.c

+44
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ static PyMethodDef test_methods[] = {
100100
{"test_with_docstring",
101101
test_with_docstring, METH_VARARGS,
102102
PyDoc_STR("This is a pretty normal docstring.")},
103+
{"func_with_unrepresentable_signature",
104+
(PyCFunction)test_with_docstring, METH_VARARGS,
105+
PyDoc_STR(
106+
"func_with_unrepresentable_signature($module, /, a, b=<x>)\n"
107+
"--\n\n"
108+
"This docstring has a signature with unrepresentable default."
109+
)},
103110
{NULL},
104111
};
105112

@@ -140,6 +147,40 @@ static PyTypeObject DocStringNoSignatureTest = {
140147
.tp_new = PyType_GenericNew,
141148
};
142149

150+
static PyMethodDef DocStringUnrepresentableSignatureTest_methods[] = {
151+
{"meth",
152+
(PyCFunction)test_with_docstring, METH_VARARGS,
153+
PyDoc_STR(
154+
"meth($self, /, a, b=<x>)\n"
155+
"--\n\n"
156+
"This docstring has a signature with unrepresentable default."
157+
)},
158+
{"classmeth",
159+
(PyCFunction)test_with_docstring, METH_VARARGS|METH_CLASS,
160+
PyDoc_STR(
161+
"classmeth($type, /, a, b=<x>)\n"
162+
"--\n\n"
163+
"This docstring has a signature with unrepresentable default."
164+
)},
165+
{"staticmeth",
166+
(PyCFunction)test_with_docstring, METH_VARARGS|METH_STATIC,
167+
PyDoc_STR(
168+
"staticmeth(a, b=<x>)\n"
169+
"--\n\n"
170+
"This docstring has a signature with unrepresentable default."
171+
)},
172+
{NULL},
173+
};
174+
175+
static PyTypeObject DocStringUnrepresentableSignatureTest = {
176+
PyVarObject_HEAD_INIT(NULL, 0)
177+
.tp_name = "_testcapi.DocStringUnrepresentableSignatureTest",
178+
.tp_basicsize = sizeof(PyObject),
179+
.tp_flags = Py_TPFLAGS_DEFAULT,
180+
.tp_methods = DocStringUnrepresentableSignatureTest_methods,
181+
.tp_new = PyType_GenericNew,
182+
};
183+
143184
int
144185
_PyTestCapi_Init_Docstring(PyObject *mod)
145186
{
@@ -149,5 +190,8 @@ _PyTestCapi_Init_Docstring(PyObject *mod)
149190
if (PyModule_AddType(mod, &DocStringNoSignatureTest) < 0) {
150191
return -1;
151192
}
193+
if (PyModule_AddType(mod, &DocStringUnrepresentableSignatureTest) < 0) {
194+
return -1;
195+
}
152196
return 0;
153197
}

Tools/c-analyzer/cpython/ignored.tsv

+1
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ Modules/_testcapi/buffer.c - testBufType -
420420
Modules/_testcapi/code.c get_code_extra_index key -
421421
Modules/_testcapi/datetime.c - test_run_counter -
422422
Modules/_testcapi/docstring.c - DocStringNoSignatureTest -
423+
Modules/_testcapi/docstring.c - DocStringUnrepresentableSignatureTest -
423424
Modules/_testcapi/exceptions.c - PyRecursingInfinitelyError_Type -
424425
Modules/_testcapi/heaptype.c - _testcapimodule -
425426
Modules/_testcapi/mem.c - FmData -

0 commit comments

Comments
 (0)