Skip to content

Commit 8113f49

Browse files
author
Stefan Krah
committed
Merge 3.5.
2 parents df1d31c + 6817c59 commit 8113f49

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

Lib/test/test_decimal.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2524,14 +2524,17 @@ def test_from_float(self):
25242524
Decimal = self.decimal.Decimal
25252525

25262526
class MyDecimal(Decimal):
2527-
pass
2527+
def __init__(self, _):
2528+
self.x = 'y'
25282529

25292530
self.assertTrue(issubclass(MyDecimal, Decimal))
25302531

25312532
r = MyDecimal.from_float(0.1)
25322533
self.assertEqual(type(r), MyDecimal)
25332534
self.assertEqual(str(r),
25342535
'0.1000000000000000055511151231257827021181583404541015625')
2536+
self.assertEqual(r.x, 'y')
2537+
25352538
bigint = 12345678901234567890123456789
25362539
self.assertEqual(MyDecimal.from_float(bigint), MyDecimal(bigint))
25372540
self.assertTrue(MyDecimal.from_float(float('nan')).is_qnan())

Modules/_decimal/_decimal.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,12 +2629,18 @@ PyDecType_FromSequenceExact(PyTypeObject *type, PyObject *v,
26292629

26302630
/* class method */
26312631
static PyObject *
2632-
dec_from_float(PyObject *dec, PyObject *pyfloat)
2632+
dec_from_float(PyObject *type, PyObject *pyfloat)
26332633
{
26342634
PyObject *context;
2635+
PyObject *result;
26352636

26362637
CURRENT_CONTEXT(context);
2637-
return PyDecType_FromFloatExact((PyTypeObject *)dec, pyfloat, context);
2638+
result = PyDecType_FromFloatExact(&PyDec_Type, pyfloat, context);
2639+
if (!PyDec_CheckExact(type) && result != NULL) {
2640+
Py_SETREF(result, PyObject_CallFunctionObjArgs(type, result, NULL));
2641+
}
2642+
2643+
return result;
26382644
}
26392645

26402646
/* create_decimal_from_float */

0 commit comments

Comments
 (0)