summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
authorRachel Dowdall <rjdowdall@gmail.com>2014-03-20 22:40:38 +0000
committerRachel Dowdall <rjdowdall@gmail.com>2014-03-20 22:40:38 +0000
commit300c8bd4c2c0c6d626a6aaeb873c22a8f8b9fc3f (patch)
tree96a2e445c19b7535a708b89aeddc378727366148 /py
parenta2f2f734ed5ee1294a44592448e5b0f45a12e254 (diff)
downloadmicropython-300c8bd4c2c0c6d626a6aaeb873c22a8f8b9fc3f.tar.gz
micropython-300c8bd4c2c0c6d626a6aaeb873c22a8f8b9fc3f.zip
Added ZeroDivisionError to float division.
Diffstat (limited to 'py')
-rw-r--r--py/obj.h1
-rw-r--r--py/objexcept.c1
-rw-r--r--py/objfloat.c9
-rw-r--r--py/qstrdefs.h1
-rw-r--r--py/runtime.c1
5 files changed, 11 insertions, 2 deletions
diff --git a/py/obj.h b/py/obj.h
index 1daa943447..8d40c3f2dd 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -201,6 +201,7 @@ extern const mp_obj_type_t mp_type_OverflowError;
extern const mp_obj_type_t mp_type_OSError;
extern const mp_obj_type_t mp_type_NotImplementedError;
extern const mp_obj_type_t mp_type_StopIteration;
+extern const mp_obj_type_t mp_type_ZeroDivisionError;
// Constant objects, globally accessible
diff --git a/py/objexcept.c b/py/objexcept.c
index 11177724da..3f636ffc6d 100644
--- a/py/objexcept.c
+++ b/py/objexcept.c
@@ -93,6 +93,7 @@ MP_DEFINE_EXCEPTION(OverflowError, BaseException)
MP_DEFINE_EXCEPTION(OSError, BaseException)
MP_DEFINE_EXCEPTION(NotImplementedError, BaseException)
MP_DEFINE_EXCEPTION(StopIteration, BaseException)
+MP_DEFINE_EXCEPTION(ZeroDivisionError, BaseException)
mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
return mp_obj_new_exception_msg_varg(exc_type, NULL);
diff --git a/py/objfloat.c b/py/objfloat.c
index 401c1145ee..704b3d5998 100644
--- a/py/objfloat.c
+++ b/py/objfloat.c
@@ -1,5 +1,6 @@
#include <stdlib.h>
#include <assert.h>
+#include <math.h>
#include "nlr.h"
#include "misc.h"
@@ -105,8 +106,12 @@ mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs_in) {
case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: val = lhs_val / rhs_val; break;
*/
case RT_BINARY_OP_TRUE_DIVIDE:
- case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: lhs_val /= rhs_val; break;
-
+ case RT_BINARY_OP_INPLACE_TRUE_DIVIDE:
+ lhs_val /= rhs_val;
+ if (isinf(lhs_val)){ // check for division by zero
+ nlr_jump(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "float division by zero"));
+ }
+ break;
case RT_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val);
case RT_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val);
case RT_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val);
diff --git a/py/qstrdefs.h b/py/qstrdefs.h
index 649f89eb1e..929fcaf061 100644
--- a/py/qstrdefs.h
+++ b/py/qstrdefs.h
@@ -47,6 +47,7 @@ Q(SyntaxError)
Q(TypeError)
Q(ValueError)
Q(OverflowError)
+Q(ZeroDivisionError)
Q(NoneType)
diff --git a/py/runtime.c b/py/runtime.c
index 9c8ba636c0..b57a74fa38 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -158,6 +158,7 @@ STATIC const mp_builtin_elem_t builtin_table[] = {
{ MP_QSTR_OSError, (mp_obj_t)&mp_type_OSError },
{ MP_QSTR_NotImplementedError, (mp_obj_t)&mp_type_NotImplementedError },
{ MP_QSTR_StopIteration, (mp_obj_t)&mp_type_StopIteration },
+ { MP_QSTR_ZeroDivisionError, (mp_obj_t)&mp_type_ZeroDivisionError },
// Extra builtins as defined by a port
MICROPY_EXTRA_BUILTINS