Skip to content

Commit

Permalink
fix(core): support unpickling older pickles
Browse files Browse the repository at this point in the history
The changes to pickling in 6a1c295 means that
in sympy>=1.9 Symbols are pickled using __getnewargs_ex__ rather than
__getstate__/__setstate__. This breaks unpickling of pickles created with
sympy<1.9 because those pickle files still need to use __setstate__ when being
unpickled.

This commit adds back the __setstate__ method to Symbol although it should only
be used when unpickling pickles created in older SymPy versions.
  • Loading branch information
oscarbenjamin committed Oct 17, 2021
1 parent 907f164 commit ef0de0c
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions sympy/core/symbol.py
Expand Up @@ -304,6 +304,15 @@ def __new_stage2__(cls, name, **assumptions):
def __getnewargs_ex__(self):
return ((self.name,), self.assumptions0)

# NOTE: __setstate__ is not needed for pickles created by __getnewargs_ex__
# but was used before Symbol was changed to use __getnewargs_ex__ in v1.9.
# Pickles created in previous SymPy versions will still need __setstate__
# so that they can be unpickled in SymPy > v1.9.

def __setstate__(self, state):
for name, value in state.items():
setattr(self, name, value)

def _hashable_content(self):
# Note: user-specified assumptions not hashed, just derived ones
return (self.name,) + tuple(sorted(self.assumptions0.items()))
Expand Down

0 comments on commit ef0de0c

Please sign in to comment.