def save_global(self, obj, name=None, pack=pickle.struct.pack):
assert obj
assert id(obj) not in self.memo
if name is None:
name = obj.__name__
module = getattr(obj, "__module__", None)
if module is None or module == "__main__":
module = pickle.whichmodule(obj, name)
if module is None or module == "__main__":
raise pickle.PicklingError(
"Can't pickle %r: module not found: %s" % (obj, module))
try:
__import__(module)
mod = sys.modules[module]
klass = getattr(mod, name)
except (ImportError, KeyError, AttributeError):
raise pickle.PicklingError(
"Can't pickle %r: it's not found as %s.%s" % (obj, module, name))
else:
if klass is not obj:
raise pickle.PicklingError(
"Can't pickle %r: it's not the same object as %s.%s" % (obj, module, name))
assert "\n" not in module
assert "\n" not in name
self.write(pickle.GLOBAL + module + '\n' + name + '\n')
self.memoize(obj)
# Some types in the types modules are not correctly referenced,
# such as types.FunctionType. This is fixed here.
评论列表
文章目录