Skip to content

Commit 18b250f

Browse files
bpo-29793: Convert some builtin types constructors to Argument Clinic. (#615)
1 parent 0b56159 commit 18b250f

14 files changed

+527
-202
lines changed

Objects/clinic/complexobject.c.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*[clinic input]
2+
preserve
3+
[clinic start generated code]*/
4+
5+
PyDoc_STRVAR(complex_new__doc__,
6+
"complex(real=0, imag=0)\n"
7+
"--\n"
8+
"\n"
9+
"Create a complex number from a real part and an optional imaginary part.\n"
10+
"\n"
11+
"This is equivalent to (real + imag*1j) where imag defaults to 0.");
12+
13+
static PyObject *
14+
complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i);
15+
16+
static PyObject *
17+
complex_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
18+
{
19+
PyObject *return_value = NULL;
20+
static const char * const _keywords[] = {"real", "imag", NULL};
21+
static _PyArg_Parser _parser = {"|OO:complex", _keywords, 0};
22+
PyObject *r = Py_False;
23+
PyObject *i = NULL;
24+
25+
if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
26+
&r, &i)) {
27+
goto exit;
28+
}
29+
return_value = complex_new_impl(type, r, i);
30+
31+
exit:
32+
return return_value;
33+
}
34+
/*[clinic end generated code: output=74035493480ab5e5 input=a9049054013a1b77]*/

Objects/clinic/descrobject.c.h

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*[clinic input]
2+
preserve
3+
[clinic start generated code]*/
4+
5+
static PyObject *
6+
mappingproxy_new_impl(PyTypeObject *type, PyObject *mapping);
7+
8+
static PyObject *
9+
mappingproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
10+
{
11+
PyObject *return_value = NULL;
12+
static const char * const _keywords[] = {"mapping", NULL};
13+
static _PyArg_Parser _parser = {"O:mappingproxy", _keywords, 0};
14+
PyObject *mapping;
15+
16+
if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
17+
&mapping)) {
18+
goto exit;
19+
}
20+
return_value = mappingproxy_new_impl(type, mapping);
21+
22+
exit:
23+
return return_value;
24+
}
25+
26+
PyDoc_STRVAR(property_init__doc__,
27+
"property(fget=None, fset=None, fdel=None, doc=None)\n"
28+
"--\n"
29+
"\n"
30+
"Property attribute.\n"
31+
"\n"
32+
" fget\n"
33+
" function to be used for getting an attribute value\n"
34+
" fset\n"
35+
" function to be used for setting an attribute value\n"
36+
" fdel\n"
37+
" function to be used for del\'ing an attribute\n"
38+
" doc\n"
39+
" docstring\n"
40+
"\n"
41+
"Typical use is to define a managed attribute x:\n"
42+
"\n"
43+
"class C(object):\n"
44+
" def getx(self): return self._x\n"
45+
" def setx(self, value): self._x = value\n"
46+
" def delx(self): del self._x\n"
47+
" x = property(getx, setx, delx, \"I\'m the \'x\' property.\")\n"
48+
"\n"
49+
"Decorators make defining new properties or modifying existing ones easy:\n"
50+
"\n"
51+
"class C(object):\n"
52+
" @property\n"
53+
" def x(self):\n"
54+
" \"I am the \'x\' property.\"\n"
55+
" return self._x\n"
56+
" @x.setter\n"
57+
" def x(self, value):\n"
58+
" self._x = value\n"
59+
" @x.deleter\n"
60+
" def x(self):\n"
61+
" del self._x");
62+
63+
static int
64+
property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset,
65+
PyObject *fdel, PyObject *doc);
66+
67+
static int
68+
property_init(PyObject *self, PyObject *args, PyObject *kwargs)
69+
{
70+
int return_value = -1;
71+
static const char * const _keywords[] = {"fget", "fset", "fdel", "doc", NULL};
72+
static _PyArg_Parser _parser = {"|OOOO:property", _keywords, 0};
73+
PyObject *fget = NULL;
74+
PyObject *fset = NULL;
75+
PyObject *fdel = NULL;
76+
PyObject *doc = NULL;
77+
78+
if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
79+
&fget, &fset, &fdel, &doc)) {
80+
goto exit;
81+
}
82+
return_value = property_init_impl((propertyobject *)self, fget, fset, fdel, doc);
83+
84+
exit:
85+
return return_value;
86+
}
87+
/*[clinic end generated code: output=729021fa9cdc46be input=a9049054013a1b77]*/

Objects/clinic/floatobject.c.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,36 @@ float_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored))
158158
return float_as_integer_ratio_impl(self);
159159
}
160160

