FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-145056: Fix merging of collections.OrderedDict and frozendict by serhiy-storchaka · Pull Request #146466 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (2) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 2 additions & 2 deletions Lib/collections/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -328,14 +328,14 @@ def __ior__(self, other):
return self

def __or__(self, other):
if not isinstance(other, dict):
if not isinstance(other, (dict, frozendict)):
return NotImplemented
new = self.__class__(self)
new.update(other)
return new

def __ror__(self, other):
if not isinstance(other, dict):
if not isinstance(other, (dict, frozendict)):
return NotImplemented
new = self.__class__(other)
new.update(self)
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_ordered_dict.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ def test_merge_operator(self):
d |= list(b.items())
expected = OrderedDict({0: 0, 1: 1, 2: 2, 3: 3})
self.assertEqual(a | dict(b), expected)
self.assertEqual(a | frozendict(b), expected)
self.assertEqual(a | b, expected)
self.assertEqual(c, expected)
self.assertEqual(d, expected)
Expand All @@ -706,12 +707,17 @@ def test_merge_operator(self):
c |= a
expected = OrderedDict({1: 1, 2: 1, 3: 3, 0: 0})
self.assertEqual(dict(b) | a, expected)
self.assertEqual(frozendict(b) | a, expected)
self.assertEqual(a.__ror__(frozendict(b)), expected)
self.assertEqual(b | a, expected)
self.assertEqual(c, expected)

self.assertIs(type(a | b), OrderedDict)
self.assertIs(type(dict(a) | b), OrderedDict)
self.assertIs(type(frozendict(a) | b), frozendict)
self.assertIs(type(b.__ror__(frozendict(a))), OrderedDict)
self.assertIs(type(a | dict(b)), OrderedDict)
self.assertIs(type(a | frozendict(b)), OrderedDict)

expected = a.copy()
a |= ()
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix merging of :class:`collections.OrderedDict` and :class:`frozendict`.
4 changes: 2 additions & 2 deletions Objects/odictobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ odict_or(PyObject *left, PyObject *right)
type = Py_TYPE(right);
other = left;
}
if (!PyDict_Check(other)) {
if (!PyAnyDict_Check(other)) {
Py_RETURN_NOTIMPLEMENTED;
}
PyObject *new = PyObject_CallOneArg((PyObject*)type, left);
Expand Down Expand Up @@ -2271,7 +2271,7 @@ static int
mutablemapping_update_arg(PyObject *self, PyObject *arg)
{
int res = 0;
if (PyDict_CheckExact(arg)) {
if (PyAnyDict_CheckExact(arg)) {
PyObject *items = PyDict_Items(arg);
if (items == NULL) {
return -1;
Expand Down
Loading

Back | FazBrowse Home | New Git URL