Skip to content

Commit fdd42c4

Browse files
bpo-20185: Convert list object implementation to Argument Clinic. (python#542)
1 parent 0710d75 commit fdd42c4

File tree

3 files changed

+527
-154
lines changed

3 files changed

+527
-154
lines changed

Lib/idlelib/idle_test/test_calltips.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def gtest(obj, out):
5858
'Create and return a new object. See help(type) for accurate signature.')
5959
gtest(list.__init__,
6060
'Initialize self. See help(type(self)) for accurate signature.')
61-
append_doc = "L.append(object) -> None -- append object to end"
61+
append_doc = "Append object to the end of the list."
6262
gtest(list.append, append_doc)
6363
gtest([].append, append_doc)
6464
gtest(List.append, append_doc)
@@ -81,9 +81,9 @@ def f(): pass
8181

8282
def test_multiline_docstring(self):
8383
# Test fewer lines than max.
84-
self.assertEqual(signature(list),
85-
"list() -> new empty list\n"
86-
"list(iterable) -> new list initialized from iterable's items")
84+
self.assertEqual(signature(range),
85+
"range(stop) -> range object\n"
86+
"range(start, stop[, step]) -> range object")
8787

8888
# Test max lines
8989
self.assertEqual(signature(bytes), '''\

Objects/clinic/listobject.c.h

+300
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
/*[clinic input]
2+
preserve
3+
[clinic start generated code]*/
4+
5+
PyDoc_STRVAR(list_insert__doc__,
6+
"insert($self, index, object, /)\n"
7+
"--\n"
8+
"\n"
9+
"Insert object before index.");
10+
11+
#define LIST_INSERT_METHODDEF \
12+
{"insert", (PyCFunction)list_insert, METH_FASTCALL, list_insert__doc__},
13+
14+
static PyObject *
15+
list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object);
16+
17+
static PyObject *
18+
list_insert(PyListObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
19+
{
20+
PyObject *return_value = NULL;
21+
Py_ssize_t index;
22+
PyObject *object;
23+
24+
if (!_PyArg_ParseStack(args, nargs, "nO:insert",
25+
&index, &object)) {
26+
goto exit;
27+
}
28+
29+
if (!_PyArg_NoStackKeywords("insert", kwnames)) {
30+
goto exit;
31+
}
32+
return_value = list_insert_impl(self, index, object);
33+
34+
exit:
35+
return return_value;
36+
}
37+
38+
PyDoc_STRVAR(list_clear__doc__,
39+
"clear($self, /)\n"
40+
"--\n"
41+
"\n"
42+
"Remove all items from list.");
43+
44+
#define LIST_CLEAR_METHODDEF \
45+
{"clear", (PyCFunction)list_clear, METH_NOARGS, list_clear__doc__},
46+
47+
static PyObject *
48+
list_clear_impl(PyListObject *self);
49+
50+
static PyObject *
51+
list_clear(PyListObject *self, PyObject *Py_UNUSED(ignored))
52+
{
53+
return list_clear_impl(self);
54+
}
55+
56+
PyDoc_STRVAR(list_copy__doc__,
57+
"copy($self, /)\n"
58+
"--\n"
59+
"\n"
60+
"Return a shallow copy of the list.");
61+
62+
#define LIST_COPY_METHODDEF \
63+
{"copy", (PyCFunction)list_copy, METH_NOARGS, list_copy__doc__},
64+
65+
static PyObject *
66+
list_copy_impl(PyListObject *self);
67+
68+
static PyObject *
69+
list_copy(PyListObject *self, PyObject *Py_UNUSED(ignored))
70+
{
71+
return list_copy_impl(self);
72+
}
73+
74+
PyDoc_STRVAR(list_append__doc__,
75+
"append($self, object, /)\n"
76+
"--\n"
77+
"\n"
78+
"Append object to the end of the list.");
79+
80+
#define LIST_APPEND_METHODDEF \
81+
{"append", (PyCFunction)list_append, METH_O, list_append__doc__},
82+
83+
PyDoc_STRVAR(list_extend__doc__,
84+
"extend($self, iterable, /)\n"
85+
"--\n"
86+
"\n"
87+
"Extend list by appending elements from the iterable.");
88+
89+
#define LIST_EXTEND_METHODDEF \
90+
{"extend", (PyCFunction)list_extend, METH_O, list_extend__doc__},
91+
92+
PyDoc_STRVAR(list_pop__doc__,
93+
"pop($self, index=-1, /)\n"
94+
"--\n"
95+
"\n"
96+
"Remove and return item at index (default last).\n"
97+
"\n"
98+
"Raises IndexError if list is empty or index is out of range.");
99+
100+
#define LIST_POP_METHODDEF \
101+
{"pop", (PyCFunction)list_pop, METH_FASTCALL, list_pop__doc__},
102+
103+
static PyObject *
104+
list_pop_impl(PyListObject *self, Py_ssize_t index);
105+
106+
static PyObject *
107+
list_pop(PyListObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
108+
{
109+
PyObject *return_value = NULL;
110+
Py_ssize_t index = -1;
111+
112+
if (!_PyArg_ParseStack(args, nargs, "|n:pop",
113+
&index)) {
114+
goto exit;
115+
}
116+
117+
if (!_PyArg_NoStackKeywords("pop", kwnames)) {
118+
goto exit;
119+
}
120+
return_value = list_pop_impl(self, index);
121+
122+
exit:
123+
return return_value;
124+
}
125+
126+
PyDoc_STRVAR(list_sort__doc__,
127+
"sort($self, /, *, key=None, reverse=False)\n"
128+
"--\n"
129+
"\n"
130+
"Stable sort *IN PLACE*.");
131+
132+
#define LIST_SORT_METHODDEF \
133+
{"sort", (PyCFunction)list_sort, METH_FASTCALL, list_sort__doc__},
134+
135+
static PyObject *
136+
list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse);
137+
138+
static PyObject *
139+
list_sort(PyListObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
140+
{
141+
PyObject *return_value = NULL;
142+
static const char * const _keywords[] = {"key", "reverse", NULL};
143+
static _PyArg_Parser _parser = {"|$Oi:sort", _keywords, 0};
144+
PyObject *keyfunc = Py_None;
145+
int reverse = 0;
146+
147+
if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
148+
&keyfunc, &reverse)) {
149+
goto exit;
150+
}
151+
return_value = list_sort_impl(self, keyfunc, reverse);
152+
153+
exit:
154+
return return_value;
155+
}
156+
157+
PyDoc_STRVAR(list_reverse__doc__,
158+
"reverse($self, /)\n"
159+
"--\n"
160+
"\n"
161+
"Reverse *IN PLACE*.");
162+
163+
#define LIST_REVERSE_METHODDEF \
164+
{"reverse", (PyCFunction)list_reverse, METH_NOARGS, list_reverse__doc__},
165+
166+
static PyObject *
167+
list_reverse_impl(PyListObject *self);
168+
169+
static PyObject *
170+
list_reverse(PyListObject *self, PyObject *Py_UNUSED(ignored))
171+
{
172+
return list_reverse_impl(self);
173+
}
174+
175+
PyDoc_STRVAR(list_index__doc__,
176+
"index($self, value, start=0, stop=sys.maxsize, /)\n"
177+
"--\n"
178+
"\n"
179+
"Return first index of value.\n"
180+
"\n"
181+
"Raises ValueError if the value is not present.");
182+
183+
#define LIST_INDEX_METHODDEF \
184+
{"index", (PyCFunction)list_index, METH_FASTCALL, list_index__doc__},
185+
186+
static PyObject *
187+
list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
188+
Py_ssize_t stop);
189+
190+
static PyObject *
191+
list_index(PyListObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
192+
{
193+
PyObject *return_value = NULL;
194+
PyObject *value;
195+
Py_ssize_t start = 0;
196+
Py_ssize_t stop = PY_SSIZE_T_MAX;
197+
198+
if (!_PyArg_ParseStack(args, nargs, "O|O&O&:index",
199+
&value, _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &stop)) {
200+
goto exit;
201+
}
202+
203+
if (!_PyArg_NoStackKeywords("index", kwnames)) {
204+
goto exit;
205+
}
206+
return_value = list_index_impl(self, value, start, stop);
207+
208+
exit:
209+
return return_value;
210+
}
211+
212+
PyDoc_STRVAR(list_count__doc__,
213+
"count($self, value, /)\n"
214+
"--\n"
215+
"\n"
216+
"Return number of occurrences of value.");
217+
218+
#define LIST_COUNT_METHODDEF \
219+
{"count", (PyCFunction)list_count, METH_O, list_count__doc__},
220+
221+
PyDoc_STRVAR(list_remove__doc__,
222+
"remove($self, value, /)\n"
223+
"--\n"
224+
"\n"
225+
"Remove first occurrence of value.\n"
226+
"\n"
227+
"Raises ValueError if the value is not present.");
228+
229+
#define LIST_REMOVE_METHODDEF \
230+
{"remove", (PyCFunction)list_remove, METH_O, list_remove__doc__},
231+
232+
PyDoc_STRVAR(list___init____doc__,
233+
"list(iterable=(), /)\n"
234+
"--\n"
235+
"\n"
236+
"Built-in mutable sequence.\n"
237+
"\n"
238+
"If no argument is given, the constructor creates a new empty list.\n"
239+
"The argument must be an iterable if specified.");
240+
241+
static int
242+
list___init___impl(PyListObject *self, PyObject *iterable);
243+
244+
static int
245+
list___init__(PyObject *self, PyObject *args, PyObject *kwargs)
246+
{
247+
int return_value = -1;
248+
PyObject *iterable = NULL;
249+
250+
if ((Py_TYPE(self) == &PyList_Type) &&
251+
!_PyArg_NoKeywords("list", kwargs)) {
252+
goto exit;
253+
}
254+
if (!PyArg_UnpackTuple(args, "list",
255+
0, 1,
256+
&iterable)) {
257+
goto exit;
258+
}
259+
return_value = list___init___impl((PyListObject *)self, iterable);
260+
261+
exit:
262+
return return_value;
263+
}
264+
265+
PyDoc_STRVAR(list___sizeof____doc__,
266+
"__sizeof__($self, /)\n"
267+
"--\n"
268+
"\n"
269+
"Return the size of the list in memory, in bytes.");
270+
271+
#define LIST___SIZEOF___METHODDEF \
272+
{"__sizeof__", (PyCFunction)list___sizeof__, METH_NOARGS, list___sizeof____doc__},
273+
274+
static PyObject *
275+
list___sizeof___impl(PyListObject *self);
276+
277+
static PyObject *
278+
list___sizeof__(PyListObject *self, PyObject *Py_UNUSED(ignored))
279+
{
280+
return list___sizeof___impl(self);
281+
}
282+
283+
PyDoc_STRVAR(list___reversed____doc__,
284+
"__reversed__($self, /)\n"
285+
"--\n"
286+
"\n"
287+
"Return a reverse iterator over the list.");
288+
289+
#define LIST___REVERSED___METHODDEF \
290+
{"__reversed__", (PyCFunction)list___reversed__, METH_NOARGS, list___reversed____doc__},
291+
292+
static PyObject *
293+
list___reversed___impl(PyListObject *self);
294+
295+
static PyObject *
296+
list___reversed__(PyListObject *self, PyObject *Py_UNUSED(ignored))
297+
{
298+
return list___reversed___impl(self);
299+
}
300+
/*[clinic end generated code: output=2a3b75efcf858ed5 input=a9049054013a1b77]*/

0 commit comments

Comments
 (0)