def test_bad_things(): f = inspect.currentframe() assert baditems(f) == [f] #assert baditems(globals()) == [f] #XXX assert badobjects(f) is f assert badtypes(f) == type(f) assert type(errors(f)) is PicklingError if IS_PYPY else TypeError d = badtypes(f, 1) assert isinstance(d, dict) assert list(badobjects(f, 1).keys()) == list(d.keys()) assert list(errors(f, 1).keys()) == list(d.keys()) s = set([(err.__class__.__name__,err.args[0]) for err in list(errors(f, 1).values())]) a = dict(s) assert len(s) is len(a) # TypeError (and possibly PicklingError) n = 1 if IS_PYPY else 2 assert len(a) is n if 'PicklingError' in a.keys() else n-1
def save_object(obj, filename, tries=2, sleep_delay=3): """ Serialize an object Parameters ---------- obj : object object, that shall be serialized and stored filename : str absolute path, name and extension of the created file tries : int number of attempts to store the file sleep_delay : int time, the method shall wait before the next attempt, when writing the file didn't work Returns ------- None """ filename = r"{}".format(filename) file_written = False for i in range(tries): try: output = open(filename, "wb") dump(obj, output, HIGHEST_PROTOCOL) output.close() file_written = True break except: time.sleep(sleep_delay) continue if not file_written: logger = logging.getLogger(__name__) logger.warning(detect.badobjects(obj)) raise RuntimeError('Unable to write file %s.' % filename)
# Author: Mike McKerns (mmckerns @caltech and @uqfoundation) # Copyright (c) 2008-2016 California Institute of Technology. # License: 3-clause BSD. The full license text is available at: # - http://trac.mystic.cacr.caltech.edu/project/pathos/browser/dill/LICENSE from dill.detect import baditems, badobjects, badtypes, errors, parent, at, globalvars from dill import settings from dill.dill import IS_PYPY from pickle import PicklingError import inspect f = inspect.currentframe() assert baditems(f) == [f] #assert baditems(globals()) == [f] #XXX assert badobjects(f) is f assert badtypes(f) == type(f) assert type(errors(f)) is PicklingError if IS_PYPY else TypeError d = badtypes(f, 1) assert isinstance(d, dict) assert list(badobjects(f, 1).keys()) == list(d.keys()) assert list(errors(f, 1).keys()) == list(d.keys()) s = set([(err.__class__.__name__, err.args[0]) for err in list(errors(f, 1).values())]) a = dict(s) assert len(s) is len(a) # TypeError (and possibly PicklingError) assert len(a) is 2 if 'PicklingError' in a.keys() else 1 x = [4, 5, 6, 7] listiter = iter(x) obj = parent(listiter, list)
#!/usr/bin/env python # # Author: Mike McKerns (mmckerns @caltech and @uqfoundation) # Copyright (c) 2008-2015 California Institute of Technology. # License: 3-clause BSD. The full license text is available at: # - http://trac.mystic.cacr.caltech.edu/project/pathos/browser/dill/LICENSE from dill.detect import baditems, badobjects, badtypes, errors, parent, at, globalvars from dill import settings import inspect f = inspect.currentframe() assert baditems(f) == [f] #assert baditems(globals()) == [f] #XXX assert badobjects(f) is f assert badtypes(f) == type(f) assert isinstance(errors(f), TypeError) d = badtypes(f, 1) assert isinstance(d, dict) assert list(badobjects(f, 1).keys()) == list(d.keys()) assert list(errors(f, 1).keys()) == list(d.keys()) s = set([(err.__class__.__name__,err.args[0]) for err in list(errors(f, 1).values())]) a = dict(s) assert len(s) is len(a) # TypeError (and possibly PicklingError) assert len(a) is 2 if 'PicklingError' in a.keys() else 1 x = [4,5,6,7] listiter = iter(x) obj = parent(listiter, list) assert obj is x