# Buggy code
def _get_serv(ret=None, commit=False):
    """
    Return a mysql cursor
    """
    _options = _get_options(ret)
    conn = MySQLdb.connect(
        host=_options.get("host"),
        user=_options.get("user"),
        passwd=_options.get("pass"),
        db=_options.get("db"),
        port=_options.get("port"),
    )
    cursor = conn.cursor()
    try:
        yield cursor
    except MySQLdb.DatabaseError as err:
        error = err.args
        sys.stderr.write(str(error))
        cursor.execute("ROLLBACK")
        raise err
    else:
        if commit:
            cursor.execute("COMMIT")
        else:
            cursor.execute("ROLLBACK")
    finally:
        conn.close()


# Traceback (TypeError)
[ERROR   ] An un-handled exception was caught by salt's global exception handler:
TypeError: 'dict' object is not callable
Traceback (most recent call last):
File "/usr/bin/salt", line 10, in <module>
salt_main()
File "/usr/lib/python2.7/dist-packages/salt/scripts.py", line 351, in salt_main
client.run()
File "/usr/lib/python2.7/dist-packages/salt/cli/salt.py", line 191, in run
for full_ret in cmd_func(**kwargs):
File "/usr/lib/python2.7/dist-packages/salt/client/__init__.py", line 594, in cmd_cli
**kwargs):
File "/usr/lib/python2.7/dist-packages/salt/client/__init__.py", line 1235, in get_cli_event_returns
expect_minions=(verbose or show_timeout)
File "/usr/lib/python2.7/dist-packages/salt/client/__init__.py", line 862, in get_iter_returns
if self.returners['{0}.get_load'.format(self.opts['master_job_cache'])](jid) == {}:
File "/usr/lib/python2.7/dist-packages/salt/returners/mysql.py", line 229, in get_load
with _get_serv(ret=None, commit=True) as cur:
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/lib/python2.7/dist-packages/salt/returners/mysql.py", line 157, in _get_serv
_options = _get_options(ret)
File "/usr/lib/python2.7/dist-packages/salt/returners/mysql.py", line 145, in _get_options
defaults=defaults)
File "/usr/lib/python2.7/dist-packages/salt/returners/__init__.py", line 85, in get_returner_options
attrs,
File "/usr/lib/python2.7/dist-packages/salt/returners/__init__.py", line 164, in _options_browser
value = _fetch_option(cfg, ret_config, virtualname, options[option])
File "/usr/lib/python2.7/dist-packages/salt/returners/__init__.py", line 125, in _fetch_option
c_cfg = cfg('{0}'.format(virtualname), {})
TypeError: 'dict' object is not callable
Traceback (most recent call last):
File "/usr/bin/salt", line 10, in <module>
salt_main()
File "/usr/lib/python2.7/dist-packages/salt/scripts.py", line 351, in salt_main
client.run()
File "/usr/lib/python2.7/dist-packages/salt/cli/salt.py", line 191, in run
for full_ret in cmd_func(**kwargs):
File "/usr/lib/python2.7/dist-packages/salt/client/__init__.py", line 594, in cmd_cli
**kwargs):
File "/usr/lib/python2.7/dist-packages/salt/client/__init__.py", line 1235, in get_cli_event_returns
expect_minions=(verbose or show_timeout)
File "/usr/lib/python2.7/dist-packages/salt/client/__init__.py", line 862, in get_iter_returns
if self.returners['{0}.get_load'.format(self.opts['master_job_cache'])](jid) == {}:
File "/usr/lib/python2.7/dist-packages/salt/returners/mysql.py", line 229, in get_load
with _get_serv(ret=None, commit=True) as cur:
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/lib/python2.7/dist-packages/salt/returners/mysql.py", line 157, in _get_serv
_options = _get_options(ret)
File "/usr/lib/python2.7/dist-packages/salt/returners/mysql.py", line 145, in _get_options
defaults=defaults)
File "/usr/lib/python2.7/dist-packages/salt/returners/__init__.py", line 85, in get_returner_options
attrs,
File "/usr/lib/python2.7/dist-packages/salt/returners/__init__.py", line 164, in _options_browser
value = _fetch_option(cfg, ret_config, virtualname, options[option])
File "/usr/lib/python2.7/dist-packages/salt/returners/__init__.py", line 125, in _fetch_option
c_cfg = cfg('{0}'.format(virtualname), {})
TypeError: 'dict' object is not callable

# Fixed code
def _get_serv(ret=None, commit=False):
    """
    Return a mysql cursor
    """
    _options = _get_options(ret)
    try:
        conn = MySQLdb.connect(
            host=_options.get("host"),
            user=_options.get("user"),
            passwd=_options.get("pass"),
            db=_options.get("db"),
            port=_options.get("port"),
        )
    except MySQLdb.connections.OperationalError as exc:
        raise salt.exceptions.SaltMasterError(
            "MySQL returner could not connect to database: {exc}".format(exc=exc)
        )
    cursor = conn.cursor()
    try:
        yield cursor
    except MySQLdb.DatabaseError as err:
        error = err.args
        sys.stderr.write(str(error))
        cursor.execute("ROLLBACK")
        raise err
    else:
        if commit:
            cursor.execute("COMMIT")
        else:
            cursor.execute("ROLLBACK")
    finally:
        conn.close()