Is there any way we can safely load a pickle?
You've asked for any way, but it partly depends not only on the way, but on the pickle in question and on what you mean by "safely."
Unless you mean something like "reasonably safely, given that I'll always know the provenance of the pickle," the answer is probably "no."
However, here are some questions to which the answer is "yes":
- Can I safely load a pickle if I'm 100% sure that I wrote it and it hasn't been modified in transit?
- Can I safely load a pickle if the source is trusted and I've checked that the file from which I'm loading the pickle is indeed from that source?
- Can I safely achieve the same thing as loading a pickle by using completely safe de/serialization logic in the vast majority of cases?
So, the first question to ask yourself is: does the third of these apply to you? Can you serialize and deserialize in a different way?
If not, do either of the first two apply?
If not, there is a project about which I just learned at PyCon called "Pikara" - it aims to "make unpickling objects as safe as it ever is going to be." It's apparently named for a pickled Polish dish; I suggested that "kimchi" might be equally apt. :-)
If an alternative method of serialization isn't ideal for your implementation, you might consider checking it out: https://github.com/latacora/pikara
Another answerer also posted an alternate unpickling method, but I can't vouch for it (although I took a good look today and it is at least interesting - I'll check back when the docs come of age).
If this answer hasn't given you 100% confidence in the way forward, then let me ask a follow-up: what you are actually trying to unpickle here?