gurudesh/pytracebugs huggingface dataset:
# Buggy code
def ext_pillar(
    minion_id,  # pylint: disable=W0613
    pillar,  # pylint: disable=W0613
    conf,
    nesting_key=None,
):
    """
    Get pillar data from Vault for the configuration ``conf``.
    """
    comps = conf.split()

    paths = [comp for comp in comps if comp.startswith("path=")]
    if not paths:
        log.error('"%s" is not a valid Vault ext_pillar config', conf)
        return {}

    vault_pillar = {}

    try:
        path = paths[0].replace("path=", "")
        path = path.format(**{"minion": minion_id})
        url = "v1/{0}".format(path)
        response = __utils__["vault.make_request"]("GET", url)
        if response.status_code == 200:
            vault_pillar = response.json().get("data", {})
        else:
            log.info("Vault secret not found for: %s", path)
    except KeyError:
        log.error("No such path in Vault: %s", path)

    if nesting_key:
        vault_pillar = {nesting_key: vault_pillar}
    return vault_pillar


# Traceback (CommandExecutionError)
$ sudo salt-run sdb.get 'sdb://sdb_vault/secret/data/salt/pillar_data/wew'
[ERROR   ] Failed to read secret! KeyError: u'wew'
Exception occurred in runner sdb.get: Traceback (most recent call last):
File "/opt/saltstack/lib/python2.7/site-packages/salt/client/mixins.py", line 387, in _low
data['return'] = self.functions[fun](*args, **kwargs)
File "/opt/saltstack/lib/python2.7/site-packages/salt/runners/sdb.py", line 27, in get
return salt.utils.sdb.sdb_get(uri, __opts__, __utils__)
File "/opt/saltstack/lib/python2.7/site-packages/salt/utils/sdb.py", line 46, in sdb_get
return loaded_db[fun](query, profile=profile)
File "/opt/saltstack/lib/python2.7/site-packages/salt/sdb/vault.py", line 122, in get
raise salt.exceptions.CommandExecutionError(e)
CommandExecutionError: u'wew'
[INFO    ] Runner completed: 20181023165316837868

# Fixed code
def ext_pillar(
    minion_id,  # pylint: disable=W0613
    pillar,  # pylint: disable=W0613
    conf,
    nesting_key=None,
):
    """
    Get pillar data from Vault for the configuration ``conf``.
    """
    comps = conf.split()

    paths = [comp for comp in comps if comp.startswith("path=")]
    if not paths:
        log.error('"%s" is not a valid Vault ext_pillar config', conf)
        return {}

    vault_pillar = {}

    try:
        path = paths[0].replace("path=", "")
        path = path.format(**{"minion": minion_id})
        version2 = __utils__["vault.is_v2"](path)
        if version2["v2"]:
            path = version2["data"]

        url = "v1/{0}".format(path)
        response = __utils__["vault.make_request"]("GET", url)
        if response.status_code == 200:
            vault_pillar = response.json().get("data", {})
        else:
            log.info("Vault secret not found for: %s", path)
    except KeyError:
        log.error("No such path in Vault: %s", path)

    if nesting_key:
        vault_pillar = {nesting_key: vault_pillar}
    return vault_pillar
# Buggy code
def set_(key, value, profile=None):
    """
    Set a key/value pair in the vault service
    """
    if "?" in key:
        path, key = key.split("?")
    else:
        path, key = key.rsplit("/", 1)

    try:
        url = "v1/{0}".format(path)
        data = {key: value}
        response = __utils__["vault.make_request"]("POST", url, profile, json=data)

        if response.status_code != 204:
            response.raise_for_status()
        return True
    except Exception as e:  # pylint: disable=broad-except
        log.error("Failed to write secret! %s: %s", type(e).__name__, e)
        raise salt.exceptions.CommandExecutionError(e)


# Traceback (CommandExecutionError)
$ sudo salt-run sdb.get 'sdb://sdb_vault/secret/data/salt/pillar_data/wew'
[ERROR   ] Failed to read secret! KeyError: u'wew'
Exception occurred in runner sdb.get: Traceback (most recent call last):
File "/opt/saltstack/lib/python2.7/site-packages/salt/client/mixins.py", line 387, in _low
data['return'] = self.functions[fun](*args, **kwargs)
File "/opt/saltstack/lib/python2.7/site-packages/salt/runners/sdb.py", line 27, in get
return salt.utils.sdb.sdb_get(uri, __opts__, __utils__)
File "/opt/saltstack/lib/python2.7/site-packages/salt/utils/sdb.py", line 46, in sdb_get
return loaded_db[fun](query, profile=profile)
File "/opt/saltstack/lib/python2.7/site-packages/salt/sdb/vault.py", line 122, in get
raise salt.exceptions.CommandExecutionError(e)
CommandExecutionError: u'wew'
[INFO    ] Runner completed: 20181023165316837868

# Fixed code
def set_(key, value, profile=None):
    """
    Set a key/value pair in the vault service
    """
    if "?" in key:
        path, key = key.split("?")
    else:
        path, key = key.rsplit("/", 1)
    data = {key: value}

    version2 = __utils__["vault.is_v2"](path)
    if version2["v2"]:
        path = version2["data"]
        data = {"data": data}

    try:
        url = "v1/{0}".format(path)
        response = __utils__["vault.make_request"]("POST", url, json=data)

        if response.status_code != 204:
            response.raise_for_status()
        return True
    except Exception as e:  # pylint: disable=broad-except
        log.error("Failed to write secret! %s: %s", type(e).__name__, e)
        raise salt.exceptions.CommandExecutionError(e)
# Buggy code
def get(key, profile=None):
    """
    Get a value from the vault service
    """
    if "?" in key:
        path, key = key.split("?")
    else:
        path, key = key.rsplit("/", 1)

    try:
        url = "v1/{0}".format(path)
        response = __utils__["vault.make_request"]("GET", url, profile)
        if response.status_code == 404:
            return None
        if response.status_code != 200:
            response.raise_for_status()
        data = response.json()["data"]

        if key in data:
            return data[key]
        return None
    except Exception as e:  # pylint: disable=broad-except
        log.error("Failed to read secret! %s: %s", type(e).__name__, e)
        raise salt.exceptions.CommandExecutionError(e)


