aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Modules/_testcapi/docstring.c
blob: efb889cba8796e95c4ae5ed09ab6c1fc110938d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "parts.h"


PyDoc_STRVAR(docstring_empty,
""
);

PyDoc_STRVAR(docstring_no_signature,
"This docstring has no signature."
);

PyDoc_STRVAR(docstring_with_invalid_signature,
"docstring_with_invalid_signature($module, /, boo)\n"
"\n"
"This docstring has an invalid signature."
);

PyDoc_STRVAR(docstring_with_invalid_signature2,
"docstring_with_invalid_signature2($module, /, boo)\n"
"\n"
"--\n"
"\n"
"This docstring also has an invalid signature."
);

PyDoc_STRVAR(docstring_with_signature,
"docstring_with_signature($module, /, sig)\n"
"--\n"
"\n"
"This docstring has a valid signature."
);

PyDoc_STRVAR(docstring_with_signature_but_no_doc,
"docstring_with_signature_but_no_doc($module, /, sig)\n"
"--\n"
"\n"
);

PyDoc_STRVAR(docstring_with_signature_and_extra_newlines,
"docstring_with_signature_and_extra_newlines($module, /, parameter)\n"
"--\n"
"\n"
"\n"
"This docstring has a valid signature and some extra newlines."
);

PyDoc_STRVAR(docstring_with_signature_with_defaults,
"docstring_with_signature_with_defaults(module, s='avocado',\n"
"        b=b'bytes', d=3.14, i=35, n=None, t=True, f=False,\n"
"        local=the_number_three, sys=sys.maxsize,\n"
"        exp=sys.maxsize - 1)\n"
"--\n"
"\n"
"\n"
"\n"
"This docstring has a valid signature with parameters,\n"
"and the parameters take defaults of varying types."
);

/* This is here to provide a docstring for test_descr. */
static PyObject *
test_with_docstring(PyObject *self, PyObject *Py_UNUSED(ignored))
{
    Py_RETURN_NONE;
}

static PyMethodDef test_methods[] = {
    {"docstring_empty",
        test_with_docstring, METH_VARARGS,
        docstring_empty},
    {"docstring_no_signature",
        test_with_docstring, METH_VARARGS,
        docstring_no_signature},
    {"docstring_no_signature_noargs",
        test_with_docstring, METH_NOARGS,
        docstring_no_signature},
    {"docstring_no_signature_o",
        test_with_docstring, METH_O,
        docstring_no_signature},
    {"docstring_with_invalid_signature",
        test_with_docstring, METH_VARARGS,
        docstring_with_invalid_signature},
    {"docstring_with_invalid_signature2",
        test_with_docstring, METH_VARARGS,
        docstring_with_invalid_signature2},
    {"docstring_with_signature",
        test_with_docstring, METH_VARARGS,
        docstring_with_signature},
    {"docstring_with_signature_and_extra_newlines",
        test_with_docstring, METH_VARARGS,
        docstring_with_signature_and_extra_newlines},
    {"docstring_with_signature_but_no_doc",
        test_with_docstring, METH_VARARGS,
        docstring_with_signature_but_no_doc},
    {"docstring_with_signature_with_defaults",
        test_with_docstring, METH_VARARGS,
        docstring_with_signature_with_defaults},
    {"no_docstring",
        test_with_docstring, METH_VARARGS},
    {"test_with_docstring",
        test_with_docstring,              METH_VARARGS,
        PyDoc_STR("This is a pretty normal docstring.")},
    {"func_with_unrepresentable_signature",
        test_with_docstring, METH_VARARGS,
        PyDoc_STR(
            "func_with_unrepresentable_signature($module, /, a, b=<x>)\n"
            "--\n\n"
            "This docstring has a signature with unrepresentable default."
        )},
    {NULL},
};

static PyMethodDef DocStringNoSignatureTest_methods[] = {
    {"meth_noargs",
        test_with_docstring, METH_NOARGS,
        docstring_no_signature},
    {"meth_o",
        test_with_docstring, METH_O,
        docstring_no_signature},
    {"meth_noargs_class",
        test_with_docstring, METH_NOARGS|METH_CLASS,
        docstring_no_signature},
    {"meth_o_class",
        test_with_docstring, METH_O|METH_CLASS,
        docstring_no_signature},
    {"meth_noargs_static",
        test_with_docstring, METH_NOARGS|METH_STATIC,
        docstring_no_signature},
    {"meth_o_static",
        test_with_docstring, METH_O|METH_STATIC,
        docstring_no_signature},
    {"meth_noargs_coexist",
        test_with_docstring, METH_NOARGS|METH_COEXIST,
        docstring_no_signature},
    {"meth_o_coexist",
        test_with_docstring, METH_O|METH_COEXIST,
        docstring_no_signature},
    {NULL},
};

static PyTypeObject DocStringNoSignatureTest = {
    PyVarObject_HEAD_INIT(NULL, 0)
    .tp_name = "_testcapi.DocStringNoSignatureTest",
    .tp_basicsize = sizeof(PyObject),
    .tp_flags = Py_TPFLAGS_DEFAULT,
    .tp_methods = DocStringNoSignatureTest_methods,
    .tp_new = PyType_GenericNew,
};

static PyMethodDef DocStringUnrepresentableSignatureTest_methods[] = {
    {"meth",
        test_with_docstring, METH_VARARGS,
        PyDoc_STR(
            "meth($self, /, a, b=<x>)\n"
            "--\n\n"
            "This docstring has a signature with unrepresentable default."
        )},
    {"classmeth",
        test_with_docstring, METH_VARARGS|METH_CLASS,
        PyDoc_STR(
            "classmeth($type, /, a, b=<x>)\n"
            "--\n\n"
            "This docstring has a signature with unrepresentable default."
        )},
    {"staticmeth",
        test_with_docstring, METH_VARARGS|METH_STATIC,
        PyDoc_STR(
            "staticmeth(a, b=<x>)\n"
            "--\n\n"
            "This docstring has a signature with unrepresentable default."
        )},
    {"with_default",
        test_with_docstring, METH_VARARGS,
        PyDoc_STR(
            "with_default($self, /, x=ONE)\n"
            "--\n\n"
            "This instance method has a default parameter value from the module scope."
        )},
    {NULL},
};

static PyTypeObject DocStringUnrepresentableSignatureTest = {
    PyVarObject_HEAD_INIT(NULL, 0)
    .tp_name = "_testcapi.DocStringUnrepresentableSignatureTest",
    .tp_basicsize = sizeof(PyObject),
    .tp_flags = Py_TPFLAGS_DEFAULT,
    .tp_methods = DocStringUnrepresentableSignatureTest_methods,
    .tp_new = PyType_GenericNew,
};

int
_PyTestCapi_Init_Docstring(PyObject *mod)
{
    if (PyModule_AddFunctions(mod, test_methods) < 0) {
        return -1;
    }
    if (PyModule_AddType(mod, &DocStringNoSignatureTest) < 0) {
        return -1;
    }
    if (PyModule_AddType(mod, &DocStringUnrepresentableSignatureTest) < 0) {
        return -1;
    }
    if (PyModule_AddObject(mod, "ONE", PyLong_FromLong(1)) < 0) {
        return -1;
    }
    return 0;
}