Skip to content

Commit 25885d1

Browse files
Issue python#27005: Optimized the float.fromhex() class method for exact float.
1 parent 871639a commit 25885d1

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: tba
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #27005: Optimized the float.fromhex() class method for exact float.
14+
It is now 2 times faster.
15+
1316
- Issue #18531: Single var-keyword argument of dict subtype was passed
1417
unscathed to the C-defined function. Now it is converted to exact dict.
1518

‎Objects/floatobject.c‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ Return a hexadecimal representation of a floating-point number.\n\
11951195
static PyObject *
11961196
float_fromhex(PyObject *cls, PyObject *arg)
11971197
{
1198-
PyObject *result_as_float, *result;
1198+
PyObject *result;
11991199
double x;
12001200
long exp, top_exp, lsb, key_digit;
12011201
char *s, *coeff_start, *s_store, *coeff_end, *exp_start, *s_end;
@@ -1410,11 +1410,10 @@ float_fromhex(PyObject *cls, PyObject *arg)
14101410
s++;
14111411
if (s != s_end)
14121412
goto parse_error;
1413-
result_as_float = Py_BuildValue("(d)", negate ? -x : x);
1414-
if (result_as_float == NULL)
1415-
return NULL;
1416-
result = PyObject_CallObject(cls, result_as_float);
1417-
Py_DECREF(result_as_float);
1413+
result = PyFloat_FromDouble(negate ? -x : x);
1414+
if (cls != (PyObject *)&PyFloat_Type && result != NULL) {
1415+
Py_SETREF(result, PyObject_CallFunctionObjArgs(cls, result));
1416+
}
14181417
return result;
14191418

14201419
overflow_error:

0 commit comments

Comments
 (0)