本文整理汇总了Python中pickle.Pickler.save_global方法的典型用法代码示例。如果您正苦于以下问题:Python Pickler.save_global方法的具体用法?Python Pickler.save_global怎么用?Python Pickler.save_global使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pickle.Pickler
的用法示例。
在下文中一共展示了Pickler.save_global方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save_function
# 需要导入模块: from pickle import Pickler [as 别名]
# 或者: from pickle.Pickler import save_global [as 别名]
def save_function(self, obj, name=None):
""" Registered with the dispatch to handle all function types.
Determines what kind of function obj is (e.g. lambda, defined at
interactive prompt, etc) and handles the pickling appropriately.
"""
name = getattr(obj, '__name__', None)
module_name = _whichmodule(obj, name)
module = sys.modules.get(module_name, None)
if module and 'cloudpickle' not in module.__name__:
self.modules.add(module)
if _is_global(obj, name=name):
return Pickler.save_global(self, obj, name=name)
elif PYPY and isinstance(obj.__code__, builtin_code_type):
return self.save_pypy_builtin_func(obj)
else:
return self.save_function_tuple(obj)
示例2: save_builtin_function_or_method
# 需要导入模块: from pickle import Pickler [as 别名]
# 或者: from pickle.Pickler import save_global [as 别名]
def save_builtin_function_or_method(self, obj):
is_bound = getattr(obj, '__self__', None) is not None
if is_bound:
# obj is a bound builtin method.
rv = (getattr, (obj.__self__, obj.__name__))
return self.save_reduce(obj=obj, *rv)
is_unbound = hasattr(obj, '__objclass__')
if is_unbound:
# obj is an unbound builtin method (accessed from its class)
rv = (getattr, (obj.__objclass__, obj.__name__))
return self.save_reduce(obj=obj, *rv)
# Otherwise, obj is not a method, but a function. Fallback to
# default pickling by attribute.
return Pickler.save_global(self, obj)
示例3: save_global
# 需要导入模块: from pickle import Pickler [as 别名]
# 或者: from pickle.Pickler import save_global [as 别名]
def save_global(self, obj, name=None, pack=struct.pack):
"""
Save a "global".
The name of this method is somewhat misleading: all types get
dispatched here.
"""
if obj.__module__ == "__main__":
return self.save_dynamic_class(obj)
try:
return Pickler.save_global(self, obj, name=name)
except Exception:
if obj.__module__ == "__builtin__" or obj.__module__ == "builtins":
if obj in _BUILTIN_TYPE_NAMES:
return self.save_reduce(
_builtin_type, (_BUILTIN_TYPE_NAMES[obj],), obj=obj)
typ = type(obj)
if typ is not obj and isinstance(obj, (type, types.ClassType)):
return self.save_dynamic_class(obj)
raise
示例4: save_global
# 需要导入模块: from pickle import Pickler [as 别名]
# 或者: from pickle.Pickler import save_global [as 别名]
def save_global(self, obj, name=None, pack=struct.pack):
"""
Save a "global".
The name of this method is somewhat misleading: all types get
dispatched here.
"""
if obj.__module__ == "__main__":
return self.save_dynamic_class(obj)
try:
return Pickler.save_global(self, obj, name=name)
except Exception:
if obj.__module__ == "__builtin__" or obj.__module__ == "builtins":
if obj in _BUILTIN_TYPE_NAMES:
return self.save_reduce(
_builtin_type, (_BUILTIN_TYPE_NAMES[obj],), obj=obj)
typ = type(obj)
if typ is not obj and isinstance(obj, (type, types.ClassType)):
return self.save_dynamic_class(obj)
raise
示例5: save_function
# 需要导入模块: from pickle import Pickler [as 别名]
# 或者: from pickle.Pickler import save_global [as 别名]
def save_function(pickler, obj):
if not _locate_function(obj): #, pickler._session):
log.info("F1: %s" % obj)
if getattr(pickler, '_recurse', False):
# recurse to get all globals referred to by obj
from .detect import globalvars
globs = globalvars(obj, recurse=True, builtin=True)
if obj in globs.values(): #ABORT: use _recurse=False
globs = obj.__globals__ if PY3 else obj.func_globals
else:
globs = obj.__globals__ if PY3 else obj.func_globals
if PY3:
pickler.save_reduce(_create_function, (obj.__code__,
globs, obj.__name__,
obj.__defaults__, obj.__closure__,
obj.__dict__), obj=obj)
else:
pickler.save_reduce(_create_function, (obj.func_code,
globs, obj.func_name,
obj.func_defaults, obj.func_closure,
obj.__dict__), obj=obj)
else:
log.info("F2: %s" % obj)
StockPickler.save_global(pickler, obj) #NOTE: also takes name=...
return
示例6: save_builtin_method
# 需要导入模块: from pickle import Pickler [as 别名]
# 或者: from pickle.Pickler import save_global [as 别名]
def save_builtin_method(pickler, obj):
if obj.__self__ is not None:
if obj.__self__ is __builtin__:
module = 'builtins' if PY3 else '__builtin__'
log.info("B1: %s" % obj)
else:
module = obj.__self__
log.info("B3: %s" % obj)
_recurse = pickler._recurse
pickler._recurse = False
pickler.save_reduce(_get_attr, (module, obj.__name__), obj=obj)
pickler._recurse = _recurse
else:
log.info("B2: %s" % obj)
StockPickler.save_global(pickler, obj)
return
示例7: save_global
# 需要导入模块: from pickle import Pickler [as 别名]
# 或者: from pickle.Pickler import save_global [as 别名]
def save_global(self, obj, name=None, pack=struct.pack):
"""
Save a "global".
The name of this method is somewhat misleading: all types get
dispatched here.
"""
if obj.__module__ == "__main__":
return self.save_dynamic_class(obj)
try:
return Pickler.save_global(self, obj, name=name)
except Exception:
if obj.__module__ == "__builtin__" or obj.__module__ == "builtins":
if obj in _BUILTIN_TYPE_NAMES:
return self.save_reduce(
_builtin_type, (_BUILTIN_TYPE_NAMES[obj],), obj=obj)
typ = type(obj)
if typ is not obj and isinstance(obj, (type, types.ClassType)):
return self.save_dynamic_class(obj)
raise
示例8: save_global
# 需要导入模块: from pickle import Pickler [as 别名]
# 或者: from pickle.Pickler import save_global [as 别名]
def save_global(self, obj, name=None, pack=struct.pack):
"""
Save a "global".
The name of this method is somewhat misleading: all types get
dispatched here.
"""
try:
module_name = _whichmodule(obj, obj.__name__)
except Exception:
module_name = _whichmodule(obj, None)
module = sys.modules.get(module_name, None)
if module and 'cloudpickle' not in module.__name__:
self.modules.add(module)
if obj is type(None):
return self.save_reduce(type, (None,), obj=obj)
elif obj is type(Ellipsis):
return self.save_reduce(type, (Ellipsis,), obj=obj)
elif obj is type(NotImplemented):
return self.save_reduce(type, (NotImplemented,), obj=obj)
elif obj in _BUILTIN_TYPE_NAMES:
return self.save_reduce(
_builtin_type, (_BUILTIN_TYPE_NAMES[obj],), obj=obj)
elif name is not None:
Pickler.save_global(self, obj, name=name)
elif not _is_global(obj, name=name):
self.save_dynamic_class(obj)
else:
Pickler.save_global(self, obj, name=name)
示例9: save_builtin_function
# 需要导入模块: from pickle import Pickler [as 别名]
# 或者: from pickle.Pickler import save_global [as 别名]
def save_builtin_function(self, obj):
if obj.__module__ == "__builtin__":
return self.save_global(obj)
return self.save_function(obj)
示例10: save_global
# 需要导入模块: from pickle import Pickler [as 别名]
# 或者: from pickle.Pickler import save_global [as 别名]
def save_global(self, obj, name=None, **kwargs):
module = getattr(obj, '__module__', None)
if module == '__main__':
warnings.warn(
MAIN_MODULE_WARNING.format(kwargs.get('name', obj.__name__))
)
_Pickler.save_global(self, obj, name=name, **kwargs)
示例11: save_global
# 需要导入模块: from pickle import Pickler [as 别名]
# 或者: from pickle.Pickler import save_global [as 别名]
def save_global(self, obj, name=None, pack=struct.pack):
"""
Save a "global".
The name of this method is somewhat misleading: all types get
dispatched here.
"""
if obj is type(None):
return self.save_reduce(type, (None,), obj=obj)
elif obj is type(Ellipsis):
return self.save_reduce(type, (Ellipsis,), obj=obj)
elif obj is type(NotImplemented):
return self.save_reduce(type, (NotImplemented,), obj=obj)
if obj.__module__ == "__main__":
return self.save_dynamic_class(obj)
try:
return Pickler.save_global(self, obj, name=name)
except Exception:
if obj.__module__ == "__builtin__" or obj.__module__ == "builtins":
if obj in _BUILTIN_TYPE_NAMES:
return self.save_reduce(
_builtin_type, (_BUILTIN_TYPE_NAMES[obj],), obj=obj)
typ = type(obj)
if typ is not obj and isinstance(obj, (type, types.ClassType)):
return self.save_dynamic_class(obj)
raise
示例12: save_global
# 需要导入模块: from pickle import Pickler [as 别名]
# 或者: from pickle.Pickler import save_global [as 别名]
def save_global(self, obj, name=None, pack=struct.pack):
"""
Save a "global".
The name of this method is somewhat misleading: all types get
dispatched here.
"""
if obj.__module__ == "__builtin__" or obj.__module__ == "builtins":
if obj in _BUILTIN_TYPE_NAMES:
return self.save_reduce(_builtin_type, (_BUILTIN_TYPE_NAMES[obj],), obj=obj)
if name is None:
name = obj.__name__
modname = getattr(obj, "__module__", None)
if modname is None:
try:
# whichmodule() could fail, see
# https://bitbucket.org/gutworth/six/issues/63/importing-six-breaks-pickling
modname = pickle.whichmodule(obj, name)
except Exception:
modname = '__main__'
if modname == '__main__':
themodule = None
else:
__import__(modname)
themodule = sys.modules[modname]
self.modules.add(themodule)
if hasattr(themodule, name) and getattr(themodule, name) is obj:
return Pickler.save_global(self, obj, name)
typ = type(obj)
if typ is not obj and isinstance(obj, (type, types.ClassType)):
self.save_dynamic_class(obj)
else:
raise pickle.PicklingError("Can't pickle %r" % obj)
示例13: save_classobj
# 需要导入模块: from pickle import Pickler [as 别名]
# 或者: from pickle.Pickler import save_global [as 别名]
def save_classobj(pickler, obj): #FIXME: enable pickler._byref
if obj.__module__ == '__main__': #XXX: use _main_module.__name__ everywhere?
log.info("C1: %s" % obj)
pickler.save_reduce(ClassType, (obj.__name__, obj.__bases__,
obj.__dict__), obj=obj)
#XXX: or obj.__dict__.copy()), obj=obj) ?
else:
log.info("C2: %s" % obj)
StockPickler.save_global(pickler, obj)
return
示例14: save_numpy_ufunc
# 需要导入模块: from pickle import Pickler [as 别名]
# 或者: from pickle.Pickler import save_global [as 别名]
def save_numpy_ufunc(pickler, obj):
log.info("Nu: %s" % obj)
StockPickler.save_global(pickler, obj)
return
# NOTE: the above 'save' performs like:
# import copy_reg
# def udump(f): return f.__name__
# def uload(name): return getattr(numpy, name)
# copy_reg.pickle(NumpyUfuncType, udump, uload)
示例15: save_type
# 需要导入模块: from pickle import Pickler [as 别名]
# 或者: from pickle.Pickler import save_global [as 别名]
def save_type(pickler, obj):
if obj in _typemap:
log.info("T1: %s" % obj)
pickler.save_reduce(_load_type, (_typemap[obj],), obj=obj)
elif obj.__module__ == '__main__':
try: # use StockPickler for special cases [namedtuple,]
[getattr(obj, attr) for attr in ('_fields','_asdict',
'_make','_replace')]
log.info("T6: %s" % obj)
StockPickler.save_global(pickler, obj)
return
except AttributeError: pass
if issubclass(type(obj), type):
# try: # used when pickling the class as code (or the interpreter)
if is_dill(pickler) and not pickler._byref:
# thanks to Tom Stepleton pointing out pickler._session unneeded
log.info("T2: %s" % obj)
_dict = _dict_from_dictproxy(obj.__dict__)
# except: # punt to StockPickler (pickle by class reference)
else:
log.info("T5: %s" % obj)
StockPickler.save_global(pickler, obj)
return
else:
log.info("T3: %s" % obj)
_dict = obj.__dict__
#print (_dict)
#print ("%s\n%s" % (type(obj), obj.__name__))
#print ("%s\n%s" % (obj.__bases__, obj.__dict__))
pickler.save_reduce(_create_type, (type(obj), obj.__name__,
obj.__bases__, _dict), obj=obj)
else:
log.info("T4: %s" % obj)
#print (obj.__dict__)
#print ("%s\n%s" % (type(obj), obj.__name__))
#print ("%s\n%s" % (obj.__bases__, obj.__dict__))
StockPickler.save_global(pickler, obj)
return