# Buggy code
def __init__(
    self,
    module_dirs,
    opts=None,
    tag="module",
    loaded_base_name=None,
    mod_type_check=None,
    pack=None,
    whitelist=None,
    virtual_enable=True,
    static_modules=None,
):  # pylint: disable=W0231
    self.inject_globals = {}
    self.opts = self.__prep_mod_opts(opts)

    self.module_dirs = module_dirs
    if opts is None:
        opts = {}
    self.tag = tag
    self.loaded_base_name = loaded_base_name or LOADED_BASE_NAME
    self.mod_type_check = mod_type_check or _mod_type

    self.pack = {} if pack is None else pack
    if "__context__" not in self.pack:
        self.pack["__context__"] = {}

    self.whitelist = whitelist
    self.virtual_enable = virtual_enable
    self.initial_load = True

    # names of modules that we don't have (errors, __virtual__, etc.)
    self.missing_modules = {}  # mapping of name -> error
    self.loaded_modules = {}  # mapping of module_name -> dict_of_functions
    self.loaded_files = set()  # TODO: just remove them from file_mapping?
    self.static_modules = static_modules if static_modules else []

    self.disabled = set(self.opts.get("disable_{0}s".format(self.tag), []))

    self.refresh_file_mapping()

    super(LazyLoader, self).__init__()  # late init the lazy loader
    # create all of the import namespaces
    _generate_module("{0}.int".format(self.loaded_base_name))
    _generate_module("{0}.int.{1}".format(self.loaded_base_name, tag))
    _generate_module("{0}.ext".format(self.loaded_base_name))
    _generate_module("{0}.ext.{1}".format(self.loaded_base_name, tag))


# Traceback (RuntimeError)
2015-05-05 15:52:48,917 [salt.loader                                ][ERROR   ][25815] Failed to read the virtual function for states: xmpp
Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1098, in process_virtual
virtual = mod.__virtual__()
File "/usr/lib/python2.6/site-packages/salt/states/xmpp.py", line 25, in __virtual__
return 'xmpp' if 'xmpp.send_msg' in __salt__ else False
File "/usr/lib64/python2.6/_abcoll.py", line 342, in __contains__
self[key]
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 85, in __getitem__
if self._load(key):
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1024, in _load
ret = _inner_load(mod_name)
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1013, in _inner_load
if self._load_module(name) and key in self._dict:
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 990, in _load_module
Depends.enforce_dependencies(self._dict, self.tag)
File "/usr/lib/python2.6/site-packages/salt/utils/decorators/__init__.py", line 81, in enforce_dependencies
for module, func, fallback_function in dependent_set:
RuntimeError: Set changed size during iteration

# Fixed code
def __init__(
    self,
    module_dirs,
    opts=None,
    tag="module",
    loaded_base_name=None,
    mod_type_check=None,
    pack=None,
    whitelist=None,
    virtual_enable=True,
    static_modules=None,
):  # pylint: disable=W0231
    """
    In pack, if any of the values are None they will be replaced with an
    empty context-specific dict
    """

    self.inject_globals = {}
    self.pack = {} if pack is None else pack
    if opts is None:
        opts = {}
    self.context_dict = salt.utils.context.ContextDict()
    self.opts = self.__prep_mod_opts(opts)

    self.module_dirs = module_dirs
    self.tag = tag
    self.loaded_base_name = loaded_base_name or LOADED_BASE_NAME
    self.mod_type_check = mod_type_check or _mod_type

    if "__context__" not in self.pack:
        self.pack["__context__"] = None

    for k, v in self.pack.iteritems():
        if v is None:  # if the value of a pack is None, lets make an empty dict
            self.context_dict.setdefault(k, {})
            self.pack[k] = salt.utils.context.NamespacedDictWrapper(
                self.context_dict, k
            )

    self.whitelist = whitelist
    self.virtual_enable = virtual_enable
    self.initial_load = True

    # names of modules that we don't have (errors, __virtual__, etc.)
    self.missing_modules = {}  # mapping of name -> error
    self.loaded_modules = {}  # mapping of module_name -> dict_of_functions
    self.loaded_files = set()  # TODO: just remove them from file_mapping?
    self.static_modules = static_modules if static_modules else []

    self.disabled = set(self.opts.get("disable_{0}s".format(self.tag), []))

    self.refresh_file_mapping()

    super(LazyLoader, self).__init__()  # late init the lazy loader
    # create all of the import namespaces
    _generate_module("{0}.int".format(self.loaded_base_name))
    _generate_module("{0}.int.{1}".format(self.loaded_base_name, tag))
    _generate_module("{0}.ext".format(self.loaded_base_name))
    _generate_module("{0}.ext.{1}".format(self.loaded_base_name, tag))