161+
PyDoc_STRVAR(float_new__doc__,
162+
"float(x=0, /)\n"
163+
"--\n"
164+
"\n"
165+
"Convert a string or number to a floating point number, if possible.");
166+
167+
static PyObject *
168+
float_new_impl(PyTypeObject *type, PyObject *x);
169+
170+
static PyObject *
171+
float_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
172+
{
173+
PyObject *return_value = NULL;
174+
PyObject *x = Py_False;
175+
176+
if ((type == &PyFloat_Type) &&
177+
!_PyArg_NoKeywords("float", kwargs)) {
178+
goto exit;
179+
}
180+
if (!PyArg_UnpackTuple(args, "float",
181+
0, 1,
182+
&x)) {
183+
goto exit;
184+
}
185+
return_value = float_new_impl(type, x);
186+
187+
exit:
188+
return return_value;
189+
}
190+
161191
PyDoc_STRVAR(float___getnewargs____doc__,
162192
"__getnewargs__($self, /)\n"
163193
"--\n"
@@ -283,4 +313,4 @@ float___format__(PyObject *self, PyObject *arg)
283313
exit:
284314
return return_value;
285315
}
286-
/*[clinic end generated code: output=9257442b321d6a8b input=a9049054013a1b77]*/
316+
/*[clinic end generated code: output=a3dafb0f6c6f1514 input=a9049054013a1b77]*/

Objects/clinic/funcobject.c.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*[clinic input]
2+
preserve
3+
[clinic start generated code]*/
4+
5+
PyDoc_STRVAR(func_new__doc__,
6+
"function(code, globals, name=None, argdefs=None, closure=None)\n"
7+
"--\n"
8+
"\n"
9+
"Create a function object.\n"
10+
"\n"
11+
" code\n"
12+
" a code object\n"
13+
" globals\n"
14+
" the globals dictionary\n"
15+
" name\n"
16+
" a string that overrides the name from the code object\n"
17+
" argdefs\n"
18+
" a tuple that specifies the default argument values\n"
19+
" closure\n"
20+
" a tuple that supplies the bindings for free variables");
21+
22+
static PyObject *
23+
func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
24+
PyObject *name, PyObject *defaults, PyObject *closure);
25+
26+
static PyObject *
27+
func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
28+
{
29+
PyObject *return_value = NULL;
30+
static const char * const _keywords[] = {"code", "globals", "name", "argdefs", "closure", NULL};
31+
static _PyArg_Parser _parser = {"O!O!|OOO:function", _keywords, 0};
32+
PyCodeObject *code;
33+
PyObject *globals;
34+
PyObject *name = Py_None;
35+
PyObject *defaults = Py_None;
36+
PyObject *closure = Py_None;
37+
38+
if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
39+
&PyCode_Type, &code, &PyDict_Type, &globals, &name, &defaults, &closure)) {
40+
goto exit;
41+
}
42+
return_value = func_new_impl(type, code, globals, name, defaults, closure);
43+
44+
exit:
45+
return return_value;
46+
}
47+
/*[clinic end generated code: output=a6ab29e4dd33010a input=a9049054013a1b77]*/

Objects/clinic/longobject.c.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22
preserve
33
[clinic start generated code]*/
44

5+
static PyObject *
6+
long_new_impl(PyTypeObject *type, PyObject *x, PyObject *obase);
7+
8+
static PyObject *
9+
long_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
10+
{
11+
PyObject *return_value = NULL;
12+
static const char * const _keywords[] = {"", "base", NULL};
13+
static _PyArg_Parser _parser = {"|OO:int", _keywords, 0};
14+
PyObject *x = NULL;
15+
PyObject *obase = NULL;
16+
17+
if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
18+
&x, &obase)) {
19+
goto exit;
20+
}
21+
return_value = long_new_impl(type, x, obase);
22+
23+
exit:
24+
return return_value;
25+
}
26+
527
PyDoc_STRVAR(int___getnewargs____doc__,
628
"__getnewargs__($self, /)\n"
729
"--\n"
@@ -189,4 +211,4 @@ int_from_bytes(PyTypeObject *type, PyObject **args, Py_ssize_t nargs, PyObject *
189211
exit:
190212
return return_value;
191213
}
192-
/*[clinic end generated code: output=a9bae2fd016e7b85 input=a9049054013a1b77]*/
214+
/*[clinic end generated code: output=c1ce9c11929b0bab input=a9049054013a1b77]*/

Objects/clinic/moduleobject.c.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*[clinic input]
2+
preserve
3+
[clinic start generated code]*/
4+
5+
PyDoc_STRVAR(module___init____doc__,
6+
"module(name, doc=None)\n"
7+
"--\n"
8+
"\n"
9+
"Create a module object.\n"
10+
"\n"
11+
"The name must be a string; the optional doc argument can have any type.");
12+
13+
static int
14+
module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc);
15+
16+
static int
17+
module___init__(PyObject *self, PyObject *args, PyObject *kwargs)
18+
{
19+
int return_value = -1;
20+
static const char * const _keywords[] = {"name", "doc", NULL};
21+
static _PyArg_Parser _parser = {"U|O:module", _keywords, 0};
22+
PyObject *name;
23+
PyObject *doc = Py_None;
24+
25+
if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
26+
&name, &doc)) {
27+
goto exit;
28+
}
29+
return_value = module___init___impl((PyModuleObject *)self, name, doc);
30+
31+
exit:
32+
return return_value;
33+
}
34+
/*[clinic end generated code: output=7b1b324bf6a590d1 input=a9049054013a1b77]*/