# Traceback (CommandExecutionError)
$ sudo salt-run sdb.get 'sdb://sdb_vault/secret/data/salt/pillar_data/wew'
[ERROR   ] Failed to read secret! KeyError: u'wew'
Exception occurred in runner sdb.get: Traceback (most recent call last):
File "/opt/saltstack/lib/python2.7/site-packages/salt/client/mixins.py", line 387, in _low
data['return'] = self.functions[fun](*args, **kwargs)
File "/opt/saltstack/lib/python2.7/site-packages/salt/runners/sdb.py", line 27, in get
return salt.utils.sdb.sdb_get(uri, __opts__, __utils__)
File "/opt/saltstack/lib/python2.7/site-packages/salt/utils/sdb.py", line 46, in sdb_get
return loaded_db[fun](query, profile=profile)
File "/opt/saltstack/lib/python2.7/site-packages/salt/sdb/vault.py", line 122, in get
raise salt.exceptions.CommandExecutionError(e)
CommandExecutionError: u'wew'
[INFO    ] Runner completed: 20181023165316837868

# Fixed code
def get(key, profile=None):
    """
    Get a value from the vault service
    """
    if "?" in key:
        path, key = key.split("?")
    else:
        path, key = key.rsplit("/", 1)

    version2 = __utils__["vault.is_v2"](path)
    if version2["v2"]:
        path = version2["data"]

    try:
        url = "v1/{0}".format(path)
        response = __utils__["vault.make_request"]("GET", url, profile)
        if response.status_code == 404:
            return None
        if response.status_code != 200:
            response.raise_for_status()
        data = response.json()["data"]

        if version2["v2"]:
            if key in data["data"]:
                return data["data"][key]
        else:
            if key in data:
                return data[key]
        return None
    except Exception as e:  # pylint: disable=broad-except
        log.error("Failed to read secret! %s: %s", type(e).__name__, e)
        raise salt.exceptions.CommandExecutionError(e)
# Buggy code
def ext_type_encoder(obj):
    if isinstance(obj, six.integer_types):
        # msgpack can't handle the very long Python longs for jids
        # Convert any very long longs to strings
        return six.text_type(obj)
    elif isinstance(obj, datetime.datetime):
        # msgpack doesn't support datetime.datetime datatype
        # So here we have converted datetime.datetime to custom datatype
        # This is msgpack Extended types numbered 78
        return msgpack.ExtType(
            78, salt.utils.stringutils.to_bytes(obj.strftime("%Y%m%dT%H:%M:%S.%f"))
        )
    # The same for immutable types
    elif isinstance(obj, immutabletypes.ImmutableDict):
        return dict(obj)
    elif isinstance(obj, immutabletypes.ImmutableList):
        return list(obj)
    elif isinstance(obj, (set, immutabletypes.ImmutableSet)):
        # msgpack can't handle set so translate it to tuple
        return tuple(obj)
    # Nothing known exceptions found. Let msgpack raise it's own.
    return obj


# Traceback (AttributeError)
ID: pip-deps
Function: pip.installed
Name: python
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/state.py", line 1913, in call
**cdata['kwargs'])
File "/usr/lib/python2.7/site-packages/salt/loader.py", line 1898, in wrapper
return f(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/salt/states/pip_state.py", line 910, in installed
if pkg_name.lower() in already_installed_packages:
AttributeError: 'NoneType' object has no attribute 'lower'
Started: 13:14:27.731677
Duration: 1944.946 ms
Changes:

# Fixed code
def ext_type_encoder(obj):
    if isinstance(obj, six.integer_types):
        # msgpack can't handle the very long Python longs for jids
        # Convert any very long longs to strings
        return six.text_type(obj)
    elif isinstance(obj, datetime.datetime):
        # msgpack doesn't support datetime.datetime datatype
        # So here we have converted datetime.datetime to custom datatype
        # This is msgpack Extended types numbered 78
        return msgpack.ExtType(
            78, salt.utils.stringutils.to_bytes(obj.strftime("%Y%m%dT%H:%M:%S.%f"))
        )
    # The same for immutable types
    elif isinstance(obj, immutabletypes.ImmutableDict):
        return dict(obj)
    elif isinstance(obj, immutabletypes.ImmutableList):
        return list(obj)
    elif isinstance(obj, (set, immutabletypes.ImmutableSet)):
        # msgpack can't handle set so translate it to tuple
        return tuple(obj)
    elif isinstance(obj, CaseInsensitiveDict):
        return dict(obj)
    # Nothing known exceptions found. Let msgpack raise it's own.
    return obj
# Buggy code
def vb_get_manager():
    """
    Creates a 'singleton' manager to communicate with a local virtualbox hypervisor.
    @return:
    @rtype: VirtualBoxManager
    """
    global _virtualboxManager
    if _virtualboxManager is None and HAS_LIBS:
        try:
            from importlib import reload
        except ImportError:
            # If we get here, we are in py2 and reload is a built-in.
            pass

        # Reloading the API extends sys.paths for subprocesses of multiprocessing, since they seem to share contexts
        reload(vboxapi)
        _virtualboxManager = vboxapi.VirtualBoxManager(None, None)

    return _virtualboxManager


# Traceback (NameError)
An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib64/python3.6/site-packages/salt/state.py", line 1913, in call
**cdata['kwargs'])
File "/usr/lib64/python3.6/site-packages/salt/loader.py", line 1898, in wrapper
return f(*args, **kwargs)
File "/usr/lib64/python3.6/site-packages/salt/states/pkg.py", line 1548, in installed
**kwargs)
File "/usr/lib64/python3.6/site-packages/salt/states/pkg.py", line 521, in _find_install_targets
cur_pkgs = __salt__['pkg.list_pkgs'](versions_as_list=True, **kwargs)
File "/usr/lib64/python3.6/site-packages/salt/modules/ebuild.py", line 426, in list_pkgs
pkgs = _vartree().dbapi.cpv_all()
File "/usr/lib64/python3.6/site-packages/salt/modules/ebuild.py", line 68, in _vartree
portage = reload(portage)
NameError: name 'reload' is not defined

