Create custom __setstate__ and __getstate__ methods.
(self)
| 1019 | return self |
| 1020 | |
| 1021 | def _make_getstate_setstate(self): |
| 1022 | """ |
| 1023 | Create custom __setstate__ and __getstate__ methods. |
| 1024 | """ |
| 1025 | # __weakref__ is not writable. |
| 1026 | state_attr_names = tuple( |
| 1027 | an for an in self._attr_names if an != "__weakref__" |
| 1028 | ) |
| 1029 | |
| 1030 | def slots_getstate(self): |
| 1031 | """ |
| 1032 | Automatically created by attrs. |
| 1033 | """ |
| 1034 | return {name: getattr(self, name) for name in state_attr_names} |
| 1035 | |
| 1036 | hash_caching_enabled = self._cache_hash |
| 1037 | |
| 1038 | def slots_setstate(self, state): |
| 1039 | """ |
| 1040 | Automatically created by attrs. |
| 1041 | """ |
| 1042 | __bound_setattr = _OBJ_SETATTR.__get__(self) |
| 1043 | if isinstance(state, tuple): |
| 1044 | # Backward compatibility with attrs instances pickled with |
| 1045 | # attrs versions before v22.2.0 which stored tuples. |
| 1046 | for name, value in zip(state_attr_names, state): |
| 1047 | __bound_setattr(name, value) |
| 1048 | else: |
| 1049 | for name in state_attr_names: |
| 1050 | if name in state: |
| 1051 | __bound_setattr(name, state[name]) |
| 1052 | |
| 1053 | # The hash code cache is not included when the object is |
| 1054 | # serialized, but it still needs to be initialized to None to |
| 1055 | # indicate that the first call to __hash__ should be a cache |
| 1056 | # miss. |
| 1057 | if hash_caching_enabled: |
| 1058 | __bound_setattr(_HASH_CACHE_FIELD, None) |
| 1059 | |
| 1060 | return slots_getstate, slots_setstate |
| 1061 | |
| 1062 | def make_unhashable(self): |
| 1063 | self._cls_dict["__hash__"] = None |