summaryrefslogtreecommitdiffstatshomepage
path: root/py/obj.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/obj.c')
-rw-r--r--py/obj.c43
1 files changed, 19 insertions, 24 deletions
diff --git a/py/obj.c b/py/obj.c
index 866b9aaefa..9ca3d5d16b 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -13,7 +13,6 @@
#include "runtime0.h"
#include "runtime.h"
#include "map.h"
-#include "strtonum.h"
mp_obj_t mp_obj_get_type(mp_obj_t o_in) {
if (MP_OBJ_IS_SMALL_INT(o_in)) {
@@ -42,21 +41,21 @@ void printf_wrapper(void *env, const char *fmt, ...) {
va_end(args);
}
-void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
+void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
if (MP_OBJ_IS_SMALL_INT(o_in)) {
print(env, "%d", (int)MP_OBJ_SMALL_INT_VALUE(o_in));
} else {
mp_obj_base_t *o = o_in;
if (o->type->print != NULL) {
- o->type->print(print, env, o_in);
+ o->type->print(print, env, o_in, kind);
} else {
print(env, "<%s>", o->type->name);
}
}
}
-void mp_obj_print(mp_obj_t o_in) {
- mp_obj_print_helper(printf_wrapper, NULL, o_in);
+void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
+ mp_obj_print_helper(printf_wrapper, NULL, o_in, kind);
}
bool mp_obj_is_callable(mp_obj_t o_in) {
@@ -109,15 +108,28 @@ bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
return val == 0;
} else if (o2 == mp_const_true) {
return val == 1;
- } else {
- return false;
+ } else if (MP_OBJ_IS_TYPE(o2, &int_type)) {
+ // If o2 is long int, dispatch to its virtual methods
+ mp_obj_base_t *o = o2;
+ if (o->type->binary_op != NULL) {
+ mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o2, o1);
+ return r == mp_const_true ? true : false;
+ }
}
+ return false;
}
} else if (MP_OBJ_IS_QSTR(o1) || MP_OBJ_IS_QSTR(o2)) {
return false;
} else if (MP_OBJ_IS_TYPE(o1, &str_type) && MP_OBJ_IS_TYPE(o2, &str_type)) {
return mp_obj_str_get(o1) == mp_obj_str_get(o2);
} else {
+ mp_obj_base_t *o = o1;
+ if (o->type->binary_op != NULL) {
+ mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o1, o2);
+ if (r != MP_OBJ_NULL) {
+ return r == mp_const_true ? true : false;
+ }
+ }
// TODO: Debugging helper
printf("Equality for '%s' and '%s' types not yet implemented\n", mp_obj_get_type_str(o1), mp_obj_get_type_str(o2));
assert(0);
@@ -137,12 +149,6 @@ bool mp_obj_less(mp_obj_t o1, mp_obj_t o2) {
}
machine_int_t mp_obj_get_int(mp_obj_t arg) {
- return mp_obj_get_int_base(arg, 0);
-}
-
-machine_int_t mp_obj_get_int_base(mp_obj_t arg, mp_obj_t base_arg) {
- const char *value;
- int base;
if (arg == mp_const_false) {
return 0;
} else if (arg == mp_const_true) {
@@ -154,17 +160,6 @@ machine_int_t mp_obj_get_int_base(mp_obj_t arg, mp_obj_t base_arg) {
// TODO work out if this should be floor, ceil or trunc
return (machine_int_t)mp_obj_float_get(arg);
#endif
- } else if (MP_OBJ_IS_TYPE(arg, &str_type)) {
- if (base_arg == 0) {
- value = qstr_str(mp_obj_str_get(arg));
- return (machine_int_t)strtonum(value, 0);
- } else if (MP_OBJ_IS_SMALL_INT(base_arg)) {
- base = MP_OBJ_SMALL_INT_VALUE(base_arg);
- value = qstr_str(mp_obj_str_get(arg));
- return (machine_int_t)strtonum(value, base);
- } else {
- nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "an integer is required"));
- }
} else {
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "can't convert %s to int", mp_obj_get_type_str(arg)));
}