[Python-checkins] r51331 - sandbox/trunk/decimal-c/_decimal.c

mateusz.rukowicz python-checkins at python.org
Thu Aug 17 02:00:46 CEST 2006


Author: mateusz.rukowicz
Date: Thu Aug 17 02:00:45 2006
New Revision: 51331

Modified:
   sandbox/trunk/decimal-c/_decimal.c
Log:
Now unpickling old decimal object should work. I am sure it's not the best way to achieve that, but I can't see other one.


Modified: sandbox/trunk/decimal-c/_decimal.c
==============================================================================
--- sandbox/trunk/decimal-c/_decimal.c	(original)
+++ sandbox/trunk/decimal-c/_decimal.c	Thu Aug 17 02:00:45 2006
@@ -2361,7 +2361,7 @@
     if (!ctx) return 0;
 
     other = (decimalobject *)_convert_to_decimal(self->ob_type,
-                                                (PyObject*)other, ctx, 0);
+                                                (PyObject*)other, ctx, 1);
 
     if (!other) return 0;
     /* XXX ??*/
@@ -8412,16 +8412,101 @@
 static PyMemberDef context_members[] = {
     {"prec", T_LONG, OFF(prec), 0},
     {"capitals", T_INT, OFF(capitals), 0},
-    {"rounding", T_INT, OFF(rounding), 0},
-    {"_rounding_decision", T_INT, OFF(rounding_dec), 0},
+/*    {"rounding", T_INT, OFF(rounding), 0}, */
+/*    {"_rounding_decision", T_INT, OFF(rounding_dec), 0}, */
     {"_clamp", T_INT, OFF(clamp), 0},
     {"flags", T_OBJECT_EX, OFF(flags), 0},
     {"traps", T_OBJECT_EX, OFF(traps), 0},
-    {"_ignored", T_OBJECT_EX, OFF(ignored), 0},
+    {"_ignored_flags", T_OBJECT_EX, OFF(ignored), 0},
     {NULL}
 };
 
 static PyObject *
+context_get_rounding(contextobject *self) {
+    return PyInt_FromLong(self->rounding);
+}
+
+static int
+context_set_rounding_(contextobject *self, PyObject *value) {
+    int new_round = -1;
+    if (PyInt_Check(value)) {
+        new_round = PyInt_AsLong(value);
+        if (PyErr_Occurred())
+            return -1;
+    }
+
+    if (PyString_Check(value)) {
+        char *buffer = PyString_AS_STRING(value);
+        if (!strcmp("ROUND_DOWN", buffer))
+            new_round = 0;
+        else if (!strcmp("ROUND_UP", buffer))
+            new_round = 1;
+        else if (!strcmp("ROUND_HALF_DOWN", buffer))
+            new_round = 2;
+        else if (!strcmp("ROUND_HALF_EVEN", buffer))
+            new_round = 3;
+        else if (!strcmp("ROUND_HALF_UP", buffer))
+            new_round = 4;
+        else if (!strcmp("ROUND_FLOOR", buffer))
+            new_round = 5;
+        else if (!strcmp("ROUND_CEILING", buffer))
+            new_round = 6;
+    }
+    else if (new_round == -1) {
+        PyErr_SetString(PyExc_TypeError, "Rounding should be int or string");
+        return -1;
+    }
+
+    if (!VALID_ROUND(new_round)) {
+        PyErr_SetString(PyExc_ValueError, "value is not a valid rounding");
+        return -1;
+    }
+    
+
+    self->rounding = new_round;
+    return 0;
+    
+}
+
+static PyObject *
+context_get_rounding_decision(contextobject *self) {
+    return PyInt_FromLong(self->rounding_dec);
+}
+
+static int
+context_set_rounding_decision_(contextobject *self, PyObject *value) {
+    int new_round_dec = -1;
+
+    if (PyInt_Check(value)) {
+        new_round_dec = PyInt_AsLong(value);
+        if (PyErr_Occurred())
+            return -1;
+    }
+
+    if (PyString_Check(value)) {
+        char *buffer = PyString_AS_STRING(value);
+
+        if (!strcmp("ALWAYS_ROUND", buffer))
+            new_round_dec = 0;
+        else if (!strcmp("NEVER_ROUND", buffer))
+            new_round_dec = 16;
+    }
+
+    else if (new_round_dec == -1) {
+        PyErr_SetString(PyExc_TypeError, "Rounding should be int or string");
+        return -1;
+    }
+
+    if (!VALID_ROUND_DEC(new_round_dec)) {
+        PyErr_SetString(PyExc_ValueError, "value is not a valid rounding decision");
+        return -1;
+    }
+
+    self->rounding_dec = new_round_dec;
+    return 0;
+}
+
+static PyObject *
 context_get_emax(contextobject *self) {
     return exp_to_pyobj(self->Emax);
 }
@@ -8451,6 +8536,9 @@
 }
 
 static PyGetSetDef context_getset[] = {
+    {"_rounding_decision", (getter)context_get_rounding_decision,
+     (setter)context_set_rounding_decision_},
+    {"rounding", (getter) context_get_rounding, (setter)context_set_rounding_},
     {"Emax", (getter) context_get_emax, (setter)context_set_emax},
     {"Emin", (getter) context_get_emin, (setter)context_set_emin},
     {NULL}  


More information about the Python-checkins mailing list