gurudesh/pytracebugs huggingface dataset:
# Buggy code
def clear_socket(self):
    """
    delete socket if you have it
    """
    if hasattr(self, "_socket"):
        if self._socket in self.poller:
            self.poller.unregister(self._socket)
        del self._socket


# Traceback (TypeError)
2014-06-16 16:52:21,682 [salt.log.setup   ][ERROR   ] An un-handled exception was caught by salt's global exception handler:
TypeError: argument of type 'Poller' is not iterable
Traceback (most recent call last):
File "/usr/bin/salt-minion", line 14, in <module>
salt_minion()
File "/usr/lib/python2.7/dist-packages/salt/scripts.py", line 43, in salt_minion
ret = minion.start()
File "/usr/lib/python2.7/dist-packages/salt/__init__.py", line 253, in start
self.prepare()
File "/usr/lib/python2.7/dist-packages/salt/__init__.py", line 237, in prepare
self.minion = salt.minion.Minion(self.config)
File "/usr/lib/python2.7/dist-packages/salt/minion.py", line 646, in __init__
if self.authenticate(timeout, safe) == 'full':
File "/usr/lib/python2.7/dist-packages/salt/minion.py", line 1283, in authenticate
creds = auth.sign_in(timeout, safe, tries)
File "/usr/lib/python2.7/dist-packages/salt/crypt.py", line 348, in sign_in
timeout=timeout
File "/usr/lib/python2.7/dist-packages/salt/payload.py", line 216, in send_auto
return self.send(enc, load, tries, timeout)
File "/usr/lib/python2.7/dist-packages/salt/payload.py", line 204, in send
self.clear_socket()
File "/usr/lib/python2.7/dist-packages/salt/payload.py", line 181, in clear_socket
if self._socket in self.poller:
TypeError: argument of type 'Poller' is not iterable

# Fixed code
def clear_socket(self):
    """
    delete socket if you have it
    """
    if hasattr(self, "_socket"):
        if isinstance(self.poller.sockets, dict):
            for socket in self.poller.sockets.keys():
                self.poller.unregister(socket)
        else:
            for socket in self.poller.sockets:
                self.poller.unregister(socket[0])
        del self._socket
# Buggy code
def compile_pillar(self, ext=True, pillar_dirs=None):
    """
    Render the pillar data and return
    """
    top, top_errors = self.get_top()
    if ext:
        if self.opts.get("ext_pillar_first", False):
            self.opts["pillar"] = self.ext_pillar({}, pillar_dirs)
            matches = self.top_matches(top)
            pillar, errors = self.render_pillar(matches)
            pillar = merge(
                pillar,
                self.opts["pillar"],
                self.merge_strategy,
                self.opts.get("renderer", "yaml"),
            )
        else:
            matches = self.top_matches(top)
            pillar, errors = self.render_pillar(matches)
            pillar = self.ext_pillar(pillar, pillar_dirs)
    else:
        matches = self.top_matches(top)
        pillar, errors = self.render_pillar(matches)
    errors.extend(top_errors)
    if self.opts.get("pillar_opts", True):
        mopts = dict(self.opts)
        if "grains" in mopts:
            mopts.pop("grains")
        # Restore the actual file_roots path. Issue 5449
        mopts["file_roots"] = self.actual_file_roots
        mopts["saltversion"] = __version__
        pillar["master"] = mopts
    if errors:
        for error in errors:
            log.critical("Pillar render error: {0}".format(error))
        pillar["_errors"] = errors
    return pillar


# Traceback (AssertionError)
2015-06-05 11:15:14,072 [salt.scripts                             ][ERROR   ][25853] Minion failed to start:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/scripts.py", line 81, in minion_process
minion.start()
File "/usr/lib/python2.7/dist-packages/salt/cli/daemons.py", line 269, in start
self.minion.tune_in()
File "/usr/lib/python2.7/dist-packages/salt/minion.py", line 1593, in tune_in
self.sync_connect_master()
File "/usr/lib/python2.7/dist-packages/salt/minion.py", line 589, in sync_connect_master
raise six.reraise(*future_exception)
File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 879, in run
yielded = self.gen.send(value)
File "/usr/lib/python2.7/dist-packages/salt/minion.py", line 597, in connect_master
yield self._post_master_init(master)
File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 242, in wrapper
Runner(result, future, yielded)
File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 817, in __init__
if self.handle_yield(first_yielded):
File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 956, in handle_yield
self.future = convert_yielded(yielded)
File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 1022, in convert_yielded
return multi_future(yielded)
File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 643, in multi_future
assert all(is_future(i) for i in children)
AssertionError
2015-06-05 11:15:14,073 [salt.scripts                             ][WARNING ][25853] ** Restarting minion **

# Fixed code
def compile_pillar(self, ext=True, pillar_dirs=None):
    ret = super(AsyncPillar, self).compile_pillar(ext=ext, pillar_dirs=pillar_dirs)
    raise tornado.gen.Return(ret)
# Buggy code
def _enqueue(revent):
    """
    Enqueue the event
    """
    __context__["inotify.que"].append(revent)


# Traceback (RuntimeError)
mkdir /etc/foo/bar
[CRITICAL] Beacon processing errored: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/minion.py", line 890, in _process_beacons
beacons = self.process_beacons(self.functions)
File "/usr/lib/python2.7/dist-packages/salt/minion.py", line 470, in process_beacons
return self.beacons.process(b_conf)
File "/usr/lib/python2.7/dist-packages/salt/beacons/__init__.py", line 56, in process
raw = self.beacons[fun_str](b_config[mod])
File "/usr/lib/python2.7/dist-packages/salt/beacons/inotify.py", line 126, in beacon
for wd in wm.watches:
RuntimeError: dictionary changed size during iteration
. No beacons will be procssed.

