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

georg.brandl python-checkins at python.org
Wed May 24 18:10:32 CEST 2006


Author: georg.brandl
Date: Wed May 24 18:10:31 2006
New Revision: 46187

Modified:
   sandbox/trunk/decimal-c/_decimal.c
Log:
Cleanup handle_* functions.



Modified: sandbox/trunk/decimal-c/_decimal.c
==============================================================================
--- sandbox/trunk/decimal-c/_decimal.c	(original)
+++ sandbox/trunk/decimal-c/_decimal.c	Wed May 24 18:10:31 2006
@@ -109,22 +109,22 @@
 static decimalobject *_new_decimalobj(PyTypeObject *, long, char, long);
 
 
-#define HANDLE_ERROR(ctx, cond, expl)                             \
+#define HANDLE_ERROR(ctx, cond, expl, onerror)                    \
     int err = (cond >= NUMSIGNALS ? S_INV_OPERATION : cond);      \
     if (! ISFLAGSET(ctx->ignored, err)) {                         \
         SETFLAG(ctx->flags, err);                                 \
         if (ISFLAGSET(ctx->traps, err)) {                         \
             PyErr_SetString(errors[err], (expl ? expl : ""));     \
-            return NULL;                                          \
+            return onerror;                                       \
         }                                                         \
     }
 
 
-static PyObject *
-handle_Clamped(PyTypeObject *type, contextobject *ctx, char *expl)
+static int
+handle_Clamped(contextobject *ctx, char *expl)
 {
-    HANDLE_ERROR(ctx, S_CLAMPED, expl);
-    Py_RETURN_NONE;
+    HANDLE_ERROR(ctx, S_CLAMPED, expl, -1);
+    return 0;
 }
 
 static decimalobject *