# Fixed code
def vb_get_manager():
    """
    Creates a 'singleton' manager to communicate with a local virtualbox hypervisor.
    @return:
    @rtype: VirtualBoxManager
    """
    global _virtualboxManager
    if _virtualboxManager is None and HAS_LIBS:
        reload(vboxapi)
        _virtualboxManager = vboxapi.VirtualBoxManager(None, None)

    return _virtualboxManager
# Buggy code
def store(bank, key, data):
    """
    Store a key value.
    """
    _init_client()
    data = __context__["serial"].dumps(data)
    query = (
        "REPLACE INTO {0} (bank, etcd_key, data) values('{1}', '{2}', '{3}')".format(
            _table_name, bank, key, data
        )
    )
    cur, cnt = run_query(client, query)
    cur.close()
    if cnt not in (1, 2):
        raise SaltCacheError(
            "Error storing {0} {1} returned {2}".format(bank, key, cnt)
        )


# Traceback (UnicodeDecodeError)
2018-06-27 14:41:34,999 [salt.master      :1783][ERROR   ][38641] Error in function _pillar:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/master.py", line 1776, in run_func
ret = getattr(self, func)(load)
File "/usr/lib/python2.7/site-packages/salt/master.py", line 1487, in _pillar
'pillar': data})
File "/usr/lib/python2.7/site-packages/salt/cache/__init__.py", line 156, in store
return self.modules[fun](bank, key, data, **self._kwargs)
File "/usr/lib/python2.7/site-packages/salt/cache/mysql_cache.py", line 176, in store
"'{3}')".format(_table_name, bank, key, data)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x82 in position 0: ordinal not in range(128)

# Fixed code
def store(bank, key, data):
    """
    Store a key value.
    """
    _init_client()
    data = __context__["serial"].dumps(data)
    query = (
        b"REPLACE INTO {0} (bank, etcd_key, data) values('{1}', '{2}', '{3}')".format(
            _table_name, bank, key, data
        )
    )

    cur, cnt = run_query(client, query)
    cur.close()
    if cnt not in (1, 2):
        raise SaltCacheError(
            "Error storing {0} {1} returned {2}".format(bank, key, cnt)
        )
# Buggy code
def _osx_memdata():
    """
    Return the memory information for BSD-like systems
    """
    grains = {"mem_total": 0, "swap_total": 0}

    sysctl = salt.utils.path.which("sysctl")
    if sysctl:
        mem = __salt__["cmd.run"]("{0} -n hw.memsize".format(sysctl))
        swap_total = __salt__["cmd.run"]("{0} -n vm.swapusage".format(sysctl)).split()[
            2
        ]
        if swap_total.endswith("K"):
            _power = 2**10
        elif swap_total.endswith("M"):
            _power = 2**20
        elif swap_total.endswith("G"):
            _power = 2**30
        swap_total = float(swap_total[:-1]) * _power

        grains["mem_total"] = int(mem) // 1024 // 1024
        grains["swap_total"] = int(swap_total) // 1024 // 1024
    return grains


# Traceback (ValueError)
$ uname
Darwin
$ launchctl limit maxfiles
maxfiles    8192           10240
$ python -V
Python 2.7.15 :: Anaconda, Inc.
$ pip install salt
$ salt-master --log-level=all
Traceback (most recent call last):
File "/Users/MacBookPro/miniconda3/envs/python2_test/bin/salt-master", line 11, in <module>
sys.exit(salt_master())
File "/Users/MacBookPro/miniconda3/envs/python2_test/lib/python2.7/site-packages/salt/scripts.py", line 95, in salt_master
import salt.cli.daemons
File "/Users/MacBookPro/miniconda3/envs/python2_test/lib/python2.7/site-packages/salt/cli/daemons.py", line 48, in <module>
import salt.utils.parsers
File "/Users/MacBookPro/miniconda3/envs/python2_test/lib/python2.7/site-packages/salt/utils/parsers.py", line 27, in <module>
import salt.config as config
File "/Users/MacBookPro/miniconda3/envs/python2_test/lib/python2.7/site-packages/salt/config/__init__.py", line 98, in <module>
_DFLT_IPC_WBUFFER = _gather_buffer_space() * .5
File "/Users/MacBookPro/miniconda3/envs/python2_test/lib/python2.7/site-packages/salt/config/__init__.py", line 91, in _gather_buffer_space
grains = salt.grains.core._memdata(os_data)
File "/Users/MacBookPro/miniconda3/envs/python2_test/lib/python2.7/site-packages/salt/grains/core.py", line 511, in _memdata
grains.update(_osx_memdata())
File "/Users/MacBookPro/miniconda3/envs/python2_test/lib/python2.7/site-packages/salt/grains/core.py", line 433, in _osx_memdata
swap_total = float(swap_total[:-1]) * _power
ValueError: invalid literal for float(): 1024,00

# Fixed code
def _osx_memdata():
    """
    Return the memory information for BSD-like systems
    """
    grains = {"mem_total": 0, "swap_total": 0}

    sysctl = salt.utils.path.which("sysctl")
    if sysctl:
        mem = __salt__["cmd.run"]("{0} -n hw.memsize".format(sysctl))
        swap_total = (
            __salt__["cmd.run"]("{0} -n vm.swapusage".format(sysctl))
            .split()[2]
            .replace(",", ".")
        )
        if swap_total.endswith("K"):
            _power = 2**10
        elif swap_total.endswith("M"):
            _power = 2**20
        elif swap_total.endswith("G"):
            _power = 2**30
        swap_total = float(swap_total[:-1]) * _power

        grains["mem_total"] = int(mem) // 1024 // 1024
        grains["swap_total"] = int(swap_total) // 1024 // 1024
    return grains