# Fixed code
def _enqueue(revent):
    """
    Enqueue the event
    """
    __context__["inotify.queue"].append(revent)
# Buggy code
def _get_notifier():
    """
    Check the context for the notifier and construct it if not present
    """
    if "inotify.notifier" in __context__:
        return __context__["inotify.notifier"]
    __context__["inotify.que"] = collections.deque()
    wm = pyinotify.WatchManager()
    __context__["inotify.notifier"] = pyinotify.Notifier(wm, _enqueue)
    return __context__["inotify.notifier"]


# Traceback (RuntimeError)
mkdir /etc/foo/bar
[CRITICAL] Beacon processing errored: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/minion.py", line 890, in _process_beacons
beacons = self.process_beacons(self.functions)
File "/usr/lib/python2.7/dist-packages/salt/minion.py", line 470, in process_beacons
return self.beacons.process(b_conf)
File "/usr/lib/python2.7/dist-packages/salt/beacons/__init__.py", line 56, in process
raw = self.beacons[fun_str](b_config[mod])
File "/usr/lib/python2.7/dist-packages/salt/beacons/inotify.py", line 126, in beacon
for wd in wm.watches:
RuntimeError: dictionary changed size during iteration
. No beacons will be procssed.

# Fixed code
def _get_notifier():
    """
    Check the context for the notifier and construct it if not present
    """
    if "inotify.notifier" not in __context__:
        __context__["inotify.queue"] = collections.deque()
        wm = pyinotify.WatchManager()
        __context__["inotify.notifier"] = pyinotify.Notifier(wm, _enqueue)
    return __context__["inotify.notifier"]
# Buggy code
def _linux_bin_exists(binary):
    """
    Does a binary exist in linux (depends on which, type, or whereis)
    """
    for search_cmd in ("which", "type -ap"):
        try:
            return __salt__["cmd.retcode"]("{0} {1}".format(search_cmd, binary)) == 0
        except salt.exceptions.CommandExecutionError:
            pass

    try:
        return (
            len(__salt__["cmd.run_stdout"]("whereis -b {0}".format(binary)).split()) > 1
        )
    except salt.exceptions.CommandExecutionError:
        return False


# Traceback (KeyError)
[ERROR   ] An un-handled exception was caught by salt's global exception handler:
KeyError: 'cmd.run_stdout'
Traceback (most recent call last):
File "/usr/bin/salt-call", line 11, in <module>
salt_call()
File "/usr/lib/python2.7/site-packages/salt/scripts.py", line 222, in salt_call
client.run()
File "/usr/lib/python2.7/site-packages/salt/cli/call.py", line 40, in run
caller = salt.cli.caller.Caller.factory(self.config)
File "/usr/lib/python2.7/site-packages/salt/cli/caller.py", line 71, in factory
return ZeroMQCaller(opts, **kwargs)
File "/usr/lib/python2.7/site-packages/salt/cli/caller.py", line 254, in __init__
super(ZeroMQCaller, self).__init__(opts)
File "/usr/lib/python2.7/site-packages/salt/cli/caller.py", line 94, in __init__
self.minion = salt.minion.SMinion(opts)
File "/usr/lib/python2.7/site-packages/salt/minion.py", line 300, in __init__
opts['grains'] = salt.loader.grains(opts)
File "/usr/lib/python2.7/site-packages/salt/loader.py", line 475, in grains
ret = fun()
File "/usr/lib/python2.7/site-packages/salt/grains/core.py", line 1031, in os_data
if _linux_bin_exists('selinuxenabled'):
File "/usr/lib/python2.7/site-packages/salt/grains/core.py", line 977, in _linux_bin_exists
return len(__salt__['cmd.run_stdout'](

# Fixed code
def _linux_bin_exists(binary):
    """
    Does a binary exist in linux (depends on which, type, or whereis)
    """
    for search_cmd in ("which", "type -ap"):
        try:
            return __salt__["cmd.retcode"]("{0} {1}".format(search_cmd, binary)) == 0
        except salt.exceptions.CommandExecutionError:
            pass

    try:
        return (
            len(
                __salt__["cmd.run_all"]("whereis -b {0}".format(binary))[
                    "stdout"
                ].split()
            )
            > 1
        )
    except salt.exceptions.CommandExecutionError:
        return False
# Buggy code
def __virtual__():
    """
    Only load this module if the ca config options are set
    """
    if HAS_SSL:
        return True
    return False, ["PyOpenSSL must be installed before this module can be used."]


# Traceback (ImportError)
# python -c 'import OpenSSL; print OpenSSL.__version__'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named OpenSSL

# Fixed code
def __virtual__():
    """
    Only load this module if the ca config options are set
    """
    if HAS_SSL and LooseVersion(OpenSSL_version) < LooseVersion("0.14"):
        if __opts__.get("ca.cert_base_path", None):
            return True
        else:
            log.error("tls module not loaded: ca.cert_base_path not set")
            return False
    else:
        return False, [
            "PyOpenSSL version 0.14 or later  must be installed before "
            " this module can be used."
        ]
# Buggy code
def _wait_for_task(task, vm_name, task_type, sleep_seconds=1, log_level="debug"):
    time_counter = 0
    while task.info.state == "running":
        message = "[ {0} ] Waiting for {1} task to finish [{2} s]".format(
            vm_name, task_type, time_counter
        )
        if log_level == "info":
            log.info(message)
        else:
            log.debug(message)
        time.sleep(int(sleep_seconds))
        time_counter += int(sleep_seconds)
    if task.info.state == "success":
        message = "[ {0} ] Successfully completed {1} task in {2} seconds".format(
            vm_name, task_type, time_counter
        )
        if log_level == "info":
            log.info(message)
        else:
            log.debug(message)
    else:
        raise task.info.error


# Traceback (TypeError)
[ERROR   ] Error creating testvm.example.com: exceptions must be old-style classes or derived from BaseException, not NoneType
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/cloud/clouds/vmware.py", line 1908, in create
_wait_for_task(task, vm_name, "clone", 5, 'info')
File "/usr/lib/python2.7/dist-packages/salt/cloud/clouds/vmware.py", line 608, in _wait_for_task
raise task.info.error
TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType
Error: There was a profile error: Failed to deploy VM

# Fixed code
def _wait_for_task(task, vm_name, task_type, sleep_seconds=1, log_level="debug"):
    time_counter = 0
    while task.info.state == "running":
        message = "[ {0} ] Waiting for {1} task to finish [{2} s]".format(
            vm_name, task_type, time_counter
        )
        if log_level == "info":
            log.info(message)
        else:
            log.debug(message)
        time.sleep(int(sleep_seconds))
        time_counter += int(sleep_seconds)
    if task.info.state == "success":
        message = "[ {0} ] Successfully completed {1} task in {2} seconds".format(
            vm_name, task_type, time_counter
        )
        if log_level == "info":
            log.info(message)
        else:
            log.debug(message)
    else:
        raise Exception(task.info.error)
# Buggy code
def _pkg_is_installed(pkg, installed_pkgs):
    """
    Helper function to determine if a package is installed

    This performs more complex comparison than just checking
    keys, such as examining source repos to see if the package
    was installed by a different name from the same repo

    :pkg str: The package to compare
    :installed_pkgs: A dictionary produced by npm list --json
    """
    if pkg_name in installed_pkgs:
        return True
    # Check to see if we are trying to install from a URI
    elif "://" in pkg_name:  # TODO Better way?
        for pkg_details in installed_pkgs.values():
            pkg_from = pkg_details.get("from", "").split("://")[1]
            if pkg_name.split("://")[1] == pkg_from:
                return True
    return False


# Traceback (KeyError)
ID: api_aglio
Function: npm.installed
Name: aglio@1.16.2
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1533, in call
**cdata['kwargs'])
File "/usr/lib/python2.7/dist-packages/salt/states/npm.py", line 152, in installed
installed_pkgs[pkg_name]['version'])
KeyError: 'version'
Started: 12:52:13.735178
Duration: 762.939 ms
Changes:

