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

Skip to content

Commit 0438683

Browse files
Backed out changeset 9f7505019767 (issue #27275).
1 parent b8c5f54 commit 0438683

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
@@ -730,64 +730,5 @@ def test_popitem(self):
730730
self.assertRaises(KeyError, d.popitem)
731731

732732

733-
class SimpleLRUCache:
734-
735-
def __init__(self, size):
736-
super().__init__()
737-
self.size = size
738-
739-
def __getitem__(self, item):
740-
value = super().__getitem__(item)
741-
self.move_to_end(item)
742-
return value
743-
744-
def __setitem__(self, key, value):
745-
while key not in self and len(self) >= self.size:
746-
self.popitem(last=False)
747-
super().__setitem__(key, value)
748-
self.move_to_end(key)
749-
750-
751-
class SimpleLRUCacheTests:
752-
753-
def test_add_after_full(self):
754-
c = self.type2test(2)
755-
c['t1'] = 1
756-
c['t2'] = 2
757-
c['t3'] = 3
758-
self.assertEqual(list(c), ['t2', 't3'])
759-
760-
def test_popitem(self):
761-
c = self.type2test(3)
762-
for i in range(1, 4):
763-
c[i] = i
764-
self.assertEqual(c.popitem(last=False), (1, 1))
765-
self.assertEqual(c.popitem(last=True), (3, 3))
766-
767-
def test_change_order_on_get(self):
768-
c = self.type2test(3)
769-
for i in range(1, 4):
770-
c[i] = i
771-
self.assertEqual(list(c), list(range(1, 4)))
772-
self.assertEqual(c[2], 2)
773-
self.assertEqual(list(c), [1, 3, 2])
774-
775-
776-
class PySimpleLRUCacheTests(SimpleLRUCacheTests, unittest.TestCase):
777-
778-
class type2test(SimpleLRUCache, py_coll.OrderedDict):
779-
pass
780-
781-
782-
@unittest.skipUnless(c_coll, 'requires the C version of the collections module')
783-
class CSimpleLRUCacheTests(SimpleLRUCacheTests, unittest.TestCase):
784-
785-
@classmethod
786-
def setUpClass(cls):
787-
class type2test(SimpleLRUCache, c_coll.OrderedDict):
788-
pass
789-
cls.type2test = type2test
790-
791-
792733
if __name__ == "__main__":
793734
unittest.main()

Misc/NEWS

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,6 @@ Core and Builtins
113113
Library
114114
-------
115115

116-
- Issue #27275: Fixed implementation of pop() and popitem() methods in
117-
subclasses of accelerated OrderedDict.
118-
119116
- Issue #28255: calendar.TextCalendar().prmonth() no longer prints a space
120117
at the start of new line after printing a month's calendar. Patch by
121118
Xiang Zhang.

Objects/odictobject.c

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

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

0 commit comments

Comments
 (0)