gurudesh/pytracebugs huggingface dataset:
# Buggy code
def calc_delta(Y, Ygrid, X, m):
    N = len(Y)
    fy = gaussian_kde(Y, bw_method="silverman")(Ygrid)
    abs_fy = np.abs(fy)
    xr = rankdata(X, method="ordinal")

    d_hat = 0
    for j in range(len(m) - 1):
        ix = np.where((xr > m[j]) & (xr <= m[j + 1]))[0]
        nm = len(ix)

        Y_ix = Y[ix]
        if not np.all(np.equal(Y_ix, Y_ix[0])):
            fyc = gaussian_kde(Y_ix, bw_method="silverman")(Ygrid)
            fy_ = np.abs(fy - fyc)
        else:
            fy_ = abs_fy

        d_hat += (nm / (2 * N)) * np.trapz(fy_, Ygrid)

    return d_hat


# Traceback (ValueError)
Traceback (most recent call last):
File "sobol.py", line 13, in <module>
param_values = saltelli.sample(1000, param_file, calc_second_order = True)
File "../../SALib/sample/saltelli.py", line 19, in sample
base_sequence = sobol_sequence.sample(N + skip_values, 2*D)
File "../../SALib/sample/sobol_sequence.py", line 47, in sample
directions = read_directions_file('joe-kuo-6.21000.txt')
File "../../SALib/sample/sobol_sequence.py", line 120, in read_directions_file
return np.array(directions)
ValueError: setting an array element with a sequence.

# Fixed code
def calc_delta(Y, Ygrid, X, m):
    """Plischke et al. (2013) delta index estimator (eqn 26) for d_hat."""
    N = len(Y)
    fy = gaussian_kde(Y, bw_method="silverman")(Ygrid)
    abs_fy = np.abs(fy)
    xr = rankdata(X, method="ordinal")

    d_hat = 0
    for j in range(len(m) - 1):
        ix = np.where((xr > m[j]) & (xr <= m[j + 1]))[0]
        nm = len(ix)

        Y_ix = Y[ix]
        if not np.all(np.equal(Y_ix, Y_ix[0])):
            fyc = gaussian_kde(Y_ix, bw_method="silverman")(Ygrid)
            fy_ = np.abs(fy - fyc)
        else:
            fy_ = abs_fy

        d_hat += (nm / (2 * N)) * np.trapz(fy_, Ygrid)

    return d_hat
# Buggy code
def sample(problem, N, seed=None):
    """Generate model inputs using Latin hypercube sampling (LHS).

    Returns a NumPy matrix containing the model inputs generated by Latin
    hypercube sampling.  The resulting matrix contains N rows and D columns,
    where D is the number of parameters.

    Parameters
    ----------
    problem : dict
        The problem definition
    N : int
        The number of samples to generate
    """
    if seed:
        np.random.seed(seed)
    D = problem["num_vars"]

    result = np.empty([N, D])
    temp = np.empty([N])
    d = 1.0 / N

    for i in range(D):
        for j in range(N):
            temp[j] = np.random.uniform(low=j * d, high=(j + 1) * d)

        np.random.shuffle(temp)

        for j in range(N):
            result[j, i] = temp[j]

    scale_samples(result, problem["bounds"])
    return result


# Traceback (ValueError)
Traceback (most recent call last):
File "sobol.py", line 13, in <module>
param_values = saltelli.sample(1000, param_file, calc_second_order = True)
File "../../SALib/sample/saltelli.py", line 19, in sample
base_sequence = sobol_sequence.sample(N + skip_values, 2*D)
File "../../SALib/sample/sobol_sequence.py", line 47, in sample
directions = read_directions_file('joe-kuo-6.21000.txt')
File "../../SALib/sample/sobol_sequence.py", line 120, in read_directions_file
return np.array(directions)
ValueError: setting an array element with a sequence.

# Fixed code
def sample(problem, N, seed=None):
    """Generate model inputs using Latin hypercube sampling (LHS).

    Returns a NumPy matrix containing the model inputs generated by Latin
    hypercube sampling.  The resulting matrix contains N rows and D columns,
    where D is the number of parameters.

    Parameters
    ----------
    problem : dict
        The problem definition
    N : int
        The number of samples to generate
    """
    if seed:
        np.random.seed(seed)
    D = problem["num_vars"]

    result = np.empty([N, D])
    temp = np.empty([N])
    d = 1.0 / N

    for i in range(D):
        for j in range(N):
            temp[j] = np.random.uniform(low=j * d, high=(j + 1) * d)

        np.random.shuffle(temp)

        for j in range(N):
            result[j, i] = temp[j]

    if not problem.get("dists"):
        scale_samples(result, problem["bounds"])
        return result
    else:
        scaled_latin = nonuniform_scale_samples(
            result, problem["bounds"], problem["dists"]
        )
        return scaled_latin
# Buggy code
def __structuredFromJson(self, data):
    if len(data) == 0:
        return None
    else:
        return json.loads(data)


# Traceback (TypeError)
import github
gh_instance = github.Github('<my-token>')
user = gh_instance.get_user()
user.name
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-ec5376e00d61> in <module>()
----> 1 user.name