# Fixed code
def _pkg_is_installed(pkg, installed_pkgs):
    """
    Helper function to determine if a package is installed

    This performs more complex comparison than just checking
    keys, such as examining source repos to see if the package
    was installed by a different name from the same repo

    :pkg str: The package to compare
    :installed_pkgs: A dictionary produced by npm list --json
    """
    if pkg_name in installed_pkgs and "version" in installed_pkgs[pkg_name]:
        return True
    # Check to see if we are trying to install from a URI
    elif "://" in pkg_name:  # TODO Better way?
        for pkg_details in installed_pkgs.values():
            pkg_from = pkg_details.get("from", "").split("://")[1]
            if pkg_name.split("://")[1] == pkg_from:
                return True
    return False
# Buggy code
def _load_accumulators():
    def _deserialize(path):
        serial = salt.payload.Serial(__opts__)
        ret = {"accumulators": {}, "accumulators_deps": {}}
        try:
            with open(path, "rb") as f:
                loaded = serial.load(f)
                return loaded if loaded else ret
        except IOError:
            return ret

    loaded = _deserialize(_get_accumulator_filepath())

    return loaded["accumulators"], loaded["accumulators_deps"]


# Traceback (NameError)
Function: file.accumulated
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/tmp/.root_26f235__salt/salt/state.py", line 1539, in call
**cdata['kwargs'])
File "/tmp/.root_26f235__salt/salt/states/file.py", line 3672, in accumulated
_persist_accummulators(accum_data, accum_deps)
File "/tmp/.root_26f235__salt/salt/states/file.py", line 291, in _persist_accummulators
serial.dump(accumm_data, f)
File "/tmp/.root_26f235__salt/salt/payload.py", line 150, in dump
fn_.write(self.dumps(msg))
File "/tmp/.root_26f235__salt/salt/payload.py", line 116, in dumps
return msgpack.dumps(msg)
NameError: global name 'msgpack' is not defined

# Fixed code
def _load_accumulators():
    def _deserialize(path):
        serial = salt.payload.Serial(__opts__)
        ret = {"accumulators": {}, "accumulators_deps": {}}
        try:
            with open(path, "rb") as f:
                loaded = serial.load(f)
                return loaded if loaded else ret
        except (IOError, NameError):
            # NameError is a msgpack error from salt-ssh
            return ret

    loaded = _deserialize(_get_accumulator_filepath())

    return loaded["accumulators"], loaded["accumulators_deps"]
# Buggy code
def _deserialize(path):
    serial = salt.payload.Serial(__opts__)
    ret = {"accumulators": {}, "accumulators_deps": {}}
    try:
        with open(path, "rb") as f:
            loaded = serial.load(f)
            return loaded if loaded else ret
    except IOError:
        return ret


# Traceback (NameError)
Function: file.accumulated
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/tmp/.root_26f235__salt/salt/state.py", line 1539, in call
**cdata['kwargs'])
File "/tmp/.root_26f235__salt/salt/states/file.py", line 3672, in accumulated
_persist_accummulators(accum_data, accum_deps)
File "/tmp/.root_26f235__salt/salt/states/file.py", line 291, in _persist_accummulators
serial.dump(accumm_data, f)
File "/tmp/.root_26f235__salt/salt/payload.py", line 150, in dump
fn_.write(self.dumps(msg))
File "/tmp/.root_26f235__salt/salt/payload.py", line 116, in dumps
return msgpack.dumps(msg)
NameError: global name 'msgpack' is not defined

# Fixed code
def _deserialize(path):
    serial = salt.payload.Serial(__opts__)
    ret = {"accumulators": {}, "accumulators_deps": {}}
    try:
        with open(path, "rb") as f:
            loaded = serial.load(f)
            return loaded if loaded else ret
    except (IOError, NameError):
        # NameError is a msgpack error from salt-ssh
        return ret
# Buggy code
def _persist_accummulators(accumulators, accumulators_deps):
    accumm_data = {"accumulators": accumulators, "accumulators_deps": accumulators_deps}

    serial = salt.payload.Serial(__opts__)
    with open(_get_accumulator_filepath(), "w+b") as f:
        serial.dump(accumm_data, f)


# Traceback (NameError)
Function: file.accumulated
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/tmp/.root_26f235__salt/salt/state.py", line 1539, in call
**cdata['kwargs'])
File "/tmp/.root_26f235__salt/salt/states/file.py", line 3672, in accumulated
_persist_accummulators(accum_data, accum_deps)
File "/tmp/.root_26f235__salt/salt/states/file.py", line 291, in _persist_accummulators
serial.dump(accumm_data, f)
File "/tmp/.root_26f235__salt/salt/payload.py", line 150, in dump
fn_.write(self.dumps(msg))
File "/tmp/.root_26f235__salt/salt/payload.py", line 116, in dumps
return msgpack.dumps(msg)
NameError: global name 'msgpack' is not defined

# Fixed code
def _persist_accummulators(accumulators, accumulators_deps):
    accumm_data = {"accumulators": accumulators, "accumulators_deps": accumulators_deps}

    serial = salt.payload.Serial(__opts__)
    try:
        with open(_get_accumulator_filepath(), "w+b") as f:
            serial.dump(accumm_data, f)
    except NameError:
        # msgpack error from salt-ssh
        pass
# Buggy code
def list_nodes(call=None):
    """
    Return a list of the VMs that are on the provider
    """
    if call == "action":
        raise SaltCloudSystemExit(
            "The list_nodes function must be called with -f or --function."
        )

    ret = {}
    nodes = list_nodes_full()
    if "error" in nodes:
        raise SaltCloudSystemExit(
            "An error occurred while listing nodes: {0}".format(
                nodes["error"]["Errors"]["Error"]["Message"]
            )
        )
    for node in nodes:
        ret[node] = {
            "id": nodes[node]["hostname"],
            "ram": nodes[node]["maxMemory"],
            "cpus": nodes[node]["maxCpu"],
        }
        if "primaryIpAddress" in nodes[node]:
            ret[node]["public_ips"] = nodes[node]["primaryIpAddress"]
        if "primaryBackendIpAddress" in nodes[node]:
            ret[node]["private_ips"] = nodes[node]["primaryBackendIpAddress"]
    return ret


# Traceback (KeyError)
[INFO    ] salt-cloud starting
[WARNING ] DEPRECATED: Mako will no longer be the default renderer for Salt Cloud maps in the Lithium release
[INFO    ] Applying map from 'sl.map'.
[INFO    ] POST https://api.softlayer.com/xmlrpc/v3.1/SoftLayer_Account
[INFO    ] Starting new HTTPS connection (1): api.softlayer.com
[ERROR   ] There was a query error: 'state'
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/cloud/cli.py", line 347, in run
dmap = mapper.map_data()
File "/usr/lib/python2.7/dist-packages/salt/cloud/__init__.py", line 1407, in map_data
matching = get_matching_by_name(name)
File "/usr/lib/python2.7/dist-packages/salt/cloud/__init__.py", line 1396, in get_matching_by_name
matches[driver] = details['state']
KeyError: 'state'

# Fixed code
def list_nodes(call=None):
    """
    Return a list of the VMs that are on the provider
    """
    if call == "action":
        raise SaltCloudSystemExit(
            "The list_nodes function must be called with -f or --function."
        )

    ret = {}
    nodes = list_nodes_full()
    if "error" in nodes:
        raise SaltCloudSystemExit(
            "An error occurred while listing nodes: {0}".format(
                nodes["error"]["Errors"]["Error"]["Message"]
            )
        )
    for node in nodes:
        ret[node] = {
            "id": nodes[node]["hostname"],
            "ram": nodes[node]["maxMemory"],
            "cpus": nodes[node]["maxCpu"],
        }
        if "primaryIpAddress" in nodes[node]:
            ret[node]["public_ips"] = nodes[node]["primaryIpAddress"]
        if "primaryBackendIpAddress" in nodes[node]:
            ret[node]["private_ips"] = nodes[node]["primaryBackendIpAddress"]
        if "status" in nodes[node]:
            ret[node]["state"] = str(nodes[node]["status"]["name"])
    return ret
# Buggy code
def get_graphics(vm_):
    """
    Returns the information on vnc for a given vm

    CLI Example:

    .. code-block:: bash

        salt '*' virt.get_graphics <vm name>
    """
    out = {
        "autoport": "None",
        "keymap": "None",
        "listen": "None",
        "port": "None",
        "type": "vnc",
    }
    xml = get_xml(vm_)
    ssock = _StringIO(xml)
    doc = minidom.parse(ssock)
    for node in doc.getElementsByTagName("domain"):
        g_nodes = node.getElementsByTagName("graphics")
        for g_node in g_nodes:
            for key in g_node.attributes:
                out[key] = g_node.getAttribute(key)
    return out


# Traceback (KeyError)
# salt x01 virt.full_info
x01:
The minion function caused an exception: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/minion.py", line 1022, in _thread_return
return_data = func(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/modules/virt.py", line 1076, in full_info
'vm_info': vm_info()}
File "/usr/lib/python2.7/dist-packages/salt/modules/virt.py", line 720, in vm_info
info[vm_] = _info(vm_)
File "/usr/lib/python2.7/dist-packages/salt/modules/virt.py", line 710, in _info
'graphics': get_graphics(vm_),
File "/usr/lib/python2.7/dist-packages/salt/modules/virt.py", line 860, in get_graphics
for key in g_node.attributes:
File "/usr/lib/python2.7/xml/dom/minidom.py", line 522, in __getitem__
return self._attrs[attname_or_tuple]
KeyError: 0

# Fixed code
def get_graphics(vm_):
    """
    Returns the information on vnc for a given vm

    CLI Example:

    .. code-block:: bash

        salt '*' virt.get_graphics <vm name>
    """
    out = {
        "autoport": "None",
        "keymap": "None",
        "listen": "None",
        "port": "None",
        "type": "vnc",
    }
    xml = get_xml(vm_)
    ssock = _StringIO(xml)
    doc = minidom.parse(ssock)
    for node in doc.getElementsByTagName("domain"):
        g_nodes = node.getElementsByTagName("graphics")
        for g_node in g_nodes:
            for key, value in g_node.attributes.items():
                out[key] = value
    return out
# Buggy code
def user_list(user=None, password=None, host=None, port=None, database="admin"):
    """
    List users of a Mongodb database

    CLI Example:

    .. code-block:: bash

        salt '*' mongodb.user_list <user> <password> <host> <port> <database>
    """
    conn = _connect(user, password, host, port)
    if not conn:
        return "Failed to connect to mongo database"

    try:
        log.info("Listing users")
        mdb = pymongo.database.Database(conn, database)

        output = []
        mongodb_version = mdb.eval("db.version()")

        if StrictVersion(mongodb_version) >= StrictVersion("2.6"):
            for user in mdb.eval("db.getUsers()"):
                output.append([("user", user["user"]), ("roles", user["roles"])])
        else:
            for user in mdb.system.users.find():
                output.append(
                    [("user", user["user"]), ("readOnly", user.get("readOnly", "None"))]
                )
        return output

    except pymongo.errors.PyMongoError as err:
        log.error("Listing users failed with error: {0}".format(str(err)))
        return str(err)


# Traceback (ValueError)
[ERROR   ] An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1529, in call
**cdata['kwargs'])
File "/usr/lib/python2.7/dist-packages/salt/states/mongodb_user.py", line 66, in present
if __salt__['mongodb.user_exists'](name, user, password, host, port, database):
File "/usr/lib/python2.7/dist-packages/salt/modules/mongodb.py", line 193, in user_exists
users = user_list(user, password, host, port, database)
File "/usr/lib/python2.7/dist-packages/salt/modules/mongodb.py", line 159, in user_list
if StrictVersion(mongodb_version) >= StrictVersion('2.6'):
File "/usr/lib/python2.7/distutils/version.py", line 40, in __init__
self.parse(vstring)
File "/usr/lib/python2.7/distutils/version.py", line 107, in parse
raise ValueError, "invalid version number '%s'" % vstring
ValueError: invalid version number '3.0.0-rc10'

# Fixed code
def user_list(user=None, password=None, host=None, port=None, database="admin"):
    """
    List users of a Mongodb database

    CLI Example:

    .. code-block:: bash

        salt '*' mongodb.user_list <user> <password> <host> <port> <database>
    """
    conn = _connect(user, password, host, port)
    if not conn:
        return "Failed to connect to mongo database"

    try:
        log.info("Listing users")
        mdb = pymongo.database.Database(conn, database)

        output = []
        mongodb_version = mdb.eval("db.version()")

        if LooseVersion(mongodb_version) >= LooseVersion("2.6"):
            for user in mdb.eval("db.getUsers()"):
                output.append([("user", user["user"]), ("roles", user["roles"])])
        else:
            for user in mdb.system.users.find():
                output.append(
                    [("user", user["user"]), ("readOnly", user.get("readOnly", "None"))]
                )
        return output

    except pymongo.errors.PyMongoError as err:
        log.error("Listing users failed with error: {0}".format(str(err)))
        return str(err)
# Buggy code
def _netstat_route_freebsd():
    """
    Return netstat routing information for FreeBSD and OS X
    """
    ret = []
    cmd = "netstat -f inet -rn | tail -n+5"
    out = __salt__["cmd.run"](cmd)
    for line in out.splitlines():
        comps = line.split()
        ret.append(
            {
                "addr_family": "inet",
                "destination": comps[0],
                "gateway": comps[1],
                "netmask": comps[2],
                "flags": comps[3],
                "interface": comps[5],
            }
        )
    cmd = "netstat -f inet6 -rn | tail -n+5"
    out = __salt__["cmd.run"](cmd)
    for line in out.splitlines():
        comps = line.split()
        ret.append(
            {
                "addr_family": "inet6",
                "destination": comps[0],
                "gateway": comps[1],
                "netmask": "",
                "flags": comps[2],
                "interface": comps[3],
            }
        )
    return ret


# Traceback (IndexError)
----------
retcode:
1
stderr:
Traceback (most recent call last):
File "/tmp/.root_8ed75c__salt/salt-call", line 4, in <module>
salt_call()
File "/tmp/.root_8ed75c__salt/salt/scripts.py", line 220, in salt_call
client.run()
File "/tmp/.root_8ed75c__salt/salt/cli/call.py", line 69, in run
caller.run()
File "/tmp/.root_8ed75c__salt/salt/cli/caller.py", line 236, in run
ret = self.call()
File "/tmp/.root_8ed75c__salt/salt/cli/caller.py", line 138, in call
ret['return'] = func(*args, **kwargs)
File "/tmp/.root_8ed75c__salt/salt/modules/network.py", line 1087, in default_route
_routes = routes()
File "/tmp/.root_8ed75c__salt/salt/modules/network.py", line 1058, in routes
routes_ = _netstat_route_linux()
File "/tmp/.root_8ed75c__salt/salt/modules/network.py", line 309, in _netstat_route_linux
'destination': comps[0],
IndexError: list index out of range
stdout:

