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

georg.brandl python-checkins at python.org
Wed May 24 17:51:22 CEST 2006


Author: georg.brandl
Date: Wed May 24 17:51:21 2006
New Revision: 46185

Modified:
   sandbox/trunk/decimal-c/_decimal.c
Log:
Clean up and implement Context.__repr__.



Modified: sandbox/trunk/decimal-c/_decimal.c
==============================================================================
--- sandbox/trunk/decimal-c/_decimal.c	(original)
+++ sandbox/trunk/decimal-c/_decimal.c	Wed May 24 17:51:21 2006
@@ -929,7 +929,7 @@
 static char *decctxkwlist[] = {"other", "context", 0};
 
 #define ENSURE_DECIMAL(methname, dec, type)                             \
-    dec = _convert_to_decimal(type, (PyObject *)dec, ctx, 1);           \
+    dec = (decimalobject *)_convert_to_decimal(type, (PyObject *)dec, ctx, 1); \
     if (!dec) {                                                         \
         PyErr_SetString(PyExc_TypeError, methname ": " #dec " must be a Decimal object"); \
         return NULL;                                                    \
@@ -951,14 +951,14 @@
         if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:" #methname, ctxkwlist, &ctx)) \
             return NULL;                                                \
         ENSURE_CONTEXT(#methname, ctx);                                 \
-        return _do_decimal_##methname(self, ctx);                       \
+        return (PyObject *)_do_decimal_##methname(self, ctx);           \
     }
 
 #define DECIMAL_BINARY_FUNC(methname)                                   \
     static PyObject *                                                   \
     decimal_##methname(decimalobject *self, PyObject *args, PyObject *kwds) \
     {                                                                   \
-        decimalobject *res;                                             \
+        PyObject *res;                                             \
         decimalobject *dec = NULL;                                      \
         contextobject *ctx = NULL;                                      \
         if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:" #methname,  \
@@ -966,11 +966,10 @@
             return NULL;                                                \
         ENSURE_CONTEXT(#methname, ctx);                                 \
         ENSURE_DECIMAL(#methname, dec, self->ob_type);                  \
-        res = _do_decimal_##methname(self, dec, ctx);                   \
+        res = (PyObject *)_do_decimal_##methname(self, dec, ctx);       \
         Py_DECREF(dec);                                                 \
         return res;                                                     \
     }
-        
 
 
 static decimalobject *
@@ -1142,7 +1141,7 @@
 DECIMAL_UNARY_FUNC(sqrt)
 
 
-static decimalobject *
+static PyObject *
 _do_decimal_to_eng_string(decimalobject *self, contextobject *ctx)
 {
     return _do_decimal_str(self, ctx, 1);
@@ -1171,7 +1170,7 @@
         PyErr_SetString(PyExc_ValueError, "invalid rounding value");
         return NULL;
     }
-    return _do_decimal_to_integral(self, ctx, rounding);
+    return (PyObject *)_do_decimal_to_integral(self, ctx, rounding);
 }
 
 
@@ -2772,31 +2771,32 @@
 }
 
 #define CONTEXT_UNARY_FUNC(name, decname)               \
-    static decimalobject *                              \
+    static PyObject *                                   \
     context_##name(contextobject *self, PyObject *a) {  \
-        decimalobject *dec_a = NULL, *res;              \
+        decimalobject *dec_a = NULL;                    \
+        PyObject *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;                                     \
+        if (dec_a == NULL) return NULL;                         \
+        res = (PyObject *)_do_decimal_##decname(dec_a, self);   \
+        Py_DECREF(dec_a);                                       \
+        return res;                                             \
     }
 
 #define CONTEXT_BINARY_FUNC(name, decname)                          \
-    static decimalobject *                                          \
+    static PyObject *                                               \
     context_##name(contextobject *self, PyObject *args) {           \
-        PyObject *a, *b;                                            \
-        decimalobject *dec_a = NULL, *dec_b = NULL, *res;           \
+        PyObject *a, *b, *res;                                      \
+        decimalobject *dec_a = NULL, *dec_b = NULL;                 \
         if (!PyArg_ParseTuple(args, "OO:" #name, &a, &b)) 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);                                           \
+        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 = (PyObject *)_do_decimal_##decname(dec_a, dec_b, self);    \
+        Py_DECREF(dec_a);                                               \
         Py_DECREF(dec_b);                                           \
         return res;                                                 \
     }
@@ -2858,7 +2858,7 @@
     Py_DECREF(dec_a);                                               
     Py_DECREF(dec_b);
     Py_DECREF(dec_c);
-    return res;                                                     
+    return (PyObject *)res;                                                     
 }
 
 
@@ -2885,7 +2885,7 @@
     res = _do_decimal_quantize(dec_a, dec_b, self, -1, 1);
     Py_DECREF(dec_a);
     Py_DECREF(dec_b);
-    return res;
+    return (PyObject *)res;
 }
 
 
