gurudesh/pytracebugs huggingface dataset:
# Buggy code
def __virtual__():
    # Ensure that daemontools is installed properly.
    BINS = frozenset(("svc", "supervise", "svok"))
    return __virtualname__ if all(salt.utils.which(b) for b in BINS) else False


# Traceback (CommandExecutionError)
salt-call service.restart salt-minion -l all

[DEBUG   ] LazyLoaded file.touch
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/cli/caller.py", line 197, in call
ret['return'] = func(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/modules/daemontools.py", line 135, in restart
if stop(name) and start(name):
File "/usr/lib/python2.7/dist-packages/salt/modules/daemontools.py", line 90, in stop
__salt__['file.touch']('{0}/down'.format(_service_path(name)))
File "/usr/lib/python2.7/dist-packages/salt/modules/file.py", line 2698, in touch
raise CommandExecutionError(exc.strerror)
CommandExecutionError: No such file or directory
Error running 'service.restart': No such file or directory

# Fixed code
def __virtual__():
    # Ensure that daemontools is installed properly.
    BINS = frozenset(("svc", "supervise", "svok"))
    if all(salt.utils.which(b) for b in BINS) and SERVICE_DIR:
        return __virtualname__
    return False
# Buggy code
def __virtual__():
    """
    Only load if requests is installed
    """
    if HAS_LIBS:
        return "zenoss"


# Traceback (ImportError)
[root@centos7t01 ~]# salt-call state.show_sls vim
local:
----------
vim:
----------
__env__:
base
__sls__:
vim
pkg:
- installed
|_
----------
order:
10000
[root@centos7t01 ~]#
[root@centos7t01 ~]# rpm -qa |grep docker
python-docker-py-1.7.2-1.el7.noarch
python-docker-scripts-0.4.4-1.el7.noarch
docker-engine-selinux-1.12.3-1.el7.centos.noarch
docker-engine-1.12.3-1.el7.centos.x86_64
[root@centos7t01 ~]#
[root@centos7t01 ~]# pip install docker-py
Requirement already satisfied: docker-py in /usr/lib/python2.7/site-packages
Requirement already satisfied: websocket-client>=0.32.0 in /usr/lib/python2.7/site-packages (from docker-py)
Requirement already satisfied: backports.ssl-match-hostname>=3.5; python_version < "3.5" in /usr/lib/python2.7/site-packages (from docker-py)
Requirement already satisfied: ipaddress>=1.0.16; python_version < "3.3" in /usr/lib/python2.7/site-packages (from docker-py)
Requirement already satisfied: six>=1.4.0 in /usr/lib/python2.7/site-packages (from docker-py)
Requirement already satisfied: requests!=2.11.0,>=2.5.2 in /usr/lib/python2.7/site-packages (from docker-py)
Requirement already satisfied: docker-pycreds>=0.2.1 in /usr/lib/python2.7/site-packages (from docker-py)
[root@centos7t01 ~]#
[root@centos7t01 ~]# python -c "import docker ; import docker.utils"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/docker/__init__.py", line 20, in <module>
from .client import Client, AutoVersionClient # flake8: noqa
File "/usr/lib/python2.7/site-packages/docker/client.py", line 19, in <module>
import requests
File "/usr/lib/python2.7/site-packages/requests/__init__.py", line 64, in <module>
from . import utils
File "/usr/lib/python2.7/site-packages/requests/utils.py", line 29, in <module>
from .exceptions import InvalidURL, InvalidHeader, FileModeWarning
ImportError: cannot import name InvalidHeader
[root@centos7t01 ~]#

# Fixed code
def __virtual__():
    """
    Only load if requests is installed
    """
    if HAS_LIBS:
        return __virtualname__
    else:
        return (
            False,
            "The '{0}' module could not be loaded: 'requests' is not installed.".format(
                __virtualname__
            ),
        )
# Buggy code
def get_location(opts=None, provider=None):
    """
    Return the region to use, in this order:
        opts['location']
        provider['location']
        get_region_from_metadata()
        DEFAULT_LOCATION
    """
    if opts is None:
        opts = __opts__
    ret = opts.get("location", provider.get("location"))
    if ret is None:
        ret = get_region_from_metadata()
    if ret is None:
        ret = DEFAULT_LOCATION
    return ret


# Traceback (NameError)
sudo salt-run fileserver.update
Exception occurred in runner fileserver.update: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/client/mixins.py", line 397, in _low
data['return'] = self.functions[fun](*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/runners/fileserver.py", line 310, in update
fileserver.update(back=backend)
File "/usr/lib/python2.7/dist-packages/salt/fileserver/__init__.py", line 468, in update
self.servers[fstr]()
File "/usr/lib/python2.7/dist-packages/salt/fileserver/s3fs.py", line 102, in update
metadata = _init()
File "/usr/lib/python2.7/dist-packages/salt/fileserver/s3fs.py", line 352, in _init
metadata = _refresh_buckets_cache_file(cache_file)
File "/usr/lib/python2.7/dist-packages/salt/fileserver/s3fs.py", line 421, in _refresh_buckets_cache_file
s3_meta = __get_s3_meta(bucket_name)
File "/usr/lib/python2.7/dist-packages/salt/fileserver/s3fs.py", line 414, in __get_s3_meta
return_bin=False)
File "/usr/lib/python2.7/dist-packages/salt/utils/s3.py", line 101, in query
location = salt.utils.aws.get_location()
File "/usr/lib/python2.7/dist-packages/salt/utils/aws.py", line 580, in get_location
opts = __opts__
NameError: global name '__opts__' is not defined

# Fixed code
def get_location(opts=None, provider=None):
    """
    Return the region to use, in this order:
        opts['location']
        provider['location']
        get_region_from_metadata()
        DEFAULT_LOCATION
    """
    if opts is None:
        opts = {}
    ret = opts.get("location")
    if ret is None and provider is not None:
        ret = provider.get("location")
    if ret is None:
        ret = get_region_from_metadata()
    if ret is None:
        ret = DEFAULT_LOCATION
    return ret
# Buggy code
def __virtual__():
    if not HAS_PYCASSA:
        return False
    return __virtualname__


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    if not HAS_PYCASSA:
        return False, "Could not import cassandra returner; pycassa is not installed."
    return __virtualname__
# Buggy code
def __virtual__():
    if not HAS_DEPS:
        return False

    # try to load some faster json libraries. In order of fastest to slowest
    json = salt.utils.import_json()
    couchbase.set_json_converters(json.dumps, json.loads)

    return __virtualname__


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    if not HAS_DEPS:
        return False, "Could not import couchbase returner; couchbase is not installed."

    # try to load some faster json libraries. In order of fastest to slowest
    json = salt.utils.import_json()
    couchbase.set_json_converters(json.dumps, json.loads)

    return __virtualname__
# Buggy code
def __virtual__():
    if not HAS_DJANGO:
        return False
    return True


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    if not HAS_DJANGO:
        return False, "Could not import django returner; django is not installed."
    return True
# Buggy code
def __virtual__():
    return __virtualname__


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    return "elasticsearch.alias_exists" in __salt__
# Buggy code
def __virtual__():
    """
    Only return if python-etcd is installed
    """
    return __virtualname__ if HAS_LIBS else False


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    """
    Only return if python-etcd is installed
    """
    if HAS_LIBS:
        return __virtualname__

    return False, "Could not import etcd returner; python-etcd is not installed."
# Buggy code
def __virtual__():
    if not HAS_INFLUXDB:
        return False
    return __virtualname__


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    if not HAS_INFLUXDB:
        return (
            False,
            "Could not import influxdb returner; "
            "influxdb python client is not installed.",
        )
    return __virtualname__
# Buggy code
def __virtual__():
    if not HAS_KAFKA:
        return False
    return __virtualname__


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    if not HAS_KAFKA:
        return False, "Could not import kafka returner; kafka-python is not installed."
    return __virtualname__
# Buggy code
def __virtual__():
    if not HAS_MEMCACHE:
        return False
    return __virtualname__


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    if not HAS_MEMCACHE:
        return (
            False,
            "Could not import memcache returner; "
            "memcache python client is not installed.",
        )
    return __virtualname__
# Buggy code
def __virtual__():
    if not HAS_PYMONGO:
        return False
    return __virtualname__


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    if not HAS_PYMONGO:
        return False, "Could not import mongo returner; pymongo is not installed."
    return __virtualname__
# Buggy code
def __virtual__():
    if not HAS_PYMONGO:
        return False
    return "mongo_return"


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    if not HAS_PYMONGO:
        return False, "Could not import mongo returner; pymongo is not installed."
    return "mongo_return"
# Buggy code
def __virtual__():
    if not HAS_MYSQL:
        return False
    return True


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    if not HAS_MYSQL:
        return (
            False,
            "Could not import mysql returner; mysql python client is not installed.",
        )
    return True
# Buggy code
def __virtual__():
    """
    Return virtualname
    """
    return __virtualname__


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    """
    Return virtualname
    """
    return "nagios.list_plugins" in __salt__
# Buggy code
def __virtual__():
    if not HAS_ODBC:
        return False
    return True


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    if not HAS_ODBC:
        return False, "Could not import odbc returner; pyodbc is not installed."
    return True
# Buggy code
def __virtual__():
    if not HAS_PG:
        return False
    return True


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    if not HAS_PG:
        return (
            False,
            "Could not import pgjsonb returner; python-psycopg2 is not installed.",
        )
    return True
# Buggy code
def __virtual__():
    if not HAS_POSTGRES:
        return False
    return __virtualname__


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    if not HAS_POSTGRES:
        return False, "Could not import postgres returner; psycopg2 is not installed."
    return __virtualname__
# Buggy code
def __virtual__():
    if not HAS_REDIS:
        return False
    return __virtualname__


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    if not HAS_REDIS:
        return (
            False,
            "Could not import redis returner; redis python client is not installed.",
        )
    return __virtualname__
# Buggy code
def __virtual__():
    if not has_raven:
        return False
    return __virtualname__


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    if not has_raven:
        return (
            False,
            "Could not import sentry returner; raven python client is not installed.",
        )
    return __virtualname__
# Buggy code
def __virtual__():
    if HAS_TWILIO:
        return __virtualname__
    else:
        return False


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    if HAS_TWILIO:
        return __virtualname__

    return False, "Could not import sms returner; twilio is not installed."
# Buggy code
def __virtual__():
    if not HAS_SQLITE3:
        return False
    return __virtualname__


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    if not HAS_SQLITE3:
        return False, "Could not import sqlite3 returner; sqlite3 is not installed."
    return __virtualname__
# Buggy code
def __virtual__():
    if not HAS_SYSLOG:
        return False
    return __virtualname__


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    if not HAS_SYSLOG:
        return False, "Could not import syslog returner; syslog is not installed."
    return __virtualname__
# Buggy code
def __virtual__():
    """
    Only load this module if right version of sleekxmpp is installed on this minion.
    """
    if HAS_LIBS:
        import sleekxmpp

        # Certain XMPP functionaility we're using doesn't work with versions under 1.3.1
        sleekxmpp_version = distutils.version.LooseVersion(sleekxmpp.__version__)
        valid_version = distutils.version.LooseVersion("1.3.1")
        if sleekxmpp_version >= valid_version:
            return __virtualname__
    return False


# Traceback (KeyError)
2016-11-03 15:55:39,687 [salt.utils.lazy                                      ][DEBUG   ][17440] Could not LazyLoad postgres_local_cache.returner
2016-11-03 15:55:39,687 [salt.minion                                          ][ERROR   ][17440] The return failed for job 20161103155539605278 'postgres_local_cache.returner'
2016-11-03 15:55:39,688 [salt.minion                                          ][ERROR   ][17440] Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 1431, in _thread_return
returner
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1055, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'postgres_local_cache.returner'

# Fixed code
def __virtual__():
    """
    Only load this module if right version of sleekxmpp is installed on this minion.
    """
    min_version = "1.3.1"
    if HAS_LIBS:
        import sleekxmpp

        # Certain XMPP functionaility we're using doesn't work with versions under 1.3.1
        sleekxmpp_version = distutils.version.LooseVersion(sleekxmpp.__version__)
        valid_version = distutils.version.LooseVersion(min_version)
        if sleekxmpp_version >= valid_version:
            return __virtualname__
    return (
        False,
        "Could not import xmpp returner; sleekxmpp python client is not "
        "installed or is older than version '{0}'.".format(min_version),
    )
# Buggy code
def get_local_client(
    c_path=os.path.join(syspaths.CONFIG_DIR, "master"),
    mopts=None,
    skip_perm_errors=False,
    io_loop=None,
):
    """
    .. versionadded:: 2014.7.0

    Read in the config and return the correct LocalClient object based on
    the configured transport

    :param IOLoop io_loop: io_loop used for events.
                           Pass in an io_loop if you want asynchronous
                           operation for obtaining events. Eg use of
                           set_event_handler() API. Otherwise, operation
                           will be synchronous.
    """
    if mopts:
        opts = mopts
    else:
        # Late import to prevent circular import
        import salt.config

        opts = salt.config.client_config(c_path)
    if opts["transport"] == "raet":
        import salt.client.raet

        return salt.client.raet.LocalClient(mopts=opts)
    # TODO: AIO core is separate from transport
    elif opts["transport"] in ("zeromq", "tcp"):
        return LocalClient(
            mopts=opts, skip_perm_errors=skip_perm_errors, io_loop=io_loop
        )


# Traceback (OSError)
2016-10-25 18:22:57,094 [salt.log.setup   ][ERROR   ][20141] An un-handled exception was caught by salt's global exception handler:
OSError: [Errno 3] No such process
Traceback (most recent call last):
File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.7/multiprocessing/util.py", line 321, in _exit_function
p._popen.terminate()
File "/usr/lib/python2.7/multiprocessing/forking.py", line 171, in terminate
os.kill(self.pid, signal.SIGTERM)
OSError: [Errno 3] No such process

# Fixed code
def get_local_client(
    c_path=os.path.join(syspaths.CONFIG_DIR, "master"),
    mopts=None,
    skip_perm_errors=False,
    io_loop=None,
    auto_reconnect=False,
):
    """
    .. versionadded:: 2014.7.0

    Read in the config and return the correct LocalClient object based on
    the configured transport

    :param IOLoop io_loop: io_loop used for events.
                           Pass in an io_loop if you want asynchronous
                           operation for obtaining events. Eg use of
                           set_event_handler() API. Otherwise, operation
                           will be synchronous.
    """
    if mopts:
        opts = mopts
    else:
        # Late import to prevent circular import
        import salt.config

        opts = salt.config.client_config(c_path)
    if opts["transport"] == "raet":
        import salt.client.raet

        return salt.client.raet.LocalClient(mopts=opts)
    # TODO: AIO core is separate from transport
    elif opts["transport"] in ("zeromq", "tcp"):
        return LocalClient(
            mopts=opts,
            skip_perm_errors=skip_perm_errors,
            io_loop=io_loop,
            auto_reconnect=auto_reconnect,
        )
# Buggy code
def get_returns_no_block(self, tag, match_type=None):
    """
    Raw function to just return events of jid excluding timeout logic

    Yield either the raw event data or None

    Pass a list of additional regular expressions as `tags_regex` to search
    the event bus for non-return data, such as minion lists returned from
    syndics.
    """

    while True:
        raw = self.event.get_event(
            wait=0.01, tag=tag, match_type=match_type, full=True, no_block=True
        )
        yield raw


# Traceback (OSError)
2016-10-25 18:22:57,094 [salt.log.setup   ][ERROR   ][20141] An un-handled exception was caught by salt's global exception handler:
OSError: [Errno 3] No such process
Traceback (most recent call last):
File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.7/multiprocessing/util.py", line 321, in _exit_function
p._popen.terminate()
File "/usr/lib/python2.7/multiprocessing/forking.py", line 171, in terminate
os.kill(self.pid, signal.SIGTERM)
OSError: [Errno 3] No such process

# Fixed code
def get_returns_no_block(self, tag, match_type=None):
    """
    Raw function to just return events of jid excluding timeout logic

    Yield either the raw event data or None

    Pass a list of additional regular expressions as `tags_regex` to search
    the event bus for non-return data, such as minion lists returned from
    syndics.
    """

    while True:
        try:
            raw = self.event.get_event(
                wait=0.01, tag=tag, match_type=match_type, full=True, no_block=True
            )
            yield raw
        except tornado.iostream.StreamClosedError:
            if self.auto_reconnect:
                log.warning("Connection to master lost. Reconnecting.")
                self.event.close_pub()
                self.event.connect_pub(timeout=self._get_timeout(None))
            else:
                raise
# Buggy code
def get_returns_no_block(self, tag, match_type=None):
    """
    Raw function to just return events of jid excluding timeout logic

    Yield either the raw event data or None

    Pass a list of additional regular expressions as `tags_regex` to search
    the event bus for non-return data, such as minion lists returned from
    syndics.
    """

    while True:
        try:
            raw = self.event.get_event(
                wait=0.01, tag=tag, match_type=match_type, full=True, no_block=True
            )
            yield raw
        except tornado.iostream.StreamClosedError:
            if self.auto_reconnect:
                log.warning("Connection to master lost. Reconnecting.")
                self.event.close_pub()
                self.event.connect_pub(timeout=self._get_timeout(None))
            else:
                raise


# Traceback (OSError)
2016-10-25 18:22:57,094 [salt.log.setup   ][ERROR   ][20141] An un-handled exception was caught by salt's global exception handler:
OSError: [Errno 3] No such process
Traceback (most recent call last):
File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.7/multiprocessing/util.py", line 321, in _exit_function
p._popen.terminate()
File "/usr/lib/python2.7/multiprocessing/forking.py", line 171, in terminate
os.kill(self.pid, signal.SIGTERM)
OSError: [Errno 3] No such process

# Fixed code
def get_returns_no_block(self, tag, match_type=None):
    """
    Raw function to just return events of jid excluding timeout logic

    Yield either the raw event data or None

    Pass a list of additional regular expressions as `tags_regex` to search
    the event bus for non-return data, such as minion lists returned from
    syndics.
    """

    while True:
        raw = self.event.get_event(
            wait=0.01,
            tag=tag,
            match_type=match_type,
            full=True,
            no_block=True,
            auto_reconnect=self.auto_reconnect,
        )
        yield raw
# Buggy code
def get_event(self, wait=0.25, tag="", full=False):
    """
    Get a single salt event.
    If no events are available, then block for up to ``wait`` seconds.
    Return the event if it matches the tag (or ``tag`` is empty)
    Otherwise return None

    If wait is 0 then block forever or until next event becomes available.
    """
    return self.event.get_event(wait=wait, tag=tag, full=full)


# Traceback (OSError)
2016-10-25 18:22:57,094 [salt.log.setup   ][ERROR   ][20141] An un-handled exception was caught by salt's global exception handler:
OSError: [Errno 3] No such process
Traceback (most recent call last):
File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.7/multiprocessing/util.py", line 321, in _exit_function
p._popen.terminate()
File "/usr/lib/python2.7/multiprocessing/forking.py", line 171, in terminate
os.kill(self.pid, signal.SIGTERM)
OSError: [Errno 3] No such process

# Fixed code
def get_event(self, wait=0.25, tag="", full=False):
    """
    Get a single salt event.
    If no events are available, then block for up to ``wait`` seconds.
    Return the event if it matches the tag (or ``tag`` is empty)
    Otherwise return None

    If wait is 0 then block forever or until next event becomes available.
    """
    return self.event.get_event(wait=wait, tag=tag, full=full, auto_reconnect=True)
# Buggy code
def listen():
    """
    An iterator to yield Salt events
    """
    event = salt.utils.event.get_event(
        "master",
        sock_dir=self.opts["sock_dir"],
        transport=self.opts["transport"],
        opts=self.opts,
        listen=True,
    )
    stream = event.iter_events(full=True)

    yield "retry: {0}\n".format(400)

    while True:
        data = next(stream)
        yield "tag: {0}\n".format(data.get("tag", ""))
        yield "data: {0}\n\n".format(json.dumps(data))


# Traceback (OSError)
2016-10-25 18:22:57,094 [salt.log.setup   ][ERROR   ][20141] An un-handled exception was caught by salt's global exception handler:
OSError: [Errno 3] No such process
Traceback (most recent call last):
File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.7/multiprocessing/util.py", line 321, in _exit_function
p._popen.terminate()
File "/usr/lib/python2.7/multiprocessing/forking.py", line 171, in terminate
os.kill(self.pid, signal.SIGTERM)
OSError: [Errno 3] No such process

# Fixed code
def listen():
    """
    An iterator to yield Salt events
    """
    event = salt.utils.event.get_event(
        "master",
        sock_dir=self.opts["sock_dir"],
        transport=self.opts["transport"],
        opts=self.opts,
        listen=True,
    )
    stream = event.iter_events(full=True, auto_reconnect=True)

    yield "retry: {0}\n".format(400)

    while True:
        data = next(stream)
        yield "tag: {0}\n".format(data.get("tag", ""))
        yield "data: {0}\n\n".format(json.dumps(data))
# Buggy code
def event_stream(handler, pipe):
    """
    An iterator to return Salt events (and optionally format them)
    """
    # blocks until send is called on the parent end of this pipe.
    pipe.recv()

    event = salt.utils.event.get_event(
        "master",
        sock_dir=self.opts["sock_dir"],
        transport=self.opts["transport"],
        opts=self.opts,
        listen=True,
    )
    stream = event.iter_events(full=True)
    SaltInfo = event_processor.SaltInfo(handler)
    while True:
        data = next(stream)
        if data:
            try:  # work around try to decode catch unicode errors
                if "format_events" in kwargs:
                    SaltInfo.process(data, salt_token, self.opts)
                else:
                    handler.send("data: {0}\n\n".format(json.dumps(data)), False)
            except UnicodeDecodeError:
                logger.error("Error: Salt event has non UTF-8 data:\n{0}".format(data))
        time.sleep(0.1)


# Traceback (OSError)
2016-10-25 18:22:57,094 [salt.log.setup   ][ERROR   ][20141] An un-handled exception was caught by salt's global exception handler:
OSError: [Errno 3] No such process
Traceback (most recent call last):
File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.7/multiprocessing/util.py", line 321, in _exit_function
p._popen.terminate()
File "/usr/lib/python2.7/multiprocessing/forking.py", line 171, in terminate
os.kill(self.pid, signal.SIGTERM)
OSError: [Errno 3] No such process

# Fixed code
def event_stream(handler, pipe):
    """
    An iterator to return Salt events (and optionally format them)
    """
    # blocks until send is called on the parent end of this pipe.
    pipe.recv()

    event = salt.utils.event.get_event(
        "master",
        sock_dir=self.opts["sock_dir"],
        transport=self.opts["transport"],
        opts=self.opts,
        listen=True,
    )
    stream = event.iter_events(full=True, auto_reconnect=True)
    SaltInfo = event_processor.SaltInfo(handler)
    while True:
        data = next(stream)
        if data:
            try:  # work around try to decode catch unicode errors
                if "format_events" in kwargs:
                    SaltInfo.process(data, salt_token, self.opts)
                else:
                    handler.send("data: {0}\n\n".format(json.dumps(data)), False)
            except UnicodeDecodeError:
                logger.error("Error: Salt event has non UTF-8 data:\n{0}".format(data))
        time.sleep(0.1)
# Buggy code
def get_event(self, wait=5, tag="", match_type=None, full=False, no_block=None):
    """
    Get a single publication.
    IF no publication available THEN block for up to wait seconds
    AND either return publication OR None IF no publication available.

    IF wait is 0 then block forever.
    """
    if not self.connected:
        self.connect_pub()
    start = time.time()
    while True:
        self.stack.serviceAll()
        if self.stack.rxMsgs:
            msg, sender = self.stack.rxMsgs.popleft()
            if "tag" not in msg and "data" not in msg:
                # Invalid event, how did this get here?
                continue
            if not msg["tag"].startswith(tag):
                # Not what we are looking for, throw it away
                continue
            if full:
                return msg
            else:
                return msg["data"]
        if start + wait < time.time():
            return None
        time.sleep(0.01)


# Traceback (OSError)
2016-10-25 18:22:57,094 [salt.log.setup   ][ERROR   ][20141] An un-handled exception was caught by salt's global exception handler:
OSError: [Errno 3] No such process
Traceback (most recent call last):
File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.7/multiprocessing/util.py", line 321, in _exit_function
p._popen.terminate()
File "/usr/lib/python2.7/multiprocessing/forking.py", line 171, in terminate
os.kill(self.pid, signal.SIGTERM)
OSError: [Errno 3] No such process

# Fixed code
def get_event(
    self,
    wait=5,
    tag="",
    match_type=None,
    full=False,
    no_block=None,
    auto_reconnect=False,
):
    """
    Get a single publication.
    IF no publication available THEN block for up to wait seconds
    AND either return publication OR None IF no publication available.

    IF wait is 0 then block forever.
    """
    if not self.connected:
        self.connect_pub()
    start = time.time()
    while True:
        self.stack.serviceAll()
        if self.stack.rxMsgs:
            msg, sender = self.stack.rxMsgs.popleft()
            if "tag" not in msg and "data" not in msg:
                # Invalid event, how did this get here?
                continue
            if not msg["tag"].startswith(tag):
                # Not what we are looking for, throw it away
                continue
            if full:
                return msg
            else:
                return msg["data"]
        if start + wait < time.time():
            return None
        time.sleep(0.01)
# Buggy code
def iter_events(self, tag="", full=False):
    """
    Creates a generator that continuously listens for events
    """
    while True:
        data = self.get_event(tag=tag, full=full)
        if data is None:
            continue
        yield data


# Traceback (OSError)
2016-10-25 18:22:57,094 [salt.log.setup   ][ERROR   ][20141] An un-handled exception was caught by salt's global exception handler:
OSError: [Errno 3] No such process
Traceback (most recent call last):
File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.7/multiprocessing/util.py", line 321, in _exit_function
p._popen.terminate()
File "/usr/lib/python2.7/multiprocessing/forking.py", line 171, in terminate
os.kill(self.pid, signal.SIGTERM)
OSError: [Errno 3] No such process

# Fixed code
def iter_events(self, tag="", full=False, auto_reconnect=False):
    """
    Creates a generator that continuously listens for events
    """
    while True:
        data = self.get_event(tag=tag, full=full, auto_reconnect=auto_reconnect)
        if data is None:
            continue
        yield data