| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 17e5264 commit 18b711c
12 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -367,6 +367,12 @@ The :mod:`test.support` module defines the following constants: | |||
| 367 | 367 | Object that is equal to anything. Used to test mixed type comparison. | |
| 368 | 368 | ||
| 369 | 369 | ||
| 370 | + .. data:: NEVER_EQ | ||
| 371 | + | ||
| 372 | + Object that is not equal to anything (even to :data:`ALWAYS_EQ`). | ||
| 373 | + Used to test mixed type comparison. | ||
| 374 | + | ||
| 375 | + | ||
| 370 | 376 | .. data:: LARGEST | |
| 371 | 377 | ||
| 372 | 378 | Object that is greater than anything (except itself). | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,6 +7,7 @@ | |||
| 7 | 7 | from functools import cmp_to_key | |
| 8 | 8 | ||
| 9 | 9 | from test import support, seq_tests | |
| 10 | + from test.support import ALWAYS_EQ, NEVER_EQ | ||
| 10 | 11 | ||
| 11 | 12 | ||
| 12 | 13 | class CommonTest(seq_tests.CommonTest): | |
@@ -329,6 +330,20 @@ def test_remove(self): | |||
| 329 | 330 | ||
| 330 | 331 | self.assertRaises(TypeError, a.remove) | |
| 331 | 332 | ||
| 333 | + a = self.type2test([1, 2]) | ||
| 334 | + self.assertRaises(ValueError, a.remove, NEVER_EQ) | ||
| 335 | + self.assertEqual(a, [1, 2]) | ||
| 336 | + a.remove(ALWAYS_EQ) | ||
| 337 | + self.assertEqual(a, [2]) | ||
| 338 | + a = self.type2test([ALWAYS_EQ]) | ||
| 339 | + a.remove(1) | ||
| 340 | + self.assertEqual(a, []) | ||
| 341 | + a = self.type2test([ALWAYS_EQ]) | ||
| 342 | + a.remove(NEVER_EQ) | ||
| 343 | + self.assertEqual(a, []) | ||
| 344 | + a = self.type2test([NEVER_EQ]) | ||
| 345 | + self.assertRaises(ValueError, a.remove, ALWAYS_EQ) | ||
| 346 | + | ||
| 332 | 347 | class BadExc(Exception): | |
| 333 | 348 | pass | |
| 334 | 349 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,6 +6,7 @@ | |||
| 6 | 6 | import sys | |
| 7 | 7 | import pickle | |
| 8 | 8 | from test import support | |
| 9 | + from test.support import ALWAYS_EQ, NEVER_EQ | ||
| 9 | 10 | ||
| 10 | 11 | # Various iterables | |
| 11 | 12 | # This is used for checking the constructor (here and in test_deque.py) | |
@@ -221,15 +222,15 @@ def test_contains(self): | |||
| 221 | 222 | self.assertRaises(TypeError, u.__contains__) | |
| 222 | 223 | ||
| 223 | 224 | def test_contains_fake(self): | |
| 224 | - class AllEq: | ||
| 225 | - # Sequences must use rich comparison against each item | ||
| 226 | - # (unless "is" is true, or an earlier item answered) | ||
| 227 | - # So instances of AllEq must be found in all non-empty sequences. | ||
| 228 | - def __eq__(self, other): | ||
| 229 | - return True | ||
| 230 | - __hash__ = None # Can't meet hash invariant requirements | ||
| 231 | - self.assertNotIn(AllEq(), self.type2test([])) | ||
| 232 | - self.assertIn(AllEq(), self.type2test([1])) | ||
| 225 | + # Sequences must use rich comparison against each item | ||
| 226 | + # (unless "is" is true, or an earlier item answered) | ||
| 227 | + # So ALWAYS_EQ must be found in all non-empty sequences. | ||
| 228 | + self.assertNotIn(ALWAYS_EQ, self.type2test([])) | ||
| 229 | + self.assertIn(ALWAYS_EQ, self.type2test([1])) | ||
| 230 | + self.assertIn(1, self.type2test([ALWAYS_EQ])) | ||
| 231 | + self.assertNotIn(NEVER_EQ, self.type2test([])) | ||
| 232 | + self.assertNotIn(ALWAYS_EQ, self.type2test([NEVER_EQ])) | ||
| 233 | + self.assertIn(NEVER_EQ, self.type2test([ALWAYS_EQ])) | ||
| 233 | 234 | ||
| 234 | 235 | def test_contains_order(self): | |
| 235 | 236 | # Sequences must test in-order. If a rich comparison has side | |
@@ -350,6 +351,11 @@ def test_count(self): | |||
| 350 | 351 | self.assertEqual(a.count(1), 3) | |
| 351 | 352 | self.assertEqual(a.count(3), 0) | |
| 352 | 353 | ||
| 354 | + self.assertEqual(a.count(ALWAYS_EQ), 9) | ||
| 355 | + self.assertEqual(self.type2test([ALWAYS_EQ, ALWAYS_EQ]).count(1), 2) | ||
| 356 | + self.assertEqual(self.type2test([ALWAYS_EQ, ALWAYS_EQ]).count(NEVER_EQ), 2) | ||
| 357 | + self.assertEqual(self.type2test([NEVER_EQ, NEVER_EQ]).count(ALWAYS_EQ), 0) | ||
| 358 | + | ||
| 353 | 359 | self.assertRaises(TypeError, a.count) | |
| 354 | 360 | ||
| 355 | 361 | class BadExc(Exception): | |
@@ -378,6 +384,11 @@ def test_index(self): | |||
| 378 | 384 | self.assertEqual(u.index(0, 3, 4), 3) | |
| 379 | 385 | self.assertRaises(ValueError, u.index, 2, 0, -10) | |
| 380 | 386 | ||
| 387 | + self.assertEqual(u.index(ALWAYS_EQ), 0) | ||
| 388 | + self.assertEqual(self.type2test([ALWAYS_EQ, ALWAYS_EQ]).index(1), 0) | ||
| 389 | + self.assertEqual(self.type2test([ALWAYS_EQ, ALWAYS_EQ]).index(NEVER_EQ), 0) | ||
| 390 | + self.assertRaises(ValueError, self.type2test([NEVER_EQ, NEVER_EQ]).index, ALWAYS_EQ) | ||
| 391 | + | ||
| 381 | 392 | self.assertRaises(TypeError, u.index) | |
| 382 | 393 | ||
| 383 | 394 | class BadExc(Exception): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -113,7 +113,7 @@ | |||
| 113 | 113 | "run_with_locale", "swap_item", | |
| 114 | 114 | "swap_attr", "Matcher", "set_memlimit", "SuppressCrashReport", "sortdict", | |
| 115 | 115 | "run_with_tz", "PGO", "missing_compiler_executable", "fd_count", | |
| 116 | - "ALWAYS_EQ", "LARGEST", "SMALLEST" | ||
| 116 | + "ALWAYS_EQ", "NEVER_EQ", "LARGEST", "SMALLEST" | ||
| 117 | 117 | ] | |
| 118 | 118 | ||
| 119 | 119 | class Error(Exception): | |
@@ -3115,6 +3115,17 @@ def __ne__(self, other): | |||
| 3115 | 3115 | ||
| 3116 | 3116 | ALWAYS_EQ = _ALWAYS_EQ() | |
| 3117 | 3117 | ||
| 3118 | + class _NEVER_EQ: | ||
| 3119 | + """ | ||
| 3120 | + Object that is not equal to anything. | ||
| 3121 | + """ | ||
| 3122 | + def __eq__(self, other): | ||
| 3123 | + return False | ||
| 3124 | + def __ne__(self, other): | ||
| 3125 | + return True | ||
| 3126 | + | ||
| 3127 | + NEVER_EQ = _NEVER_EQ() | ||
| 3128 | + | ||
| 3118 | 3129 | @functools.total_ordering | |
| 3119 | 3130 | class _LARGEST: | |
| 3120 | 3131 | """ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,7 @@ | |||
| 3 | 3 | import sys | |
| 4 | 4 | import unittest | |
| 5 | 5 | from test.support import run_unittest, TESTFN, unlink, cpython_only | |
| 6 | - from test.support import check_free_after_iterating | ||
| 6 | + from test.support import check_free_after_iterating, ALWAYS_EQ, NEVER_EQ | ||
| 7 | 7 | import pickle | |
| 8 | 8 | import collections.abc | |
| 9 | 9 | ||
@@ -41,6 +41,14 @@ def __init__(self, n): | |||
| 41 | 41 | def __iter__(self): | |
| 42 | 42 | return BasicIterClass(self.n) | |
| 43 | 43 | ||
| 44 | + class IteratorProxyClass: | ||
| 45 | + def __init__(self, i): | ||
| 46 | + self.i = i | ||
| 47 | + def __next__(self): | ||
| 48 | + return next(self.i) | ||
| 49 | + def __iter__(self): | ||
| 50 | + return self | ||
| 51 | + | ||
| 44 | 52 | class SequenceClass: | |
| 45 | 53 | def __init__(self, n): | |
| 46 | 54 | self.n = n | |
@@ -50,6 +58,12 @@ def __getitem__(self, i): | |||
| 50 | 58 | else: | |
| 51 | 59 | raise IndexError | |
| 52 | 60 | ||
| 61 | + class SequenceProxyClass: | ||
| 62 | + def __init__(self, s): | ||
| 63 | + self.s = s | ||
| 64 | + def __getitem__(self, i): | ||
| 65 | + return self.s[i] | ||
| 66 | + | ||
| 53 | 67 | class UnlimitedSequenceClass: | |
| 54 | 68 | def __getitem__(self, i): | |
| 55 | 69 | return i | |
@@ -635,6 +649,13 @@ def test_in_and_not_in(self): | |||
| 635 | 649 | for i in "abc", -1, 5, 42.42, (3, 4), [], {1: 1}, 3-12j, sc5: | |
| 636 | 650 | self.assertNotIn(i, sc5) | |
| 637 | 651 | ||
| 652 | + self.assertIn(ALWAYS_EQ, IteratorProxyClass(iter([1]))) | ||
| 653 | + self.assertIn(ALWAYS_EQ, SequenceProxyClass([1])) | ||
| 654 | + self.assertNotIn(ALWAYS_EQ, IteratorProxyClass(iter([NEVER_EQ]))) | ||
| 655 | + self.assertNotIn(ALWAYS_EQ, SequenceProxyClass([NEVER_EQ])) | ||
| 656 | + self.assertIn(NEVER_EQ, IteratorProxyClass(iter([ALWAYS_EQ]))) | ||
| 657 | + self.assertIn(NEVER_EQ, SequenceProxyClass([ALWAYS_EQ])) | ||
| 658 | + | ||
| 638 | 659 | self.assertRaises(TypeError, lambda: 3 in 12) | |
| 639 | 660 | self.assertRaises(TypeError, lambda: 3 not in map) | |
| 640 | 661 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + Fixed minor inconsistency in :meth:`list.__contains__`, | ||
| 2 | + :meth:`tuple.__contains__` and a few other places. The collection's item is | ||
| 3 | + now always at the left and the needle is on the right of ``==``. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -937,7 +937,7 @@ _asyncio_Future_remove_done_callback(FutureObj *self, PyObject *fn) | |||
| 937 | 937 | ENSURE_FUTURE_ALIVE(self) | |
| 938 | 938 | ||
| 939 | 939 | if (self->fut_callback0 != NULL) { | |
| 940 | - int cmp = PyObject_RichCompareBool(fn, self->fut_callback0, Py_EQ); | ||
| 940 | + int cmp = PyObject_RichCompareBool(self->fut_callback0, fn, Py_EQ); | ||
| 941 | 941 | if (cmp == -1) { | |
| 942 | 942 | return NULL; | |
| 943 | 943 | } | |
@@ -962,7 +962,7 @@ _asyncio_Future_remove_done_callback(FutureObj *self, PyObject *fn) | |||
| 962 | 962 | if (len == 1) { | |
| 963 | 963 | PyObject *cb_tup = PyList_GET_ITEM(self->fut_callbacks, 0); | |
| 964 | 964 | int cmp = PyObject_RichCompareBool( | |
| 965 | - fn, PyTuple_GET_ITEM(cb_tup, 0), Py_EQ); | ||
| 965 | + PyTuple_GET_ITEM(cb_tup, 0), fn, Py_EQ); | ||
| 966 | 966 | if (cmp == -1) { | |
| 967 | 967 | return NULL; | |
| 968 | 968 | } | |
@@ -984,7 +984,7 @@ _asyncio_Future_remove_done_callback(FutureObj *self, PyObject *fn) | |||
| 984 | 984 | int ret; | |
| 985 | 985 | PyObject *item = PyList_GET_ITEM(self->fut_callbacks, i); | |
| 986 | 986 | Py_INCREF(item); | |
| 987 | - ret = PyObject_RichCompareBool(fn, PyTuple_GET_ITEM(item, 0), Py_EQ); | ||
| 987 | + ret = PyObject_RichCompareBool(PyTuple_GET_ITEM(item, 0), fn, Py_EQ); | ||
| 988 | 988 | if (ret == 0) { | |
| 989 | 989 | if (j < len) { | |
| 990 | 990 | PyList_SET_ITEM(newlist, j, item); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5600,8 +5600,7 @@ list_contains(PyListObject *a, PyObject *el) | |||
| 5600 | 5600 | int cmp; | |
| 5601 | 5601 | ||
| 5602 | 5602 | for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i) | |
| 5603 | - cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i), | ||
| 5604 | - Py_EQ); | ||
| 5603 | + cmp = PyObject_RichCompareBool(PyList_GET_ITEM(a, i), el, Py_EQ); | ||
| 5605 | 5604 | return cmp; | |
| 5606 | 5605 | } | |
| 5607 | 5606 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2016,7 +2016,7 @@ _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation) | |||
| 2016 | 2016 | break; | |
| 2017 | 2017 | } | |
| 2018 | 2018 | ||
| 2019 | - cmp = PyObject_RichCompareBool(obj, item, Py_EQ); | ||
| 2019 | + cmp = PyObject_RichCompareBool(item, obj, Py_EQ); | ||
| 2020 | 2020 | Py_DECREF(item); | |
| 2021 | 2021 | if (cmp < 0) | |
| 2022 | 2022 | goto Fail; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4392,7 +4392,7 @@ dictitems_contains(_PyDictViewObject *dv, PyObject *obj) | |||
| 4392 | 4392 | return 0; | |
| 4393 | 4393 | } | |
| 4394 | 4394 | Py_INCREF(found); | |
| 4395 | - result = PyObject_RichCompareBool(value, found, Py_EQ); | ||
| 4395 | + result = PyObject_RichCompareBool(found, value, Py_EQ); | ||
| 4396 | 4396 | Py_DECREF(found); | |
| 4397 | 4397 | return result; | |
| 4398 | 4398 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments