diff -r 9a98ff4a2290 Objects/floatobject.c
--- a/Objects/floatobject.c Wed Jan 22 05:49:11 2014 -0800
+++ b/Objects/floatobject.c Thu Jan 23 15:34:54 2014 +0800
@@ -9,6 +9,10 @@
#include
#include
+/*[clinic input]
+class float
+[clinic start generated code]*/
+/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
/* Special free list
free_list is a singly-linked list of available PyFloatObjects, linked
@@ -1622,18 +1626,72 @@
"'unknown', 'IEEE, big-endian' or 'IEEE, little-endian' best describes the\n"
"format of floating point numbers used by the C type named by typestr.");
+/*[clinic input]
+float.__set_format__
+
+ self: self(type="PyTypeObject *")
+ typestr: str
+ fmt: str
+ /
+
+float.__setformat__(typestr, fmt) -> None
+
+You probably don't want to use this function. It exists mainly to be
+used in Python's test suite.
+
+typestr must be 'double' or 'float'. fmt must be one of 'unknown',
+'IEEE, big-endian' or 'IEEE, little-endian', and in addition can only be
+one of the latter two if it appears to match the underlying C reality.
+
+Override the automatic determination of C-level floating point type.
+This affects how floats are converted to and from binary strings.
+[clinic start generated code]*/
+
+PyDoc_STRVAR(float___set_format____doc__,
+"__set_format__(typestr, fmt)\n"
+"float.__setformat__(typestr, fmt) -> None\n"
+"\n"
+"You probably don\'t want to use this function. It exists mainly to be\n"
+"used in Python\'s test suite.\n"
+"\n"
+"typestr must be \'double\' or \'float\'. fmt must be one of \'unknown\',\n"
+"\'IEEE, big-endian\' or \'IEEE, little-endian\', and in addition can only be\n"
+"one of the latter two if it appears to match the underlying C reality.\n"
+"\n"
+"Override the automatic determination of C-level floating point type.\n"
+"This affects how floats are converted to and from binary strings.");
+
+#define FLOAT___SET_FORMAT___METHODDEF \
+ {"__set_format__", (PyCFunction)float___set_format__, METH_VARARGS, float___set_format____doc__},
+
static PyObject *
-float_setformat(PyTypeObject *v, PyObject* args)
+float___set_format___impl(PyTypeObject *self, const char *typestr, const char *fmt);
+
+static PyObject *
+float___set_format__(PyObject *self, PyObject *args)
{
- char* typestr;
- char* format;
+ PyObject *return_value = NULL;
+ const char *typestr;
+ const char *fmt;
+
+ if (!PyArg_ParseTuple(args,
+ "ss:__set_format__",
+ &typestr, &fmt))
+ goto exit;
+ return_value = float___set_format___impl((PyTypeObject *)self, typestr, fmt);
+
+exit:
+ return return_value;
+}
+
+static PyObject *
+float___set_format___impl(PyTypeObject *self, const char *typestr, const char *fmt)
+/*[clinic end generated code: checksum=910f0933f989cdb4cd316b7dca6e2afa3b939202]*/
+{
float_format_type f;
float_format_type detected;
float_format_type *p;
- if (!PyArg_ParseTuple(args, "ss:__setformat__", &typestr, &format))
- return NULL;
-
if (strcmp(typestr, "double") == 0) {
p = &double_format;
detected = detected_double_format;
@@ -1649,13 +1707,13 @@
return NULL;
}
- if (strcmp(format, "unknown") == 0) {
+ if (strcmp(fmt, "unknown") == 0) {
f = unknown_format;
}
- else if (strcmp(format, "IEEE, little-endian") == 0) {
+ else if (strcmp(fmt, "IEEE, little-endian") == 0) {
f = ieee_little_endian_format;
}
- else if (strcmp(format, "IEEE, big-endian") == 0) {
+ else if (strcmp(fmt, "IEEE, big-endian") == 0) {
f = ieee_big_endian_format;
}
else {
@@ -1678,35 +1736,58 @@
Py_RETURN_NONE;
}
-PyDoc_STRVAR(float_setformat_doc,
-"float.__setformat__(typestr, fmt) -> None\n"
-"\n"
-"You probably don't want to use this function. It exists mainly to be\n"
-"used in Python's test suite.\n"
-"\n"
-"typestr must be 'double' or 'float'. fmt must be one of 'unknown',\n"
-"'IEEE, big-endian' or 'IEEE, little-endian', and in addition can only be\n"
-"one of the latter two if it appears to match the underlying C reality.\n"
-"\n"
-"Override the automatic determination of C-level floating point type.\n"
-"This affects how floats are converted to and from binary strings.");
-
static PyObject *
float_getzero(PyObject *v, void *closure)
{
return PyFloat_FromDouble(0.0);
}
+/*[clinic input]
+float.__format__
+
+ format_spec: unicode
+ /
+
+float.__format__(format_spec) -> string
+
+Formats the float according to format_spec.
+[clinic start generated code]*/
+
+PyDoc_STRVAR(float___format____doc__,
+"__format__(format_spec)\n"
+"float.__format__(format_spec) -> string\n"
+"\n"
+"Formats the float according to format_spec.");
+
+#define FLOAT___FORMAT___METHODDEF \
+ {"__format__", (PyCFunction)float___format__, METH_VARARGS, float___format____doc__},
+
static PyObject *
-float__format__(PyObject *self, PyObject *args)
+float___format___impl(PyObject *self, PyObject *format_spec);
+
+static PyObject *
+float___format__(PyObject *self, PyObject *args)
{
+ PyObject *return_value = NULL;
PyObject *format_spec;
+
+ if (!PyArg_ParseTuple(args,
+ "U:__format__",
+ &format_spec))
+ goto exit;
+ return_value = float___format___impl(self, format_spec);
+
+exit:
+ return return_value;
+}
+
+static PyObject *
+float___format___impl(PyObject *self, PyObject *format_spec)
+/*[clinic end generated code: checksum=d23ad373283d316eff645a41636578616cd380e4]*/
+{
_PyUnicodeWriter writer;
int ret;
- if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
- return NULL;
-
_PyUnicodeWriter_Init(&writer);
ret = _PyFloat_FormatAdvancedWriter(
&writer,
@@ -1719,12 +1800,6 @@
return _PyUnicodeWriter_Finish(&writer);
}
-PyDoc_STRVAR(float__format__doc,
-"float.__format__(format_spec) -> string\n"
-"\n"
-"Formats the float according to format_spec.");
-
-
static PyMethodDef float_methods[] = {
{"conjugate", (PyCFunction)float_float, METH_NOARGS,
"Return self, the complex conjugate of any float."},
@@ -1752,10 +1827,8 @@
{"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS},
{"__getformat__", (PyCFunction)float_getformat,
METH_O|METH_CLASS, float_getformat_doc},
- {"__setformat__", (PyCFunction)float_setformat,
- METH_VARARGS|METH_CLASS, float_setformat_doc},
- {"__format__", (PyCFunction)float__format__,
- METH_VARARGS, float__format__doc},
+ FLOAT___SET_FORMAT___METHODDEF
+ FLOAT___FORMAT___METHODDEF
{NULL, NULL} /* sentinel */
};