Skip to content

Instantly share code, notes, and snippets.

@pzelasko
Forked from jneight/debug_pickle.py
Created May 23, 2021 02:52
Show Gist options
  • Save pzelasko/90c1c13acd86f6c9c0aa4a3fa69dadba to your computer and use it in GitHub Desktop.
Save pzelasko/90c1c13acd86f6c9c0aa4a3fa69dadba to your computer and use it in GitHub Desktop.
Debug unpickled errors.
# from http://stackoverflow.com/questions/569754/how-to-tell-for-which-object-attribute-pickle-fails
"""
Show which fields cannot be pickled
"""
import pickle
def get_pickling_errors(obj,seen=None):
if seen is None:
seen = []
try:
state = obj.__getstate__()
except AttributeError:
return
if state is None:
return
if isinstance(state,tuple):
if not isinstance(state[0],dict):
state=state[1]
else:
state=state[0].update(state[1])
result = {}
for i in state:
try:
pickle.dumps(state[i],protocol=2)
except pickle.PicklingError:
if not state[i] in seen:
seen.append(state[i])
result[i]=get_pickling_errors(state[i],seen)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment