summaryrefslogtreecommitdiff
path: root/Objects/floatobject.c
diff options
context:
space:
mode:
Diffstat (limited to 'Objects/floatobject.c')
-rw-r--r--Objects/floatobject.c46
1 files changed, 26 insertions, 20 deletions
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index a08a85272c..1dca947906 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -9,11 +9,6 @@
#include <ctype.h>
#include <float.h>
-#undef MAX
-#undef MIN
-#define MAX(x, y) ((x) < (y) ? (y) : (x))
-#define MIN(x, y) ((x) < (y) ? (x) : (y))
-
/* Special free list
free_list is a singly-linked list of available PyFloatObjects, linked
@@ -114,7 +109,7 @@ PyFloat_GetInfo(void)
PyObject *
PyFloat_FromDouble(double fval)
{
- register PyFloatObject *op = free_list;
+ PyFloatObject *op = free_list;
if (op != NULL) {
free_list = (PyFloatObject *) Py_TYPE(op);
numfree--;
@@ -124,7 +119,7 @@ PyFloat_FromDouble(double fval)
return PyErr_NoMemory();
}
/* Inline PyObject_New */
- PyObject_INIT(op, &PyFloat_Type);
+ (void)PyObject_INIT(op, &PyFloat_Type);
op->ob_fval = fval;
return (PyObject *) op;
}
@@ -136,6 +131,7 @@ PyFloat_FromString(PyObject *v)
double x;
PyObject *s_buffer = NULL;
Py_ssize_t len;
+ Py_buffer view = {NULL, NULL};
PyObject *result = NULL;
if (PyUnicode_Check(v)) {
@@ -148,9 +144,14 @@ PyFloat_FromString(PyObject *v)
return NULL;
}
}
- else if (PyObject_AsCharBuffer(v, &s, &len)) {
- PyErr_SetString(PyExc_TypeError,
- "float() argument must be a string or a number");
+ else if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) == 0) {
+ s = (const char *)view.buf;
+ len = view.len;
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "float() argument must be a string or a number, not '%.200s'",
+ Py_TYPE(v)->tp_name);
return NULL;
}
last = s + len;
@@ -174,6 +175,7 @@ PyFloat_FromString(PyObject *v)
else
result = PyFloat_FromDouble(x);
+ PyBuffer_Release(&view);
Py_XDECREF(s_buffer);
return result;
}
@@ -218,6 +220,7 @@ PyFloat_AsDouble(PyObject *op)
if (fo == NULL)
return -1;
if (!PyFloat_Check(fo)) {
+ Py_DECREF(fo);
PyErr_SetString(PyExc_TypeError,
"nb_float should return float object");
return -1;
@@ -246,7 +249,7 @@ PyFloat_AsDouble(PyObject *op)
static int
convert_to_double(PyObject **v, double *dbl)
{
- register PyObject *obj = *v;
+ PyObject *obj = *v;
if (PyLong_Check(obj)) {
*dbl = PyLong_AsDouble(obj);
@@ -1131,7 +1134,7 @@ float_hex(PyObject *v)
}
m = frexp(fabs(x), &e);
- shift = 1 - MAX(DBL_MIN_EXP - e, 0);
+ shift = 1 - Py_MAX(DBL_MIN_EXP - e, 0);
m = ldexp(m, shift);
e -= shift;
@@ -1285,8 +1288,8 @@ float_fromhex(PyObject *cls, PyObject *arg)
fdigits = coeff_end - s_store;
if (ndigits == 0)
goto parse_error;
- if (ndigits > MIN(DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2,
- LONG_MAX/2 + 1 - DBL_MAX_EXP)/4)
+ if (ndigits > Py_MIN(DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2,
+ LONG_MAX/2 + 1 - DBL_MAX_EXP)/4)
goto insane_length_error;
/* [p <exponent>] */
@@ -1342,7 +1345,7 @@ float_fromhex(PyObject *cls, PyObject *arg)
/* lsb = exponent of least significant bit of the *rounded* value.
This is top_exp - DBL_MANT_DIG unless result is subnormal. */
- lsb = MAX(top_exp, (long)DBL_MIN_EXP) - DBL_MANT_DIG;
+ lsb = Py_MAX(top_exp, (long)DBL_MIN_EXP) - DBL_MANT_DIG;
x = 0.0;
if (exp >= lsb) {
@@ -1421,7 +1424,7 @@ Create a floating-point number from a hexadecimal string.\n\
>>> float.fromhex('0x1.ffffp10')\n\
2047.984375\n\
>>> float.fromhex('-0x1p-1074')\n\
--4.9406564584124654e-324");
+-5e-324");
static PyObject *
@@ -1711,7 +1714,7 @@ float__format__(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
return NULL;
- _PyUnicodeWriter_Init(&writer, 0);
+ _PyUnicodeWriter_Init(&writer);
ret = _PyFloat_FormatAdvancedWriter(
&writer,
self,
@@ -1858,7 +1861,7 @@ PyTypeObject PyFloat_Type = {
float_new, /* tp_new */
};
-void
+int
_PyFloat_Init(void)
{
/* We attempt to determine if this machine is using IEEE
@@ -1908,8 +1911,11 @@ _PyFloat_Init(void)
float_format = detected_float_format;
/* Init float info */
- if (FloatInfoType.tp_name == 0)
- PyStructSequence_InitType(&FloatInfoType, &floatinfo_desc);
+ if (FloatInfoType.tp_name == NULL) {
+ if (PyStructSequence_InitType2(&FloatInfoType, &floatinfo_desc) < 0)
+ return 0;
+ }
+ return 1;
}
int