Skip to content

Commit 438d504

Browse files
committed
objtype: Add equality test for type types.
1 parent 91cbe60 commit 438d504

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

py/objtype.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,16 @@ STATIC bool type_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
367367
return false;
368368
}
369369

370+
STATIC mp_obj_t type_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
371+
switch (op) {
372+
case MP_BINARY_OP_EQUAL:
373+
// Types can be equal only if it's the same type structure,
374+
// we don't even need to check for 2nd arg type.
375+
return MP_BOOL(lhs_in == rhs_in);
376+
}
377+
return NULL;
378+
}
379+
370380
const mp_obj_type_t mp_type_type = {
371381
{ &mp_type_type },
372382
.name = MP_QSTR_type,
@@ -375,6 +385,7 @@ const mp_obj_type_t mp_type_type = {
375385
.call = type_call,
376386
.load_attr = type_load_attr,
377387
.store_attr = type_store_attr,
388+
.binary_op = type_binary_op,
378389
};
379390

380391
mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@
44
print(hash(list) != 0)
55
class Foo: pass
66
print(hash(Foo) != 0)
7+
8+
print(int == int)
9+
print(int != list)
10+
11+
d = {}
12+
d[int] = float

0 commit comments

Comments
 (0)