# Fixed code
def _netstat_route_freebsd():
    """
    Return netstat routing information for FreeBSD and OS X
    """
    ret = []
    cmd = "netstat -f inet -rn | tail -n+5"
    out = __salt__["cmd.run"](cmd, python_shell=True)
    for line in out.splitlines():
        comps = line.split()
        ret.append(
            {
                "addr_family": "inet",
                "destination": comps[0],
                "gateway": comps[1],
                "netmask": comps[2],
                "flags": comps[3],
                "interface": comps[5],
            }
        )
    cmd = "netstat -f inet6 -rn | tail -n+5"
    out = __salt__["cmd.run"](cmd, python_shell=True)
    for line in out.splitlines():
        comps = line.split()
        ret.append(
            {
                "addr_family": "inet6",
                "destination": comps[0],
                "gateway": comps[1],
                "netmask": "",
                "flags": comps[2],
                "interface": comps[3],
            }
        )
    return ret
# Buggy code
def _netstat_route_netbsd():
    """
    Return netstat routing information for NetBSD
    """
    ret = []
    cmd = "netstat -f inet -rn | tail -n+5"
    out = __salt__["cmd.run"](cmd)
    for line in out.splitlines():
        comps = line.split()
        ret.append(
            {
                "addr_family": "inet",
                "destination": comps[0],
                "gateway": comps[1],
                "netmask": "",
                "flags": comps[3],
                "interface": comps[6],
            }
        )
    cmd = "netstat -f inet6 -rn | tail -n+5"
    out = __salt__["cmd.run"](cmd)
    for line in out.splitlines():
        comps = line.split()
        ret.append(
            {
                "addr_family": "inet6",
                "destination": comps[0],
                "gateway": comps[1],
                "netmask": "",
                "flags": comps[3],
                "interface": comps[6],
            }
        )
    return ret


# Traceback (IndexError)
----------
retcode:
1
stderr:
Traceback (most recent call last):
File "/tmp/.root_8ed75c__salt/salt-call", line 4, in <module>
salt_call()
File "/tmp/.root_8ed75c__salt/salt/scripts.py", line 220, in salt_call
client.run()
File "/tmp/.root_8ed75c__salt/salt/cli/call.py", line 69, in run
caller.run()
File "/tmp/.root_8ed75c__salt/salt/cli/caller.py", line 236, in run
ret = self.call()
File "/tmp/.root_8ed75c__salt/salt/cli/caller.py", line 138, in call
ret['return'] = func(*args, **kwargs)
File "/tmp/.root_8ed75c__salt/salt/modules/network.py", line 1087, in default_route
_routes = routes()
File "/tmp/.root_8ed75c__salt/salt/modules/network.py", line 1058, in routes
routes_ = _netstat_route_linux()
File "/tmp/.root_8ed75c__salt/salt/modules/network.py", line 309, in _netstat_route_linux
'destination': comps[0],
IndexError: list index out of range
stdout:

# Fixed code
def _netstat_route_netbsd():
    """
    Return netstat routing information for NetBSD
    """
    ret = []
    cmd = "netstat -f inet -rn | tail -n+5"
    out = __salt__["cmd.run"](cmd, python_shell=True)
    for line in out.splitlines():
        comps = line.split()
        ret.append(
            {
                "addr_family": "inet",
                "destination": comps[0],
                "gateway": comps[1],
                "netmask": "",
                "flags": comps[3],
                "interface": comps[6],
            }
        )
    cmd = "netstat -f inet6 -rn | tail -n+5"
    out = __salt__["cmd.run"](cmd, python_shell=True)
    for line in out.splitlines():
        comps = line.split()
        ret.append(
            {
                "addr_family": "inet6",
                "destination": comps[0],
                "gateway": comps[1],
                "netmask": "",
                "flags": comps[3],
                "interface": comps[6],
            }
        )
    return ret
# Buggy code
def _netstat_route_openbsd():
    """
    Return netstat routing information for OpenBSD
    """
    ret = []
    cmd = "netstat -f inet -rn | tail -n+5"
    out = __salt__["cmd.run"](cmd)
    for line in out.splitlines():
        comps = line.split()
        ret.append(
            {
                "addr_family": "inet",
                "destination": comps[0],
                "gateway": comps[1],
                "netmask": "",
                "flags": comps[2],
                "interface": comps[7],
            }
        )
    cmd = "netstat -f inet6 -rn | tail -n+5"
    out = __salt__["cmd.run"](cmd)
    for line in out.splitlines():
        comps = line.split()
        ret.append(
            {
                "addr_family": "inet6",
                "destination": comps[0],
                "gateway": comps[1],
                "netmask": "",
                "flags": comps[2],
                "interface": comps[7],
            }
        )
    return ret