/usr/local/lib/python3.2/dist-packages/github/AuthenticatedUser.py in name(self)
166         :type: string
167         """
--> 168         self._completeIfNotSet(self._name)
169         return self._NoneIfNotSet(self._name)
170

/usr/local/lib/python3.2/dist-packages/github/GithubObject.py in _completeIfNotSet(self, value)
61     def _completeIfNotSet(self, value):
62         if not self.__completed and value is NotSet:
---> 63             self.__complete()
64
65     def __complete(self):

/usr/local/lib/python3.2/dist-packages/github/GithubObject.py in __complete(self)
68             self._url,
69             None,
---> 70             None
71         )
72         self._useAttributes(data)

/usr/local/lib/python3.2/dist-packages/github/Requester.py in requestJsonAndCheck(self, verb, url, parameters, input)
77
78     def requestJsonAndCheck(self, verb, url, parameters, input):
---> 79         return self.__check(*self.requestJson(verb, url, parameters, input))
80
81     def requestMultipartAndCheck(self, verb, url, parameters, input):

/usr/local/lib/python3.2/dist-packages/github/Requester.py in __check(self, status, responseHeaders, output)
83
84     def __check(self, status, responseHeaders, output):
---> 85         output = self.__structuredFromJson(output)
86         if status >= 400:
87             raise GithubException.GithubException(status, output)

/usr/local/lib/python3.2/dist-packages/github/Requester.py in __structuredFromJson(self, data)
92             return None
93         else:
---> 94             return json.loads(data)
95
96     def requestJson(self, verb, url, parameters, input):

/usr/lib/python3.2/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
307             parse_int is None and parse_float is None and
308             parse_constant is None and object_pairs_hook is None and not kw):
--> 309         return _default_decoder.decode(s)
310     if cls is None:
311         cls = JSONDecoder

/usr/lib/python3.2/json/decoder.py in decode(self, s, _w)
351
352         """
--> 353         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
354         end = _w(s, end).end()
355         if end != len(s):

TypeError: can't use a string pattern on a bytes-like object

# Fixed code
def __structuredFromJson(self, data):
    if len(data) == 0:
        return None
    else:
        if atLeastPython3 and isinstance(data, bytes):
            data = data.decode("utf-8")
        return json.loads(data)
# Buggy code
def __init__(self, login_or_token, password, base_url, timeout):
    if password is not None:
        login = login_or_token
        self.__authorizationHeader = "Basic " + base64.b64encode(
            login + ":" + password
        ).replace("\n", "")
    elif login_or_token is not None:
        token = login_or_token
        self.__authorizationHeader = "token " + token
    else:
        self.__authorizationHeader = None

    self.__base_url = base_url
    o = urlparse.urlparse(base_url)
    self.__hostname = o.hostname
    self.__port = o.port
    self.__prefix = o.path
    self.__timeout = timeout
    if o.scheme == "https":
        self.__connectionClass = self.__httpsConnectionClass
    elif o.scheme == "http":
        self.__connectionClass = self.__httpConnectionClass
    else:
        assert False  # pragma no cover
    self.rate_limiting = (5000, 5000)


# Traceback (AssertionError)
from github import Github
gh = Github( "login", "password", "base_url")
for repo in gh.get_user().get_repos():
...         print repo.name
...
repo1
repo2
my-person-linux-kernel-repo
the-secret-macosx-repo

for repo in gh.get_organization( org_name ).get_repos():
...         print repo.name
...

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.macosx-10.7-intel/egg/github/Organization.py", line 311, in get_repos
File "build/bdist.macosx-10.7-intel/egg/github/Requester.py", line 60, in requestAndCheck
File "build/bdist.macosx-10.7-intel/egg/github/Requester.py", line 76, in requestRaw
AssertionError

# Fixed code
def __init__(self, login_or_token, password, base_url, timeout):
    if password is not None:
        login = login_or_token
        self.__authorizationHeader = "Basic " + base64.b64encode(
            login + ":" + password
        ).replace("\n", "")
    elif login_or_token is not None:
        token = login_or_token
        self.__authorizationHeader = "token " + token
    else:
        self.__authorizationHeader = None

    self.__base_url = base_url
    o = urlparse.urlparse(base_url)
    self.__hostname = o.hostname
    self.__port = o.port
    self.__prefix = o.path
    self.__timeout = timeout
    self.__scheme = o.scheme
    if o.scheme == "https":
        self.__connectionClass = self.__httpsConnectionClass
    elif o.scheme == "http":
        self.__connectionClass = self.__httpConnectionClass
    else:
        assert False  # pragma no cover
    self.rate_limiting = (5000, 5000)
# Buggy code
def get_scheduled_rule_func(data):
    def func(*args):
        rule_name = data.get("Name")
        client = aws_stack.connect_to_service("events")
        targets = client.list_targets_by_rule(Rule=rule_name)["Targets"]
        if targets:
            LOG.debug(
                "Notifying %s targets in response to triggered Events rule %s"
                % (len(targets), rule_name)
            )
        for target in targets:
            arn = target.get("Arn")
            event = json.loads(target.get("Input") or "{}")
            aws_stack.send_event_to_target(arn, event)

    return func


# Traceback (botocore.exceptions.ClientError)
2020-12-29T04:01:46:WARNING:bootstrap.py: Thread run method <function get_scheduled_rule_func.<locals>.func at 0x7f0ceb256e50>(None) failed: Traceback (most recent call last):
File "/opt/code/localstack/localstack/utils/bootstrap.py", line 514, in run
self.func(self.params)
File "/opt/code/localstack/localstack/services/events/events_listener.py", line 75, in func
sqs_client.send_message(QueueUrl=queue_url, MessageBody=json.dumps(event))
File "/opt/code/localstack/.venv/lib/python3.8/site-packages/botocore/client.py", line 316, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/opt/code/localstack/.venv/lib/python3.8/site-packages/botocore/client.py", line 635, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (MissingParameter) when calling the SendMessage operation: The request must contain the parameter MessageGroupId.

# Fixed code
def get_scheduled_rule_func(data):
    def func(*args):
        rule_name = data.get("Name")
        client = aws_stack.connect_to_service("events")
        targets = client.list_targets_by_rule(Rule=rule_name)["Targets"]
        if targets:
            LOG.debug(
                "Notifying %s targets in response to triggered Events rule %s"
                % (len(targets), rule_name)
            )
        for target in targets:
            arn = target.get("Arn")
            event = json.loads(target.get("Input") or "{}")
            attr = aws_stack.get_events_target_attributes(target)
            aws_stack.send_event_to_target(arn, event, target_attributes=attr)

    return func
