blueloveTH 2 years ago
parent
commit
07be627cb8
9 changed files with 9 additions and 71 deletions
  1. 1 1
      docs/features/ub.md
  2. 0 3
      python/_set.py
  3. 1 5
      python/collections.py
  4. 1 1
      src/ceval.h
  5. 0 11
      src/cffi.h
  6. 0 8
      src/linalg.h
  7. 2 32
      src/pocketpy.h
  8. 0 1
      src/str.h
  9. 4 9
      src/vm.h

+ 1 - 1
docs/features/ub.md

@@ -10,5 +10,5 @@ These are the undefined behaviours of pkpy. The behaviour of pkpy is undefined i
 3. Use goto statement to jump out of a context block.
 4. Type `T`'s `__new__` returns an object that is not an instance of `T`.
 5. Call `__new__` with a type that is not a subclass of `type`.
-6. `__eq__`, `__ne__` or `__contains__`, etc.. returns a value that is not a boolean.
+6. `__eq__`, `__lt__` or `__contains__`, etc.. returns a value that is not a boolean.
 7. Division by zero.

+ 0 - 3
python/_set.py

@@ -74,9 +74,6 @@ class set:
     def __eq__(self, other):
         return self.__xor__(other).__len__() == 0
 
-    def __ne__(self, other):
-        return self.__xor__(other).__len__() != 0
-    
     def isdisjoint(self, other):
         return self.__and__(other).__len__() == 0
     

+ 1 - 5
python/collections.py

@@ -72,10 +72,6 @@ class deque:
                 return False
             t1, t2 = t1.next, t2.next
         return True
-    
-    def __ne__(self, __o: object) -> bool:
-        return not self == __o
-
 
 def Counter(iterable):
     a = {}
@@ -84,4 +80,4 @@ def Counter(iterable):
             a[x] += 1
         else:
             a[x] = 1
-    return a
+    return a

+ 1 - 1
src/ceval.h

@@ -337,7 +337,7 @@ __NEXT_STEP:;
     TARGET(COMPARE_NE)
         _1 = POPX();
         _0 = TOP();
-        TOP() = VAR(py_not_equals(_0, _1));
+        TOP() = VAR(!py_equals(_0, _1));
         DISPATCH()
     TARGET(COMPARE_GT)
         BINARY_OP_SPECIAL(__gt__);

+ 0 - 11
src/cffi.h

@@ -62,10 +62,6 @@ struct VoidP{
             if(!is_non_tagged_type(rhs, VoidP::_type(vm))) return false;
             return _CAST(VoidP&, lhs) == _CAST(VoidP&, rhs);
         });
-        vm->bind__ne__(OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
-            if(!is_non_tagged_type(rhs, VoidP::_type(vm))) return true;
-            return _CAST(VoidP&, lhs) != _CAST(VoidP&, rhs);
-        });
         vm->bind__gt__(OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
             return _CAST(VoidP&, lhs).ptr > CAST(VoidP&, rhs).ptr;
         });
@@ -229,13 +225,6 @@ struct C99Struct{
             return self.size == other.size && memcmp(self.p, other.p, self.size) == 0;
         });
 
-        vm->bind__ne__(OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
-            C99Struct& self = _CAST(C99Struct&, lhs);
-            if(!is_non_tagged_type(rhs, C99Struct::_type(vm))) return true;
-            C99Struct& other = _CAST(C99Struct&, rhs);
-            return self.size != other.size || memcmp(self.p, other.p, self.size) != 0;
-        });
-
         // patch VoidP
         type = vm->_t(VoidP::_type(vm));
 

+ 0 - 8
src/linalg.h

