Example #1
0
 def inplaceOperator(self, op):
     x, y = self.vm.popn(2)
     if op == "POWER":
         x **= y
     elif op == "MULTIPLY":
         x *= y
     elif op == "DIVIDE":
         # Overwritten __div__ is not picked up by x //= y
         # which seems to puck up FLOOR_DIVIDE
         # See Python 2.7 test_augassign.py
         if hasattr(x, "__idiv__"):
             x = x.__idiv__(y)
         else:
             x //= y
     elif op == "FLOOR_DIVIDE":
         x //= y
     elif op == "TRUE_DIVIDE":
         x /= y
     elif op == "MODULO":
         x %= y
     elif op == "ADD":
         x += y
     elif op == "SUBTRACT":
         x -= y
     elif op == "LSHIFT":
         x <<= y
     elif op == "RSHIFT":
         x >>= y
     elif op == "AND":
         x &= y
     elif op == "XOR":
         x ^= y
     elif op == "OR":
         x |= y
     # 3.5 on
     elif op == "MATRIX_MULTIPLY":
         operator.imatmul(x, y)
     else:  # pragma: no cover
         raise self.PyVMError("Unknown in-place operator: %r" % op)
     self.vm.push(x)
    def test_class_binary_inplace_operators(self):
        class WithLotsOfOperators(Class):
            def __iadd__(self, other):
                return (self, "iadd", other)

            def __isub__(self, other):
                return (self, "isub", other)

            def __imul__(self, other):
                return (self, "imul", other)

            def __imod__(self, other):
                return (self, "imod", other)

            def __itruediv__(self, other):
                return (self, "itruediv", other)

            def __ifloordiv__(self, other):
                return (self, "ifloordiv", other)

            def __ilshift__(self, other):
                return (self, "ilshift", other)

            def __irshift__(self, other):
                return (self, "irshift", other)

            def __ior__(self, other):
                return (self, "ior", other)

            def __iand__(self, other):
                return (self, "iand", other)

            def __ixor__(self, other):
                return (self, "ixor", other)

            def __imatmul__(self, other):
                return (self, "imatmul", other)

        c = WithLotsOfOperators()

        self.assertEqual(operator.iadd(c, 0), (c, "iadd", 0))
        self.assertEqual(operator.isub(c, 0), (c, "isub", 0))
        self.assertEqual(operator.imul(c, 0), (c, "imul", 0))
        self.assertEqual(operator.imod(c, 0), (c, "imod", 0))
        self.assertEqual(operator.itruediv(c, 0), (c, "itruediv", 0))
        self.assertEqual(operator.ifloordiv(c, 0), (c, "ifloordiv", 0))
        self.assertEqual(operator.ilshift(c, 0), (c, "ilshift", 0))
        self.assertEqual(operator.irshift(c, 0), (c, "irshift", 0))
        self.assertEqual(operator.ior(c, 0), (c, "ior", 0))
        self.assertEqual(operator.iand(c, 0), (c, "iand", 0))
        self.assertEqual(operator.ixor(c, 0), (c, "ixor", 0))
        self.assertEqual(operator.imatmul(c, 0), (c, "imatmul", 0))
