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

georg.brandl python-checkins at python.org
Wed May 24 15:11:10 CEST 2006


Author: georg.brandl
Date: Wed May 24 15:11:06 2006
New Revision: 46168

Modified:
   sandbox/trunk/decimal-c/_decimal.c
Log:
"Implement" some context methods.



Modified: sandbox/trunk/decimal-c/_decimal.c
==============================================================================
--- sandbox/trunk/decimal-c/_decimal.c	(original)
+++ sandbox/trunk/decimal-c/_decimal.c	Wed May 24 15:11:06 2006
@@ -49,7 +49,6 @@
 #define SIGN_POSSNAN 6
 #define SIGN_NEGSNAN 7
 
-
 /* Rounding constants */
 
 #define ROUND_DOWN      0
@@ -1259,6 +1258,7 @@
         res = _do_##func((decimalobject *)self,                         \
                          (decimalobject *)other, ctx);                  \
         Py_DECREF(other);                                               \
+        return (PyObject *)res;                                         \
     }
 
 #define DECIMAL_SPECIAL_1FUNC(func)                                 \
@@ -1306,11 +1306,80 @@
 }
 DECIMAL_SPECIAL_2FUNC(decimal_subtract)
 
-STUB(multiply)
-STUB(divide)
-STUB(remainder)
-STUB(divmod)
-STUB(power)
+static decimalobject *
+_do_decimal_multiply(decimalobject *self, decimalobject *other,
+                     contextobject *ctx)
+{
+}
+DECIMAL_SPECIAL_2FUNC(decimal_multiply)
+
+static decimalobject *
+_do_decimal_divide(decimalobject *self, decimalobject *other,
+                   contextobject *ctx)
+{
+}
+DECIMAL_SPECIAL_2FUNC(decimal_divide)
+
+
+static decimalobject *
+_do_decimal_floor_div(decimalobject *self, decimalobject *other,
+                      contextobject *ctx)
+{
+}
+DECIMAL_SPECIAL_2FUNC(decimal_floor_div)
+
+static decimalobject *
+_do_decimal_true_div(decimalobject *self, decimalobject *other,
+                     contextobject *ctx)
+{
+}
+DECIMAL_SPECIAL_2FUNC(decimal_true_div)
+
+static decimalobject *
+_do_decimal_remainder(decimalobject *self, decimalobject *other,
+                      contextobject *ctx)
+{
+}
+DECIMAL_SPECIAL_2FUNC(decimal_remainder)
+
+static decimalobject *
+_do_decimal_divmod(decimalobject *self, decimalobject *other,
+                   contextobject *ctx)
+{
+}
+DECIMAL_SPECIAL_2FUNC(decimal_divmod)
+
+static decimalobject *
+_do_decimal_power(decimalobject *self, decimalobject *other,
+                           decimalobject *modulo, contextobject *ctx)
+{
+}
+
+static PyObject *                                                       
+decimal_power(PyObject *self, PyObject *other, PyObject *modulo) {                               
+    decimalobject *res;                                                
+    contextobject *ctx = getcontext();
+    if (!ctx) return NULL;                                              
+    other = _convert_to_decimal(self->ob_type, other, ctx, 0);          
+    if (other == NULL || other == Py_NotImplemented) return other;
+    if (modulo == Py_None) {
+        Py_INCREF(modulo);
+    } else {
+        modulo = _convert_to_decimal(self->ob_type, modulo, ctx, 0);
+        if (modulo == NULL || modulo == Py_NotImplemented) {
+            Py_DECREF(other);
+            return modulo;
+        }
+    }
+    res = _do_decimal_power((decimalobject *)self,                             
+                            (decimalobject *)other,
+                            (decimalobject *)modulo,
+                            ctx);                      
+    Py_DECREF(other);
+    Py_DECREF(modulo);
+    return (PyObject *)res;
+}
+
 
 static decimalobject *
 _do_decimal_negative(decimalobject *self, contextobject *ctx)
@@ -1490,9 +1559,6 @@
     return res;
 }
 
-STUB(floor_div)
-STUB(true_div)
-
 static int
 decimal_nonzero(decimalobject *self)
 {
@@ -2376,22 +2442,54 @@
     return PyInt_FromSsize_t(ETOP(self));
 }
 
