summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/objfloat.c2
-rw-r--r--py/objint.c19
-rw-r--r--py/objint_longlong.c8
-rw-r--r--py/objint_mpz.c9
-rw-r--r--py/objtype.c69
5 files changed, 62 insertions, 45 deletions
diff --git a/py/objfloat.c b/py/objfloat.c
index 3916c340b..d7bc6c7ff 100644
--- a/py/objfloat.c
+++ b/py/objfloat.c
@@ -124,6 +124,8 @@ check_zero_division:
nlr_jump(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "float division by zero"));
}
break;
+ case MP_BINARY_OP_POWER:
+ case MP_BINARY_OP_INPLACE_POWER: lhs_val = MICROPY_FLOAT_C_FUN(pow)(lhs_val, rhs_val); break;
case MP_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val);
case MP_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val);
case MP_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val);
diff --git a/py/objint.c b/py/objint.c
index 27834b81f..3a853eab8 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -1,3 +1,4 @@
+#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
@@ -9,6 +10,8 @@
#include "parsenum.h"
#include "mpz.h"
#include "objint.h"
+#include "runtime0.h"
+#include "runtime.h"
#if MICROPY_ENABLE_FLOAT
#include <math.h>
@@ -59,16 +62,20 @@ void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj
}
}
-// This is called only for non-SMALL_INT
+// This is called for operations on SMALL_INT that are not handled by mp_unary_op
mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
- assert(0);
- return mp_const_none;
+ return MP_OBJ_NULL;
}
-// This is called only for non-SMALL_INT
+// This is called for operations on SMALL_INT that are not handled by mp_binary_op
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
- assert(0);
- return mp_const_none;
+ if (op == MP_BINARY_OP_MULTIPLY) {
+ if (MP_OBJ_IS_STR(rhs_in) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_list)) {
+ // multiply is commutative for these types, so delegate to them
+ return mp_binary_op(op, rhs_in, lhs_in);
+ }
+ }
+ return MP_OBJ_NULL;
}
// This is called only with strings whose value doesn't fit in SMALL_INT
diff --git a/py/objint_longlong.c b/py/objint_longlong.c
index e64cc6734..acbd477a9 100644
--- a/py/objint_longlong.c
+++ b/py/objint_longlong.c
@@ -9,6 +9,7 @@
#include "mpz.h"
#include "objint.h"
#include "runtime0.h"
+#include "runtime.h"
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
@@ -57,6 +58,13 @@ mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
} else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
rhs_val = ((mp_obj_int_t*)rhs_in)->val;
} else {
+ if (op == MP_BINARY_OP_MULTIPLY) {
+ if (MP_OBJ_IS_STR(rhs_in) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_list)) {
+ // multiply is commutative for these types, so delegate to them
+ return mp_binary_op(op, rhs_in, lhs_in);
+ }
+ }
+ // unsupported operation/type
return MP_OBJ_NULL;
}
diff --git a/py/objint_mpz.c b/py/objint_mpz.c
index 7f12fbcd0..27f77d14b 100644
--- a/py/objint_mpz.c
+++ b/py/objint_mpz.c
@@ -11,6 +11,7 @@
#include "mpz.h"
#include "objint.h"
#include "runtime0.h"
+#include "runtime.h"
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
@@ -74,7 +75,13 @@ mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
return mp_obj_complex_binary_op(op, mpz_as_float(zlhs), 0, rhs_in);
#endif
} else {
- // unsupported type
+ if (op == MP_BINARY_OP_MULTIPLY) {
+ if (MP_OBJ_IS_STR(rhs_in) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_list)) {
+ // multiply is commutative for these types, so delegate to them
+ return mp_binary_op(op, rhs_in, lhs_in);
+ }
+ }
+ // unsupported operation/type
return MP_OBJ_NULL;
}
diff --git a/py/objtype.c b/py/objtype.c
index 11200c913..f4ce6a4f8 100644
--- a/py/objtype.c
+++ b/py/objtype.c
@@ -184,7 +184,31 @@ STATIC const qstr binary_op_method_name[] = {
[MP_BINARY_OP_EXCEPTION_MATCH] = MP_QSTR_, // not implemented, used to make sure array has full size
};
+// Given a member that was extracted from an instance, convert it correctly
+// and put the result in the dest[] array for a possible method call.
+// Conversion means dealing with static/class methods, callables, and values.
+// see http://docs.python.org/3.3/howto/descriptor.html
+STATIC void class_convert_return_attr(mp_obj_t self, mp_obj_t member, mp_obj_t *dest) {
+ if (MP_OBJ_IS_TYPE(member, &mp_type_staticmethod)) {
+ // return just the function
+ dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
+ } else if (MP_OBJ_IS_TYPE(member, &mp_type_classmethod)) {
+ // return a bound method, with self being the type of this object
+ dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
+ dest[1] = mp_obj_get_type(self);
+ } else if (mp_obj_is_callable(member)) {
+ // return a bound method, with self being this object
+ dest[0] = member;
+ dest[1] = self;
+ } else {
+ // class member is a value, so just return that value
+ dest[0] = member;
+ }
+}
+
STATIC mp_obj_t class_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
+ // Note: For ducktyping, CPython does not look in the instance members or use
+ // __getattr__ or __getattribute__. It only looks in the class dictionary.
mp_obj_class_t *lhs = lhs_in;
qstr op_name = binary_op_method_name[op];
if (op_name == 0) {
@@ -192,7 +216,11 @@ STATIC mp_obj_t class_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
}
mp_obj_t member = mp_obj_class_lookup(lhs->base.type, op_name);
if (member != MP_OBJ_NULL) {
- return mp_call_function_2(member, lhs_in, rhs_in);
+ mp_obj_t dest[3];
+ dest[1] = MP_OBJ_NULL;
+ class_convert_return_attr(lhs_in, member, dest);
+ dest[2] = rhs_in;
+ return mp_call_method_n_kw(1, 0, dest);
} else {
return MP_OBJ_NULL;
}
@@ -209,24 +237,7 @@ STATIC void class_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
}
mp_obj_t member = mp_obj_class_lookup(self->base.type, attr);
if (member != MP_OBJ_NULL) {
- // check if the methods are functions, static or class methods
- // see http://docs.python.org/3.3/howto/descriptor.html
- // TODO check that this is the correct place to have this logic
- if (MP_OBJ_IS_TYPE(member, &mp_type_staticmethod)) {
- // return just the function
- dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
- } else if (MP_OBJ_IS_TYPE(member, &mp_type_classmethod)) {
- // return a bound method, with self being the type of this object
- dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
- dest[1] = mp_obj_get_type(self_in);
- } else if (mp_obj_is_callable(member)) {
- // return a bound method, with self being this object
- dest[0] = member;
- dest[1] = self_in;
- } else {
- // class member is a value, so just return that value
- dest[0] = member;
- }
+ class_convert_return_attr(self_in, member, dest);
return;
}
@@ -432,25 +443,7 @@ STATIC void super_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
assert(MP_OBJ_IS_TYPE(items[i], &mp_type_type));
mp_obj_t member = mp_obj_class_lookup((mp_obj_type_t*)items[i], attr);
if (member != MP_OBJ_NULL) {
- // XXX this and the code in class_load_attr need to be factored out
- // check if the methods are functions, static or class methods
- // see http://docs.python.org/3.3/howto/descriptor.html
- // TODO check that this is the correct place to have this logic
- if (MP_OBJ_IS_TYPE(member, &mp_type_staticmethod)) {
- // return just the function
- dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
- } else if (MP_OBJ_IS_TYPE(member, &mp_type_classmethod)) {
- // return a bound method, with self being the type of this object
- dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
- dest[1] = mp_obj_get_type(self->obj);
- } if (mp_obj_is_callable(member)) {
- // return a bound method, with self being this object
- dest[0] = member;
- dest[1] = self->obj;
- } else {
- // class member is a value, so just return that value
- dest[0] = member;
- }
+ class_convert_return_attr(self, member, dest);
return;
}
}