# Buggy code
def reconstruct_object(flags, value):
    """Reconstructs the value (if necessary) after having saved it in a
    dictionary
    """
    if not isinstance(flags, list):
        flags = parse_flag_string(flags)
    if "sig" in flags:
        if isinstance(value, dict):
            from hyperspy.signal import Signal

            value = Signal(**value)
            value._assign_subclass()
        return value
    if "fn" in flags:
        ifdill, thing = value
        if ifdill is None:
            return thing
        if ifdill in [False, "False", b"False"]:
            return types.FunctionType(marshal.loads(thing), globals())
        if ifdill in [True, "True", b"True"]:
            if not dill_avail:
                raise ValueError(
                    "the dictionary was constructed using "
                    '"dill" package, which is not available on the system'
                )
            else:
                return dill.loads(thing)
        # should not be reached
        raise ValueError("The object format is not recognized")
    return value


# Traceback (EOFError)
m = tmp.models.restore('BK_w_fine_structure')
---------------------------------------------------------------------------
EOFError                                  Traceback (most recent call last)
<ipython-input-349-668408e20346> in <module>()
----> 1 tmp.models.restore('BK_w_fine_structure')

/home/josh/git_repos/hyperspy/hyperspy/signal.py in restore(self, name)
253         name = self._check_name(name, True)
254         d = self._models.get_item(name + '._dict').as_dictionary()
--> 255         return self._signal.create_model(dictionary=copy.deepcopy(d))
256
257     def __repr__(self):

/home/josh/git_repos/hyperspy/hyperspy/_signals/eels.py in create_model(self, ll, auto_background, auto_add_edges, GOS, dictionary)
1254                           auto_add_edges=auto_add_edges,
1255                           GOS=GOS,
-> 1256                           dictionary=dictionary)
1257         return model
1258

/home/josh/git_repos/hyperspy/hyperspy/models/eelsmodel.py in __init__(self, spectrum, auto_background, auto_add_edges, ll, GOS, dictionary)
84             auto_background = False
85             auto_add_edges = False
---> 86             self._load_dictionary(dictionary)
87
88         if auto_background is True:

/home/josh/git_repos/hyperspy/hyperspy/model.py in _load_dictionary(self, dic)
255
256                 self.append(getattr(components, comp['_id_name'])(**init_args))
--> 257                 id_dict.update(self[-1]._load_dictionary(comp))
258             # deal with twins:
259             for comp in dic['components']:

/home/josh/git_repos/hyperspy/hyperspy/component.py in _load_dictionary(self, dic)
1079                 if hasattr(self, idname):
1080                     par = getattr(self, idname)
-> 1081                     t_id = par._load_dictionary(p)
1082                     id_dict[t_id] = par
1083                 else:

/home/josh/git_repos/hyperspy/hyperspy/component.py in _load_dictionary(self, dictionary)
183         """
184         if dictionary['_id_name'] == self._id_name:
--> 185             load_from_dictionary(self, dictionary)
186             return dictionary['self']
187         else:

/home/josh/git_repos/hyperspy/hyperspy/misc/export_dictionary.py in load_from_dictionary(target, dic)
182         flags = parse_flag_string(flags_str)
183         if 'id' not in flags:
--> 184             value = reconstruct_object(flags, value)
185             if 'init' in flags:
186                 new_whitelist[key] = (flags_str, value)

/home/josh/git_repos/hyperspy/hyperspy/misc/export_dictionary.py in reconstruct_object(flags, value)
215             return thing
216         if ifdill in [False, 'False', b'False']:
--> 217             return types.FunctionType(marshal.loads(thing), globals())
218         if ifdill in [True, 'True', b'True']:
219             if not dill_avail:

EOFError: marshal data too short

# Fixed code
def reconstruct_object(flags, value):
    """Reconstructs the value (if necessary) after having saved it in a
    dictionary
    """
    if not isinstance(flags, list):
        flags = parse_flag_string(flags)
    if "sig" in flags:
        if isinstance(value, dict):
            from hyperspy.signal import Signal

            value = Signal(**value)
            value._assign_subclass()
        return value
    if "fn" in flags:
        ifdill, thing = value
        if ifdill is None:
            return thing
        if ifdill in [True, "True", b"True"]:
            return dill.loads(thing)
        # should not be reached
        raise ValueError("The object format is not recognized")
    return value