-CSTUB(abs)
-
-
-CSTUB(add)
-
-
+#define CONTEXT_UNARY_FUNC(name, decname)               \
+    static decimalobject *                              \
+    context_##name(contextobject *self, PyObject *a) {  \
+        decimalobject *dec_a = NULL, *res;              \
+        dec_a = (decimalobject *)_convert_to_decimal(   \
+            &PyDecimal_DecimalType, a, self, 1);        \
+        if (dec_a == NULL) return NULL;                 \
+        res = _do_decimal_##decname(dec_a, self);       \
+        Py_DECREF(dec_a);                               \
+        return res;                                     \
+    }
+
+#define CONTEXT_BINARY_FUNC(name, decname)                          \
+    static decimalobject *                                          \
+    context_##name(contextobject *self, PyObject *args) {           \
+        PyObject *a, *b;                                            \
+        decimalobject *dec_a = NULL, *dec_b = NULL, *res;           \
+        if (!PyArg_ParseTuple(args, "OO:" #name)) return NULL;      \
+        dec_a = (decimalobject *)_convert_to_decimal(               \
+            &PyDecimal_DecimalType, a, self, 1);                    \
+        if (dec_a == NULL) return NULL;                             \
+        dec_b = (decimalobject *)_convert_to_decimal(               \
+            &PyDecimal_DecimalType, b, self, 1);                    \
+        if (dec_b == NULL) { Py_DECREF(dec_a); return NULL; }       \
+        res = _do_decimal_##decname(dec_a, dec_b, self);            \
+        Py_DECREF(dec_a);                                           \
+        Py_DECREF(dec_b);                                           \
+        return res;                                                 \
+    }
+
+/* helper so that I can use the CONTEXT_UNARY_FUNC macro above */
+#define _do_decimal_abs_with_round(a, b) \
+    _do_decimal_absolute(a, b, 1)
+
+CONTEXT_UNARY_FUNC(abs, abs_with_round)
+CONTEXT_BINARY_FUNC(add, add)
+CONTEXT_BINARY_FUNC(divide, divide)
+CONTEXT_BINARY_FUNC(divide_int, floor_div)
+CONTEXT_BINARY_FUNC(divmod, divmod)
+CONTEXT_UNARY_FUNC(minus, negative)
+CONTEXT_BINARY_FUNC(multiply, multiply)
+CONTEXT_UNARY_FUNC(plus, positive)
+CONTEXT_BINARY_FUNC(subtract, subtract)
 
 CSTUB(compare)
-CSTUB(divide)
-CSTUB(divmod)
-CSTUB(max)
 CSTUB(min)
-CSTUB(minus)
-CSTUB(multiply)
+CSTUB(max)
 CSTUB(normalize)
-CSTUB(plus)
 CSTUB(power)
 CSTUB(quantize)
 CSTUB(reduce)
@@ -2399,7 +2497,6 @@
 CSTUB(remainder_near)
 CSTUB(same_quantum)
 CSTUB(sqrt)
-CSTUB(subtract)
 CSTUB(to_eng_string)
 CSTUB(to_integral)
 CSTUB(to_sci_string)
@@ -2417,47 +2514,49 @@
     {"Etop",            (PyCFunction)context_Etop,
      METH_NOARGS},
     {"abs",             (PyCFunction)context_abs,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_O},
     {"add",             (PyCFunction)context_add,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_VARARGS},
     {"compare",         (PyCFunction)context_compare,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_VARARGS},
     {"divide",          (PyCFunction)context_divide,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_VARARGS},
+    {"divide_int",      (PyCFunction)context_divide_int,
+     METH_VARARGS},
     {"divmod",          (PyCFunction)context_divmod,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_VARARGS},
     {"max",             (PyCFunction)context_max,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_VARARGS},
     {"min",             (PyCFunction)context_min,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_VARARGS},
     {"minus",           (PyCFunction)context_minus,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_O},
     {"multiply",        (PyCFunction)context_multiply,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_VARARGS},
     {"normalize",       (PyCFunction)context_normalize,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_O},
     {"plus",            (PyCFunction)context_plus,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_O},
     {"power",           (PyCFunction)context_power,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_VARARGS},
     {"quantize",        (PyCFunction)context_quantize,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_VARARGS},
     {"remainder",       (PyCFunction)context_remainder,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_VARARGS},
     {"remainder_near",  (PyCFunction)context_remainder_near,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_VARARGS},
     {"same_quantum",    (PyCFunction)context_same_quantum,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_VARARGS},
     {"sqrt",            (PyCFunction)context_sqrt,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_O},
     {"subtract",        (PyCFunction)context_subtract,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_VARARGS},
     {"to_eng_string",   (PyCFunction)context_to_eng_string,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_O},
     {"to_integral",     (PyCFunction)context_to_integral,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_O},
     {"to_sci_string",   (PyCFunction)context_to_sci_string,
-     METH_VARARGS | METH_KEYWORDS},
+     METH_O},
     {"__copy__",        (PyCFunction)context_copy,
      METH_NOARGS},
     {"_shallow_copy",   (PyCFunction)context_copy,


More information about the Python-checkins mailing list