Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b7d14a0

Browse files
Merge from 3.5.
2 parents 9440787 + 0438683 commit b7d14a0

3 files changed

Lines changed: 22 additions & 69 deletions

File tree

Lib/test/test_ordered_dict.py

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -775,64 +775,5 @@ def test_popitem(self):
775775
self.assertRaises(KeyError, d.popitem)
776776

777777

778-
class SimpleLRUCache:
779-
780-
def __init__(self, size):
781-
super().__init__()
782-
self.size = size
783-
784-
def __getitem__(self, item):
785-
value = super().__getitem__(item)
786-
self.move_to_end(item)
787-
return value
788-
789-
def __setitem__(self, key, value):
790-
while key not in self and len(self) >= self.size:
791-
self.popitem(last=False)
792-
super().__setitem__(key, value)
793-
self.move_to_end(key)
794-
795-
796-
class SimpleLRUCacheTests:
797-
798-
def test_add_after_full(self):
799-
c = self.type2test(2)
800-
c['t1'] = 1
801-
c['t2'] = 2
802-
c['t3'] = 3
803-
self.assertEqual(list(c), ['t2', 't3'])
804-
805-
def test_popitem(self):
806-
c = self.type2test(3)
807-
for i in range(1, 4):
808-
c[i] = i
809-
self.assertEqual(c.popitem(last=False), (1, 1))
810-
self.assertEqual(c.popitem(last=True), (3, 3))
811-
812-
def test_change_order_on_get(self):
813-
c = self.type2test(3)
814-
for i in range(1, 4):
815-
c[i] = i
816-
self.assertEqual(list(c), list(range(1, 4)))
817-
self.assertEqual(c[2], 2)
818-
self.assertEqual(list(c), [1, 3, 2])
819-
820-
821-
class PySimpleLRUCacheTests(SimpleLRUCacheTests, unittest.TestCase):
822-
823-
class type2test(SimpleLRUCache, py_coll.OrderedDict):
824-
pass
825-
826-
827-
@unittest.skipUnless(c_coll, 'requires the C version of the collections module')
828-
class CSimpleLRUCacheTests(SimpleLRUCacheTests, unittest.TestCase):
829-
830-
@classmethod
831-
def setUpClass(cls):
832-
class type2test(SimpleLRUCache, c_coll.OrderedDict):
833-
pass
834-
cls.type2test = type2test
835-
836-
837778
if __name__ == "__main__":
838779
unittest.main()

Misc/NEWS

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ Core and Builtins
3131
Library
3232
-------
3333

34-
- Issue #27275: Fixed implementation of pop() and popitem() methods in
35-
subclasses of accelerated OrderedDict.
36-
3734
- Issue #18844: The various ways of specifing weights for random.choices()
3835
now produce the same result sequences.
3936

Objects/odictobject.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,13 +1102,28 @@ _odict_popkey_hash(PyObject *od, PyObject *key, PyObject *failobj,
11021102
}
11031103

11041104
/* Now delete the value from the dict. */
1105-
if (node != NULL) {
1106-
value = _PyDict_GetItem_KnownHash(od, key, hash); /* borrowed */
1107-
if (value != NULL) {
1108-
Py_INCREF(value);
1109-
if (_PyDict_DelItem_KnownHash(od, key, hash) < 0) {
1110-
Py_DECREF(value);
1111-
return NULL;
1105+
if (PyODict_CheckExact(od)) {
1106+
if (node != NULL) {
1107+
value = _PyDict_GetItem_KnownHash(od, key, hash); /* borrowed */
1108+
if (value != NULL) {
1109+
Py_INCREF(value);
1110+
if (_PyDict_DelItem_KnownHash(od, key, hash) < 0) {
1111+
Py_DECREF(value);
1112+
return NULL;
1113+
}
1114+
}
1115+
}
1116+
}
1117+
else {
1118+
int exists = PySequence_Contains(od, key);
1119+
if (exists < 0)
1120+
return NULL;
1121+
if (exists) {
1122+
value = PyObject_GetItem(od, key);
1123+
if (value != NULL) {
1124+
if (PyObject_DelItem(od, key) == -1) {
1125+
Py_CLEAR(value);
1126+
}
11121127
}
11131128
}
11141129
}

0 commit comments

Comments
 (0)