# Buggy code
def find_module(self, module_name, package_path):
    if module_name.startswith("tornado"):
        return self
    return None


# Traceback (TypeError)
% docker run --rm -it -v $PWD:/testing terminalmage/issues:56061 salt-call test.ping
[ERROR   ] Failed to import grains esxi, this is due most likely to a syntax error:
Traceback (most recent call last):
File "/testing/salt/loader.py", line 1607, in _load_module
mod = imp.load_module(mod_namespace, fn_, fpath, desc)
File "/testing/salt/grains/esxi.py", line 16, in <module>
import salt.modules.vsphere
File "/testing/salt/modules/vsphere.py", line 222, in <module>
import jsonschema
File "/usr/lib/python2.7/site-packages/jsonschema/__init__.py", line 18, in <module>
from jsonschema.validators import (
File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 170, in <module>
meta_schema=_utils.load_schema("draft3"),
File "/usr/lib/python2.7/site-packages/jsonschema/_utils.py", line 57, in load_schema
data = pkgutil.get_data('jsonschema', "schemas/{0}.json".format(name))
File "/usr/lib64/python2.7/pkgutil.py", line 578, in get_data
loader = get_loader(package)
File "/usr/lib64/python2.7/pkgutil.py", line 464, in get_loader
return find_loader(fullname)
File "/usr/lib64/python2.7/pkgutil.py", line 475, in find_loader
loader = importer.find_module(fullname)
TypeError: find_module() takes exactly 3 arguments (2 given)
local:
True

# Fixed code
def find_module(self, module_name, package_path=None):
    if module_name.startswith("tornado"):
        return self
    return None
# Buggy code
def _error_msg_iface(iface, option, expected):
    """
    Build an appropriate error message from a given option and
    a list of expected values.
    """
    msg = "Invalid option -- Interface: {0}, Option: {1}, Expected: [{2}]"
    return msg.format(iface, option, "|".join(expected))


# Traceback (TypeError)
myhost.someplace.com:

State: - network
Name:      eth0
Function:  managed
Result:    True
Comment:   Interface eth0 is up to date.
Changes:

State: - network
Name:      system
Function:  system
Result:    False
Comment:   An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/state.py", line 1237, in call
*cdata['args'], **cdata['kwargs'])
File "/usr/lib/python2.7/site-packages/salt/states/network.py", line 350, in system
new = __salt__['ip.build_network_settings'](**kwargs)
File "/usr/lib/python2.7/site-packages/salt/modules/rh_ip.py", line 1017, in build_network_settings
opts = _parse_network_settings(settings, current_network_settings)
File "/usr/lib/python2.7/site-packages/salt/modules/rh_ip.py", line 682, in _parse_network_settings
_raise_error_network('networking', valid)
File "/usr/lib/python2.7/site-packages/salt/modules/rh_ip.py", line 732, in _raise_error_network
msg = _error_msg_network(option, expected)
File "/usr/lib/python2.7/site-packages/salt/modules/rh_ip.py", line 98, in _error_msg_network
return msg.format(option, '|'.join(expected))
TypeError: sequence item 4: expected string, bool found

Changes:

# Fixed code
def _error_msg_iface(iface, option, expected):
    """
    Build an appropriate error message from a given option and
    a list of expected values.
    """
    msg = "Invalid option -- Interface: {0}, Option: {1}, Expected: [{2}]"
    return msg.format(iface, option, "|".join(str(e) for e in expected))
# Buggy code
def _error_msg_network(option, expected):
    """
    Build an appropriate error message from a given option and
    a list of expected values.
    """
    msg = "Invalid network setting -- Setting: {0}, Expected: [{1}]"
    return msg.format(option, "|".join(expected))


# Traceback (TypeError)
myhost.someplace.com:

State: - network
Name:      eth0
Function:  managed
Result:    True
Comment:   Interface eth0 is up to date.
Changes:

State: - network
Name:      system
Function:  system
Result:    False
Comment:   An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/state.py", line 1237, in call
*cdata['args'], **cdata['kwargs'])
File "/usr/lib/python2.7/site-packages/salt/states/network.py", line 350, in system
new = __salt__['ip.build_network_settings'](**kwargs)
File "/usr/lib/python2.7/site-packages/salt/modules/rh_ip.py", line 1017, in build_network_settings
opts = _parse_network_settings(settings, current_network_settings)
File "/usr/lib/python2.7/site-packages/salt/modules/rh_ip.py", line 682, in _parse_network_settings
_raise_error_network('networking', valid)
File "/usr/lib/python2.7/site-packages/salt/modules/rh_ip.py", line 732, in _raise_error_network
msg = _error_msg_network(option, expected)
File "/usr/lib/python2.7/site-packages/salt/modules/rh_ip.py", line 98, in _error_msg_network
return msg.format(option, '|'.join(expected))
TypeError: sequence item 4: expected string, bool found

Changes:

# Fixed code
def _error_msg_network(option, expected):
    """
    Build an appropriate error message from a given option and
    a list of expected values.
    """
    msg = "Invalid network setting -- Setting: {0}, Expected: [{1}]"
    return msg.format(option, "|".join(str(e) for e in expected))
# Buggy code
def __aiter__(self):
    raise Return(self)


