blueloveTH 2 лет назад
Родитель
Сommit
8c4ccaa240
2 измененных файлов с 15 добавлено и 1 удалено
  1. 3 1
      src/vm.cpp
  2. 12 0
      tests/99_builtin_func.py

+ 3 - 1
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{

+ 12 - 0
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);