diff --git a/src/vm.cpp b/src/vm.cpp index 13ba14bd..4c712d82 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -360,7 +360,9 @@ i64 VM::py_hash(PyObject* obj){ PyObject* ret = call_method(self, f); return CAST(i64, ret); } - // it flow reaches here, obj must not be the trivial `object` type + // if it is trivial `object`, return PK_BITS + if(ti == &_all_types[tp_object]) return PK_BITS(obj); + // otherwise, we check if it has a custom __eq__ other than object.__eq__ bool has_custom_eq = false; if(ti->m__eq__) has_custom_eq = true; else{ diff --git a/tests/99_builtin_func.py b/tests/99_builtin_func.py index 0e53819e..90f4b3f3 100644 --- a/tests/99_builtin_func.py +++ b/tests/99_builtin_func.py @@ -845,6 +845,18 @@ try: except TypeError: pass +a = hash(object()) # object is hashable +a = hash(A()) # A is hashable +class B: + def __eq__(self, o): return True + +try: + hash(B()) + print('未能拦截错误, 在测试 B.__hash__') + exit(1) +except TypeError: + pass + # 未完全测试准确性----------------------------------------------- # 116: 1009: _vm->bind__repr__(_vm->tp_mappingproxy, [](VM* vm, PyObject* obj) { # #####: 1010: MappingProxy& self = _CAST(MappingProxy&, obj);