# Traceback (SyntaxError)
Think you missed one on the ext.tornado,, building on Amazon Linux 2 Py 3 which is 3.7
5466 /usr/lib/rpm/sepdebugcrcfix: Updated 0 CRC32s, 0 CRC32s did match.
5467 BUILDSTDERR: find: 'debug': No such file or directory
5468 + /usr/lib/rpm/check-buildroot
5469 + /usr/lib/rpm/redhat/brp-compress
5470 + /usr/lib/rpm/redhat/brp-strip-static-archive /usr/bin/strip
5471 + /usr/lib/rpm/brp-python-bytecompile /usr/bin/python 1
5472 Bytecompiling .py files below /builddir/build/BUILDROOT/salt-2999.0.0nb202001290009059414103-0.amzn2.x86_64/usr/lib/python3.7 using /usr/bin/python3.7
5473 *** Error compiling '/builddir/build/BUILDROOT/salt-2999.0.0nb202001290009059414103-0.amzn2.x86_64/usr/lib/python3.7/site-packages/salt/ext/tornado/test/asyncio_test.py'...
5474   File "/usr/lib/python3.7/asyncio_test.py", line 50
5475     ensure_future = asyncio.async
5476                                 ^
5477 SyntaxError: invalid syntax
5478 BUILDSTDERR: error: Bad exit status from /var/tmp/rpm-tmp.24mNaY (%install)
5479 BUILDSTDERR:     Bad exit status from /var/tmp/rpm-tmp.24mNaY (%install)
5480 RPM build errors:
5481 Child return code was: 1
5482 EXCEPTION: [Error()]
5483 Traceback (most recent call last):
5484   File "/usr/lib/python3.7/site-packages/mockbuild/trace_decorator.py", line 96, in trace
5485     result = func(*args, **kw)
5486   File "/usr/lib/python3.7/site-packages/mockbuild/util.py", line 734, in do_with_status
5487     raise exception.Error("Command failed: \n # %s\n%s" % (command, output), child.returncode)
5488 mockbuild.exception.Error: Command failed:
got an asyncio keyword

# Fixed code
def __aiter__(self):
    raise self
# Buggy code
def get_tree_from_branch(self, ref):
    """
    Return a pygit2.Tree object matching a head ref fetched into
    refs/remotes/origin/
    """
    try:
        return (
            self.repo.lookup_reference("refs/remotes/origin/{0}".format(ref))
            .get_object()
            .tree
        )
    except KeyError:
        return None


# Traceback (AttributeError)
Exception occurred in runner fileserver.file_list: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/client/mixins.py", line 387, in _low
data['return'] = self.functions[fun](*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/runners/fileserver.py", line 193, in file_list
return fileserver.file_list(load=load)
File "/usr/lib/python2.7/dist-packages/salt/utils/decorators/__init__.py", line 594, in wrapped
**salt.utils.data.decode_dict(kwargs)
File "/usr/lib/python2.7/dist-packages/salt/fileserver/__init__.py", line 752, in file_list
ret.update(self.servers[fstr](load))
File "/usr/lib/python2.7/dist-packages/salt/fileserver/gitfs.py", line 185, in file_list
return _gitfs().file_list(load)
File "/usr/lib/python2.7/dist-packages/salt/utils/gitfs.py", line 2934, in file_list
return self._file_lists(load, 'files')
File "/usr/lib/python2.7/dist-packages/salt/utils/gitfs.py", line 2911, in _file_lists
repo_files, repo_symlinks = repo.file_list(load['saltenv'])
File "/usr/lib/python2.7/dist-packages/salt/utils/gitfs.py", line 1777, in file_list
tree = self.get_tree(tgt_env)
File "/usr/lib/python2.7/dist-packages/salt/utils/gitfs.py", line 1016, in get_tree
candidate = func(tgt_ref)
File "/usr/lib/python2.7/dist-packages/salt/utils/gitfs.py", line 1852, in get_tree_from_branch
'refs/remotes/origin/{0}'.format(ref)).get_object().tree
AttributeError: '_pygit2.Reference' object has no attribute 'get_object'

# Fixed code
def get_tree_from_branch(self, ref):
    """
    Return a pygit2.Tree object matching a head ref fetched into
    refs/remotes/origin/
    """
    try:
        return self.peel(
            self.repo.lookup_reference("refs/remotes/origin/{0}".format(ref))
        ).tree
    except KeyError:
        return None
# Buggy code
def get_tree_from_tag(self, ref):
    """
    Return a pygit2.Tree object matching a tag ref fetched into refs/tags/
    """
    try:
        return self.repo.lookup_reference("refs/tags/{0}".format(ref)).get_object().tree
    except KeyError:
        return None


# Traceback (AttributeError)
Exception occurred in runner fileserver.file_list: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/client/mixins.py", line 387, in _low
data['return'] = self.functions[fun](*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/runners/fileserver.py", line 193, in file_list
return fileserver.file_list(load=load)
File "/usr/lib/python2.7/dist-packages/salt/utils/decorators/__init__.py", line 594, in wrapped
**salt.utils.data.decode_dict(kwargs)
File "/usr/lib/python2.7/dist-packages/salt/fileserver/__init__.py", line 752, in file_list
ret.update(self.servers[fstr](load))
File "/usr/lib/python2.7/dist-packages/salt/fileserver/gitfs.py", line 185, in file_list
return _gitfs().file_list(load)
File "/usr/lib/python2.7/dist-packages/salt/utils/gitfs.py", line 2934, in file_list
return self._file_lists(load, 'files')
File "/usr/lib/python2.7/dist-packages/salt/utils/gitfs.py", line 2911, in _file_lists
repo_files, repo_symlinks = repo.file_list(load['saltenv'])
File "/usr/lib/python2.7/dist-packages/salt/utils/gitfs.py", line 1777, in file_list
tree = self.get_tree(tgt_env)
File "/usr/lib/python2.7/dist-packages/salt/utils/gitfs.py", line 1016, in get_tree
candidate = func(tgt_ref)
File "/usr/lib/python2.7/dist-packages/salt/utils/gitfs.py", line 1852, in get_tree_from_branch
'refs/remotes/origin/{0}'.format(ref)).get_object().tree
AttributeError: '_pygit2.Reference' object has no attribute 'get_object'

# Fixed code
def get_tree_from_tag(self, ref):
    """
    Return a pygit2.Tree object matching a tag ref fetched into refs/tags/
    """
    try:
        return self.peel(self.repo.lookup_reference("refs/tags/{0}".format(ref))).tree
    except KeyError:
        return None
# Buggy code
def active():
    """
    Return current active profile

    CLI Example:

    .. code-block:: bash

        salt '*' tuned.active
    """

    # turn off all profiles
    result = __salt__["cmd.run"]("tuned-adm active")
    pattern = re.compile(r"""(?P<stmt>Current active profile:) (?P<profile>\w+.*)""")
    match = re.match(pattern, result)
    return "{0}".format(match.group("profile"))


