What’s an accurate way of checking whether an object can be atomically pickled? When I say “atomically pickled”, I mean without considering other objects it may refer to. For example, this list:
l = [threading.Lock()]
is not a a pickleable object, because it refers to a Lock which is not pickleable. But atomically, this list itself is pickleable.
So how do you check whether an object is atomically pickleable? (I’m guessing the check should be done on the class, but I’m not sure.)
I want it to behave like this:
>>> is_atomically_pickleable(3)
True
>>> is_atomically_pickleable(3.1)
True
>>> is_atomically_pickleable([1, 2, 3])
True
>>> is_atomically_pickleable(threading.Lock())
False
>>> is_atomically_pickleable(open('whatever', 'r'))
False
Etc.
I ended up coding my own solution to this.
Here’s the code. Here are the tests. It’s part of GarlicSim, so you can use it by installing
garlicsimand doingfrom garlicsim.general_misc import pickle_tools.If you want to use it on Python 3 code, use the Python 3 fork of
garlicsim.Here is an excerpt from the module (may be outdated):