[Python-checkins] r46196 - sandbox/trunk/decimal-c/_decimal.c sandbox/trunk/decimal-c/decimal.py
jack.diederich
python-checkins at python.org
Wed May 24 20:44:04 CEST 2006
Author: jack.diederich
Date: Wed May 24 20:44:03 2006
New Revision: 46196
Modified:
sandbox/trunk/decimal-c/_decimal.c
sandbox/trunk/decimal-c/decimal.py
Log:
* Context._raise_error() replacement
Modified: sandbox/trunk/decimal-c/_decimal.c
==============================================================================
--- sandbox/trunk/decimal-c/_decimal.c (original)
+++ sandbox/trunk/decimal-c/_decimal.c Wed May 24 20:44:03 2006
@@ -3126,6 +3126,147 @@
return ret;
}
+static PyObject *
+_do_context_error_dispatch(int flag, PyObject *args)
+{
+ contextobject *ctx;
+ decimalobject *thing;
+ PyTypeObject *ctx_type;
+ char *expl;
+ long sign;
+ int two;
+ int ret;
+
+ switch (flag) {
+ case S_CLAMPED:
+ case S_SUBNORMAL:
+ case S_UNDERFLOW:
+ case S_INEXACT:
+ case S_ROUNDED:
+ if (!PyArg_ParseTuple(args, "OO:_raise_error", &ctx, &expl))
+ return NULL;
+ switch (flag) {
+ case S_CLAMPED:
+ ret = handle_Clamped(ctx, expl);
+ break;
+ case S_SUBNORMAL:
+ ret = handle_Subnormal(ctx, expl);
+ break;
+ case S_UNDERFLOW:
+ ret = handle_Underflow(ctx, expl);
+ break;
+ case S_INEXACT:
+ ret = handle_Inexact(ctx, expl);
+ break;
+ case S_ROUNDED:
+ ret = handle_Rounded(ctx, expl);
+ break;
+ };
+ break;
+ case C_INV_CONTEXT:
+ case C_DIV_IMPOSSIBLE:
+ case C_CONV_SYNTAX:
+ if (!PyArg_ParseTuple(args, "OOs:_raise_error", &ctx_type, &ctx, &expl))
+ return NULL;
+ switch (flag) {
+ case C_INV_CONTEXT:
+ return (PyObject *)handle_InvalidContext(ctx_type, ctx, expl);
+ break;
+ case C_DIV_IMPOSSIBLE:
+ return (PyObject *)handle_DivisionImpossible(ctx_type, ctx, expl);
+ break;
+ case C_CONV_SYNTAX:
+ return (PyObject *)handle_ConversionSyntax(ctx_type, ctx, expl);
+ break;
+ };
+ break;
+ case S_INV_OPERATION:
+ if (!PyArg_ParseTuple(args, "OOsO:_raise_error", &ctx_type, &ctx, &expl, &thing))
+ return NULL;
+ return (PyObject *)handle_InvalidOperation(ctx_type, ctx, expl, thing);
+ break;
+ case S_DIV_BY_ZERO:
+ if (!PyArg_ParseTuple(args, "OOsli:_raise_error", &ctx_type, &ctx, &expl, &sign, &two))
+ return NULL;
+ return (PyObject *)handle_DivisionByZero(ctx_type, ctx, expl, sign, two);
+ break;
+ case C_DIV_UNDEFINED:
+ if (!PyArg_ParseTuple(args, "OOsl:_raise_error", &ctx_type, &ctx, &expl, &two))
+ return NULL;
+ return (PyObject *)handle_DivisionUndefined(ctx_type, ctx, expl, two);
+ break;
+ case S_OVERFLOW:
+ if (!PyArg_ParseTuple(args, "OOsl:_raise_error", &ctx_type, &ctx, &expl, &sign))
+ return NULL;
+ return (PyObject *)handle_Overflow(ctx_type, ctx, expl, sign);
+ break;
+ default:
+ printf("flag %d\n", flag);
+ PyErr_SetString(PyExc_ValueError, "bad handler in _do_context_error_dispatch");
+ return NULL;
+ }
+ return PyLong_FromLong((long)ret);
+}
+
+static PyObject *
+context_raise_error(contextobject *self, PyObject *args, PyObject *kwds)
+{
+ static char *kwlist[] = {"explanation", 0};
+ PyObject *condition = NULL;
+ PyObject *explanation = NULL;
+ PyObject *rest = NULL;
+ PyObject *handler;
+ PyObject *dummy;
+ int flag = -1;
+ int i;
+
+ if (!PyArg_ParseTuple(args, "O|OO:_raise_error", &condition, &explanation, &rest))
+ return NULL;
+
+ dummy = PyTuple_New(0);
+ if (dummy == NULL)
+ return NULL;
+
+
+ if (explanation == NULL && !PyArg_ParseTupleAndKeywords(dummy, kwds,
+ "O:_raise_error", kwlist,
+ &explanation))
+ return NULL;
+
+ if ((condition == ConversionSyntax) ||
+ (condition == DivisionImpossible) ||
+ (condition == DivisionUndefined) ||
+ (condition == InvalidContext)) {
+ handler = InvalidOperation;
+ } else {
+ /* reuse the condition */
+ handler = condition;
+ }
+ for (i = 0; i < NUMSIGNALS; i++) {
+ if (errors[i] == handler) {
+ flag = i;
+ break;
+ }
+ }
+
+ if (ISFLAGSET(self->ignored, flag)) {
+ return _do_context_error_dispatch(flag, args);
+ } else {
+ SETFLAG(self->ignored, flag);
+ if (!ISFLAGSET(self->traps, flag)) {
+ for (i = 0; i < NUMSIGNALS; i++) {
+ if (errors[i] == condition) {
+ flag = i;
+ break;
+ }
+ }
+ return _do_context_error_dispatch(flag, args);
+
+ }
+ }
+ PyErr_SetString(handler, PyString_AsString(explanation));
+ return NULL;
+}
static PyMethodDef context_methods[] = {
{"clear_flags", (PyCFunction)context_clear_flags,
METH_NOARGS},
@@ -3190,6 +3331,8 @@
METH_VARARGS},
{"_set_rounding", (PyCFunction)context_set_rounding,
METH_VARARGS},
+ {"_raise_error", (PyCFunction)context_raise_error,
+ METH_VARARGS | METH_KEYWORDS},
{"__copy__", (PyCFunction)context_copy,
METH_NOARGS},
{"_shallow_copy", (PyCFunction)context_copy,
Modified: sandbox/trunk/decimal-c/decimal.py
==============================================================================
--- sandbox/trunk/decimal-c/decimal.py (original)
+++ sandbox/trunk/decimal-c/decimal.py Wed May 24 20:44:03 2006
@@ -2158,32 +2158,6 @@
s.append('traps=[' + ', '.join([t.__name__ for t, v in self.traps.items() if v]) + ']')
return ', '.join(s) + ')'
-## FROM HERE
-
- def _raise_error(self, condition, explanation = None, *args):
- """Handles an error
-
- If the flag is in _ignored_flags, returns the default response.
- Otherwise, it increments the flag, then, if the corresponding
- trap_enabler is set, it reaises the exception. Otherwise, it returns
- the default value after incrementing the flag.
- """
- error = _condition_map.get(condition, condition)
- if error in self._ignored_flags:
- #Don't touch the flag
- return error().handle(self, *args)
-
- self.flags[error] += 1
- if not self.traps[error]:
- #The errors define how to handle themselves.
- return condition().handle(self, *args)
-
- # Errors should only be risked on copies of the context
- #self._ignored_flags = []
- raise error, explanation
-
-## TO HERE
-
def create_decimal(self, num='0'):
"""Creates a new Decimal instance but using self as context."""
d = Decimal(num, context=self)
More information about the Python-checkins
mailing list