[Python-checkins] cpython (3.5): Issue #27006: from_float(): call the subclass' __new__() and __init__().

stefan.krah python-checkins at python.org
Mon Jun 20 06:12:01 EDT 2016


https://hg.python.org/cpython/rev/dd3f48f1df86
changeset:   102110:dd3f48f1df86
branch:      3.5
parent:      102106:1cbe4cf863c5
user:        Stefan Krah <skrah at bytereef.org>
date:        Mon Jun 20 12:10:13 2016 +0200
summary:
  Issue #27006: from_float(): call the subclass' __new__() and __init__().

files:
  Lib/test/test_decimal.py    |   5 ++++-
  Modules/_decimal/_decimal.c |  10 ++++++++--
  2 files changed, 12 insertions(+), 3 deletions(-)


diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -2491,7 +2491,8 @@
         Decimal = self.decimal.Decimal
 
         class MyDecimal(Decimal):
-            pass
+            def __init__(self, _):
+                self.x = 'y'
 
         self.assertTrue(issubclass(MyDecimal, Decimal))
 
@@ -2499,6 +2500,8 @@
         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())
diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c
--- a/Modules/_decimal/_decimal.c
+++ b/Modules/_decimal/_decimal.c
@@ -2630,12 +2630,18 @@
 
 /* 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 */

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list