Skip to content

Commit cf8ef94

Browse files
committed
Fix bug in pickling support of frozendict.
See issue #55.
1 parent 0b50f95 commit cf8ef94

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

unpythonic/collections.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,19 @@ def __new__(cls, *ms, **bindings): # make the empty frozendict() a singleton
351351
return _the_empty_frozendict
352352
return super().__new__(cls) # object() takes no args, but we need to see them
353353

354+
# Pickling support.
355+
# https://github.com/Technologicat/unpythonic/issues/55
356+
# https://docs.python.org/3/library/pickle.html#object.__getnewargs_ex__
357+
# https://docs.python.org/3/library/pickle.html#object.__getnewargs__
358+
def __getnewargs__(self):
359+
if self is not _the_empty_frozendict:
360+
# In our case it doesn't matter what the value is, as long as there is one,
361+
# because `__new__` uses the *presence* of any args to know the instance is
362+
# nonempty, and hence an instance should actually be created instead of
363+
# just returning the empty singleton frozendict.
364+
return ("nonempty",)
365+
return ()
366+
354367
def __init__(self, *ms, **bindings):
355368
"""Arguments:
356369

0 commit comments

Comments
 (0)