# Traceback (AttributeError)
# salt 'mon1*' tuned.active
mon1.ceph:
The minion function caused an exception: Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/salt/minion.py", line 1664, in _thread_return
return_data = minion_instance.executors[fname](opts, data, func, args, kwargs)
File "/usr/lib/python3.6/site-packages/salt/executors/direct_call.py", line 12, in execute
return func(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/salt/modules/tuned.py", line 77, in active
return '{0}'.format(match.group('profile'))
AttributeError: 'NoneType' object has no attribute 'group'
ERROR: Minions returned with non-zero exit code

# Fixed code
def active():
    """
    Return current active profile

    CLI Example:

    .. code-block:: bash

        salt '*' tuned.active
    """

    # turn off all profiles
    result = __salt__["cmd.run_all"]("tuned-adm active", ignore_retcode=True)
    if result["retcode"] != 0:
        return "none"
    pattern = re.compile(r"""(?P<stmt>Current active profile:) (?P<profile>\w+.*)""")
    match = re.match(pattern, result["stdout"])
    return "{0}".format(match.group("profile"))
# Buggy code
def __build_parts(text):
    lexer = shlex.shlex(text)
    lexer.whitespace_split = True
    lexer.commenters = ""
    if "'" in text:
        lexer.quotes = '"'
    elif '"' in text:
        lexer.quotes = "'"
    return list(lexer)


# Traceback (ValueError)
ID: /etc/vimrc
Function: file.append
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/state.py", line 1913, in call
**cdata['kwargs'])
File "/usr/lib/python2.7/site-packages/salt/loader.py", line 1898, in wrapper
return f(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/salt/states/file.py", line 5057, in append
salt.utils.stringutils.build_whitespace_split_regex(chunk),
File "/usr/lib/python2.7/site-packages/salt/utils/stringutils.py", line 344, in build_whitespace_split_regex
parts = [re.escape(s) for s in __build_parts(line)]
File "/usr/lib/python2.7/site-packages/salt/utils/stringutils.py", line 340, in __build_parts
return list(lexer)
File "/usr/lib64/python2.7/shlex.py", line 269, in next
token = self.get_token()
File "/usr/lib64/python2.7/shlex.py", line 96, in get_token
raw = self.read_token()
File "/usr/lib64/python2.7/shlex.py", line 172, in read_token
raise ValueError, "No closing quotation"
ValueError: No closing quotation
Started: 01:35:06.020111
Duration: 202.605 ms
Changes:

# Fixed code
def __build_parts(text):
    lexer = shlex.shlex(text)
    lexer.whitespace_split = True
    lexer.commenters = ""
    if r"'\"" in text:
        lexer.quotes = ""
    elif "'" in text:
        lexer.quotes = '"'
    elif '"' in text:
        lexer.quotes = "'"
    return list(lexer)
# Buggy code
def render_cheetah_tmpl(tmplstr, context, tmplpath=None):
    """
    Render a Cheetah template.
    """
    from Cheetah.Template import Template

    return salt.utils.data.decode(Template(tmplstr, searchList=[context]))


# Traceback (TypeError)
2019-02-19 11:34:09,030 [salt.state       :320 ][ERROR   ][6600] Traceback (most recent call last):
File "c:\salt\bin\lib\site-packages\salt\utils\templates.py", line 175, in render_tmpl
if salt.utils.stringutils.to_unicode(output, encoding=SLS_ENCODING).endswith(('\n', os.linesep)):
File "c:\salt\bin\lib\site-packages\salt\utils\stringutils.py", line 161, in to_unicode
raise TypeError('expected str or bytearray')
TypeError: expected str or bytearray

# Fixed code
def render_cheetah_tmpl(tmplstr, context, tmplpath=None):
    """
    Render a Cheetah template.
    """
    from Cheetah.Template import Template

    # Compile the template and render it into the class
    tclass = Template.compile(tmplstr)
    data = tclass(namespaces=[context])

    # Figure out which method to call based on the type of tmplstr
    if six.PY3 and isinstance(tmplstr, six.string_types):
        # This should call .__unicode__()
        res = str(data)
    elif six.PY2 and isinstance(tmplstr, six.text_type):
        # Expicitly call .__unicode__()
        res = data.__unicode__()
    elif isinstance(tmplstr, six.binary_type):
        # This should call .__str()
        res = str(data)
    else:
        raise SaltRenderError(
            "Unknown type {!s} for Cheetah template while trying to render.".format(
                type(tmplstr)
            )
        )

    # Now we can decode it to the correct encoding
    return salt.utils.data.decode(res)
# Buggy code
def file_list(self, tgt_env):
    """
    Get file list for the target environment using GitPython
    """
    files = set()
    symlinks = {}
    tree = self.get_tree(tgt_env)
    if not tree:
        # Not found, return empty objects
        return files, symlinks
    if self.root(tgt_env):
        try:
            tree = tree / self.root(tgt_env)
        except KeyError:
            return files, symlinks
        relpath = lambda path: os.path.relpath(path, self.root(tgt_env))
    else:
        relpath = lambda path: path
    add_mountpoint = lambda path: salt.utils.path.join(
        self.mountpoint(tgt_env), path, use_posixpath=True
    )
    for file_blob in tree.traverse():
        if not isinstance(file_blob, git.Blob):
            continue
        file_path = add_mountpoint(relpath(file_blob.path))
        files.add(file_path)
        if stat.S_ISLNK(file_blob.mode):
            stream = six.StringIO()
            file_blob.stream_data(stream)
            stream.seek(0)
            link_tgt = stream.read()
            stream.close()
            symlinks[file_path] = link_tgt
    return files, symlinks


# Traceback (TypeError)
Traceback (most recent call last):
File "gitpythontest.py", line 16, in <module>
file_blob.stream_data(stream)
File "/home/vagrant/.local/lib/python3.6/site-packages/git/objects/base.py", line 119, in stream_data
stream_copy(istream, ostream)
File "/home/vagrant/.local/lib/python3.6/site-packages/git/util.py", line 124, in stream_copy
destination.write(chunk)
TypeError: string argument expected, got 'bytes'

# Fixed code
def file_list(self, tgt_env):
    """
    Get file list for the target environment using GitPython
    """
    files = set()
    symlinks = {}
    tree = self.get_tree(tgt_env)
    if not tree:
        # Not found, return empty objects
        return files, symlinks
    if self.root(tgt_env):
        try:
            tree = tree / self.root(tgt_env)
        except KeyError:
            return files, symlinks
        relpath = lambda path: os.path.relpath(path, self.root(tgt_env))
    else:
        relpath = lambda path: path
    add_mountpoint = lambda path: salt.utils.path.join(
        self.mountpoint(tgt_env), path, use_posixpath=True
    )
    for file_blob in tree.traverse():
        if not isinstance(file_blob, git.Blob):
            continue
        file_path = add_mountpoint(relpath(file_blob.path))
        files.add(file_path)
        if stat.S_ISLNK(file_blob.mode):
            stream = six.BytesIO()
            file_blob.stream_data(stream)
            stream.seek(0)
            link_tgt = salt.utils.stringutils.to_str(stream.read())
            stream.close()
            symlinks[file_path] = link_tgt
    return files, symlinks
# Buggy code
def __init__(self, name, inicontents="", separator="=", commenter="#"):
    super(_Ini, self).__init__(name, inicontents, separator, commenter)


# Traceback (TypeError)
local:
----------
ID: ini-present
Function: ini.options_present
Name: /tmp/test.ini
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/home/js/work/git/salt/salt/state.py", line 1918, in call
**cdata['kwargs'])
File "/home/js/work/git/salt/salt/loader.py", line 1830, in wrapper
return f(*args, **kwargs)
File "/home/js/work/git/salt/salt/states/ini_manage.py", line 68, in options_present
if cur_value == six.text_type(sections[section][key]):
TypeError: string indices must be integers
Started: 11:44:26.917548
Duration: 2.082 ms
Changes:
----------
ID: ini-absent
Function: ini.options_absent
Name: /tmp/test.ini
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/home/js/work/git/salt/salt/state.py", line 1918, in call
**cdata['kwargs'])
File "/home/js/work/git/salt/salt/loader.py", line 1830, in wrapper
return f(*args, **kwargs)
File "/home/js/work/git/salt/salt/states/ini_manage.py", line 142, in options_absent
cur_section = __salt__['ini.get_section'](name, section, separator)
File "/home/js/work/git/salt/salt/modules/ini_manage.py", line 159, in get_section
for key, value in six.iteritems(inifile.get(section, {})):
File "/home/js/work/git/salt/salt/ext/six.py", line 606, in iteritems
return d.iteritems(**kw)
AttributeError: 'unicode' object has no attribute 'iteritems'
Started: 11:44:26.921172
Duration: 1.063 ms
Changes:
----------
ID: ini-sect-present
Function: ini.sections_present
Name: /tmp/test.ini
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/home/js/work/git/salt/salt/state.py", line 1918, in call
**cdata['kwargs'])
File "/home/js/work/git/salt/salt/loader.py", line 1830, in wrapper
return f(*args, **kwargs)
File "/home/js/work/git/salt/salt/states/ini_manage.py", line 207, in sections_present
if dict(sections[section]) == cur_section:
TypeError: list indices must be integers, not unicode
Started: 11:44:26.923578
Duration: 0.851 ms
Changes:

