Skip to content

Commit

Permalink
make OMD picklable under py3, fixes #337
Browse files Browse the repository at this point in the history
  • Loading branch information
mahmoud committed Apr 18, 2023
1 parent f892a2b commit 504b9d4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion boltons/dictutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,17 @@ class OrderedMultiDict(dict):
behavior, just use :meth:`~OrderedMultiDict.todict()`.
"""
def __new__(cls, *a, **kw):
ret = super(OrderedMultiDict, cls).__new__(cls)
ret._clear_ll()
return ret

def __init__(self, *args, **kwargs):
if len(args) > 1:
raise TypeError('%s expected at most 1 argument, got %s'
% (self.__class__.__name__, len(args)))
super(OrderedMultiDict, self).__init__()

self._clear_ll()
if args:
self.update_extend(args[0])
if kwargs:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_dictutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ def test_copy():
return


def test_omd_pickle():
import pickle

empty = OMD()
assert pickle.loads(pickle.dumps(empty)) == empty

nonempty = OMD([('a', 1), ('b', 2)])
assert pickle.loads(pickle.dumps(nonempty)) == nonempty


def test_clear():
for itemset in _ITEMSETS:
omd = OMD(itemset)
Expand Down

0 comments on commit 504b9d4

Please sign in to comment.