Skip to content

bpo-29793: Convert some builtin types constructors to Argument Clinic. #615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Objects/clinic/complexobject.c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*[clinic input]
preserve
[clinic start generated code]*/

PyDoc_STRVAR(complex_new__doc__,
"complex(real=0, imag=0)\n"
"--\n"
"\n"
"Create a complex number from a real part and an optional imaginary part.\n"
"\n"
"This is equivalent to (real + imag*1j) where imag defaults to 0.");

static PyObject *
complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i);

static PyObject *
complex_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
static const char * const _keywords[] = {"real", "imag", NULL};
static _PyArg_Parser _parser = {"|OO:complex", _keywords, 0};
PyObject *r = Py_False;
PyObject *i = NULL;

if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
&r, &i)) {
goto exit;
}
return_value = complex_new_impl(type, r, i);

exit:
return return_value;
}
/*[clinic end generated code: output=74035493480ab5e5 input=a9049054013a1b77]*/
87 changes: 87 additions & 0 deletions Objects/clinic/descrobject.c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*[clinic input]
preserve
[clinic start generated code]*/

static PyObject *
mappingproxy_new_impl(PyTypeObject *type, PyObject *mapping);

static PyObject *
mappingproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
static const char * const _keywords[] = {"mapping", NULL};
static _PyArg_Parser _parser = {"O:mappingproxy", _keywords, 0};
PyObject *mapping;

if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
&mapping)) {
goto exit;
}
return_value = mappingproxy_new_impl(type, mapping);

exit:
return return_value;
}

PyDoc_STRVAR(property_init__doc__,
"property(fget=None, fset=None, fdel=None, doc=None)\n"
"--\n"
"\n"
"Property attribute.\n"
"\n"
" fget\n"
" function to be used for getting an attribute value\n"
" fset\n"
" function to be used for setting an attribute value\n"
" fdel\n"
" function to be used for del\'ing an attribute\n"
" doc\n"
" docstring\n"
"\n"
"Typical use is to define a managed attribute x:\n"
"\n"
"class C(object):\n"
" def getx(self): return self._x\n"
" def setx(self, value): self._x = value\n"
" def delx(self): del self._x\n"
" x = property(getx, setx, delx, \"I\'m the \'x\' property.\")\n"
"\n"
"Decorators make defining new properties or modifying existing ones easy:\n"
"\n"
"class C(object):\n"
" @property\n"
" def x(self):\n"
" \"I am the \'x\' property.\"\n"
" return self._x\n"
" @x.setter\n"
" def x(self, value):\n"
" self._x = value\n"
" @x.deleter\n"
" def x(self):\n"
" del self._x");

static int
property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset,
PyObject *fdel, PyObject *doc);

static int
property_init(PyObject *self, PyObject *args, PyObject *kwargs)
{
int return_value = -1;
static const char * const _keywords[] = {"fget", "fset", "fdel", "doc", NULL};
static _PyArg_Parser _parser = {"|OOOO:property", _keywords, 0};
PyObject *fget = NULL;
PyObject *fset = NULL;
PyObject *fdel = NULL;
PyObject *doc = NULL;

if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
&fget, &fset, &fdel, &doc)) {
goto exit;
}
return_value = property_init_impl((propertyobject *)self, fget, fset, fdel, doc);

exit:
return return_value;
}
/*[clinic end generated code: output=729021fa9cdc46be input=a9049054013a1b77]*/
32 changes: 31 additions & 1 deletion Objects/clinic/floatobject.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,36 @@ float_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored))
return float_as_integer_ratio_impl(self);
}

PyDoc_STRVAR(float_new__doc__,
"float(x=0, /)\n"
"--\n"
"\n"
"Convert a string or number to a floating point number, if possible.");

static PyObject *
float_new_impl(PyTypeObject *type, PyObject *x);

static PyObject *
float_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyObject *x = Py_False;

if ((type == &PyFloat_Type) &&
!_PyArg_NoKeywords("float", kwargs)) {
goto exit;
}
if (!PyArg_UnpackTuple(args, "float",
0, 1,
&x)) {
goto exit;
}
return_value = float_new_impl(type, x);

exit:
return return_value;
}

PyDoc_STRVAR(float___getnewargs____doc__,
"__getnewargs__($self, /)\n"
"--\n"
Expand Down Expand Up @@ -283,4 +313,4 @@ float___format__(PyObject *self, PyObject *arg)
exit:
return return_value;
}
/*[clinic end generated code: output=9257442b321d6a8b input=a9049054013a1b77]*/
/*[clinic end generated code: output=a3dafb0f6c6f1514 input=a9049054013a1b77]*/
47 changes: 47 additions & 0 deletions Objects/clinic/funcobject.c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*[clinic input]
preserve
[clinic start generated code]*/

PyDoc_STRVAR(func_new__doc__,
"function(code, globals, name=None, argdefs=None, closure=None)\n"
"--\n"
"\n"
"Create a function object.\n"
"\n"
" code\n"
" a code object\n"
" globals\n"
" the globals dictionary\n"
" name\n"
" a string that overrides the name from the code object\n"
" argdefs\n"
" a tuple that specifies the default argument values\n"
" closure\n"
" a tuple that supplies the bindings for free variables");