@@ -2930,7 +2930,7 @@
     if (dec_a == NULL)
         return NULL;
 
-    res = _do_decimal_to_integral(dec_a, self, -1);
+    res = (PyObject *)_do_decimal_to_integral(dec_a, self, -1);
     Py_DECREF(dec_a);
     return res;
 }
@@ -3057,12 +3057,25 @@
     c->ob_type->tp_free(c);
 }
 
+#define TEST_AND_CAT(num, name)                 \
+    if (ISFLAGSET(self->flags, num)) {          \
+        strcat(flags, name);                    \
+        strcat(flags, ", ");                    \
+        flaglen += strlen(name) + 2;            \
+    }                                           \
+    if (ISFLAGSET(self->traps, num)) {          \
+        strcat(traps, name);                    \
+        strcat(traps, ", ");                    \
+        traplen += strlen(name) + 2;            \
+    }
+
 static PyObject *
 context_repr(contextobject *self)
 {
     char roundstr[20] = "ROUND_";
-    char flags[1000] = "{}";
-    char traps[1000] = "[]";
+    char flags[250] = "["; /* 250 is enough for 12 error names of max. 17 chars */
+    char traps[250] = "["; /* and commas inbetween. */
+    long flaglen = 1, traplen = 1;
 
     switch (self->rounding) {
         case ROUND_UP:
@@ -3090,7 +3103,24 @@
             strcpy(roundstr, "None");
     }
 
-    /* XXX: flags/traps */       
+    TEST_AND_CAT(S_CLAMPED, "Clamped");
+    TEST_AND_CAT(S_INV_OPERATION, "InvalidOperation");
+    TEST_AND_CAT(S_DIV_BY_ZERO, "DivisionByZero");
+    TEST_AND_CAT(S_INEXACT, "Inexact");
+    TEST_AND_CAT(S_ROUNDED, "Rounded");
+    TEST_AND_CAT(S_SUBNORMAL, "Subnormal");
+    TEST_AND_CAT(S_OVERFLOW, "Overflow");
+    TEST_AND_CAT(S_UNDERFLOW, "Underflow");
+
+    /* if no flag/trap */
+    if (flaglen == 1)
+        flaglen = 3;
+    if (traplen == 1)
+        traplen = 3;
+    flags[flaglen-2] = ']';
+    flags[flaglen-1] = 0;
+    traps[traplen-2] = ']';
+    traps[traplen-1] = 0;
 
     return PyString_FromFormat("Context(prec=%ld, rounding=%s, Emin=%ld, "
                                "Emax=%ld, capitals=%d, flags=%s, traps=%s)",
@@ -3345,34 +3375,6 @@
 };
 
 
-static int
-_add_handle_method(PyObject *tp, PyMethodDef *ml)
-{
-    PyObject *meth, *cfunc;
-    static PyObject *module;
-
-    if (!module) {
-        module = PyString_FromString(MODULE_NAME);
-        if (!module)
-            return -1;
-    }
-    
-    cfunc = PyCFunction_NewEx(ml, NULL, module);
-    if (!cfunc)
-        return -1;
-
-    meth = PyMethod_New(cfunc, NULL, tp);
-    if (!meth) {
-        Py_DECREF(cfunc);
-        return -1;
-    }
-    
-    PyObject_SetAttrString(tp, "handle", meth);
-    Py_DECREF(cfunc);
-    Py_DECREF(meth);
-    return 0;
-}    
-
 #define ADD_CONST(m, name)                               \
     if (PyModule_AddIntConstant(m, #name, name) < 0) {   \
         return;                                          \


More information about the Python-checkins mailing list