Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
apmorton committed Nov 24, 2023
1 parent e1ec816 commit 6b67dc5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/frozendict/_frozendictpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ def frozendict_reversed(self, *args, **kwargs):
frozendict.update = immutable
frozendict.__delattr__ = immutable
frozendict.__setattr__ = immutable
frozendict.__module__ = 'frozendict'

_sentinel = object()
out_of_range_err_tpl = "{name} index {index} out of max range {sign}{maxpos}"
Expand Down
4 changes: 4 additions & 0 deletions src/frozendict/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# this provides compatibility for older pickles
# created on a python-only implementation that
# specifically mention frozendict.core.frozendict
from frozendict import frozendict
10 changes: 9 additions & 1 deletion test/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,12 @@ def module_prefix(self):
return ""

return "frozendict."


@pytest.fixture
def module_patch(self, monkeypatch):
import frozendict

if self.is_subclass or not frozendict.c_ext:
return

monkeypatch.setattr(frozendict, 'frozendict', self.FrozendictClass)
8 changes: 5 additions & 3 deletions test/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ def test_equals_dict(self, fd, fd_dict):
assert fd == fd_dict

@pytest.mark.parametrize("protocol", range(pickle.HIGHEST_PROTOCOL + 1))
def test_pickle(self, fd, protocol):
def test_pickle(self, fd, protocol, module_patch):
dump = pickle.dumps(fd, protocol=protocol)
assert dump
assert pickle.loads(dump) == fd
assert b'core' not in dump
assert b'_frozendictpy' not in dump

def test_constructor_iterator(self, fd, fd_items):
assert self.FrozendictClass(fd_items) == fd
Expand Down Expand Up @@ -257,14 +259,14 @@ def test_xor_items(self, fd, fd_dict, fd2, fd_dict_2):
assert fd.items() ^ fd2.items() == res

@pytest.mark.parametrize("protocol", range(pickle.HIGHEST_PROTOCOL + 1))
def test_pickle_iter_key(self, fd, protocol):
def test_pickle_iter_key(self, fd, protocol, module_patch):
orig = iter(fd.keys())
dump = pickle.dumps(orig, protocol=protocol)
assert dump
assert tuple(pickle.loads(dump)) == tuple(orig)

@pytest.mark.parametrize("protocol", range(pickle.HIGHEST_PROTOCOL + 1))
def test_pickle_iter_item(self, fd, protocol):
def test_pickle_iter_item(self, fd, protocol, module_patch):
orig = iter(fd.items())
dump = pickle.dumps(orig, protocol=protocol)
assert dump
Expand Down
9 changes: 9 additions & 0 deletions test/frozendict_only.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pickle
import pytest
from copy import copy, deepcopy
from .base import FrozendictTestBase
Expand Down Expand Up @@ -41,3 +42,11 @@ def test_init(self, fd):
def test_del_empty(self):
fd = self.FrozendictClass({1: 2})
assert fd.delete(1) is self.FrozendictClass()

def test_load_core_pickle(self):
obj = pickle.loads(b'cfrozendict.core\nfrozendict\np0\n((dp1\nVa\np2\nI1\nstp3\nRp4\n.')
assert obj == {'a': 1}

def test_load_non_core_pickle(self):
obj = pickle.loads(b'cfrozendict\nfrozendict\np0\n((dp1\nVa\np2\nI1\nstp3\nRp4\n.')
assert obj == {'a': 1}

0 comments on commit 6b67dc5

Please sign in to comment.