static PyObject *
func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
PyObject *name, PyObject *defaults, PyObject *closure);

static PyObject *
func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
static const char * const _keywords[] = {"code", "globals", "name", "argdefs", "closure", NULL};
static _PyArg_Parser _parser = {"O!O!|OOO:function", _keywords, 0};
PyCodeObject *code;
PyObject *globals;
PyObject *name = Py_None;
PyObject *defaults = Py_None;
PyObject *closure = Py_None;

if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
&PyCode_Type, &code, &PyDict_Type, &globals, &name, &defaults, &closure)) {
goto exit;
}
return_value = func_new_impl(type, code, globals, name, defaults, closure);

exit:
return return_value;
}
/*[clinic end generated code: output=a6ab29e4dd33010a input=a9049054013a1b77]*/
24 changes: 23 additions & 1 deletion Objects/clinic/longobject.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@
preserve
[clinic start generated code]*/

static PyObject *
long_new_impl(PyTypeObject *type, PyObject *x, PyObject *obase);

static PyObject *
long_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
static const char * const _keywords[] = {"", "base", NULL};
static _PyArg_Parser _parser = {"|OO:int", _keywords, 0};
PyObject *x = NULL;
PyObject *obase = NULL;

if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
&x, &obase)) {
goto exit;
}
return_value = long_new_impl(type, x, obase);

exit:
return return_value;
}

PyDoc_STRVAR(int___getnewargs____doc__,
"__getnewargs__($self, /)\n"
"--\n"
Expand Down Expand Up @@ -189,4 +211,4 @@ int_from_bytes(PyTypeObject *type, PyObject **args, Py_ssize_t nargs, PyObject *
exit:
return return_value;
}
/*[clinic end generated code: output=a9bae2fd016e7b85 input=a9049054013a1b77]*/
/*[clinic end generated code: output=c1ce9c11929b0bab input=a9049054013a1b77]*/
34 changes: 34 additions & 0 deletions Objects/clinic/moduleobject.c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*[clinic input]
preserve
[clinic start generated code]*/

PyDoc_STRVAR(module___init____doc__,
"module(name, doc=None)\n"
"--\n"
"\n"
"Create a module object.\n"
"\n"
"The name must be a string; the optional doc argument can have any type.");

static int
module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc);

static int
module___init__(PyObject *self, PyObject *args, PyObject *kwargs)
{
int return_value = -1;
static const char * const _keywords[] = {"name", "doc", NULL};
static _PyArg_Parser _parser = {"U|O:module", _keywords, 0};
PyObject *name;
PyObject *doc = Py_None;

if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
&name, &doc)) {
goto exit;
}
return_value = module___init___impl((PyModuleObject *)self, name, doc);

exit:
return return_value;
}
/*[clinic end generated code: output=7b1b324bf6a590d1 input=a9049054013a1b77]*/
26 changes: 26 additions & 0 deletions Objects/clinic/structseq.c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*[clinic input]
preserve
[clinic start generated code]*/

static PyObject *
structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict);

static PyObject *
structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
static const char * const _keywords[] = {"sequence", "dict", NULL};
static _PyArg_Parser _parser = {"O|O:structseq", _keywords, 0};
PyObject *arg;
PyObject *dict = NULL;

if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
&arg, &dict)) {
goto exit;
}
return_value = structseq_new_impl(type, arg, dict);

exit:
return return_value;
}
/*[clinic end generated code: output=cd643eb89b5d312a input=a9049054013a1b77]*/
40 changes: 23 additions & 17 deletions Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
#include "Python.h"
#include "structmember.h"

/*[clinic input]
class complex "PyComplexObject *" "&PyComplex_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=819e057d2d10f5ec]*/

#include "clinic/complexobject.c.h"

/* elementary operations on complex numbers */

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

/*[clinic input]
@classmethod
complex.__new__ as complex_new
real as r: object(c_default="Py_False") = 0
imag as i: object(c_default="NULL") = 0

Create a complex number from a real part and an optional imaginary part.

This is equivalent to (real + imag*1j) where imag defaults to 0.
[clinic start generated code]*/

static PyObject *
complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
/*[clinic end generated code: output=b6c7dd577b537dc1 input=e3d6b77ddcf280da]*/
{
PyObject *r, *i, *tmp;
PyObject *tmp;
PyNumberMethods *nbr, *nbi = NULL;
Py_complex cr, ci;
int own_r = 0;
int cr_is_complex = 0;
int ci_is_complex = 0;
static char *kwlist[] = {"real", "imag", 0};

r = Py_False;
i = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist,
&r, &i))
return NULL;

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

PyDoc_STRVAR(complex_doc,
"complex(real[, imag]) -> complex number\n"
"\n"
"Create a complex number from a real part and an optional imaginary part.\n"
"This is equivalent to (real + imag*1j) where imag defaults to 0.");

static PyNumberMethods complex_as_number = {
(binaryfunc)complex_add, /* nb_add */
(binaryfunc)complex_sub, /* nb_subtract */
Expand Down Expand Up @@ -1119,8 +1125,8 @@ PyTypeObject PyComplex_Type = {
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
complex_doc, /* tp_doc */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
complex_new__doc__, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
complex_richcompare, /* tp_richcompare */
Expand Down
Loading