# Traceback (IndexError)
----------
retcode:
1
stderr:
Traceback (most recent call last):
File "/tmp/.root_8ed75c__salt/salt-call", line 4, in <module>
salt_call()
File "/tmp/.root_8ed75c__salt/salt/scripts.py", line 220, in salt_call
client.run()
File "/tmp/.root_8ed75c__salt/salt/cli/call.py", line 69, in run
caller.run()
File "/tmp/.root_8ed75c__salt/salt/cli/caller.py", line 236, in run
ret = self.call()
File "/tmp/.root_8ed75c__salt/salt/cli/caller.py", line 138, in call
ret['return'] = func(*args, **kwargs)
File "/tmp/.root_8ed75c__salt/salt/modules/network.py", line 1087, in default_route
_routes = routes()
File "/tmp/.root_8ed75c__salt/salt/modules/network.py", line 1058, in routes
routes_ = _netstat_route_linux()
File "/tmp/.root_8ed75c__salt/salt/modules/network.py", line 309, in _netstat_route_linux
'destination': comps[0],
IndexError: list index out of range
stdout:

# Fixed code
def _netstat_route_openbsd():
    """
    Return netstat routing information for OpenBSD
    """
    ret = []
    cmd = "netstat -f inet -rn | tail -n+5"
    out = __salt__["cmd.run"](cmd, python_shell=True)
    for line in out.splitlines():
        comps = line.split()
        ret.append(
            {
                "addr_family": "inet",
                "destination": comps[0],
                "gateway": comps[1],
                "netmask": "",
                "flags": comps[2],
                "interface": comps[7],
            }
        )
    cmd = "netstat -f inet6 -rn | tail -n+5"
    out = __salt__["cmd.run"](cmd, python_shell=True)
    for line in out.splitlines():
        comps = line.split()
        ret.append(
            {
                "addr_family": "inet6",
                "destination": comps[0],
                "gateway": comps[1],
                "netmask": "",
                "flags": comps[2],
                "interface": comps[7],
            }
        )
    return ret
# Buggy code
def interface_ip(iface):
    """
    Return the interface details
    """
    return interfaces().get(iface, {}).get("inet", {})[0].get("address", {})


# Traceback (KeyError)
user@master:~% sudo salt controller network.interface_ip br-int
controller:
The minion function caused an exception: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/minion.py", line 1022, in _thread_return
return_data = func(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/modules/network.py", line 480, in interface_ip
return salt.utils.network.interface_ip(iface)
File "/usr/lib/python2.7/dist-packages/salt/utils/network.py", line 738, in interface_ip
return interfaces().get(iface, {}).get('inet', {})[0].get('address', {})
KeyError: 0

user@master:~%

# Fixed code
def interface_ip(iface):
    """
    Return the interface details
    """
    try:
        return interfaces().get(iface, {}).get("inet", {})[0].get("address", {})
    except KeyError:
        return {}  # iface has no IP
# Buggy code
def flush(name, family="ipv4", **kwargs):
    """
    .. versionadded:: 2014.1.0

    Flush current iptables state

    family
        Networking family, either ipv4 or ipv6

    """
    ret = {"name": name, "changes": {}, "result": None, "comment": ""}

    for ignore in _STATE_INTERNAL_KEYWORDS:
        if ignore in kwargs:
            del kwargs[ignore]

    if "table" not in kwargs:
        kwargs["table"] = "filter"

    if "chain" not in kwargs:
        kwargs["chain"] = ""
    if __opts__["test"]:
        ret["comment"] = (
            "iptables rules in {0} table {1} chain {2} family needs to be flushed".format(
                name, kwargs["table"], family
            )
        )
        return ret
    if not __salt__["iptables.flush"](kwargs["table"], kwargs["chain"], family):
        ret["changes"] = {"locale": name}
        ret["result"] = True
        ret["comment"] = (
            "Flush iptables rules in {0} table {1} chain {2} family".format(
                kwargs["table"], kwargs["chain"], family
            )
        )
        return ret
    else:
        ret["result"] = False
        ret["comment"] = "Failed to flush iptables rules"
        return ret


# Traceback (KeyError)
ID: service.oat.fe firewall policy DROP
Function: iptables.set_policy
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1551, in call
**cdata['kwargs'])
File "/usr/lib/python2.7/dist-packages/salt/states/iptables.py", line 678, in set_policy
kwargs['table'],
KeyError: 'table'
Started: 16:02:36.888739
Duration: 1.261 ms
Changes:

# Fixed code
def flush(name, table="filter", family="ipv4", **kwargs):
    """
    .. versionadded:: 2014.1.0

    Flush current iptables state

    table
        The table that owns the chain that should be modified

    family
        Networking family, either ipv4 or ipv6

    """
    ret = {"name": name, "changes": {}, "result": None, "comment": ""}

    for ignore in _STATE_INTERNAL_KEYWORDS:
        if ignore in kwargs:
            del kwargs[ignore]

    if "table" not in kwargs:
        table = "filter"

    if "chain" not in kwargs:
        kwargs["chain"] = ""
    if __opts__["test"]:
        ret["comment"] = (
            "iptables rules in {0} table {1} chain {2} family needs to be flushed".format(
                name, table, family
            )
        )
        return ret
    if not __salt__["iptables.flush"](table, kwargs["chain"], family):
        ret["changes"] = {"locale": name}
        ret["result"] = True
        ret["comment"] = (
            "Flush iptables rules in {0} table {1} chain {2} family".format(
                table, kwargs["chain"], family
            )
        )
        return ret
    else:
        ret["result"] = False
        ret["comment"] = "Failed to flush iptables rules"
        return ret
# Buggy code
def file_list(self, load):
    """
    Return a list of files from the dominant environment
    """
    if "env" in load:
        salt.utils.warn_until(
            "Boron",
            "Passing a salt environment should be done using 'saltenv' "
            "not 'env'. This functionality will be removed in Salt "
            "Boron.",
        )
        load["saltenv"] = load.pop("env")

    ret = set()
    if "saltenv" not in load:
        return []
    for fsb in self._gen_back(None):
        fstr = "{0}.file_list".format(fsb)
        if fstr in self.servers:
            ret.update(self.servers[fstr](load))
    # some *fs do not handle prefix. Ensure it is filtered
    prefix = load.get("prefix", "").strip("/")
    if prefix != "":
        ret = [f for f in ret if f.startswith(prefix)]
    return sorted(ret)


