Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def dump_session(filename='/tmp/session.pkl', main=None, byref=False, **kwds):
"""pickle the current state of __main__ to a file"""
from .settings import settings
protocol = settings['protocol']
if main is None: main = _main_module
if hasattr(filename, 'write'):
f = filename
else:
f = open(filename, 'wb')
try:
if byref:
main = _stash_modules(main)
pickler = Pickler(f, protocol, **kwds)
pickler._main = main #FIXME: dill.settings are disabled
pickler._byref = False # disable pickling by name reference
pickler._recurse = False # disable pickling recursion for globals
pickler._session = True # is best indicator of when pickling a session
pickler.dump(main)
finally:
if f is not filename: # If newly opened file
f.close()
return
def is_dill(pickler, child=None):
"check the dill-ness of your pickler"
if (child is False) or PY34 or (not hasattr(pickler.__class__, 'mro')):
return 'dill' in pickler.__module__
return Pickler in pickler.__class__.mro()
def dump(obj, file, protocol=None, byref=None, fmode=None, recurse=None, **kwds):#, strictio=None):
"""pickle an object to a file"""
from .settings import settings
protocol = settings['protocol'] if protocol is None else int(protocol)
_kwds = kwds.copy()
_kwds.update(dict(byref=byref, fmode=fmode, recurse=recurse))
Pickler(file, protocol, **_kwds).dump(obj)
return
def __init__(self, *args, **kwds):
settings = Pickler.settings
_ignore = kwds.pop('ignore', None)
StockUnpickler.__init__(self, *args, **kwds)
self._main = _main_module
self._ignore = settings['ignore'] if _ignore is None else _ignore
def _extend():
"""extend pickle with all of dill's registered types"""
# need to have pickle not choke on _main_module? use is_dill(pickler)
for t,func in Pickler.dispatch.items():
try:
StockPickler.dispatch[t] = func
except: #TypeError, PicklingError, UnpicklingError
log.info("skip: %s" % t)
else: pass
return