Skip to content

Commit b5c51d3

Browse files
bpo-20185: Convert float object implementation to Argument Clinic. (#543)
Based on patch by Vajrasky Kok.
1 parent fdd42c4 commit b5c51d3

File tree

3 files changed

+499
-157
lines changed

3 files changed

+499
-157
lines changed

Objects/clinic/floatobject.c.h

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
/*[clinic input]
2+
preserve
3+
[clinic start generated code]*/
4+
5+
PyDoc_STRVAR(float_is_integer__doc__,
6+
"is_integer($self, /)\n"
7+
"--\n"
8+
"\n"
9+
"Return True if the float is an integer.");
10+
11+
#define FLOAT_IS_INTEGER_METHODDEF \
12+
{"is_integer", (PyCFunction)float_is_integer, METH_NOARGS, float_is_integer__doc__},
13+
14+
static PyObject *
15+
float_is_integer_impl(PyObject *self);
16+
17+
static PyObject *
18+
float_is_integer(PyObject *self, PyObject *Py_UNUSED(ignored))
19+
{
20+
return float_is_integer_impl(self);
21+
}
22+
23+
PyDoc_STRVAR(float___trunc____doc__,
24+
"__trunc__($self, /)\n"
25+
"--\n"
26+
"\n"
27+
"Return the Integral closest to x between 0 and x.");
28+
29+
#define FLOAT___TRUNC___METHODDEF \
30+
{"__trunc__", (PyCFunction)float___trunc__, METH_NOARGS, float___trunc____doc__},
31+
32+
static PyObject *
33+
float___trunc___impl(PyObject *self);
34+
35+
static PyObject *
36+
float___trunc__(PyObject *self, PyObject *Py_UNUSED(ignored))
37+
{
38+
return float___trunc___impl(self);
39+
}
40+
41+
PyDoc_STRVAR(float___round____doc__,
42+
"__round__($self, ndigits=None, /)\n"
43+
"--\n"
44+
"\n"
45+
"Return the Integral closest to x, rounding half toward even.\n"
46+
"\n"
47+
"When an argument is passed, work like built-in round(x, ndigits).");
48+
49+
#define FLOAT___ROUND___METHODDEF \
50+
{"__round__", (PyCFunction)float___round__, METH_FASTCALL, float___round____doc__},
51+
52+
static PyObject *
53+
float___round___impl(PyObject *self, PyObject *o_ndigits);
54+
55+
static PyObject *
56+
float___round__(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
57+
{
58+
PyObject *return_value = NULL;
59+
PyObject *o_ndigits = NULL;
60+
61+
if (!_PyArg_UnpackStack(args, nargs, "__round__",
62+
0, 1,
63+
&o_ndigits)) {
64+
goto exit;
65+
}
66+
67+
if (!_PyArg_NoStackKeywords("__round__", kwnames)) {
68+
goto exit;
69+
}
70+
return_value = float___round___impl(self, o_ndigits);
71+
72+
exit:
73+
return return_value;
74+
}
75+
76+
PyDoc_STRVAR(float_conjugate__doc__,
77+
"conjugate($self, /)\n"
78+
"--\n"
79+
"\n"
80+
"Return self, the complex conjugate of any float.");
81+
82+
#define FLOAT_CONJUGATE_METHODDEF \
83+
{"conjugate", (PyCFunction)float_conjugate, METH_NOARGS, float_conjugate__doc__},
84+
85+
static PyObject *
86+
float_conjugate_impl(PyObject *self);
87+
88+
static PyObject *
89+
float_conjugate(PyObject *self, PyObject *Py_UNUSED(ignored))
90+
{
91+
return float_conjugate_impl(self);
92+
}
93+
94+
PyDoc_STRVAR(float_hex__doc__,
95+
"hex($self, /)\n"
96+
"--\n"
97+
"\n"
98+
"Return a hexadecimal representation of a floating-point number.\n"
99+
"\n"
100+
">>> (-0.1).hex()\n"
101+
"\'-0x1.999999999999ap-4\'\n"
102+
">>> 3.14159.hex()\n"
103+
"\'0x1.921f9f01b866ep+1\'");
104+
105+
#define FLOAT_HEX_METHODDEF \
106+
{"hex", (PyCFunction)float_hex, METH_NOARGS, float_hex__doc__},
107+
108+
static PyObject *
109+
float_hex_impl(PyObject *self);
110+
111+
static PyObject *
112+
float_hex(PyObject *self, PyObject *Py_UNUSED(ignored))
113+
{
114+
return float_hex_impl(self);
115+
}
116+
117+
PyDoc_STRVAR(float_fromhex__doc__,
118+
"fromhex($type, string, /)\n"
119+
"--\n"
120+
"\n"
121+
"Create a floating-point number from a hexadecimal string.\n"
122+
"\n"
123+
">>> float.fromhex(\'0x1.ffffp10\')\n"
124+
"2047.984375\n"
125+
">>> float.fromhex(\'-0x1p-1074\')\n"
126+
"-5e-324");
127+
128+
#define FLOAT_FROMHEX_METHODDEF \
129+
{"fromhex", (PyCFunction)float_fromhex, METH_O|METH_CLASS, float_fromhex__doc__},
130+
131+
PyDoc_STRVAR(float_as_integer_ratio__doc__,
132+
"as_integer_ratio($self, /)\n"
133+
"--\n"
134+
"\n"
135+
"Return integer ratio.\n"
136+
"\n"
137+
"Return a pair of integers, whose ratio is exactly equal to the original float\n"
138+
"and with a positive denominator.\n"
139+
"\n"
140+
"Raise OverflowError on infinities and a ValueError on NaNs.\n"
141+
"\n"
142+
">>> (10.0).as_integer_ratio()\n"
143+
"(10, 1)\n"
144+
">>> (0.0).as_integer_ratio()\n"
145+
"(0, 1)\n"
146+
">>> (-.25).as_integer_ratio()\n"
147+
"(-1, 4)");
148+
149+
#define FLOAT_AS_INTEGER_RATIO_METHODDEF \
150+
{"as_integer_ratio", (PyCFunction)float_as_integer_ratio, METH_NOARGS, float_as_integer_ratio__doc__},
151+
152+
static PyObject *
153+
float_as_integer_ratio_impl(PyObject *self);
154+
155+
static PyObject *
156+
float_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored))
157+
{
158+
return float_as_integer_ratio_impl(self);
159+
}
160+
161+
PyDoc_STRVAR(float___getnewargs____doc__,
162+
"__getnewargs__($self, /)\n"
163+
"--\n"
164+
"\n");
165+
166+
#define FLOAT___GETNEWARGS___METHODDEF \
167+
{"__getnewargs__", (PyCFunction)float___getnewargs__, METH_NOARGS, float___getnewargs____doc__},
168+
169+
static PyObject *
170+
float___getnewargs___impl(PyObject *self);
171+
172+
static PyObject *
173+
float___getnewargs__(PyObject *self, PyObject *Py_UNUSED(ignored))
174+
{
175+
return float___getnewargs___impl(self);
176+
}
177+
178+
PyDoc_STRVAR(float___getformat____doc__,
179+
"__getformat__($type, typestr, /)\n"
180+
"--\n"
181+
"\n"
182+
"You probably don\'t want to use this function.\n"
183+
"\n"
184+
" typestr\n"
185+
" Must be \'double\' or \'float\'.\n"
186+
"\n"
187+
"It exists mainly to be used in Python\'s test suite.\n"
188+
"\n"
189+
"This function returns whichever of \'unknown\', \'IEEE, big-endian\' or \'IEEE,\n"
190+
"little-endian\' best describes the format of floating point numbers used by the\n"
191+
"C type named by typestr.");
192+
193+
#define FLOAT___GETFORMAT___METHODDEF \
194+
{"__getformat__", (PyCFunction)float___getformat__, METH_O|METH_CLASS, float___getformat____doc__},
195+
196+
static PyObject *
197+
float___getformat___impl(PyTypeObject *type, const char *typestr);
198+
199+
static PyObject *
200+
float___getformat__(PyTypeObject *type, PyObject *arg)
201+
{
202+
PyObject *return_value = NULL;
203+
const char *typestr;
204+
205+
if (!PyArg_Parse(arg, "s:__getformat__", &typestr)) {
206+
goto exit;
207+
}
208+
return_value = float___getformat___impl(type, typestr);
209+
210+
exit:
211+
return return_value;
212+
}
213+
214+
PyDoc_STRVAR(float___set_format____doc__,
215+
"__set_format__($type, typestr, fmt, /)\n"
216+
"--\n"
217+
"\n"
218+
"You probably don\'t want to use this function.\n"
219+
"\n"
220+
" typestr\n"
221+
" Must be \'double\' or \'float\'.\n"
222+
" fmt\n"
223+
" Must be one of \'unknown\', \'IEEE, big-endian\' or \'IEEE, little-endian\',\n"
224+
" and in addition can only be one of the latter two if it appears to\n"
225+
" match the underlying C reality.\n"
226+
"\n"
227+
"It exists mainly to be used in Python\'s test suite.\n"
228+
"\n"
229+
"Override the automatic determination of C-level floating point type.\n"
230+
"This affects how floats are converted to and from binary strings.");
231+
232+
#define FLOAT___SET_FORMAT___METHODDEF \
233+
{"__set_format__", (PyCFunction)float___set_format__, METH_FASTCALL|METH_CLASS, float___set_format____doc__},
234+
235+
static PyObject *
236+
float___set_format___impl(PyTypeObject *type, const char *typestr,
237+
const char *fmt);
238+
239+
static PyObject *
240+
float___set_format__(PyTypeObject *type, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
241+
{
242+
PyObject *return_value = NULL;
243+
const char *typestr;
244+
const char *fmt;
245+
246+
if (!_PyArg_ParseStack(args, nargs, "ss:__set_format__",
247+
&typestr, &fmt)) {
248+
goto exit;
249+
}
250+
251+
if (!_PyArg_NoStackKeywords("__set_format__", kwnames)) {
252+
goto exit;
253+
}
254+
return_value = float___set_format___impl(type, typestr, fmt);
255+
256+
exit:
257+
return return_value;
258+
}
259+
260+
PyDoc_STRVAR(float___format____doc__,
261+
"__format__($self, format_spec, /)\n"
262+
"--\n"
263+
"\n"
264+
"Formats the float according to format_spec.");
265+
266+
#define FLOAT___FORMAT___METHODDEF \
267+
{"__format__", (PyCFunction)float___format__, METH_O, float___format____doc__},
268+
269+
static PyObject *
270+
float___format___impl(PyObject *self, PyObject *format_spec);
271+
272+
static PyObject *
273+
float___format__(PyObject *self, PyObject *arg)
274+
{
275+
PyObject *return_value = NULL;
276+
PyObject *format_spec;
277+
278+
if (!PyArg_Parse(arg, "U:__format__", &format_spec)) {
279+
goto exit;
280+
}
281+
return_value = float___format___impl(self, format_spec);
282+
283+
exit:
284+
return return_value;
285+
}
286+
/*[clinic end generated code: output=9257442b321d6a8b input=a9049054013a1b77]*/

0 commit comments

Comments
 (0)