# Buggy code
def func(*args):
    rule_name = data.get("Name")
    client = aws_stack.connect_to_service("events")
    targets = client.list_targets_by_rule(Rule=rule_name)["Targets"]
    if targets:
        LOG.debug(
            "Notifying %s targets in response to triggered Events rule %s"
            % (len(targets), rule_name)
        )
    for target in targets:
        arn = target.get("Arn")
        event = json.loads(target.get("Input") or "{}")
        aws_stack.send_event_to_target(arn, event)


# Traceback (botocore.exceptions.ClientError)
2020-12-29T04:01:46:WARNING:bootstrap.py: Thread run method <function get_scheduled_rule_func.<locals>.func at 0x7f0ceb256e50>(None) failed: Traceback (most recent call last):
File "/opt/code/localstack/localstack/utils/bootstrap.py", line 514, in run
self.func(self.params)
File "/opt/code/localstack/localstack/services/events/events_listener.py", line 75, in func
sqs_client.send_message(QueueUrl=queue_url, MessageBody=json.dumps(event))
File "/opt/code/localstack/.venv/lib/python3.8/site-packages/botocore/client.py", line 316, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/opt/code/localstack/.venv/lib/python3.8/site-packages/botocore/client.py", line 635, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (MissingParameter) when calling the SendMessage operation: The request must contain the parameter MessageGroupId.

# Fixed code
def func(*args):
    rule_name = data.get("Name")
    client = aws_stack.connect_to_service("events")
    targets = client.list_targets_by_rule(Rule=rule_name)["Targets"]
    if targets:
        LOG.debug(
            "Notifying %s targets in response to triggered Events rule %s"
            % (len(targets), rule_name)
        )
    for target in targets:
        arn = target.get("Arn")
        event = json.loads(target.get("Input") or "{}")
        attr = aws_stack.get_events_target_attributes(target)
        aws_stack.send_event_to_target(arn, event, target_attributes=attr)
# Buggy code
def send_event_to_target(arn, event):
    if ":lambda:" in arn:
        from localstack.services.awslambda import lambda_api

        lambda_api.run_lambda(event=event, context={}, func_arn=arn)

    elif ":sns:" in arn:
        sns_client = connect_to_service("sns")
        sns_client.publish(TopicArn=arn, Message=json.dumps(event))

    elif ":sqs:" in arn:
        sqs_client = connect_to_service("sqs")
        queue_url = get_sqs_queue_url(arn)
        sqs_client.send_message(QueueUrl=queue_url, MessageBody=json.dumps(event))

    elif ":states" in arn:
        stepfunctions_client = connect_to_service("stepfunctions")
        stepfunctions_client.start_execution(
            stateMachineArn=arn, input=json.dumps(event)
        )

    else:
        LOG.info('Unsupported Events rule target ARN "%s"' % arn)


# Traceback (botocore.exceptions.ClientError)
2020-12-29T04:01:46:WARNING:bootstrap.py: Thread run method <function get_scheduled_rule_func.<locals>.func at 0x7f0ceb256e50>(None) failed: Traceback (most recent call last):
File "/opt/code/localstack/localstack/utils/bootstrap.py", line 514, in run
self.func(self.params)
File "/opt/code/localstack/localstack/services/events/events_listener.py", line 75, in func
sqs_client.send_message(QueueUrl=queue_url, MessageBody=json.dumps(event))
File "/opt/code/localstack/.venv/lib/python3.8/site-packages/botocore/client.py", line 316, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/opt/code/localstack/.venv/lib/python3.8/site-packages/botocore/client.py", line 635, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (MissingParameter) when calling the SendMessage operation: The request must contain the parameter MessageGroupId.

# Fixed code
def send_event_to_target(arn, event, target_attributes=None):
    if ":lambda:" in arn:
        from localstack.services.awslambda import lambda_api

        lambda_api.run_lambda(event=event, context={}, func_arn=arn)

    elif ":sns:" in arn:
        sns_client = connect_to_service("sns")
        sns_client.publish(TopicArn=arn, Message=json.dumps(event))

    elif ":sqs:" in arn:
        sqs_client = connect_to_service("sqs")
        queue_url = get_sqs_queue_url(arn)

        msg_group_id = (target_attributes or {}).get("MessageGroupId")
        kwargs = {"MessageGroupId": msg_group_id} if msg_group_id else {}
        sqs_client.send_message(
            QueueUrl=queue_url, MessageBody=json.dumps(event), **kwargs
        )

    elif ":states" in arn:
        stepfunctions_client = connect_to_service("stepfunctions")
        stepfunctions_client.start_execution(
            stateMachineArn=arn, input=json.dumps(event)
        )

    else:
        LOG.info('Unsupported Events rule target ARN "%s"' % arn)
# Buggy code
def __init__(self):
    super(LambdaExecutorSeparateContainers, self).__init__()
    self.next_port = 1
    self.max_port = LAMBDA_API_UNIQUE_PORTS
    self.port_offset = LAMBDA_API_PORT_OFFSET