Objects/clinic/structseq.c.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*[clinic input]
2+
preserve
3+
[clinic start generated code]*/
4+
5+
static PyObject *
6+
structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict);
7+
8+
static PyObject *
9+
structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
10+
{
11+
PyObject *return_value = NULL;
12+
static const char * const _keywords[] = {"sequence", "dict", NULL};
13+
static _PyArg_Parser _parser = {"O|O:structseq", _keywords, 0};
14+
PyObject *arg;
15+
PyObject *dict = NULL;
16+
17+
if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
18+
&arg, &dict)) {
19+
goto exit;
20+
}
21+
return_value = structseq_new_impl(type, arg, dict);
22+
23+
exit:
24+
return return_value;
25+
}
26+
/*[clinic end generated code: output=cd643eb89b5d312a input=a9049054013a1b77]*/

Objects/complexobject.c

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
#include "Python.h"
99
#include "structmember.h"
1010

11+
/*[clinic input]
12+
class complex "PyComplexObject *" "&PyComplex_Type"
13+
[clinic start generated code]*/
14+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=819e057d2d10f5ec]*/
15+
16+
#include "clinic/complexobject.c.h"
17+
1118
/* elementary operations on complex numbers */
1219

1320
static Py_complex c_1 = {1., 0.};
@@ -912,22 +919,27 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
912919
return result;
913920
}
914921

922+
/*[clinic input]
923+
@classmethod
924+
complex.__new__ as complex_new
925+
real as r: object(c_default="Py_False") = 0
926+
imag as i: object(c_default="NULL") = 0
927+
928+
Create a complex number from a real part and an optional imaginary part.
929+
930+
This is equivalent to (real + imag*1j) where imag defaults to 0.
931+
[clinic start generated code]*/
932+
915933
static PyObject *
916-
complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
934+
complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
935+
/*[clinic end generated code: output=b6c7dd577b537dc1 input=e3d6b77ddcf280da]*/
917936
{
918-
PyObject *r, *i, *tmp;
937+
PyObject *tmp;
919938
PyNumberMethods *nbr, *nbi = NULL;
920939
Py_complex cr, ci;
921940
int own_r = 0;
922941
int cr_is_complex = 0;
923942
int ci_is_complex = 0;
924-
static char *kwlist[] = {"real", "imag", 0};
925-
926-
r = Py_False;
927-
i = NULL;
928-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist,
929-
&r, &i))
930-
return NULL;
931943

932944
/* Special-case for a single argument when type(arg) is complex. */
933945
if (PyComplex_CheckExact(r) && i == NULL &&
@@ -1057,12 +1069,6 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
10571069
return complex_subtype_from_doubles(type, cr.real, ci.real);
10581070
}
10591071

1060-
PyDoc_STRVAR(complex_doc,
1061-
"complex(real[, imag]) -> complex number\n"
1062-
"\n"
1063-
"Create a complex number from a real part and an optional imaginary part.\n"
1064-
"This is equivalent to (real + imag*1j) where imag defaults to 0.");
1065-
10661072
static PyNumberMethods complex_as_number = {
10671073
(binaryfunc)complex_add, /* nb_add */
10681074
(binaryfunc)complex_sub, /* nb_subtract */
@@ -1119,8 +1125,8 @@ PyTypeObject PyComplex_Type = {
11191125
PyObject_GenericGetAttr, /* tp_getattro */
11201126
0, /* tp_setattro */
11211127
0, /* tp_as_buffer */
1122-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1123-
complex_doc, /* tp_doc */
1128+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1129+
complex_new__doc__, /* tp_doc */
11241130
0, /* tp_traverse */
11251131
0, /* tp_clear */
11261132
complex_richcompare, /* tp_richcompare */

0 commit comments

Comments
 (0)