summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--py/runtime.c12
-rw-r--r--tests/basics/tests/is-isnot.py14
2 files changed, 26 insertions, 0 deletions
diff --git a/py/runtime.c b/py/runtime.c
index f43e804b40..b2d9c7e960 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -461,6 +461,18 @@ mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
// then fail
// note that list does not implement + or +=, so that inplace_concat is reached first for +=
+ // deal with is, is not
+ if (op == RT_COMPARE_OP_IS) {
+ // TODO: may need to handle strings specially, CPython appears to
+ // assume all strings are interned (so "is" == "==" for strings)
+ return MP_BOOL(lhs == rhs);
+ }
+ if (op == RT_COMPARE_OP_IS_NOT) {
+ // TODO: may need to handle strings specially, CPython appears to
+ // assume all strings are interned (so "is" == "==" for strings)
+ return MP_BOOL(lhs != rhs);
+ }
+
// deal with == and != for all types
if (op == RT_COMPARE_OP_EQUAL || op == RT_COMPARE_OP_NOT_EQUAL) {
if (mp_obj_equal(lhs, rhs)) {
diff --git a/tests/basics/tests/is-isnot.py b/tests/basics/tests/is-isnot.py
new file mode 100644
index 0000000000..990190aa41
--- /dev/null
+++ b/tests/basics/tests/is-isnot.py
@@ -0,0 +1,14 @@
+print(1 is 1)
+print(1 is 2)
+print(1 is not 1)
+print(1 is not 2)
+
+
+print([1, 2] is [1, 2])
+a = [1, 2]
+b = a
+print(b is a)
+
+# TODO: strings require special "is" handling, postponed
+# until qstr refactor.
+#print("a" is "a")