# Traceback (Exception)
2019-12-20T09:45:49:DEBUG:localstack.services.awslambda.lambda_executors: Using entrypoint "/var/rapid/init --bootstrap /var/runtime/bootstrap" for container "localstack_lambda_arn_aws_lambda_us-east-1_000000000000_function_rb-files-hub-service-dev-rb-files-hub" on network "host".
2019-12-20T09:45:49:DEBUG:localstack.services.awslambda.lambda_executors: Command for docker-reuse Lambda executor:  docker exec -e NODE_ENV="$NODE_ENV" -e AWS_LAMBDA_EVENT_BODY="$AWS_LAMBDA_EVENT_BODY" -e HOSTNAME="$HOSTNAME" -e LOCALSTACK_HOSTNAME="$LOCALSTACK_HOSTNAME" -e AWS_LAMBDA_FUNCTION_NAME="$AWS_LAMBDA_FUNCTION_NAME" -e AWS_LAMBDA_FUNCTION_VERSION="$AWS_LAMBDA_FUNCTION_VERSION" -e AWS_LAMBDA_FUNCTION_INVOKED_ARN="$AWS_LAMBDA_FUNCTION_INVOKED_ARN" -e _LAMBDA_SERVER_PORT="$_LAMBDA_SERVER_PORT" localstack_lambda_arn_aws_lambda_us-east-1_000000000000_function_rb-files-hub-service-dev-rb-files-hub /var/rapid/init --bootstrap /var/runtime/bootstrap index.handler
2019-12-20T09:45:49:DEBUG:localstack.services.awslambda.lambda_executors: Running lambda cmd:  docker exec -e NODE_ENV="$NODE_ENV" -e AWS_LAMBDA_EVENT_BODY="$AWS_LAMBDA_EVENT_BODY" -e HOSTNAME="$HOSTNAME" -e LOCALSTACK_HOSTNAME="$LOCALSTACK_HOSTNAME" -e AWS_LAMBDA_FUNCTION_NAME="$AWS_LAMBDA_FUNCTION_NAME" -e AWS_LAMBDA_FUNCTION_VERSION="$AWS_LAMBDA_FUNCTION_VERSION" -e AWS_LAMBDA_FUNCTION_INVOKED_ARN="$AWS_LAMBDA_FUNCTION_INVOKED_ARN" -e _LAMBDA_SERVER_PORT="$_LAMBDA_SERVER_PORT" localstack_lambda_arn_aws_lambda_us-east-1_000000000000_function_rb-files-hub-service-dev-rb-files-hub /var/rapid/init --bootstrap /var/runtime/bootstrap index.handler
2019-12-20T09:45:49:WARNING:bootstrap.py: Thread run method <function LambdaExecutor.execute.<locals>.do_execute at 0x7f59b11db4d0>(None) failed: Traceback (most recent call last):
File "/opt/code/localstack/localstack/utils/bootstrap.py", line 413, in run
self.func(self.params)
File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 70, in do_execute
result, log_output = self._execute(func_arn, func_details, event, context, version)
File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 241, in _execute
result, log_output = self.run_lambda_executor(cmd, stdin, environment)
File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 161, in run_lambda_executor
(return_code, result, log_output))
Exception: Lambda process returned error status code: 1. Result: . Output:
2019/12/20 09:45:49 listen tcp :9001: bind: address already in use

# Fixed code
def __init__(self):
    super(LambdaExecutorSeparateContainers, self).__init__()
    self.max_port = LAMBDA_API_UNIQUE_PORTS
    self.port_offset = LAMBDA_API_PORT_OFFSET
# Buggy code
def record_service_health(api, status):
    data = {api: status}
    health_url = "%s://%s:%s/health" % (
        get_service_protocol(),
        config.LOCALHOST,
        config.PORT_WEB_UI,
    )
    try:
        requests.put(health_url, data=json.dumps(data))
    except Exception:
        # ignore for now, if the service is not running
        pass


# Traceback (TypeError)
2020-06-09T11:04:42:ERROR:localstack.services.dynamodb.dynamodb_starter: DynamoDB health check failed: an integer is required (got type NoneType) Traceback (most recent call last):
File "/opt/code/localstack/localstack/services/dynamodb/dynamodb_starter.py", line 23, in check_dynamodb
wait_for_port_open(PORT_DYNAMODB_BACKEND, http_path='/', expect_success=False, sleep_time=1)
File "/opt/code/localstack/localstack/utils/common.py", line 412, in wait_for_port_open
return retry(check, sleep=sleep_time, retries=retries)
File "/opt/code/localstack/localstack/utils/common.py", line 460, in retry
raise raise_error
File "/opt/code/localstack/localstack/utils/common.py", line 456, in retry
return function(**kwargs)
File "/opt/code/localstack/localstack/utils/common.py", line 409, in check
if not is_port_open(port, http_path=http_path, expect_success=expect_success):
File "/opt/code/localstack/localstack/utils/common.py", line 392, in is_port_open
result = sock.connect_ex((host, port))
TypeError: an integer is required (got type NoneType)

# Fixed code
def record_service_health(api, status):
    data = {api: status}
    health_url = "%s://%s:%s/health" % (
        get_service_protocol(),
        config.LOCALHOST,
        config.EDGE_PORT,
    )
    try:
        requests.put(health_url, data=json.dumps(data))
    except Exception:
        # ignore for now, if the service is not running
        pass
# Buggy code
def start_elasticsearch_instance():
    # Note: keep imports here to avoid circular dependencies
    from localstack.services.es import es_starter
    from localstack.services.infra import check_infra, Plugin

    api_name = "elasticsearch"
    plugin = Plugin(
        api_name,
        start=es_starter.start_elasticsearch,
        check=es_starter.check_elasticsearch,
    )
    t1 = plugin.start(asynchronous=True)
    # sleep some time to give Elasticsearch enough time to come up
    time.sleep(8)
    apis = [api_name]
    # ensure that all infra components are up and running
    check_infra(apis=apis, additional_checks=[es_starter.check_elasticsearch])
    # restore persisted data
    persistence.restore_persisted_data(apis=apis)
    return t1


# Traceback (TypeError)
2020-02-05T17:05:30:ERROR:flask.app: Exception on / [POST]
Traceback (most recent call last):
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/opt/code/localstack/localstack/services/firehose/firehose_api.py", line 276, in post_request
es_update=es_update, version_id=version_id)
TypeError: update_destination() got an unexpected keyword argument 'es_update'

# Fixed code
def start_elasticsearch_instance():
    # Note: keep imports here to avoid circular dependencies
    from localstack.services.es import es_starter
    from localstack.services.infra import check_infra, Plugin

    api_name = "elasticsearch"
    plugin = Plugin(
        api_name,
        start=es_starter.start_elasticsearch,
        check=es_starter.check_elasticsearch,
    )
    t1 = plugin.start(asynchronous=True)
    # sleep some time to give Elasticsearch enough time to come up
    time.sleep(8)
    apis = [api_name]
    # ensure that all infra components are up and running
    check_infra(apis=apis, additional_checks=[es_starter.check_elasticsearch])
    return t1