Example #3
0
 def test_inplace(self):
     class C(object):
         def __iadd__     (self, other): return "iadd"
         def __iand__     (self, other): return "iand"
         def __idiv__     (self, other): return "idiv"
         def __ifloordiv__(self, other): return "ifloordiv"
         def __ilshift__  (self, other): return "ilshift"
         def __imod__     (self, other): return "imod"
         def __imul__     (self, other): return "imul"
         def __imatmul__  (self, other): return "imatmul"
         def __ior__      (self, other): return "ior"
         def __ipow__     (self, other): return "ipow"
         def __irshift__  (self, other): return "irshift"
         def __isub__     (self, other): return "isub"
         def __itruediv__ (self, other): return "itruediv"
         def __ixor__     (self, other): return "ixor"
         def __getitem__(self, other): return 5  # so that C is a sequence
     c = C()
     self.assertEqual(operator.iadd     (c, 5), "iadd")
     self.assertEqual(operator.iand     (c, 5), "iand")
     self.assertEqual(operator.idiv     (c, 5), "idiv")
     self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv")
     self.assertEqual(operator.ilshift  (c, 5), "ilshift")
     self.assertEqual(operator.imod     (c, 5), "imod")
     self.assertEqual(operator.imul     (c, 5), "imul")
     self.assertEqual(operator.imatmul  (c, 5), "imatmul")
     self.assertEqual(operator.ior      (c, 5), "ior")
     self.assertEqual(operator.ipow     (c, 5), "ipow")
     self.assertEqual(operator.irshift  (c, 5), "irshift")
     self.assertEqual(operator.isub     (c, 5), "isub")
     self.assertEqual(operator.itruediv (c, 5), "itruediv")
     self.assertEqual(operator.ixor     (c, 5), "ixor")
     self.assertEqual(operator.iconcat  (c, c), "iadd")
     self.assertEqual(operator.irepeat  (c, 5), "imul")
     self.assertEqual(operator.__iadd__     (c, 5), "iadd")
     self.assertEqual(operator.__iand__     (c, 5), "iand")
     self.assertEqual(operator.__idiv__     (c, 5), "idiv")
     self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv")
     self.assertEqual(operator.__ilshift__  (c, 5), "ilshift")
     self.assertEqual(operator.__imod__     (c, 5), "imod")
     self.assertEqual(operator.__imul__     (c, 5), "imul")
     self.assertEqual(operator.__imatmul__  (c, 5), "imatmul")
     self.assertEqual(operator.__ior__      (c, 5), "ior")
     self.assertEqual(operator.__ipow__     (c, 5), "ipow")
     self.assertEqual(operator.__irshift__  (c, 5), "irshift")
     self.assertEqual(operator.__isub__     (c, 5), "isub")
     self.assertEqual(operator.__itruediv__ (c, 5), "itruediv")
     self.assertEqual(operator.__ixor__     (c, 5), "ixor")
     self.assertEqual(operator.__iconcat__  (c, c), "iadd")
     self.assertEqual(operator.__irepeat__  (c, 5), "imul")
Example #4
0
    def test_nb_ops_inplace(self):
        import operator
        mod = self.make_module(r"""
            @DEFINE_PointObject

            #define MYSLOT(NAME)                                               \
                HPyDef_SLOT(p_##NAME, NAME##_impl, HPy_nb_##NAME);             \
                static HPy NAME##_impl(HPyContext ctx, HPy self, HPy other)    \
                {                                                              \
                    HPy s = HPyUnicode_FromString(ctx, #NAME);                 \
                    HPy res = HPyTuple_Pack(ctx, 3, self, s, other);           \
                    HPy_Close(ctx, s);                                         \
                    return res;                                                \
                }

            MYSLOT(inplace_add)
            MYSLOT(inplace_and)
            MYSLOT(inplace_floor_divide)
            MYSLOT(inplace_lshift)
            MYSLOT(inplace_multiply)
            MYSLOT(inplace_or)
            MYSLOT(inplace_remainder)
            MYSLOT(inplace_rshift)
            MYSLOT(inplace_subtract)
            MYSLOT(inplace_true_divide)
            MYSLOT(inplace_xor)
            MYSLOT(inplace_matrix_multiply)

            @EXPORT_POINT_TYPE(&p_inplace_add, &p_inplace_and, &p_inplace_floor_divide, &p_inplace_lshift, &p_inplace_multiply, &p_inplace_or, &p_inplace_remainder, &p_inplace_rshift, &p_inplace_subtract, &p_inplace_true_divide, &p_inplace_xor, &p_inplace_matrix_multiply)
            @INIT
        """)
        p = mod.Point()
        tmp = p
        tmp += 42
        assert tmp == (p, "inplace_add", 42)
        tmp = p
        tmp &= 42
        assert tmp == (p, "inplace_and", 42)
        tmp = p
        tmp //= 42
        assert tmp == (p, "inplace_floor_divide", 42)
        tmp = p
        tmp <<= 42
        assert tmp == (p, "inplace_lshift", 42)
        tmp = p
        tmp *= 42
        assert tmp == (p, "inplace_multiply", 42)
        tmp = p
        tmp |= 42
        assert tmp == (p, "inplace_or", 42)
        tmp = p
        tmp %= 42
        assert tmp == (p, "inplace_remainder", 42)
        tmp = p
        tmp >>= 42
        assert tmp == (p, "inplace_rshift", 42)
        tmp = p
        tmp -= 42
        assert tmp == (p, "inplace_subtract", 42)
        tmp = p
        tmp /= 42
        assert tmp == (p, "inplace_true_divide", 42)
        tmp = p
        tmp ^= 42
        assert tmp == (p, "inplace_xor", 42)
        #
        # we can't use '@=' because we want to be importable on py27
        tmp = p
        tmp = operator.imatmul(p, 42)
        assert tmp == (p, "inplace_matrix_multiply", 42)