@@ -356,7 +356,6 @@ struct PyVec2: Vec2 {
         BIND_VEC_FLOAT_OP(2, __mul__, *)
         BIND_VEC_FLOAT_OP(2, __truediv__, /)
         BIND_VEC_VEC_OP(2, __eq__, ==)
-        BIND_VEC_VEC_OP(2, __ne__, !=)
         BIND_VEC_FIELD(2, x)
         BIND_VEC_FIELD(2, y)
         BIND_VEC_FUNCTION_1(2, dot)
@@ -399,7 +398,6 @@ struct PyVec3: Vec3 {
         BIND_VEC_FLOAT_OP(3, __mul__, *)
         BIND_VEC_FLOAT_OP(3, __truediv__, /)
         BIND_VEC_VEC_OP(3, __eq__, ==)
-        BIND_VEC_VEC_OP(3, __ne__, !=)
         BIND_VEC_FIELD(3, x)
         BIND_VEC_FIELD(3, y)
         BIND_VEC_FIELD(3, z)
@@ -573,12 +571,6 @@ struct PyMat3x3: Mat3x3{
             return VAR(self == other);
         });
 
-        vm->bind_method<1>(type, "__ne__", [](VM* vm, ArgsView args){
-            PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
-            PyMat3x3& other = CAST(PyMat3x3&, args[1]);
-            return VAR(self != other);
-        });
-
         vm->bind_method<0>(type, "determinant", [](VM* vm, ArgsView args){
             PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
             return VAR(self.determinant());

+ 2 - 32
src/pocketpy.h

@@ -70,7 +70,6 @@ inline void init_builtins(VM* _vm) {
     BIND_NUM_LOGICAL_OPT(__gt__, >, false)
     BIND_NUM_LOGICAL_OPT(__ge__, >=, false)
     BIND_NUM_LOGICAL_OPT(__eq__, ==, true)
-    BIND_NUM_LOGICAL_OPT(__ne__, !=, true)
 
 #undef BIND_NUM_ARITH_OPT
 #undef BIND_NUM_LOGICAL_OPT
@@ -206,7 +205,6 @@ inline void init_builtins(VM* _vm) {
     });
 
     _vm->bind__eq__(_vm->tp_object, [](VM* vm, PyObject* lhs, PyObject* rhs) { return lhs == rhs; });
-    _vm->bind__ne__(_vm->tp_object, [](VM* vm, PyObject* lhs, PyObject* rhs) { return lhs != rhs; });
     _vm->bind__hash__(_vm->tp_object, [](VM* vm, PyObject* obj) { return BITS(obj); });
 
     _vm->bind_constructor<2>("type", CPP_LAMBDA(vm->_t(args[1])));
@@ -390,10 +388,6 @@ inline void init_builtins(VM* _vm) {
         if(!is_non_tagged_type(rhs, vm->tp_str)) return false;
         return _CAST(Str&, lhs) == _CAST(Str&, rhs);
     });
-    _vm->bind__ne__(_vm->tp_str, [](VM* vm, PyObject* lhs, PyObject* rhs) {
-        if(!is_non_tagged_type(rhs, vm->tp_str)) return true;
-        return _CAST(Str&, lhs) != _CAST(Str&, rhs);
-    });
     _vm->bind__gt__(_vm->tp_str, [](VM* vm, PyObject* lhs, PyObject* rhs) {
         return _CAST(Str&, lhs) > CAST(Str&, rhs);
     });
@@ -506,15 +500,11 @@ inline void init_builtins(VM* _vm) {
         List& b = _CAST(List&, rhs);
         if(a.size() != b.size()) return false;
         for(int i=0; i<a.size(); i++){
-            if(vm->py_not_equals(a[i], b[i])) return false;
+            if(!vm->py_equals(a[i], b[i])) return false;
         }
         return true;
     });
 
-    _vm->bind__ne__(_vm->tp_list, [](VM* vm, PyObject* lhs, PyObject* rhs) {
-        return !vm->py_equals(lhs, rhs);
-    });
-
     _vm->bind_method<1>("list", "index", [](VM* vm, ArgsView args) {
         List& self = _CAST(List&, args[0]);
         PyObject* obj = args[1];
@@ -663,15 +653,11 @@ inline void init_builtins(VM* _vm) {
         const Tuple& other = CAST(Tuple&, rhs);
         if(self.size() != other.size()) return false;
         for(int i = 0; i < self.size(); i++) {
-            if(vm->py_not_equals(self[i], other[i])) return false;
+            if(!vm->py_equals(self[i], other[i])) return false;
         }
         return true;
     });
 
-    _vm->bind__ne__(_vm->tp_tuple, [](VM* vm, PyObject* lhs, PyObject* rhs) {
-        return !vm->py_equals(lhs, rhs);
-    });
-
     _vm->bind__hash__(_vm->tp_tuple, [](VM* vm, PyObject* obj) {
         i64 x = 1000003;
         const Tuple& items = CAST(Tuple&, obj);
@@ -718,9 +704,6 @@ inline void init_builtins(VM* _vm) {
     _vm->bind__eq__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) {
         return _CAST(bool, lhs) == CAST(bool, rhs);
     });
-    _vm->bind__ne__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) {
-        return _CAST(bool, lhs) != CAST(bool, rhs);
-    });
     _vm->bind__repr__(_vm->_type("ellipsis"), [](VM* vm, PyObject* self) {
         return VAR("Ellipsis");
     });
@@ -787,10 +770,6 @@ inline void init_builtins(VM* _vm) {
     _vm->bind__eq__(_vm->tp_bytes, [](VM* vm, PyObject* lhs, PyObject* rhs) {
         return _CAST(Bytes&, lhs) == _CAST(Bytes&, rhs);
     });
-    _vm->bind__ne__(_vm->tp_bytes, [](VM* vm, PyObject* lhs, PyObject* rhs) {
-        return _CAST(Bytes&, lhs) != _CAST(Bytes&, rhs);
-    });
-
     /************ slice ************/
     _vm->bind_constructor<4>("slice", [](VM* vm, ArgsView args) {
         return VAR(Slice(args[1], args[2], args[3]));
@@ -1016,10 +995,6 @@ inline void init_builtins(VM* _vm) {
         return true;
     });
 
-    _vm->bind__ne__(_vm->tp_dict, [](VM* vm, PyObject* a, PyObject* b) {
-        return !vm->py_equals(a, b);
-    });
-
     RangeIter::register_class(_vm, _vm->builtins);
     ArrayIter::register_class(_vm, _vm->builtins);
     StringIter::register_class(_vm, _vm->builtins);
@@ -1338,11 +1313,6 @@ inline void VM::post_init(){
         if(!is_non_tagged_type(rhs, vm->tp_bound_method)) return false;
         return _CAST(BoundMethod&, lhs) == _CAST(BoundMethod&, rhs);
     });
-    bind__ne__(tp_bound_method, [](VM* vm, PyObject* lhs, PyObject* rhs){
-        if(!is_non_tagged_type(rhs, vm->tp_bound_method)) return true;
-        return _CAST(BoundMethod&, lhs) != _CAST(BoundMethod&, rhs);
-    });
-
     _t(tp_slice)->attr().set("start", property([](VM* vm, ArgsView args){
         return CAST(Slice&, args[0]).start;
     }));

+ 0 - 1
src/str.h

@@ -389,7 +389,6 @@ const StrName __neg__ = StrName::get("__neg__");        // unused
 const StrName __bool__ = StrName::get("__bool__");      // unused
 // logical operators
 const StrName __eq__ = StrName::get("__eq__");
-const StrName __ne__ = StrName::get("__ne__");
 const StrName __lt__ = StrName::get("__lt__");
 const StrName __le__ = StrName::get("__le__");
 const StrName __gt__ = StrName::get("__gt__");

+ 4 - 9
src/vm.h

@@ -67,7 +67,6 @@ struct PyTypeInfo{
     PyObject* (*m__bool__)(VM* vm, PyObject*) = nullptr;
 
     bool (*m__eq__)(VM* vm, PyObject*, PyObject*) = nullptr;
-    bool (*m__ne__)(VM* vm, PyObject*, PyObject*) = nullptr;
     bool (*m__lt__)(VM* vm, PyObject*, PyObject*) = nullptr;
     bool (*m__le__)(VM* vm, PyObject*, PyObject*) = nullptr;
     bool (*m__gt__)(VM* vm, PyObject*, PyObject*) = nullptr;
@@ -325,6 +324,10 @@ public:
         return &_all_types[OBJ_GET(Type, obj)];
     }
 
+    PyTypeInfo* _type_info(Type type){
+        return &_all_types[type];
+    }
+
     const PyTypeInfo* _inst_type_info(PyObject* obj){
         if(is_int(obj)) return &_all_types[tp_int];
         if(is_float(obj)) return &_all_types[tp_float];
@@ -365,7 +368,6 @@ public:
     }
 
     BIND_LOGICAL_SPECIAL(__eq__)
-    BIND_LOGICAL_SPECIAL(__ne__)
     BIND_LOGICAL_SPECIAL(__lt__)
     BIND_LOGICAL_SPECIAL(__le__)
     BIND_LOGICAL_SPECIAL(__gt__)
@@ -438,13 +440,6 @@ public:
         return call_method(lhs, __eq__, rhs) == True;
     }
 
-    bool py_not_equals(PyObject* lhs, PyObject* rhs){
-        if(lhs == rhs) return false;
-        const PyTypeInfo* ti = _inst_type_info(lhs);
-        if(ti->m__ne__) return ti->m__ne__(this, lhs, rhs);
-        return call_method(lhs, __ne__, rhs) == True;
-    }
-
     template<int ARGC>
     PyObject* bind_func(Str type, Str name, NativeFuncC fn) {
         return bind_func<ARGC>(_find_type_object(type), name, fn);