# Buggy code
def forward_request(self, method, path, data, headers):
    data = data and json.loads(to_str(data))

    if re.match(PATH_REGEX_USER_REQUEST, path):
        search_match = re.search(PATH_REGEX_USER_REQUEST, path)
        api_id = search_match.group(1)
        stage = search_match.group(2)
        relative_path_w_query_params = "/%s" % search_match.group(3)
        return invoke_rest_api(
            api_id,
            stage,
            method,
            relative_path_w_query_params,
            data,
            headers,
            path=path,
        )

    if re.match(PATH_REGEX_AUTHORIZERS, path):
        return handle_authorizers(method, path, data, headers)

    if re.match(PATH_REGEX_RESPONSES, path):
        search_match = re.search(PATH_REGEX_RESPONSES, path)
        api_id = search_match.group(1)
        if method == "GET":
            return get_gateway_responses(api_id)
        if method == "PUT":
            response_type = search_match.group(2).lstrip("/")
            return put_gateway_response(api_id, response_type, data)

    return True


# Traceback (PermissionError)
2019-11-08T18:53:53:ERROR:localstack.services.generic_proxy: Exception running proxy on port 8081: [Errno 13] Permission denied: '/tmp/localstack/server.test.pem' Traceback (most recent call last):
File "/opt/code/localstack/localstack/services/generic_proxy.py", line 384, in run_cmd
combined_file, cert_file_name, key_file_name = GenericProxy.create_ssl_cert(serial_number=self.port)
File "/opt/code/localstack/localstack/services/generic_proxy.py", line 401, in create_ssl_cert
return generate_ssl_cert(SERVER_CERT_PEM_FILE, serial_number=serial_number)
File "/opt/code/localstack/localstack/utils/common.py", line 828, in generate_ssl_cert
save_file(target_file, file_content)
File "/opt/code/localstack/localstack/utils/common.py", line 642, in save_file
with open(file, mode) as f:
PermissionError: [Errno 13] Permission denied: '/tmp/localstack/server.test.pem'

# Fixed code
def forward_request(self, method, path, data, headers):
    if re.match(PATH_REGEX_USER_REQUEST, path):
        search_match = re.search(PATH_REGEX_USER_REQUEST, path)
        api_id = search_match.group(1)
        stage = search_match.group(2)
        relative_path_w_query_params = "/%s" % search_match.group(3)
        return invoke_rest_api(
            api_id,
            stage,
            method,
            relative_path_w_query_params,
            data,
            headers,
            path=path,
        )

    data = data and json.loads(to_str(data))

    if re.match(PATH_REGEX_AUTHORIZERS, path):
        return handle_authorizers(method, path, data, headers)

    if re.match(PATH_REGEX_RESPONSES, path):
        search_match = re.search(PATH_REGEX_RESPONSES, path)
        api_id = search_match.group(1)
        if method == "GET":
            return get_gateway_responses(api_id)
        if method == "PUT":
            response_type = search_match.group(2).lstrip("/")
            return put_gateway_response(api_id, response_type, data)

    return True
# Buggy code
def __init__(self, name, start, check=None, listener=None):
    self.plugin_name = name
    self.start_function = start
    self.listener = listener
    self.check_function = check


# Traceback (PermissionError)
2019-11-08T18:53:53:ERROR:localstack.services.generic_proxy: Exception running proxy on port 8081: [Errno 13] Permission denied: '/tmp/localstack/server.test.pem' Traceback (most recent call last):
File "/opt/code/localstack/localstack/services/generic_proxy.py", line 384, in run_cmd
combined_file, cert_file_name, key_file_name = GenericProxy.create_ssl_cert(serial_number=self.port)
File "/opt/code/localstack/localstack/services/generic_proxy.py", line 401, in create_ssl_cert
return generate_ssl_cert(SERVER_CERT_PEM_FILE, serial_number=serial_number)
File "/opt/code/localstack/localstack/utils/common.py", line 828, in generate_ssl_cert
save_file(target_file, file_content)
File "/opt/code/localstack/localstack/utils/common.py", line 642, in save_file
with open(file, mode) as f:
PermissionError: [Errno 13] Permission denied: '/tmp/localstack/server.test.pem'

# Fixed code
def __init__(self, name, start, check=None, listener=None, priority=0):
    self.plugin_name = name
    self.start_function = start
    self.listener = listener
    self.check_function = check
    self.priority = priority
# Buggy code
def register_plugin(plugin):
    SERVICE_PLUGINS[plugin.name()] = plugin


# Traceback (PermissionError)
2019-11-08T18:53:53:ERROR:localstack.services.generic_proxy: Exception running proxy on port 8081: [Errno 13] Permission denied: '/tmp/localstack/server.test.pem' Traceback (most recent call last):
File "/opt/code/localstack/localstack/services/generic_proxy.py", line 384, in run_cmd
combined_file, cert_file_name, key_file_name = GenericProxy.create_ssl_cert(serial_number=self.port)
File "/opt/code/localstack/localstack/services/generic_proxy.py", line 401, in create_ssl_cert
return generate_ssl_cert(SERVER_CERT_PEM_FILE, serial_number=serial_number)
File "/opt/code/localstack/localstack/utils/common.py", line 828, in generate_ssl_cert
save_file(target_file, file_content)
File "/opt/code/localstack/localstack/utils/common.py", line 642, in save_file
with open(file, mode) as f:
PermissionError: [Errno 13] Permission denied: '/tmp/localstack/server.test.pem'

# Fixed code
def register_plugin(plugin):
    existing = SERVICE_PLUGINS.get(plugin.name())
    if existing:
        if existing.priority > plugin.priority:
            return
    SERVICE_PLUGINS[plugin.name()] = plugin
# Buggy code
def event_type_matches(events, action, api_method):
    """check whether any of the event types in `events` matches the
    given `action` and `api_method`, and return the first match."""
    for event in events:
        regex = event.replace("*", "[^:]*")
        action_string = "s3:%s:%s" % (action, api_method)
        match = re.match(regex, action_string)
        if match:
            return match
    return False


