Skip to content

Commit

Permalink
Merge pull request #278 from tmshn/session-fileobj
Browse files Browse the repository at this point in the history
Fixed dump|load_session to take file-like object
  • Loading branch information
mmckerns committed Sep 9, 2018
2 parents 5ec4e57 + da894d7 commit b509fd2
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions dill/_dill.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,10 @@ def dump_session(filename='/tmp/session.pkl', main=None, byref=False):
from .settings import settings
protocol = settings['protocol']
if main is None: main = _main_module
f = open(filename, 'wb')
if hasattr(filename, 'write'):
f = filename
else:
f = open(filename, 'wb')
try:
if byref:
main = _stash_modules(main)
Expand All @@ -388,13 +391,17 @@ def dump_session(filename='/tmp/session.pkl', main=None, byref=False):
pickler._session = True # is best indicator of when pickling a session
pickler.dump(main)
finally:
f.close()
if f is not filename: # If newly opened file
f.close()
return

def load_session(filename='/tmp/session.pkl', main=None):
"""update the __main__ module with the state from the session file"""
if main is None: main = _main_module
f = open(filename, 'rb')
if hasattr(filename, 'read'):
f = filename
else:
f = open(filename, 'rb')
try:
unpickler = Unpickler(f)
unpickler._main = main
Expand All @@ -404,7 +411,8 @@ def load_session(filename='/tmp/session.pkl', main=None):
main.__dict__.update(module.__dict__)
_restore_modules(main)
finally:
f.close()
if f is not filename: # If newly opened file
f.close()
return

### End: Pickle the Interpreter
Expand Down

0 comments on commit b509fd2

Please sign in to comment.