summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapi/docstring.c
blob: b680171cc1437a58878e3dbb5930274f65bae0d6 (plain)
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
#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",
        (PyCFunction)test_with_docstring, METH_VARARGS,
        docstring_empty},
    {"docstring_no_signature",
        (PyCFunction)test_with_docstring, METH_VARARGS,
        docstring_no_signature},
    {"docstring_no_signature_noargs",
        (PyCFunction)test_with_docstring, METH_NOARGS,
        docstring_no_signature},
    {"docstring_no_signature_o",
        (PyCFunction)test_with_docstring, METH_O,
        docstring_no_signature},
    {"docstring_with_invalid_signature",
        (PyCFunction)test_with_docstring, METH_VARARGS,
        docstring_with_invalid_signature},
    {"docstring_with_invalid_signature2",
        (PyCFunction)test_with_docstring, METH_VARARGS,
        docstring_with_invalid_signature2},
    {"docstring_with_signature",
        (PyCFunction)test_with_docstring, METH_VARARGS,
        docstring_with_signature},
    {"docstring_with_signature_and_extra_newlines",
        (PyCFunction)test_with_docstring, METH_VARARGS,
        docstring_with_signature_and_extra_newlines},
    {"docstring_with_signature_but_no_doc",
        (PyCFunction)test_with_docstring, METH_VARARGS,
        docstring_with_signature_but_no_doc},
    {"docstring_with_signature_with_defaults",
        (PyCFunction)test_with_docstring, METH_VARARGS,
        docstring_with_signature_with_defaults},
    {"no_docstring",
        (PyCFunction)test_with_docstring, METH_VARARGS},
    {"test_with_docstring",
        test_with_docstring,              METH_VARARGS,
        PyDoc_STR("This is a pretty normal docstring.")},
    {NULL},
};

static PyMethodDef DocStringNoSignatureTest_methods[] = {
    {"meth_noargs",
        (PyCFunction)test_with_docstring, METH_NOARGS,
        docstring_no_signature},
    {"meth_o",
        (PyCFunction)test_with_docstring, METH_O,
        docstring_no_signature},
    {"meth_noargs_class",
        (PyCFunction)test_with_docstring, METH_NOARGS|METH_CLASS,
        docstring_no_signature},
    {"meth_o_class",
        (PyCFunction)test_with_docstring, METH_O|METH_CLASS,
        docstring_no_signature},
    {"meth_noargs_static",
        (PyCFunction)test_with_docstring, METH_NOARGS|METH_STATIC,
        docstring_no_signature},
    {"meth_o_static",
        (PyCFunction)test_with_docstring, METH_O|METH_STATIC,
        docstring_no_signature},
    {"meth_noargs_coexist",
        (PyCFunction)test_with_docstring, METH_NOARGS|METH_COEXIST,
        docstring_no_signature},
    {"meth_o_coexist",
        (PyCFunction)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,
};

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