# Traceback (AttributeError)
2017-11-06T16:27:30:ERROR:localstack.services.generic_proxy: Error forwarding request: 'list' object has no attribute 'get' Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/localstack/services/generic_proxy.py", line 161, in forward
path=path, data=data, headers=forward_headers)
File "/usr/local/lib/python3.6/site-packages/localstack/services/s3/s3_listener.py", line 402, in forward_request
events = config.get('Event')
AttributeError: 'list' object has no attribute 'get'

# Fixed code
def event_type_matches(events, action, api_method):
    """check whether any of the event types in `events` matches the
    given `action` and `api_method`, and return the first match."""
    events = events or []
    for event in events:
        regex = event.replace("*", "[^:]*")
        action_string = "s3:%s:%s" % (action, api_method)
        match = re.match(regex, action_string)
        if match:
            return match
    return False
# Buggy code
def _send_cors_headers(self, response=None):
    headers = response and response.headers or {}
    if "Access-Control-Allow-Origin" not in headers:
        self.send_header("Access-Control-Allow-Origin", "*")
    if "Access-Control-Allow-Methods" not in headers:
        self.send_header("Access-Control-Allow-Methods", ",".join(CORS_ALLOWED_METHODS))
    if "Access-Control-Allow-Headers" not in headers:
        requested_headers = self.headers.get("Access-Control-Request-Headers", "")
        requested_headers = (
            re.split(r"[,\s]+", requested_headers) + CORS_ALLOWED_HEADERS
        )
        self.send_header("Access-Control-Allow-Headers", ",".join(requested_headers))
    if "Access-Control-Expose-Headers" not in headers:
        self.send_header("Access-Control-Expose-Headers", ",".join(CORS_EXPOSE_HEADERS))


# Traceback (KeyError)
localstack_1        | 2019-09-09T10:46:22:ERROR:localstack.services.generic_proxy: Error forwarding request: 'QueueUrl' Traceback (most recent call last):
localstack_1        |   File "/opt/code/localstack/localstack/services/generic_proxy.py", line 234, in forward
localstack_1        |     path=path, data=data, headers=forward_headers)
localstack_1        |   File "/opt/code/localstack/localstack/services/sqs/sqs_listener.py", line 53, in forward_request
localstack_1        |     self._set_queue_attributes(req_data)
localstack_1        |   File "/opt/code/localstack/localstack/services/sqs/sqs_listener.py", line 245, in _set_queue_attributes
localstack_1        |     queue_url = req_data['QueueUrl'][0]
localstack_1        | KeyError: 'QueueUrl'

# Fixed code
def _send_cors_headers(self, response=None):
    headers = response and response.headers or {}
    if "Access-Control-Allow-Origin" not in headers:
        self.send_header("Access-Control-Allow-Origin", "*")
    if "Access-Control-Allow-Methods" not in headers:
        self.send_header("Access-Control-Allow-Methods", ",".join(CORS_ALLOWED_METHODS))
    if "Access-Control-Allow-Headers" not in headers:
        requested_headers = self.headers.get("Access-Control-Request-Headers", "")
        requested_headers = (
            re.split(r"[,\s]+", requested_headers) + CORS_ALLOWED_HEADERS
        )
        self.send_header(
            "Access-Control-Allow-Headers",
            ",".join([h for h in requested_headers if h]),
        )
    if "Access-Control-Expose-Headers" not in headers:
        self.send_header("Access-Control-Expose-Headers", ",".join(CORS_EXPOSE_HEADERS))
# Buggy code
def forward_request(self, method, path, data, headers):
    if method == "OPTIONS":
        return 200

    req_data = self.parse_request_data(method, path, data)

    if req_data:
        action = req_data.get("Action", [None])[0]
        if action == "SendMessage":
            new_response = self._send_message(path, data, req_data, headers)
            if new_response:
                return new_response
        elif action == "SetQueueAttributes":
            self._set_queue_attributes(req_data)

        if "QueueName" in req_data:
            encoded_data = urlencode(req_data, doseq=True) if method == "POST" else ""
            modified_url = None
            if method == "GET":
                base_path = path.partition("?")[0]
                modified_url = "%s?%s" % (base_path, urlencode(req_data, doseq=True))
            request = Request(
                data=encoded_data, url=modified_url, headers=headers, method=method
            )
            return request

    return True


# Traceback (KeyError)
localstack_1        | 2019-09-09T10:46:22:ERROR:localstack.services.generic_proxy: Error forwarding request: 'QueueUrl' Traceback (most recent call last):
localstack_1        |   File "/opt/code/localstack/localstack/services/generic_proxy.py", line 234, in forward
localstack_1        |     path=path, data=data, headers=forward_headers)
localstack_1        |   File "/opt/code/localstack/localstack/services/sqs/sqs_listener.py", line 53, in forward_request
localstack_1        |     self._set_queue_attributes(req_data)
localstack_1        |   File "/opt/code/localstack/localstack/services/sqs/sqs_listener.py", line 245, in _set_queue_attributes
localstack_1        |     queue_url = req_data['QueueUrl'][0]
localstack_1        | KeyError: 'QueueUrl'

# Fixed code
def forward_request(self, method, path, data, headers):
    if method == "OPTIONS":
        return 200

    req_data = self.parse_request_data(method, path, data)

    if req_data:
        action = req_data.get("Action", [None])[0]
        if action == "SendMessage":
            new_response = self._send_message(path, data, req_data, headers)
            if new_response:
                return new_response
        elif action == "SetQueueAttributes":
            self._set_queue_attributes(path, req_data, headers)

        if "QueueName" in req_data:
            encoded_data = urlencode(req_data, doseq=True) if method == "POST" else ""
            modified_url = None
            if method == "GET":
                base_path = path.partition("?")[0]
                modified_url = "%s?%s" % (base_path, urlencode(req_data, doseq=True))
            request = Request(
                data=encoded_data, url=modified_url, headers=headers, method=method
            )
            return request

    return True