Example #5
0
 def imatmul_usecase(x, y):
     return operator.imatmul(x, y)
Example #6
0
 def imatmul_usecase(x, y):
     return operator.imatmul(x, y)
Example #7
0
    def test_inplace(self):
        class C(object):
            def __iadd__(self, other):
                return "iadd"

            def __iand__(self, other):
                return "iand"

            def __idiv__(self, other):
                return "idiv"

            def __ifloordiv__(self, other):
                return "ifloordiv"

            def __ilshift__(self, other):
                return "ilshift"

            def __imod__(self, other):
                return "imod"

            def __imul__(self, other):
                return "imul"

            def __imatmul__(self, other):
                return "imatmul"

            def __ior__(self, other):
                return "ior"

            def __ipow__(self, other):
                return "ipow"

            def __irshift__(self, other):
                return "irshift"

            def __isub__(self, other):
                return "isub"

            def __itruediv__(self, other):
                return "itruediv"

            def __ixor__(self, other):
                return "ixor"

            def __getitem__(self, other):
                return 5  # so that C is a sequence

        c = C()
        self.assertEqual(operator.iadd(c, 5), "iadd")
        self.assertEqual(operator.iand(c, 5), "iand")
        self.assertEqual(operator.idiv(c, 5), "idiv")
        self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv")
        self.assertEqual(operator.ilshift(c, 5), "ilshift")
        self.assertEqual(operator.imod(c, 5), "imod")
        self.assertEqual(operator.imul(c, 5), "imul")
        self.assertEqual(operator.imatmul(c, 5), "imatmul")
        self.assertEqual(operator.ior(c, 5), "ior")
        self.assertEqual(operator.ipow(c, 5), "ipow")
        self.assertEqual(operator.irshift(c, 5), "irshift")
        self.assertEqual(operator.isub(c, 5), "isub")
        self.assertEqual(operator.itruediv(c, 5), "itruediv")
        self.assertEqual(operator.ixor(c, 5), "ixor")
        self.assertEqual(operator.iconcat(c, c), "iadd")
        self.assertEqual(operator.irepeat(c, 5), "imul")
        self.assertEqual(operator.__iadd__(c, 5), "iadd")
        self.assertEqual(operator.__iand__(c, 5), "iand")
        self.assertEqual(operator.__idiv__(c, 5), "idiv")
        self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv")
        self.assertEqual(operator.__ilshift__(c, 5), "ilshift")
        self.assertEqual(operator.__imod__(c, 5), "imod")
        self.assertEqual(operator.__imul__(c, 5), "imul")
        self.assertEqual(operator.__imatmul__(c, 5), "imatmul")
        self.assertEqual(operator.__ior__(c, 5), "ior")
        self.assertEqual(operator.__ipow__(c, 5), "ipow")
        self.assertEqual(operator.__irshift__(c, 5), "irshift")
        self.assertEqual(operator.__isub__(c, 5), "isub")
        self.assertEqual(operator.__itruediv__(c, 5), "itruediv")
        self.assertEqual(operator.__ixor__(c, 5), "ixor")
        self.assertEqual(operator.__iconcat__(c, c), "iadd")
        self.assertEqual(operator.__irepeat__(c, 5), "imul")
Example #8
0
 def update_event(self, inp=-1):
     self.set_output_val(0, operator.imatmul(self.input(0), self.input(1)))