summaryrefslogtreecommitdiff
path: root/Modules/mathmodule.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1991-12-16 15:44:24 +0000
committerGuido van Rossum <guido@python.org>1991-12-16 15:44:24 +0000
commit27ffb09cfacb6aa7ebc2e01521ee4d1f6028eebe (patch)
tree2d4161aef02113813f02a22d3e0b454e7dd80d49 /Modules/mathmodule.c
parente83dba00b638af93dd3a3a8d75e86bfb01a637d6 (diff)
downloadcpython-27ffb09cfacb6aa7ebc2e01521ee4d1f6028eebe.tar.gz
Improve error handling.
Diffstat (limited to 'Modules/mathmodule.c')
-rw-r--r--Modules/mathmodule.c35
1 files changed, 30 insertions, 5 deletions
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index 751f02b2b7..fdc576d609 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -35,6 +35,26 @@ extern int errno;
#include <math.h>
+#ifdef HUGE_VAL
+#define CHECK(x) if (errno != 0) ; \
+ else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
+ else errno = ERANGE
+#else
+#define CHECK(x) /* Don't know how to check */
+#endif
+
+static object *
+math_error()
+{
+ if (errno == EDOM)
+ err_setstr(ValueError, "math domain error");
+ else if (errno == ERANGE)
+ err_setstr(OverflowError, "math range error");
+ else
+ err_errno(RuntimeError);
+ return NULL;
+}
+
static object *
math_1(args, func)
object *args;
@@ -45,8 +65,9 @@ math_1(args, func)
return NULL;
errno = 0;
x = (*func)(x);
+ CHECK(x);
if (errno != 0)
- return err_errno(RuntimeError);
+ return math_error();
else
return newfloatobject(x);
}
@@ -61,8 +82,9 @@ math_2(args, func)
return NULL;
errno = 0;
x = (*func)(x, y);
+ CHECK(x);
if (errno != 0)
- return err_errno(RuntimeError);
+ return math_error();
else
return newfloatobject(x);
}
@@ -120,8 +142,9 @@ math_frexp(self, args)
return NULL;
errno = 0;
x = frexp(x, &i);
+ CHECK(x);
if (errno != 0)
- return err_errno(RuntimeError);
+ return math_error();
v = newtupleobject(2);
if (v != NULL) {
settupleitem(v, 0, newfloatobject(x));
@@ -145,8 +168,9 @@ math_ldexp(self, args)
return NULL;
errno = 0;
x = ldexp(x, (int)y);
+ CHECK(x);
if (errno != 0)
- return err_errno(RuntimeError);
+ return math_error();
else
return newfloatobject(x);
}
@@ -162,8 +186,9 @@ math_modf(self, args)
return NULL;
errno = 0;
x = modf(x, &y);
+ CHECK(x);
if (errno != 0)
- return err_errno(RuntimeError);
+ return math_error();
v = newtupleobject(2);
if (v != NULL) {
settupleitem(v, 0, newfloatobject(x));