blueloveTH 2 年 前
コミット
66052fadd5
3 ファイル変更27 行追加1 行削除
  1. 7 0
      src/obj.h
  2. 12 0
      src/pocketpy.h
  3. 8 1
      tests/99_bugs.py

+ 7 - 0
src/obj.h

@@ -56,6 +56,13 @@ struct BoundMethod {
     PyObject* self;
     PyObject* func;
     BoundMethod(PyObject* self, PyObject* func) : self(self), func(func) {}
+    
+    bool operator==(const BoundMethod& rhs) const noexcept {
+        return self == rhs.self && func == rhs.func;
+    }
+    bool operator!=(const BoundMethod& rhs) const noexcept {
+        return self != rhs.self || func != rhs.func;
+    }
 };
 
 struct Range {

+ 12 - 0
src/pocketpy.h

@@ -972,6 +972,18 @@ inline void VM::post_init(){
         return CAST(BoundMethod&, args[0]).func;
     }));
 
+    vm->bind_method<1>(_t(tp_bound_method), "__eq__", [](VM* vm, ArgsView args){
+        if(!is_non_tagged_type(args[1], vm->tp_bound_method)) return vm->False;
+        bool ok = _CAST(BoundMethod&, args[0]) == _CAST(BoundMethod&, args[1]);
+        return VAR(ok);
+    });
+
+    vm->bind_method<1>(_t(tp_bound_method), "__ne__", [](VM* vm, ArgsView args){
+        if(!is_non_tagged_type(args[1], vm->tp_bound_method)) return vm->True;
+        bool ok = _CAST(BoundMethod&, args[0]) != _CAST(BoundMethod&, args[1]);
+        return VAR(ok);
+    });
+
     _t(tp_slice)->attr().set("start", property([](VM* vm, ArgsView args){
         return CAST(Slice&, args[0]).start;
     }));

+ 8 - 1
tests/99_bugs.py

@@ -11,4 +11,11 @@ def f(x):
         return 1
 
 assert f(2) == 1
-assert f(0) == None
+assert f(0) == None
+
+a = [1, 2]
+b = [3, 4]
+assert a.append == a.append
+assert a.append is not a.append
+assert a.append is not b.append
+assert a.append != b.append