# Fixed code
def __init__(self, name, inicontents="", separator="=", commenter="#"):
    super(_Section, self).__init__(self)
    self.name = name
    self.inicontents = inicontents
    self.sep = separator
    self.com = commenter

    opt_regx_prefix = r"(\s*)(.+?)\s*"
    opt_regx_suffix = r"\s*(.*)\s*"
    self.opt_regx_str = r"{0}(\{1}){2}".format(
        opt_regx_prefix, self.sep, opt_regx_suffix
    )
    self.opt_regx = re.compile(self.opt_regx_str)
# Buggy code
def _get_service(name):
    """
    Get information about a service.  If the service is not found, raise an
    error

    :param str name: Service label, file name, or full path

    :return: The service information for the service, otherwise an Error
    :rtype: dict
    """
    services = salt.utils.mac_utils.available_services()
    name = name.lower()

    if name in services:
        # Match on label
        return services[name]

    for service in six.itervalues(services):
        if service["file_path"].lower() == name:
            # Match on full path
            return service
        basename, ext = os.path.splitext(service["file_name"])
        if basename.lower() == name:
            # Match on basename
            return service

    # Could not find service
    raise CommandExecutionError("Service not found: {0}".format(name))


# Traceback (NameError)
Traceback (most recent call last):
File "/testing/scripts/salt-call", line 11, in <module>
salt_call()
File "/testing/salt/scripts.py", line 400, in salt_call
client.run()
File "/testing/salt/cli/call.py", line 57, in run
caller.run()
File "/testing/salt/cli/caller.py", line 134, in run
ret = self.call()
File "/testing/salt/cli/caller.py", line 212, in call
ret['return'] = func(*args, **kwargs)
File "/testing/salt/modules/mac_service.py", line 130, in launchctl
return salt.utils.mac_utils.launchctl(sub_cmd, *args, **kwargs)
File "/testing/salt/utils/mac_utils.py", line 270, in launchctl
ret = salt.modules.cmdmod.run_all(cmd, **kwargs)
File "/testing/salt/modules/cmdmod.py", line 1834, in run_all
**kwargs)
File "/testing/salt/modules/cmdmod.py", line 357, in _run
if not _check_avail(cmd):
File "/testing/salt/modules/cmdmod.py", line 230, in _check_avail
if __salt__['config.get']('cmd_blacklist_glob'):
NameError: global name '__salt__' is not defined