# Traceback (UnicodeDecodeError)
2015-02-07 21:51:36,769 [salt.master      ][ERROR   ] Error in function _file_list:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1336, in run_func
ret = getattr(self, func)(load)
File "/usr/lib/python2.7/dist-packages/salt/fileserver/__init__.py", line 455, in file_list
return sorted(ret)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 64: ordinal not in range(128)

# Fixed code
def file_list(self, load):
    """
    Return a list of files from the dominant environment
    """
    if "env" in load:
        salt.utils.warn_until(
            "Boron",
            "Passing a salt environment should be done using 'saltenv' "
            "not 'env'. This functionality will be removed in Salt "
            "Boron.",
        )
        load["saltenv"] = load.pop("env")

    ret = set()
    if "saltenv" not in load:
        return []
    for fsb in self._gen_back(None):
        fstr = "{0}.file_list".format(fsb)
        if fstr in self.servers:
            ret.update(self.servers[fstr](load))
    # upgrade all set elements to a common encoding
    ret = [salt.utils.sdecode(f) for f in ret]
    # some *fs do not handle prefix. Ensure it is filtered
    prefix = load.get("prefix", "").strip("/")
    if prefix != "":
        ret = [f for f in ret if f.startswith(prefix)]
    return sorted(ret)
# Buggy code
def file_list_emptydirs(self, load):
    """
    List all emptydirs in the given environment
    """
    if "env" in load:
        salt.utils.warn_until(
            "Boron",
            "Passing a salt environment should be done using 'saltenv' "
            "not 'env'. This functionality will be removed in Salt "
            "Boron.",
        )
        load["saltenv"] = load.pop("env")

    ret = set()
    if "saltenv" not in load:
        return []
    for fsb in self._gen_back(None):
        fstr = "{0}.file_list_emptydirs".format(fsb)
        if fstr in self.servers:
            ret.update(self.servers[fstr](load))
    # some *fs do not handle prefix. Ensure it is filtered
    prefix = load.get("prefix", "").strip("/")
    if prefix != "":
        ret = [f for f in ret if f.startswith(prefix)]
    return sorted(ret)


# Traceback (UnicodeDecodeError)
2015-02-07 21:51:36,769 [salt.master      ][ERROR   ] Error in function _file_list:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1336, in run_func
ret = getattr(self, func)(load)
File "/usr/lib/python2.7/dist-packages/salt/fileserver/__init__.py", line 455, in file_list
return sorted(ret)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 64: ordinal not in range(128)

# Fixed code
def file_list_emptydirs(self, load):
    """
    List all emptydirs in the given environment
    """
    if "env" in load:
        salt.utils.warn_until(
            "Boron",
            "Passing a salt environment should be done using 'saltenv' "
            "not 'env'. This functionality will be removed in Salt "
            "Boron.",
        )
        load["saltenv"] = load.pop("env")

    ret = set()
    if "saltenv" not in load:
        return []
    for fsb in self._gen_back(None):
        fstr = "{0}.file_list_emptydirs".format(fsb)
        if fstr in self.servers:
            ret.update(self.servers[fstr](load))
    # upgrade all set elements to a common encoding
    ret = [salt.utils.sdecode(f) for f in ret]
    # some *fs do not handle prefix. Ensure it is filtered
    prefix = load.get("prefix", "").strip("/")
    if prefix != "":
        ret = [f for f in ret if f.startswith(prefix)]
    return sorted(ret)
# Buggy code
def dir_list(self, load):
    """
    List all directories in the given environment
    """
    if "env" in load:
        salt.utils.warn_until(
            "Boron",
            "Passing a salt environment should be done using 'saltenv' "
            "not 'env'. This functionality will be removed in Salt "
            "Boron.",
        )
        load["saltenv"] = load.pop("env")

    ret = set()
    if "saltenv" not in load:
        return []
    for fsb in self._gen_back(None):
        fstr = "{0}.dir_list".format(fsb)
        if fstr in self.servers:
            ret.update(self.servers[fstr](load))
    # some *fs do not handle prefix. Ensure it is filtered
    prefix = load.get("prefix", "").strip("/")
    if prefix != "":
        ret = [f for f in ret if f.startswith(prefix)]
    return sorted(ret)


# Traceback (UnicodeDecodeError)
2015-02-07 21:51:36,769 [salt.master      ][ERROR   ] Error in function _file_list:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1336, in run_func
ret = getattr(self, func)(load)
File "/usr/lib/python2.7/dist-packages/salt/fileserver/__init__.py", line 455, in file_list
return sorted(ret)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 64: ordinal not in range(128)

# Fixed code
def dir_list(self, load):
    """
    List all directories in the given environment
    """
    if "env" in load:
        salt.utils.warn_until(
            "Boron",
            "Passing a salt environment should be done using 'saltenv' "
            "not 'env'. This functionality will be removed in Salt "
            "Boron.",
        )
        load["saltenv"] = load.pop("env")

    ret = set()
    if "saltenv" not in load:
        return []
    for fsb in self._gen_back(None):
        fstr = "{0}.dir_list".format(fsb)
        if fstr in self.servers:
            ret.update(self.servers[fstr](load))
    # upgrade all set elements to a common encoding
    ret = [salt.utils.sdecode(f) for f in ret]
    # some *fs do not handle prefix. Ensure it is filtered
    prefix = load.get("prefix", "").strip("/")
    if prefix != "":
        ret = [f for f in ret if f.startswith(prefix)]
    return sorted(ret)