]> granicus.if.org Git - python/commitdiff
Issue #27006: from_float(): call the subclass' __new__() and __init__().
authorStefan Krah <skrah@bytereef.org>
Mon, 20 Jun 2016 10:10:13 +0000 (12:10 +0200)
committerStefan Krah <skrah@bytereef.org>
Mon, 20 Jun 2016 10:10:13 +0000 (12:10 +0200)
Lib/test/test_decimal.py
Modules/_decimal/_decimal.c

index c0d21b1c80fefe6e7aaa0d0586e4f59744568b01..cde5d78bef4f388cf89c0dfc2faf4a00393f7f90 100644 (file)
@@ -2491,7 +2491,8 @@ class PythonAPItests(unittest.TestCase):
         Decimal = self.decimal.Decimal
 
         class MyDecimal(Decimal):
-            pass
+            def __init__(self, _):
+                self.x = 'y'
 
         self.assertTrue(issubclass(MyDecimal, Decimal))
 
@@ -2499,6 +2500,8 @@ class PythonAPItests(unittest.TestCase):
         self.assertEqual(type(r), MyDecimal)
         self.assertEqual(str(r),
                 '0.1000000000000000055511151231257827021181583404541015625')
+        self.assertEqual(r.x, 'y')
+
         bigint = 12345678901234567890123456789
         self.assertEqual(MyDecimal.from_float(bigint), MyDecimal(bigint))
         self.assertTrue(MyDecimal.from_float(float('nan')).is_qnan())
index 112b44fda7b5846428e11ce54c35697869ad726d..e69d715bb214975602751275b0a064bbf3e65fbc 100644 (file)
@@ -2630,12 +2630,18 @@ PyDecType_FromSequenceExact(PyTypeObject *type, PyObject *v,
 
 /* class method */
 static PyObject *
-dec_from_float(PyObject *dec, PyObject *pyfloat)
+dec_from_float(PyObject *type, PyObject *pyfloat)
 {
     PyObject *context;
+    PyObject *result;
 
     CURRENT_CONTEXT(context);
-    return PyDecType_FromFloatExact((PyTypeObject *)dec, pyfloat, context);
+    result = PyDecType_FromFloatExact(&PyDec_Type, pyfloat, context);
+    if (!PyDec_CheckExact(type) && result != NULL) {
+        Py_SETREF(result, PyObject_CallFunctionObjArgs(type, result, NULL));
+    }
+
+    return result;
 }
 
 /* create_decimal_from_float */