@@ -133,6 +133,7 @@
 {
     decimalobject *res;
     long sign, i;
+    HANDLE_ERROR(ctx, S_INV_OPERATION, expl, NULL);
 
     if (thing == NULL) {
         Py_INCREF(PyDecimal_NaN);
@@ -151,7 +152,7 @@
 static decimalobject *
 handle_ConversionSyntax(PyTypeObject *type, contextobject *ctx, char *expl)
 {
-    HANDLE_ERROR(ctx, C_CONV_SYNTAX, expl);
+    HANDLE_ERROR(ctx, C_CONV_SYNTAX, expl, NULL);
 
     Py_INCREF(PyDecimal_NaN);
     return PyDecimal_NaN;
@@ -161,63 +162,66 @@
 handle_DivisionByZero(PyTypeObject *type, contextobject *ctx, char *expl,
                       long sign, int two)
 {
-    HANDLE_ERROR(ctx, S_DIV_BY_ZERO, expl);
+    decimalobject *res;
+    HANDLE_ERROR(ctx, S_DIV_BY_ZERO, expl, NULL);
+    
+    if (sign & 1)
+        res = PyDecimal_NegInf;
+    else
+        res = PyDecimal_Inf;
 
-    /* XXX: return tuple */
-    Py_RETURN_NONE;
+    Py_INCREF(res);
+    return res;
 }
 
 static decimalobject *
 handle_DivisionImpossible(PyTypeObject *type, contextobject *ctx, char *expl)
 {
-    HANDLE_ERROR(ctx, C_DIV_IMPOSSIBLE, expl);
+    HANDLE_ERROR(ctx, C_DIV_IMPOSSIBLE, expl, NULL);
     
-    /* XXX: return tuple */
-    Py_RETURN_NONE;
+    Py_INCREF(PyDecimal_NaN);
+    return PyDecimal_NaN;
 }
 
 static decimalobject *
 handle_DivisionUndefined(PyTypeObject *type, contextobject *ctx,
                          char *expl, int two)
 {
-    HANDLE_ERROR(ctx, C_DIV_UNDEFINED, expl);
-
-    /* XXX: return tuple? */
+    HANDLE_ERROR(ctx, C_DIV_UNDEFINED, expl, NULL);
 
     Py_INCREF(PyDecimal_NaN);
     return PyDecimal_NaN;
 }
 
-static PyObject *
-handle_Inexact(PyTypeObject *type, contextobject *ctx, char *expl)
+static int
+handle_Inexact(contextobject *ctx, char *expl)
 {
-    HANDLE_ERROR(ctx, S_INEXACT, expl);
-    Py_RETURN_NONE;
+    HANDLE_ERROR(ctx, S_INEXACT, expl, -1);
+    return 0;
 }
 
 static decimalobject *
 handle_InvalidContext(PyTypeObject *type, contextobject *ctx, char *expl)
 {
-    HANDLE_ERROR(ctx, C_INV_CONTEXT, expl);
+    HANDLE_ERROR(ctx, C_INV_CONTEXT, expl, NULL);
     
     Py_INCREF(PyDecimal_NaN);
     return PyDecimal_NaN;
 }
 
-static PyObject *
-handle_Rounded(PyTypeObject *type, contextobject *ctx, char *expl)
+static int
+handle_Rounded(contextobject *ctx, char *expl)
 {
-    HANDLE_ERROR(ctx, S_ROUNDED, expl);
-
-    Py_RETURN_NONE;
+    HANDLE_ERROR(ctx, S_ROUNDED, expl, -1);
+    return 0;
 }
 
-static PyObject *
-handle_Subnormal(PyTypeObject *type, contextobject *ctx, char *expl)
+static int
+handle_Subnormal(contextobject *ctx, char *expl)
 {
-    HANDLE_ERROR(ctx, S_SUBNORMAL, expl);
+    HANDLE_ERROR(ctx, S_SUBNORMAL, expl, -1);
 
-    Py_RETURN_NONE;
+    return 0;
 }
 
 static decimalobject *
@@ -226,7 +230,7 @@
     decimalobject *res;
     long i;
 
-    HANDLE_ERROR(ctx, S_OVERFLOW, expl);
+    HANDLE_ERROR(ctx, S_OVERFLOW, expl, NULL);
 
     assert(lsign == 0 || lsign == 1);
 
@@ -265,12 +269,11 @@
     return res;
 }
 
-static PyObject *
-handle_Underflow(PyTypeObject *type, contextobject *ctx, char *expl)
+static int
+handle_Underflow(contextobject *ctx, char *expl)
 {
-    HANDLE_ERROR(ctx, S_UNDERFLOW, expl);
-
-    Py_RETURN_NONE;
+    HANDLE_ERROR(ctx, S_UNDERFLOW, expl, -1);
+    return 0;
 }
 
 
@@ -563,7 +566,6 @@
 {
     decimalobject *new, *new2 = NULL;
     contextobject *ctx2 = NULL;
-    PyObject *errres;
     long i, expdiff;
     round_func rnd_func;
     
@@ -596,12 +598,10 @@
         while (i--)
             new->digits[i] = 0;
         
-        errres = handle_Rounded(self->ob_type, ctx, NULL);
-        if (!errres) {
+        if (handle_Rounded(ctx, NULL) != 0) {
             Py_DECREF(new);
             return NULL; /* error was set */
         }
-        Py_DECREF(errres); /* we don't need the returned object */
         return new;
     }
     
@@ -652,12 +652,10 @@
     /* All lost digits are 0, so just clobber new */
     new->ob_size = prec;
     new->exp -= expdiff;
-    errres = handle_Rounded(self->ob_type, ctx, NULL);
-    if (!errres) {
+    if (handle_Rounded(ctx, NULL) != 0) {
         Py_DECREF(new);
         return NULL;
     }
-    Py_DECREF(errres);
     return new;
 
 no_way:
@@ -678,11 +676,9 @@
     Py_DECREF(new);
     if (!new2)
         goto error;
-    errres = handle_Rounded(self->ob_type, ctx, NULL);
-    if (!errres)
+    if (handle_Rounded(ctx, NULL) != 0)
         goto error;
-    errres = handle_Rounded(self->ob_type, ctx, "Changed in rounding");
-    if (!errres)
+    if (handle_Inexact(ctx, "Changed in rounding") != 0)
         goto error;
     Py_XDECREF(ctx2);
     return new2;
@@ -765,9 +761,8 @@
     adj = ADJUSTED(tmp);
     if (decimal_nonzero(tmp)) {
         if (adj < ctx->Emin) {
-            ans = handle_Subnormal(self->ob_type, ctx, NULL);
-            Py_DECREF(tmp);
-            return ans;
+            if (handle_Subnormal(ctx, NULL) != 0)
+                return NULL;
         } else if (adj > ctx->Emax) {
             ans = handle_InvalidOperation(self->ob_type, ctx,
                                           "rescale(a, INF)", NULL);
@@ -783,7 +778,7 @@
 static decimalobject *
 _fixexponents(decimalobject *self, contextobject *ctx)
 {
-    decimalobject *ans = NULL, *tmp;
+    decimalobject *ans = NULL;
     long adj;
     assert(!ISSPECIAL(self));
 
@@ -796,42 +791,32 @@
 	        	if (!ans)
                     return NULL;
                 ans->exp = Etiny;
-                tmp = handle_Clamped(self->ob_type, ctx, NULL);
-                if (!tmp)
+                if (handle_Clamped(ctx, NULL) != 0)
                     goto err;
-                Py_DECREF(tmp);
                 return ans;
             }
             ans = _decimal_rescale(self, Etiny, ctx, -1, 1);
             if (!ans)
                 return NULL;
-            tmp = handle_Subnormal(self->ob_type, ctx, NULL);
-            if (!tmp)
+            if (handle_Subnormal(ctx, NULL) != 0)
                 goto err;
-            Py_DECREF(tmp);
             if (ISFLAGSET(ctx->flags, S_INEXACT)) {
-                tmp = handle_Underflow(self->ob_type, ctx, NULL);
-                if (!tmp)
+                if (handle_Underflow(ctx, NULL) != 0)
                     goto err;
-                Py_DECREF(tmp);
             }
             return ans;
         } else {
             if (decimal_nonzero(self)) {
-                tmp = handle_Subnormal(self->ob_type, ctx, NULL);
-                if (!tmp)
+                if (handle_Subnormal(ctx, NULL) != 0)
                     return NULL;
-                Py_DECREF(tmp);
             }
             /* this returns self, below */
         }
     } else {
         long Etop = ETOP(ctx);
         if (ctx->clamp && self->exp > Etop) {
-            tmp = handle_Clamped(self->ob_type, ctx, NULL);
-            if (!tmp)
+            if (handle_Clamped(ctx, NULL) != 0)
                 return NULL;
-            Py_DECREF(tmp);
             ans = _decimal_rescale(self, Etop, ctx, -1, 1);
             if (!ans)
                 return NULL;
@@ -843,20 +828,14 @@
                     if (!ans)
                         return NULL;
                     ans->exp = ctx->Emax;
-                    tmp = handle_Clamped(self->ob_type, ctx, NULL);
-                    if (!tmp)
+                    if (handle_Clamped(ctx, NULL) != 0)
                         goto err;
-                    Py_DECREF(tmp);
                     return ans;
                 }
-                tmp = handle_Inexact(self->ob_type, ctx, NULL);
-                if (!tmp)
+                if (handle_Inexact(ctx, NULL) != 0)
                     return NULL;
-                Py_DECREF(tmp);
-                tmp = handle_Rounded(self->ob_type, ctx, NULL);
-                if (!tmp)
+                if (handle_Rounded(ctx, NULL) != 0)
                     return NULL;
-                Py_DECREF(tmp);
                 return handle_Overflow(self->ob_type, ctx, "above Emax", self->sign);
             }
         }


More information about the Python-checkins mailing list