# Buggy code
def _send_message(self, path, data, req_data, headers):
    queue_url = req_data.get("QueueUrl", [path.partition("?")[0]])[0]
    queue_name = queue_url[queue_url.rindex("/") + 1 :]
    message_body = req_data.get("MessageBody", [None])[0]
    message_attributes = self.format_message_attributes(req_data)
    region_name = extract_region_from_auth_header(headers)

    process_result = lambda_api.process_sqs_message(
        message_body, message_attributes, queue_name, region_name=region_name
    )
    if process_result:
        # If a Lambda was listening, do not add the message to the queue
        new_response = Response()
        new_response._content = SUCCESSFUL_SEND_MESSAGE_XML_TEMPLATE.format(
            message_attr_hash=md5(data),
            message_body_hash=md5(message_body),
            message_id=str(uuid.uuid4()),
        )
        new_response.status_code = 200
        return new_response


# Traceback (KeyError)
localstack_1        | 2019-09-09T10:46:22:ERROR:localstack.services.generic_proxy: Error forwarding request: 'QueueUrl' Traceback (most recent call last):
localstack_1        |   File "/opt/code/localstack/localstack/services/generic_proxy.py", line 234, in forward
localstack_1        |     path=path, data=data, headers=forward_headers)
localstack_1        |   File "/opt/code/localstack/localstack/services/sqs/sqs_listener.py", line 53, in forward_request
localstack_1        |     self._set_queue_attributes(req_data)
localstack_1        |   File "/opt/code/localstack/localstack/services/sqs/sqs_listener.py", line 245, in _set_queue_attributes
localstack_1        |     queue_url = req_data['QueueUrl'][0]
localstack_1        | KeyError: 'QueueUrl'

# Fixed code
def _send_message(self, path, data, req_data, headers):
    queue_url = self._queue_url(path, req_data, headers)
    queue_name = queue_url[queue_url.rindex("/") + 1 :]
    message_body = req_data.get("MessageBody", [None])[0]
    message_attributes = self.format_message_attributes(req_data)
    region_name = extract_region_from_auth_header(headers)

    process_result = lambda_api.process_sqs_message(
        message_body, message_attributes, queue_name, region_name=region_name
    )
    if process_result:
        # If a Lambda was listening, do not add the message to the queue
        new_response = Response()
        new_response._content = SUCCESSFUL_SEND_MESSAGE_XML_TEMPLATE.format(
            message_attr_hash=md5(data),
            message_body_hash=md5(message_body),
            message_id=str(uuid.uuid4()),
        )
        new_response.status_code = 200
        return new_response
# Buggy code
def _set_queue_attributes(self, req_data):
    queue_url = req_data["QueueUrl"][0]
    attrs = self._format_attributes(req_data)
    # select only the attributes in UNSUPPORTED_ATTRIBUTE_NAMES
    attrs = dict([(k, v) for k, v in attrs.items() if k in UNSUPPORTED_ATTRIBUTE_NAMES])
    QUEUE_ATTRIBUTES[queue_url] = QUEUE_ATTRIBUTES.get(queue_url) or {}
    QUEUE_ATTRIBUTES[queue_url].update(attrs)


# Traceback (KeyError)
localstack_1        | 2019-09-09T10:46:22:ERROR:localstack.services.generic_proxy: Error forwarding request: 'QueueUrl' Traceback (most recent call last):
localstack_1        |   File "/opt/code/localstack/localstack/services/generic_proxy.py", line 234, in forward
localstack_1        |     path=path, data=data, headers=forward_headers)
localstack_1        |   File "/opt/code/localstack/localstack/services/sqs/sqs_listener.py", line 53, in forward_request
localstack_1        |     self._set_queue_attributes(req_data)
localstack_1        |   File "/opt/code/localstack/localstack/services/sqs/sqs_listener.py", line 245, in _set_queue_attributes
localstack_1        |     queue_url = req_data['QueueUrl'][0]
localstack_1        | KeyError: 'QueueUrl'

# Fixed code
def _set_queue_attributes(self, path, req_data, headers):
    queue_url = self._queue_url(path, req_data, headers)
    attrs = self._format_attributes(req_data)
    # select only the attributes in UNSUPPORTED_ATTRIBUTE_NAMES
    attrs = dict([(k, v) for k, v in attrs.items() if k in UNSUPPORTED_ATTRIBUTE_NAMES])
    QUEUE_ATTRIBUTES[queue_url] = QUEUE_ATTRIBUTES.get(queue_url) or {}
    QUEUE_ATTRIBUTES[queue_url].update(attrs)
# Buggy code
def _add_queue_attributes(self, req_data, content_str):
    flags = re.MULTILINE | re.DOTALL
    queue_url = req_data["QueueUrl"][0]
    regex = r"(.*<GetQueueAttributesResult>)(.*)(</GetQueueAttributesResult>.*)"
    attrs = re.sub(regex, r"\2", content_str, flags=flags)
    for key, value in QUEUE_ATTRIBUTES.get(queue_url, {}).items():
        if not re.match(r"<Name>\s*%s\s*</Name>" % key, attrs, flags=flags):
            attrs += "<Attribute><Name>%s</Name><Value>%s</Value></Attribute>" % (
                key,
                value,
            )
    content_str = (
        re.sub(regex, r"\1", content_str, flags=flags)
        + attrs
        + re.sub(regex, r"\3", content_str, flags=flags)
    )
    return content_str


# Traceback (KeyError)
localstack_1        | 2019-09-09T10:46:22:ERROR:localstack.services.generic_proxy: Error forwarding request: 'QueueUrl' Traceback (most recent call last):
localstack_1        |   File "/opt/code/localstack/localstack/services/generic_proxy.py", line 234, in forward
localstack_1        |     path=path, data=data, headers=forward_headers)
localstack_1        |   File "/opt/code/localstack/localstack/services/sqs/sqs_listener.py", line 53, in forward_request
localstack_1        |     self._set_queue_attributes(req_data)
localstack_1        |   File "/opt/code/localstack/localstack/services/sqs/sqs_listener.py", line 245, in _set_queue_attributes
localstack_1        |     queue_url = req_data['QueueUrl'][0]
localstack_1        | KeyError: 'QueueUrl'

