1 #include "parts.h"
2
3
4 PyDoc_STRVAR(docstring_empty,
5 ""
6 );
7
8 PyDoc_STRVAR(docstring_no_signature,
9 "This docstring has no signature."
10 );
11
12 PyDoc_STRVAR(docstring_with_invalid_signature,
13 "docstring_with_invalid_signature($module, /, boo)\n"
14 "\n"
15 "This docstring has an invalid signature."
16 );
17
18 PyDoc_STRVAR(docstring_with_invalid_signature2,
19 "docstring_with_invalid_signature2($module, /, boo)\n"
20 "\n"
21 "--\n"
22 "\n"
23 "This docstring also has an invalid signature."
24 );
25
26 PyDoc_STRVAR(docstring_with_signature,
27 "docstring_with_signature($module, /, sig)\n"
28 "--\n"
29 "\n"
30 "This docstring has a valid signature."
31 );
32
33 PyDoc_STRVAR(docstring_with_signature_but_no_doc,
34 "docstring_with_signature_but_no_doc($module, /, sig)\n"
35 "--\n"
36 "\n"
37 );
38
39 PyDoc_STRVAR(docstring_with_signature_and_extra_newlines,
40 "docstring_with_signature_and_extra_newlines($module, /, parameter)\n"
41 "--\n"
42 "\n"
43 "\n"
44 "This docstring has a valid signature and some extra newlines."
45 );
46
47 PyDoc_STRVAR(docstring_with_signature_with_defaults,
48 "docstring_with_signature_with_defaults(module, s='avocado',\n"
49 " b=b'bytes', d=3.14, i=35, n=None, t=True, f=False,\n"
50 " local=the_number_three, sys=sys.maxsize,\n"
51 " exp=sys.maxsize - 1)\n"
52 "--\n"
53 "\n"
54 "\n"
55 "\n"
56 "This docstring has a valid signature with parameters,\n"
57 "and the parameters take defaults of varying types."
58 );
59
60 /* This is here to provide a docstring for test_descr. */
61 static PyObject *
test_with_docstring(PyObject * self,PyObject * Py_UNUSED (ignored))62 test_with_docstring(PyObject *self, PyObject *Py_UNUSED(ignored))
63 {
64 Py_RETURN_NONE;
65 }
66
67 static PyMethodDef test_methods[] = {
68 {"docstring_empty",
69 (PyCFunction)test_with_docstring, METH_VARARGS,
70 docstring_empty},
71 {"docstring_no_signature",
72 (PyCFunction)test_with_docstring, METH_VARARGS,
73 docstring_no_signature},
74 {"docstring_no_signature_noargs",
75 (PyCFunction)test_with_docstring, METH_NOARGS,
76 docstring_no_signature},
77 {"docstring_no_signature_o",
78 (PyCFunction)test_with_docstring, METH_O,
79 docstring_no_signature},
80 {"docstring_with_invalid_signature",
81 (PyCFunction)test_with_docstring, METH_VARARGS,
82 docstring_with_invalid_signature},
83 {"docstring_with_invalid_signature2",
84 (PyCFunction)test_with_docstring, METH_VARARGS,
85 docstring_with_invalid_signature2},
86 {"docstring_with_signature",
87 (PyCFunction)test_with_docstring, METH_VARARGS,
88 docstring_with_signature},
89 {"docstring_with_signature_and_extra_newlines",
90 (PyCFunction)test_with_docstring, METH_VARARGS,
91 docstring_with_signature_and_extra_newlines},
92 {"docstring_with_signature_but_no_doc",
93 (PyCFunction)test_with_docstring, METH_VARARGS,
94 docstring_with_signature_but_no_doc},
95 {"docstring_with_signature_with_defaults",
96 (PyCFunction)test_with_docstring, METH_VARARGS,
97 docstring_with_signature_with_defaults},
98 {"no_docstring",
99 (PyCFunction)test_with_docstring, METH_VARARGS},
100 {"test_with_docstring",
101 test_with_docstring, METH_VARARGS,
102 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 )},
110 {NULL},
111 };
112
113 static PyMethodDef DocStringNoSignatureTest_methods[] = {
114 {"meth_noargs",
115 (PyCFunction)test_with_docstring, METH_NOARGS,
116 docstring_no_signature},
117 {"meth_o",
118 (PyCFunction)test_with_docstring, METH_O,
119 docstring_no_signature},
120 {"meth_noargs_class",
121 (PyCFunction)test_with_docstring, METH_NOARGS|METH_CLASS,
122 docstring_no_signature},
123 {"meth_o_class",
124 (PyCFunction)test_with_docstring, METH_O|METH_CLASS,
125 docstring_no_signature},
126 {"meth_noargs_static",
127 (PyCFunction)test_with_docstring, METH_NOARGS|METH_STATIC,
128 docstring_no_signature},
129 {"meth_o_static",
130 (PyCFunction)test_with_docstring, METH_O|METH_STATIC,
131 docstring_no_signature},
132 {"meth_noargs_coexist",
133 (PyCFunction)test_with_docstring, METH_NOARGS|METH_COEXIST,
134 docstring_no_signature},
135 {"meth_o_coexist",
136 (PyCFunction)test_with_docstring, METH_O|METH_COEXIST,
137 docstring_no_signature},
138 {NULL},
139 };
140
141 static PyTypeObject DocStringNoSignatureTest = {
142 PyVarObject_HEAD_INIT(NULL, 0)
143 .tp_name = "_testcapi.DocStringNoSignatureTest",
144 .tp_basicsize = sizeof(PyObject),
145 .tp_flags = Py_TPFLAGS_DEFAULT,
146 .tp_methods = DocStringNoSignatureTest_methods,
147 .tp_new = PyType_GenericNew,
148 };
149
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 {"with_default",
173 (PyCFunction)test_with_docstring, METH_VARARGS,
174 PyDoc_STR(
175 "with_default($self, /, x=ONE)\n"
176 "--\n\n"
177 "This instance method has a default parameter value from the module scope."
178 )},
179 {NULL},
180 };
181
182 static PyTypeObject DocStringUnrepresentableSignatureTest = {
183 PyVarObject_HEAD_INIT(NULL, 0)
184 .tp_name = "_testcapi.DocStringUnrepresentableSignatureTest",
185 .tp_basicsize = sizeof(PyObject),
186 .tp_flags = Py_TPFLAGS_DEFAULT,
187 .tp_methods = DocStringUnrepresentableSignatureTest_methods,
188 .tp_new = PyType_GenericNew,
189 };
190
191 int
_PyTestCapi_Init_Docstring(PyObject * mod)192 _PyTestCapi_Init_Docstring(PyObject *mod)
193 {
194 if (PyModule_AddFunctions(mod, test_methods) < 0) {
195 return -1;
196 }
197 if (PyModule_AddType(mod, &DocStringNoSignatureTest) < 0) {
198 return -1;
199 }
200 if (PyModule_AddType(mod, &DocStringUnrepresentableSignatureTest) < 0) {
201 return -1;
202 }
203 if (PyModule_AddObject(mod, "ONE", PyLong_FromLong(1)) < 0) {
204 return -1;
205 }
206 return 0;
207 }
208