| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1834,7 +1834,7 @@ itself. This means, for example, that one shared object can contain a second: | |||
| 1834 | 1834 | >>> l = manager.list(range(10)) | |
| 1835 | 1835 | >>> l._callmethod('__len__') | |
| 1836 | 1836 | 10 | |
| 1837 | - >>> l._callmethod('__getslice__', (2, 7)) # equiv to `l[2:7]` | ||
| 1837 | + >>> l._callmethod('__getitem__', (slice(2, 7),)) # equiv to `l[2:7]` | ||
| 1838 | 1838 | [2, 3, 4, 5, 6] | |
| 1839 | 1839 | >>> l._callmethod('__getitem__', (20,)) # equiv to `l[20]` | |
| 1840 | 1840 | Traceback (most recent call last): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1621,19 +1621,19 @@ The full list of supported magic methods is: | |||
| 1621 | 1621 | * ``__hash__``, ``__sizeof__``, ``__repr__`` and ``__str__`` | |
| 1622 | 1622 | * ``__dir__``, ``__format__`` and ``__subclasses__`` | |
| 1623 | 1623 | * ``__floor__``, ``__trunc__`` and ``__ceil__`` | |
| 1624 | - * Comparisons: ``__cmp__``, ``__lt__``, ``__gt__``, ``__le__``, ``__ge__``, | ||
| 1624 | + * Comparisons: ``__lt__``, ``__gt__``, ``__le__``, ``__ge__``, | ||
| 1625 | 1625 | ``__eq__`` and ``__ne__`` | |
| 1626 | 1626 | * Container methods: ``__getitem__``, ``__setitem__``, ``__delitem__``, | |
| 1627 | - ``__contains__``, ``__len__``, ``__iter__``, ``__getslice__``, | ||
| 1628 | - ``__setslice__``, ``__reversed__`` and ``__missing__`` | ||
| 1627 | + ``__contains__``, ``__len__``, ``__iter__``, ``__reversed__`` | ||
| 1628 | + and ``__missing__`` | ||
| 1629 | 1629 | * Context manager: ``__enter__`` and ``__exit__`` | |
| 1630 | 1630 | * Unary numeric methods: ``__neg__``, ``__pos__`` and ``__invert__`` | |
| 1631 | 1631 | * The numeric methods (including right hand and in-place variants): | |
| 1632 | - ``__add__``, ``__sub__``, ``__mul__``, ``__div__``, | ||
| 1632 | + ``__add__``, ``__sub__``, ``__mul__``, ``__matmul__``, ``__div__``, ``__truediv__``, | ||
| 1633 | 1633 | ``__floordiv__``, ``__mod__``, ``__divmod__``, ``__lshift__``, | |
| 1634 | 1634 | ``__rshift__``, ``__and__``, ``__xor__``, ``__or__``, and ``__pow__`` | |
| 1635 | - * Numeric conversion methods: ``__complex__``, ``__int__``, ``__float__``, | ||
| 1636 | - ``__index__`` and ``__coerce__`` | ||
| 1635 | + * Numeric conversion methods: ``__complex__``, ``__int__``, ``__float__`` | ||
| 1636 | + and ``__index__`` | ||
| 1637 | 1637 | * Descriptor methods: ``__get__``, ``__set__`` and ``__delete__`` | |
| 1638 | 1638 | * Pickling: ``__reduce__``, ``__reduce_ex__``, ``__getinitargs__``, | |
| 1639 | 1639 | ``__getnewargs__``, ``__getstate__`` and ``__setstate__`` | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -960,13 +960,12 @@ def __ge__(self, other, context=None): | |||
| 960 | 960 | return self._cmp(other) >= 0 | |
| 961 | 961 | ||
| 962 | 962 | def compare(self, other, context=None): | |
| 963 | - """Compares one to another. | ||
| 963 | + """Compare self to other. Return a decimal value: | ||
| 964 | 964 | ||
| 965 | - -1 => a < b | ||
| 966 | - 0 => a = b | ||
| 967 | - 1 => a > b | ||
| 968 | - NaN => one is NaN | ||
| 969 | - Like __cmp__, but returns Decimal instances. | ||
| 965 | + a or b is a NaN ==> Decimal('NaN') | ||
| 966 | + a < b ==> Decimal('-1') | ||
| 967 | + a == b ==> Decimal('0') | ||
| 968 | + a > b ==> Decimal('1') | ||
| 970 | 969 | """ | |
| 971 | 970 | other = _convert_other(other, raiseit=True) | |
| 972 | 971 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -651,7 +651,7 @@ class date: | |||
| 651 | 651 | Operators: | |
| 652 | 652 | ||
| 653 | 653 | __repr__, __str__ | |
| 654 | - __cmp__, __hash__ | ||
| 654 | + __eq__, __le__, __lt__, __ge__, __gt__, __hash__ | ||
| 655 | 655 | __add__, __radd__, __sub__ (add/radd only with timedelta arg) | |
| 656 | 656 | ||
| 657 | 657 | Methods: | |
@@ -786,7 +786,8 @@ def day(self): | |||
| 786 | 786 | """day (1-31)""" | |
| 787 | 787 | return self._day | |
| 788 | 788 | ||
| 789 | - # Standard conversions, __cmp__, __hash__ (and helpers) | ||
| 789 | + # Standard conversions, __eq__, __le__, __lt__, __ge__, __gt__, | ||
| 790 | + # __hash__ (and helpers) | ||
| 790 | 791 | ||
| 791 | 792 | def timetuple(self): | |
| 792 | 793 | "Return local time tuple compatible with time.localtime()." | |
@@ -1010,7 +1011,7 @@ class time: | |||
| 1010 | 1011 | Operators: | |
| 1011 | 1012 | ||
| 1012 | 1013 | __repr__, __str__ | |
| 1013 | - __cmp__, __hash__ | ||
| 1014 | + __eq__, __le__, __lt__, __ge__, __gt__, __hash__ | ||
| 1014 | 1015 | ||
| 1015 | 1016 | Methods: | |
| 1016 | 1017 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -88,19 +88,10 @@ def __init__(self, _val): | |||
| 88 | 88 | _val = _val.decode('utf-8') | |
| 89 | 89 | self.val = _val | |
| 90 | 90 | ||
| 91 | - def __cmp__(self, other): | ||
| 92 | - if not isinstance(other, DeclTypesTests.Foo): | ||
| 93 | - raise ValueError | ||
| 94 | - if self.val == other.val: | ||
| 95 | - return 0 | ||
| 96 | - else: | ||
| 97 | - return 1 | ||
| 98 | - | ||
| 99 | 91 | def __eq__(self, other): | |
| 100 | - c = self.__cmp__(other) | ||
| 101 | - if c is NotImplemented: | ||
| 102 | - return c | ||
| 103 | - return c == 0 | ||
| 92 | + if not isinstance(other, DeclTypesTests.Foo): | ||
| 93 | + return NotImplemented | ||
| 94 | + return self.val == other.val | ||
| 104 | 95 | ||
| 105 | 96 | def __conform__(self, protocol): | |
| 106 | 97 | if protocol is sqlite.PrepareProtocol: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -64,7 +64,7 @@ def test_read(self): | |||
| 64 | 64 | self.assertEqual(d, d) | |
| 65 | 65 | self.assertNotEqual(p, d) | |
| 66 | 66 | self.assertNotEqual(d, p) | |
| 67 | - #__non__zero__ | ||
| 67 | + #bool | ||
| 68 | 68 | if p: self.fail("Empty mapping must compare to False") | |
| 69 | 69 | if not d: self.fail("Full mapping must compare to True") | |
| 70 | 70 | # keys(), items(), iterkeys() ... | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -194,9 +194,9 @@ def foo(self, val): pass | |||
| 194 | 194 | # check that the property's __isabstractmethod__ descriptor does the | |
| 195 | 195 | # right thing when presented with a value that fails truth testing: | |
| 196 | 196 | class NotBool(object): | |
| 197 | - def __nonzero__(self): | ||
| 197 | + def __bool__(self): | ||
| 198 | 198 | raise ValueError() | |
| 199 | - __len__ = __nonzero__ | ||
| 199 | + __len__ = __bool__ | ||
| 200 | 200 | with self.assertRaises(ValueError): | |
| 201 | 201 | class F(C): | |
| 202 | 202 | def bar(self): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -144,14 +144,6 @@ def __imatmul__(self, val): | |||
| 144 | 144 | output.append("__imatmul__ called") | |
| 145 | 145 | return self | |
| 146 | 146 | ||
| 147 | - def __div__(self, val): | ||
| 148 | - output.append("__div__ called") | ||
| 149 | - def __rdiv__(self, val): | ||
| 150 | - output.append("__rdiv__ called") | ||
| 151 | - def __idiv__(self, val): | ||
| 152 | - output.append("__idiv__ called") | ||
| 153 | - return self | ||
| 154 | - | ||
| 155 | 147 | def __floordiv__(self, val): | |
| 156 | 148 | output.append("__floordiv__ called") | |
| 157 | 149 | return self | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,8 +13,12 @@ | |||
| 13 | 13 | "rsub", | |
| 14 | 14 | "mul", | |
| 15 | 15 | "rmul", | |
| 16 | + "matmul", | ||
| 17 | + "rmatmul", | ||
| 16 | 18 | "truediv", | |
| 17 | 19 | "rtruediv", | |
| 20 | + "floordiv", | ||
| 21 | + "rfloordiv", | ||
| 18 | 22 | "mod", | |
| 19 | 23 | "rmod", | |
| 20 | 24 | "divmod", | |
@@ -174,15 +178,31 @@ def testBinaryOps(self): | |||
| 174 | 178 | 1 * testme | |
| 175 | 179 | self.assertCallStack([("__rmul__", (testme, 1))]) | |
| 176 | 180 | ||
| 177 | - if 1/2 == 0: | ||
| 178 | - callLst[:] = [] | ||
| 179 | - testme / 1 | ||
| 180 | - self.assertCallStack([("__div__", (testme, 1))]) | ||
| 181 | + callLst[:] = [] | ||
| 182 | + testme @ 1 | ||
| 183 | + self.assertCallStack([("__matmul__", (testme, 1))]) | ||
| 184 | + | ||
| 185 | + callLst[:] = [] | ||
| 186 | + 1 @ testme | ||
| 187 | + self.assertCallStack([("__rmatmul__", (testme, 1))]) | ||
| 188 | + | ||
| 189 | + callLst[:] = [] | ||
| 190 | + testme / 1 | ||
| 191 | + self.assertCallStack([("__truediv__", (testme, 1))]) | ||
| 181 | 192 | ||
| 182 | 193 | ||
| 183 | - callLst[:] = [] | ||
| 184 | - 1 / testme | ||
| 185 | - self.assertCallStack([("__rdiv__", (testme, 1))]) | ||
| 194 | + callLst[:] = [] | ||
| 195 | + 1 / testme | ||
| 196 | + self.assertCallStack([("__rtruediv__", (testme, 1))]) | ||
| 197 | + | ||
| 198 | + callLst[:] = [] | ||
| 199 | + testme // 1 | ||
| 200 | + self.assertCallStack([("__floordiv__", (testme, 1))]) | ||
| 201 | + | ||
| 202 | + | ||
| 203 | + callLst[:] = [] | ||
| 204 | + 1 // testme | ||
| 205 | + self.assertCallStack([("__rfloordiv__", (testme, 1))]) | ||
| 186 | 206 | ||
| 187 | 207 | callLst[:] = [] | |
| 188 | 208 | testme % 1 | |
@@ -444,12 +464,16 @@ class BadTypeClass: | |||
| 444 | 464 | def __int__(self): | |
| 445 | 465 | return None | |
| 446 | 466 | __float__ = __int__ | |
| 467 | + __complex__ = __int__ | ||
| 447 | 468 | __str__ = __int__ | |
| 448 | 469 | __repr__ = __int__ | |
| 449 | - __oct__ = __int__ | ||
| 450 | - __hex__ = __int__ | ||
| 470 | + __bytes__ = __int__ | ||
| 471 | + __bool__ = __int__ | ||
| 472 | + __index__ = __int__ | ||
| 473 | + def index(x): | ||
| 474 | + return [][x] | ||
| 451 | 475 | ||
| 452 | - for f in [int, float, str, repr, oct, hex]: | ||
| 476 | + for f in [float, complex, str, repr, bytes, bin, oct, hex, bool, index]: | ||
| 453 | 477 | self.assertRaises(TypeError, f, BadTypeClass()) | |
| 454 | 478 | ||
| 455 | 479 | def testHashStuff(self): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,7 +21,9 @@ def __init__(self, *args, **kwargs): | |||
| 21 | 21 | 'add': '+', | |
| 22 | 22 | 'sub': '-', | |
| 23 | 23 | 'mul': '*', | |
| 24 | - 'div': '/', | ||
| 24 | + 'matmul': '@', | ||
| 25 | + 'truediv': '/', | ||
| 26 | + 'floordiv': '//', | ||
| 25 | 27 | 'divmod': 'divmod', | |
| 26 | 28 | 'pow': '**', | |
| 27 | 29 | 'lshift': '<<', | |
@@ -52,8 +54,6 @@ def __init__(self, *args, **kwargs): | |||
| 52 | 54 | 'invert': '~', | |
| 53 | 55 | 'int': 'int', | |
| 54 | 56 | 'float': 'float', | |
| 55 | - 'oct': 'oct', | ||
| 56 | - 'hex': 'hex', | ||
| 57 | 57 | } | |
| 58 | 58 | ||
| 59 | 59 | for name, expr in list(self.unops.items()): | |
@@ -82,12 +82,6 @@ def unop_test(self, a, res, expr="len(a)", meth="__len__"): | |||
| 82 | 82 | def binop_test(self, a, b, res, expr="a+b", meth="__add__"): | |
| 83 | 83 | d = {'a': a, 'b': b} | |
| 84 | 84 | ||
| 85 | - # XXX Hack so this passes before 2.3 when -Qnew is specified. | ||
| 86 | - if meth == "__div__" and 1/2 == 0.5: | ||
| 87 | - meth = "__truediv__" | ||
| 88 | - | ||
| 89 | - if meth == '__divmod__': pass | ||
| 90 | - | ||
| 91 | 85 | self.assertEqual(eval(expr, d), res) | |
| 92 | 86 | t = type(a) | |
| 93 | 87 | m = getattr(t, meth) | |
@@ -221,7 +215,7 @@ def test_dicts(self): | |||
| 221 | 215 | def number_operators(self, a, b, skip=[]): | |
| 222 | 216 | dict = {'a': a, 'b': b} | |
| 223 | 217 | ||
| 224 | - for name, expr in list(self.binops.items()): | ||
| 218 | + for name, expr in self.binops.items(): | ||
| 225 | 219 | if name not in skip: | |
| 226 | 220 | name = "__%s__" % name | |
| 227 | 221 | if hasattr(a, name): | |
@@ -261,7 +255,7 @@ def test_complexes(self): | |||
| 261 | 255 | # Testing complex operations... | |
| 262 | 256 | self.number_operators(100.0j, 3.0j, skip=['lt', 'le', 'gt', 'ge', | |
| 263 | 257 | 'int', 'float', | |
| 264 | - 'divmod', 'mod']) | ||
| 258 | + 'floordiv', 'divmod', 'mod']) | ||
| 265 | 259 | ||
| 266 | 260 | class Number(complex): | |
| 267 | 261 | __slots__ = ['prec'] | |
@@ -4177,9 +4171,8 @@ def check(expr, x, y): | |||
| 4177 | 4171 | ('__sub__', 'x - y', 'x -= y'), | |
| 4178 | 4172 | ('__mul__', 'x * y', 'x *= y'), | |
| 4179 | 4173 | ('__matmul__', 'x @ y', 'x @= y'), | |
| 4180 | - ('__truediv__', 'operator.truediv(x, y)', None), | ||
| 4181 | - ('__floordiv__', 'operator.floordiv(x, y)', None), | ||
| 4182 | - ('__div__', 'x / y', 'x /= y'), | ||
| 4174 | + ('__truediv__', 'x / y', 'x /= y'), | ||
| 4175 | + ('__floordiv__', 'x // y', 'x //= y'), | ||
| 4183 | 4176 | ('__mod__', 'x % y', 'x %= y'), | |
| 4184 | 4177 | ('__divmod__', 'divmod(x, y)', None), | |
| 4185 | 4178 | ('__pow__', 'x ** y', 'x **= y'), | |
@@ -4241,8 +4234,8 @@ class X(object): | |||
| 4241 | 4234 | # Also check type_getattro for correctness. | |
| 4242 | 4235 | class Meta(type): | |
| 4243 | 4236 | pass | |
| 4244 | - class X(object): | ||
| 4245 | - __metaclass__ = Meta | ||
| 4237 | + class X(metaclass=Meta): | ||
| 4238 | + pass | ||
| 4246 | 4239 | X.a = 42 | |
| 4247 | 4240 | Meta.a = Descr("a") | |
| 4248 | 4241 | self.assertEqual(X.a, 42) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments