[Python-checkins] cpython (merge 3.3 -> default): Merge 3.3.

stefan.krah python-checkins at python.org
Wed Nov 7 23:21:19 CET 2012


http://hg.python.org/cpython/rev/57649a9a683c
changeset:   80294:57649a9a683c
parent:      80291:859ef54bdce2
parent:      80293:1b6c972457e6
user:        Stefan Krah <skrah at bytereef.org>
date:        Wed Nov 07 23:20:10 2012 +0100
summary:
  Merge 3.3.

files:
  Lib/test/test_decimal.py    |   5 ++++
  Misc/NEWS                   |   3 ++
  Modules/_decimal/_decimal.c |  29 +++++++++++++++++++++++-
  3 files changed, 35 insertions(+), 2 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
@@ -2047,6 +2047,11 @@
         self.assertIs(type(d), MyDecimal)
         self.assertEqual(d, d1)
 
+        a = Decimal('1.0')
+        b = MyDecimal(a)
+        self.assertIs(type(b), MyDecimal)
+        self.assertEqual(a, b)
+
     def test_implicit_context(self):
         Decimal = self.decimal.Decimal
         getcontext = self.decimal.getcontext
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -110,6 +110,9 @@
 Library
 -------
 
+- Issue #16431: Use the type information when constructing a Decimal subtype
+  from a Decimal argument.
+
 - Issue #15641: Clean up deprecated classes from importlib
   Patch by Taras Lyapun.
 
diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c
--- a/Modules/_decimal/_decimal.c
+++ b/Modules/_decimal/_decimal.c
@@ -2338,6 +2338,32 @@
     return dec;
 }
 
+/* Return a new PyDecObject (subtype) from a Decimal. */
+static PyObject *
+PyDecType_FromDecimalExact(PyTypeObject *type, PyObject *v, PyObject *context)
+{
+    PyObject *dec;
+    uint32_t status = 0;
+
+    if (type == &PyDec_Type) {
+        Py_INCREF(v);
+        return v;
+    }
+
+    dec = PyDecType_New(type);
+    if (dec == NULL) {
+        return NULL;
+    }
+
+    mpd_qcopy(MPD(dec), MPD(v), &status);
+    if (dec_addstatus(context, status)) {
+        Py_DECREF(dec);
+        return NULL;
+    }
+
+    return dec;
+}
+
 static PyObject *
 sequence_as_tuple(PyObject *v, PyObject *ex, const char *mesg)
 {
@@ -2642,8 +2668,7 @@
         return PyDecType_FromSsizeExact(type, 0, context);
     }
     else if (PyDec_Check(v)) {
-        Py_INCREF(v);
-        return v;
+        return PyDecType_FromDecimalExact(type, v, context);
     }
     else if (PyUnicode_Check(v)) {
         return PyDecType_FromUnicodeExactWS(type, v, context);

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


More information about the Python-checkins mailing list