# Fixed code
def _get_service(name):
    """
    Get information about a service.  If the service is not found, raise an
    error

    :param str name: Service label, file name, or full path

    :return: The service information for the service, otherwise an Error
    :rtype: dict
    """
    services = __utils__["mac_utils.available_services"]()
    name = name.lower()

    if name in services:
        # Match on label
        return services[name]

    for service in six.itervalues(services):
        if service["file_path"].lower() == name:
            # Match on full path
            return service
        basename, ext = os.path.splitext(service["file_name"])
        if basename.lower() == name:
            # Match on basename
            return service

    # Could not find service
    raise CommandExecutionError("Service not found: {0}".format(name))
# Buggy code
def launchctl(sub_cmd, *args, **kwargs):
    """
    Run a launchctl command and raise an error if it fails

    :param str sub_cmd: Sub command supplied to launchctl

    :param tuple args: Tuple containing additional arguments to pass to
        launchctl

    :param dict kwargs: Dictionary containing arguments to pass to
        ``cmd.run_all``

    :param bool return_stdout: A keyword argument.  If true return the stdout
        of the launchctl command

    :return: ``True`` if successful, raise ``CommandExecutionError`` if not, or
        the stdout of the launchctl command if requested
    :rtype: bool, str

    CLI Example:

    .. code-block:: bash

        salt '*' service.launchctl debug org.cups.cupsd
    """
    return salt.utils.mac_utils.launchctl(sub_cmd, *args, **kwargs)


# Traceback (NameError)
Traceback (most recent call last):
File "/testing/scripts/salt-call", line 11, in <module>
salt_call()
File "/testing/salt/scripts.py", line 400, in salt_call
client.run()
File "/testing/salt/cli/call.py", line 57, in run
caller.run()
File "/testing/salt/cli/caller.py", line 134, in run
ret = self.call()
File "/testing/salt/cli/caller.py", line 212, in call
ret['return'] = func(*args, **kwargs)
File "/testing/salt/modules/mac_service.py", line 130, in launchctl
return salt.utils.mac_utils.launchctl(sub_cmd, *args, **kwargs)
File "/testing/salt/utils/mac_utils.py", line 270, in launchctl
ret = salt.modules.cmdmod.run_all(cmd, **kwargs)
File "/testing/salt/modules/cmdmod.py", line 1834, in run_all
**kwargs)
File "/testing/salt/modules/cmdmod.py", line 357, in _run
if not _check_avail(cmd):
File "/testing/salt/modules/cmdmod.py", line 230, in _check_avail
if __salt__['config.get']('cmd_blacklist_glob'):
NameError: global name '__salt__' is not defined

# Fixed code
def launchctl(sub_cmd, *args, **kwargs):
    """
    Run a launchctl command and raise an error if it fails

    :param str sub_cmd: Sub command supplied to launchctl

    :param tuple args: Tuple containing additional arguments to pass to
        launchctl

    :param dict kwargs: Dictionary containing arguments to pass to
        ``cmd.run_all``

    :param bool return_stdout: A keyword argument.  If true return the stdout
        of the launchctl command

    :return: ``True`` if successful, raise ``CommandExecutionError`` if not, or
        the stdout of the launchctl command if requested
    :rtype: bool, str

    CLI Example:

    .. code-block:: bash

        salt '*' service.launchctl debug org.cups.cupsd
    """
    return __utils__["mac_utils.launchctl"](sub_cmd, *args, **kwargs)
# Buggy code
def list_(name=None, runas=None):
    """
    Run launchctl list and return the output

    :param str name: The name of the service to list

    :param str runas: User to run launchctl commands

    :return: If a name is passed returns information about the named service,
        otherwise returns a list of all services and pids
    :rtype: str

    CLI Example:

    .. code-block:: bash

        salt '*' service.list
        salt '*' service.list org.cups.cupsd
    """
    if name:
        # Get service information and label
        service = _get_service(name)
        label = service["plist"]["Label"]

        # Collect information on service: will raise an error if it fails
        return launchctl(
            "list", label, return_stdout=True, output_loglevel="trace", runas=runas
        )

    # Collect information on all services: will raise an error if it fails
    return launchctl("list", return_stdout=True, output_loglevel="trace", runas=runas)


# Traceback (NameError)
Traceback (most recent call last):
File "/testing/scripts/salt-call", line 11, in <module>
salt_call()
File "/testing/salt/scripts.py", line 400, in salt_call
client.run()
File "/testing/salt/cli/call.py", line 57, in run
caller.run()
File "/testing/salt/cli/caller.py", line 134, in run
ret = self.call()
File "/testing/salt/cli/caller.py", line 212, in call
ret['return'] = func(*args, **kwargs)
File "/testing/salt/modules/mac_service.py", line 130, in launchctl
return salt.utils.mac_utils.launchctl(sub_cmd, *args, **kwargs)
File "/testing/salt/utils/mac_utils.py", line 270, in launchctl
ret = salt.modules.cmdmod.run_all(cmd, **kwargs)
File "/testing/salt/modules/cmdmod.py", line 1834, in run_all
**kwargs)
File "/testing/salt/modules/cmdmod.py", line 357, in _run
if not _check_avail(cmd):
File "/testing/salt/modules/cmdmod.py", line 230, in _check_avail
if __salt__['config.get']('cmd_blacklist_glob'):
NameError: global name '__salt__' is not defined

# Fixed code
def list_(name=None, runas=None):
    """
    Run launchctl list and return the output

    :param str name: The name of the service to list

    :param str runas: User to run launchctl commands

    :return: If a name is passed returns information about the named service,
        otherwise returns a list of all services and pids
    :rtype: str

    CLI Example:

    .. code-block:: bash

        salt '*' service.list
        salt '*' service.list org.cups.cupsd
    """
    if name:
        # Get service information and label
        service = _get_service(name)
        label = service["plist"]["Label"]

        # Collect information on service: will raise an error if it fails
        return launchctl("list", label, return_stdout=True, runas=runas)

    # Collect information on all services: will raise an error if it fails
    return launchctl("list", return_stdout=True, runas=runas)