# Fixed code
def _add_queue_attributes(self, path, req_data, content_str, headers):
    flags = re.MULTILINE | re.DOTALL
    queue_url = self._queue_url(path, req_data, headers)
    regex = r"(.*<GetQueueAttributesResult>)(.*)(</GetQueueAttributesResult>.*)"
    attrs = re.sub(regex, r"\2", content_str, flags=flags)
    for key, value in QUEUE_ATTRIBUTES.get(queue_url, {}).items():
        if not re.match(r"<Name>\s*%s\s*</Name>" % key, attrs, flags=flags):
            attrs += "<Attribute><Name>%s</Name><Value>%s</Value></Attribute>" % (
                key,
                value,
            )
    content_str = (
        re.sub(regex, r"\1", content_str, flags=flags)
        + attrs
        + re.sub(regex, r"\3", content_str, flags=flags)
    )
    return content_str
# Buggy code
def strip_chunk_signatures(data):
    # For clients that use streaming v4 authentication, the request contains chunk signatures
    # in the HTTP body (see example below) which we need to strip as moto cannot handle them
    #
    # 17;chunk-signature=6e162122ec4962bea0b18bc624025e6ae4e9322bdc632762d909e87793ac5921
    # <payload data ...>
    # 0;chunk-signature=927ab45acd82fc90a3c210ca7314d59fedc77ce0c914d79095f8cc9563cf2c70

    data_new = re.sub(
        b"(\r\n)?[0-9a-fA-F]+;chunk-signature=[0-9a-f]{64}(\r\n){,2}",
        b"",
        data,
        flags=re.MULTILINE | re.DOTALL,
    )
    if data_new != data:
        # trim \r (13) or \n (10)
        for i in range(0, 2):
            if data_new[0] in (10, 13):
                data_new = data_new[1:]
        for i in range(0, 6):
            if data_new[-1] in (10, 13):
                data_new = data_new[:-1]
    return data_new


# Traceback (IndexError)
2017-11-10T12:42:51:ERROR:localstack.services.generic_proxy: Error forwarding request: string index out of range Traceback (most recent call last):

File "/opt/code/localstack/localstack/services/generic_proxy.py", line 162, in forward
path=path, data=data, headers=forward_headers)

File "/opt/code/localstack/localstack/services/s3/s3_listener.py", line 383, in forward_request
modified_data = strip_chunk_signatures(data)

File "/opt/code/localstack/localstack/services/s3/s3_listener.py", line 252, in strip_chunk_signatures
if data_new[0] in (10, 13):

IndexError: string index out of range

# Fixed code
def strip_chunk_signatures(data):
    # For clients that use streaming v4 authentication, the request contains chunk signatures
    # in the HTTP body (see example below) which we need to strip as moto cannot handle them
    #
    # 17;chunk-signature=6e162122ec4962bea0b18bc624025e6ae4e9322bdc632762d909e87793ac5921
    # <payload data ...>
    # 0;chunk-signature=927ab45acd82fc90a3c210ca7314d59fedc77ce0c914d79095f8cc9563cf2c70

    data_new = re.sub(
        b"(\r\n)?[0-9a-fA-F]+;chunk-signature=[0-9a-f]{64}(\r\n){,2}",
        b"",
        data,
        flags=re.MULTILINE | re.DOTALL,
    )
    if data_new != data:
        # trim \r (13) or \n (10)
        for i in range(0, 2):
            if len(data_new) and data_new[0] in (10, 13):
                data_new = data_new[1:]
        for i in range(0, 6):
            if len(data_new) and data_new[-1] in (10, 13):
                data_new = data_new[:-1]
    return data_new
# Buggy code
def filter_rules_match(filters, object_path):
    """check whether the given object path matches all of the given filters"""
    filters = filters or {}
    key_filter = filters.get("S3Key", filters.get("Key", {}))
    for rule in key_filter.get("FilterRule", []):
        if rule["Name"] == "prefix":
            if not prefix_with_slash(object_path).startswith(
                prefix_with_slash(rule["Value"])
            ):
                return False
        elif rule["Name"] == "suffix":
            if not object_path.endswith(rule["Value"]):
                return False
        else:
            LOGGER.warning('Unknown filter name: "%s"' % rule["Name"])
    return True


# Traceback (TypeError)
2017-10-10T10:58:34:ERROR:localstack.services.generic_proxy: Error forwarding request: string indices must be integers Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/localstack/services/generic_proxy.py", line 194, in forward
path=path, data=data, headers=forward_headers, response=response)
File "/usr/local/lib/python3.6/site-packages/localstack/services/s3/s3_listener.py", line 436, in return_response
send_notifications(method, bucket_name, object_path)
File "/usr/local/lib/python3.6/site-packages/localstack/services/s3/s3_listener.py", line 122, in send_notifications
filter_rules_match(config.get('Filter'), object_path)):
File "/usr/local/lib/python3.6/site-packages/localstack/services/s3/s3_listener.py", line 50, in filter_rules_match
if rule['Name'] == 'prefix':
TypeError: string indices must be integers

# Fixed code
def filter_rules_match(filters, object_path):
    """check whether the given object path matches all of the given filters"""
    filters = filters or {}
    s3_filter = _get_s3_filter(filters)
    for rule in s3_filter.get("FilterRule", []):
        if rule["Name"] == "prefix":
            if not prefix_with_slash(object_path).startswith(
                prefix_with_slash(rule["Value"])
            ):
                return False
        elif rule["Name"] == "suffix":
            if not object_path.endswith(rule["Value"]):
                return False
        else:
            LOGGER.warning('Unknown filter name: "%s"' % rule["Name"])
    return True