Skip to content

Commit

Permalink
Fix bug in pickling MappingProxyType in PyPy 3.7+ (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
anivegesana committed Jun 10, 2022
1 parent 0ce3baf commit dc9a1b9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
31 changes: 7 additions & 24 deletions dill/_dill.py
Original file line number Diff line number Diff line change
Expand Up @@ -1878,30 +1878,13 @@ def save_dictproxy(pickler, obj):
pickler.save_reduce(DictProxyType, (mapping,), obj=obj)
log.info("# Mp")
return
elif not IS_PYPY:
if not OLD33:
@register(DictProxyType)
def save_dictproxy(pickler, obj):
log.info("Mp: %s" % obj)
pickler.save_reduce(DictProxyType, (obj.copy(),), obj=obj)
log.info("# Mp")
return
else:
# The following function is based on 'saveDictProxy' from spickle
# Copyright (c) 2011 by science+computing ag
# License: http://www.apache.org/licenses/LICENSE-2.0
@register(DictProxyType)
def save_dictproxy(pickler, obj):
log.info("Dp: %s" % obj)
attr = obj.get('__dict__')
#pickler.save_reduce(_create_dictproxy, (attr,'nested'), obj=obj)
if type(attr) == GetSetDescriptorType and attr.__name__ == "__dict__" \
and getattr(attr.__objclass__, "__dict__", None) == obj:
pickler.save_reduce(getattr, (attr.__objclass__,"__dict__"),obj=obj)
log.info("# Dp")
return
# all bad below... so throw ReferenceError or TypeError
raise ReferenceError("%s does not reference a class __dict__" % obj)
else:
@register(DictProxyType)
def save_dictproxy(pickler, obj):
log.info("Mp: %s" % obj)
pickler.save_reduce(DictProxyType, (obj.copy(),), obj=obj)
log.info("# Mp")
return

@register(SliceType)
def save_slice(pickler, obj):
Expand Down
6 changes: 5 additions & 1 deletion tests/test_dictviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
# - https://github.com/uqfoundation/dill/blob/master/LICENSE

import dill
from dill._dill import OLD310, MAPPING_PROXY_TRICK
from dill._dill import OLD310, MAPPING_PROXY_TRICK, DictProxyType

def test_dictproxy():
assert dill.copy(DictProxyType({'a': 2}))

def test_dictviews():
x = {'a': 1}
Expand All @@ -31,5 +34,6 @@ def test_dictproxy_trick():
assert dict(seperate_views[1]) == new_x

if __name__ == '__main__':
test_dictproxy()
test_dictviews()
test_dictproxy_trick()

0 comments on commit dc9a1b9

Please sign in to comment.