internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: emissary-ingress/emissary

Issue:
'unicode' is an undefined name in Python 3
flake8 testing of https://github.com/datawire/ambassador on Python 3.6.3

2.28s$ time flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics__
```
./ambassador/ambassador_diag/envoy.py:223:45: F821 undefined name 'active_cluster_map'
                if True or (cluster_name in active_cluster_map):
                                            ^
./scripts/versioner.py:103:16: F821 undefined name 'unicode'
        return unicode(self)
               ^
2     F821 undefined name 'active_cluster_map'
2
```


Discussion:

Resolving patch:
diff --git a/scripts/versioner.py b/scripts/versioner.py
--- a/scripts/versioner.py
+++ b/scripts/versioner.py
@@ -100,7 +100,7 @@ def __unicode__(self):
                  self.version, str(self.versioned_commit)[0:8]))
 
     def __str__(self):
-        return unicode(self)
+        return str(self)
 
     def recent_commits(self, since_tag=None):
         if not since_tag:


Test patch:
Repository: emissary-ingress/emissary

Issue:
Ambassador can crash hard with no annotations
`kubewatch.py` can crash if no annotations are found. This is obviously suboptimal.


Discussion:
See also #497 

Resolving patch:
diff --git a/ambassador/kubewatch.py b/ambassador/kubewatch.py
--- a/ambassador/kubewatch.py
+++ b/ambassador/kubewatch.py
@@ -179,6 +179,8 @@ def update_from_service(self, svc):
         source = get_source(svc)
         config = get_annotation(svc)
 
+        logger.debug("update_from_svc: key %s, config %s" % (key, yaml.safe_dump(config)))
+
         if config is None:
             self.delete(svc)
         else:
@@ -218,6 +220,9 @@ def update(self, key, config):
     def delete(self, svc):
         with self.mutex:
             key = get_filename(svc)
+
+            logger.debug("delete: dropping key %s" % key)
+
             if key in self.configs:
                 del self.configs[key]
                 self.poke()
@@ -373,10 +378,16 @@ def sync(restarter):
             svc_list = v1.list_service_for_all_namespaces()
 
         if svc_list:
+            logger.debug("sync: found %d service%s" % 
+                         (len(svc_list.items), ("" if (len(svc_list.items) == 1) else "s")))
+
             for svc in svc_list.items:
                 restarter.update_from_service(svc)
+        else:
+            logger.debug("sync: no services found")
 
-    logger.debug("Changes detected, regenerating envoy config.")
+    # Always generate an initial envoy config.    
+    logger.debug("Generating initial Envoy config")
     restarter.restart()
 
 def watch_loop(restarter):
@@ -400,10 +411,11 @@ def watch_loop(restarter):
                 restarter.delete(evt["object"])
             else:
                 restarter.update_from_service(evt["object"])
+
+        logger.info("watch loop exited?")
     else:
         logger.info("No K8s, idling")
-        while True:
-            time.sleep(60)
+        time.sleep(60)
 
 @click.command()
 @click.argument("mode", type=click.Choice(["sync", "watch"]))
@@ -488,11 +500,12 @@ def main(mode, ambassador_config_dir, envoy_config_file, delay, pid):
             try:
                 # this is in a loop because sometimes the auth expires
                 # or the connection dies
+                logger.debug("starting watch loop")
                 watch_loop(restarter)
             except KeyboardInterrupt:
                 raise
             except:
-                logging.exception("could not watch for Kubernetes service changes")
+                logger.exception("could not watch for Kubernetes service changes")
     else:
          raise ValueError(mode)
 


Test patch:
Repository: locustio/locust

Issue:
collection of exceptions broken since 4ca0eef5
the var 'traceback' in runners.py on_locust_error overrides the module 'traceback' so collecting tracebacks fails. Seems like a 2 line fix. PR upcoming



Discussion:

Resolving patch:
diff --git a/locust/core.py b/locust/core.py
--- a/locust/core.py
+++ b/locust/core.py
@@ -273,7 +273,7 @@ def run(self, *args, **kwargs):
             except GreenletExit:
                 raise
             except Exception as e:
-                events.locust_error.fire(locust_instance=self, exception=e, traceback=sys.exc_info()[2])
+                events.locust_error.fire(locust_instance=self, exception=e, tb=sys.exc_info()[2])
                 sys.stderr.write("\n" + traceback.format_exc())
                 self.wait()
     
diff --git a/locust/events.py b/locust/events.py
--- a/locust/events.py
+++ b/locust/events.py
@@ -60,7 +60,7 @@ def fire(self, **kwargs):
 
 * *locust_instance*: Locust class instance where the exception occurred
 * *exception*: Exception that was thrown
-* *traceback*: Traceback object (from sys.exc_info()[2])
+* *tb*: Traceback object (from sys.exc_info()[2])
 """
 
 report_to_master = EventHook()
diff --git a/locust/runners.py b/locust/runners.py
--- a/locust/runners.py
+++ b/locust/runners.py
@@ -190,8 +190,8 @@ def __init__(self, locust_classes, options):
         super(LocalLocustRunner, self).__init__(locust_classes, options)
 
         # register listener thats logs the exception for the local runner
-        def on_locust_error(locust_instance, exeption, traceback):
-            formatted_tb = "".join(traceback.format_tb(traceback))
+        def on_locust_error(locust_instance, exeption, tb):
+            formatted_tb = "".join(traceback.format_tb(tb))
             self.log_exception("local", str(exception), formatted_tb)
         events.locust_error += on_locust_error
 
@@ -373,8 +373,8 @@ def on_quitting():
         events.quitting += on_quitting
 
         # register listener thats sends locust exceptions to master
-        def on_locust_error(locust_instance, exception, traceback):
-            formatted_tb = "".join(traceback.format_tb(traceback))
+        def on_locust_error(locust_instance, exception, tb):
+            formatted_tb = "".join(traceback.format_tb(tb))
             self.client.send(Message("exception", {"msg" : str(exception), "traceback" : formatted_tb}, self.client_id))
         events.locust_error += on_locust_error
 


Test patch:
Repository: locustio/locust

Issue:
requests.exceptions.ConnectionError: ('Connection aborted.', ResponseNotReady('Request-sent',))
I wanted to offer this up not as an issue, but as a solution to one that I found today.

I had a test that when run on a specific server would always fail with this unhelpful message:
requests.exceptions.ConnectionError: ('Connection aborted.', ResponseNotReady('Request-sent',))

The test had multiple requests to the same client within a single task and a colleague suspected it was something to do with the connection from the first request not being properly closed.

After a lot of playing around with timeouts and attempting to close out the first connection before the next one was sent (both of which did not solve the issue), I found a stackoverflow article with the same issue:
http://stackoverflow.com/questions/30033516/single-session-multiple-post-get-in-python-requests

The quick and dirty solution was to update to requests 2.7.0. At the time of getting this error I was on 2.6.2. I also noticed that the default version for locust is on 2.4. If you are experiencing this issue, simply update to 2.7 and you should be good!



Discussion:
Huh, I had this same error in my own program. I simply followed [this guide](http://docs.python-requests.org/en/latest/user/install/) on updating to 2.7.0. Thanks for the tip!

Hi @giantryansaul 

Would the pip request dependency be dependant on the version of python? A 'pip install locustio' on python 2.7.10 seems to give me 'python-requests/2.7.0'. I say this, as I have been logging request headers. 

```
'User-Agent': 'python-requests/2.7.0 CPython/2.7.10 Linux/3.10.0-229.el7.x86_64'
```

@aidylewis Looking at the fix from @cgoldberg the library requirements are specified as requests>=2.4.1 and not requests=2.4.1, so your pip request may have simply found 2.7.0 as the best match with python 2.7.10. That is about the extent of my pip knowledge though.


Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -47,7 +47,7 @@ def run(self):
     packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
     include_package_data=True,
     zip_safe=False,
-    install_requires=["gevent==1.0.1", "flask>=0.10.1", "requests>=2.4.1", "msgpack-python>=0.4.2"],
+    install_requires=["gevent==1.0.1", "flask>=0.10.1", "requests>=2.9.1", "msgpack-python>=0.4.2"],
     tests_require=['unittest2', 'mock', 'pyzmq'],
     entry_points={
         'console_scripts': [


Test patch:
Repository: locustio/locust

Issue:
Add Python 3.6 to build pipeline



Discussion:

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -28,6 +28,7 @@
         "Programming Language :: Python :: 3.3",
         "Programming Language :: Python :: 3.4",
         "Programming Language :: Python :: 3.5",
+        "Programming Language :: Python :: 3.6",
         "Intended Audience :: Developers",
         "Intended Audience :: System Administrators",
     ],


Test patch:
Repository: locustio/locust

Issue:
Test file can not be named locust.py (or any other name that is the same as an existing python package)
Thanks for writing the locust test tool. It's very interesting!

I follow the instruction on http://locust.io/,  first I installed 

```
pip install locustio
pip install pyzmq
```

Then I copied and pasted the sample "Simple example" to  locust.py in my local directory and run it against a server listening on another PC (10.3.0.3) as the following but got error:

```
 locust -f locust.py -H http://10.3.0.3
[2014-04-04 12:03:28,901] john-HP/ERROR/locust.main: No Locust class found!
```

Not sure what have gone wrong. 

Thanks. 



Discussion:
Hi!

I believe you got this error because your locust test file is named locust.py. I just tested this and got the same error. 

I'm marking this issue as a bug, and changing the name of it. In the meanwhile you can get it working by just renaming your test file (for example to locustfile.py).

Thanks for the report!

Thanks Jonatan, it works now.  Glad I stumbled upon a bug. 

Great! I'll re-open this issue and use it to track the bug :).

Sure. Sorry, I shouldn't try to close it.

I got the same error when having file name as  locust.py.

+1 :)

I have the error no matter what my file is named. ?

where is full path of this in windows

Hi,
got the same error, tried to rename the locust but the error still apears. Note: I'm running it from ubuntu VM. Do you have any advices?

The problema here is that the error is not clear. I think you can improve error message if locust file is named `locust.py`.


Resolving patch:
diff --git a/locust/main.py b/locust/main.py
--- a/locust/main.py
+++ b/locust/main.py
@@ -350,10 +350,15 @@ def main():
         sys.exit(0)
 
     locustfile = find_locustfile(options.locustfile)
+
     if not locustfile:
         logger.error("Could not find any locustfile! Ensure file ends in '.py' and see --help for available options.")
         sys.exit(1)
 
+    if locustfile == "locust.py":
+        logger.error("The locustfile must not be named `locust.py`. Please rename the file and try again.")
+        sys.exit(1)
+
     docstring, locusts = load_locustfile(locustfile)
 
     if options.list_commands:


Test patch:
Repository: locustio/locust

Issue:
Default implementation of on_request_success and _failure lacks **kwargs declaration
### Description of issue / feature request
When implementing new (custom) client or trying to inject more metadata into results the user will usually need to introduce dedicated success/failure handlers that may extend the default list of parameters and/or fire success/failure events manually (probably with additional arguments).

Take this for example: 
https://gist.github.com/karol-brejna-i/da6295a4d7bf25c2796fa95c622e7d80

### Expected behavior
Users should be able to introduce additional request handlers that include extended parameter lists.

### Actual behavior
Because of the way the default request handlers are implemented (https://github.com/locustio/locust/blob/5e1ccc578e0984692890df455b00a94e06559321/locust/stats.py#L557), Locust will fail in this case with something similar to:
https://gist.github.com/karol-brejna-i/4ab6b0a283a5cc56e4013acef31622f8

### Environment settings (for bug reports)
- OS: doesn't affect the results; Docker, tested on Ubuntu 16.04, Windows 10
- Python version: 3.6
- Locust version: 0.8.1

### Steps to reproduce (for bug reports)
Define additional handlers, fire request_success with additional arguments as described in https://medium.com/p/183d2ae4a4c2


### Suggested solution

Making the implementation adhere to the following advice (https://docs.locust.io/en/latest/extending-locust.html):

> It’s highly recommended that you add a wildcard keyword argument in your listeners (the **kw in the code above), to prevent your code from breaking if new arguments are added in a future version.

would solve the issue.


Discussion:

Resolving patch:
diff --git a/locust/stats.py b/locust/stats.py
--- a/locust/stats.py
+++ b/locust/stats.py
@@ -554,10 +554,10 @@ def median_from_dict(total, count):
 A global instance for holding the statistics. Should be removed eventually.
 """
 
-def on_request_success(request_type, name, response_time, response_length):
+def on_request_success(request_type, name, response_time, response_length, **kwargs):
     global_stats.log_request(request_type, name, response_time, response_length)
 
-def on_request_failure(request_type, name, response_time, exception):
+def on_request_failure(request_type, name, response_time, exception, **kwargs):
     global_stats.log_error(request_type, name, exception)
 
 def on_report_to_master(client_id, data):


Test patch:
Repository: locustio/locust

Issue:
Don't rely on obsolete msgpack-python

msgpack-python looks obsolete -> https://pypi.org/project/msgpack-python/
"This package is deprecated. Install msgpack instead."

but msgpack doesn't provide pythonegg(msgpack-python).

Please consider switching to msgpack directly instead.


Discussion:

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -40,7 +40,7 @@
     packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
     include_package_data=True,
     zip_safe=False,
-    install_requires=["gevent>=1.2.2", "flask>=0.10.1", "requests>=2.9.1", "msgpack-python>=0.4.2", "six>=1.10.0", "pyzmq>=16.0.2"],
+    install_requires=["gevent>=1.2.2", "flask>=0.10.1", "requests>=2.9.1", "msgpack>=0.4.2", "six>=1.10.0", "pyzmq>=16.0.2"],
     test_suite="locust.test",
     tests_require=['mock'],
     entry_points={


Test patch:
Repository: locustio/locust

Issue:
slave client_id collisions in large environments
I'm running 64 locust slave processes per host, and getting client_id collisions regularly. 

https://github.com/locustio/locust/blob/4bde2ca90f28bde649ff4ce2c86e36a6e6260856/locust/runners.py#L355

The randomint here probably _increases_ the chances of collisions, since it's added to a millisecond scale time function.

Recommend using `uuid4()` or `uuid5(namespace, name)` using the hostname as a namespace.


Discussion:

Resolving patch:
diff --git a/locust/runners.py b/locust/runners.py
--- a/locust/runners.py
+++ b/locust/runners.py
@@ -4,7 +4,7 @@
 import socket
 import traceback
 import warnings
-from hashlib import md5
+from uuid import uuid4
 from time import time
 
 import gevent
@@ -352,7 +352,7 @@ def slave_count(self):
 class SlaveLocustRunner(DistributedLocustRunner):
     def __init__(self, *args, **kwargs):
         super(SlaveLocustRunner, self).__init__(*args, **kwargs)
-        self.client_id = socket.gethostname() + "_" + md5(str(time() + random.randint(0,10000)).encode('utf-8')).hexdigest()
+        self.client_id = socket.gethostname() + "_" + uuid4().hex
         
         self.client = rpc.Client(self.master_host, self.master_port)
         self.greenlet = Group()


Test patch:
Repository: locustio/locust

Issue:
Locust exits with 1 on timeouts / errors
Depending on how others are deploying this tool from the command line it may be a non-issue, but it's causing problems for us. Returning non-zero with the same code that we use when there are misconfigurations for, say, one 500 response code out of thousands is not subtle enough.

By the time we exit in this case, the entire test was already completed and requests were sent. Whatever downstream metrics systems will have valid data, etc. If we retry based on a non-zero exit code, we run the test twice. Users of the tool should probably be doing something more intelligent downstream to determine if the test was a failure or not than if there was at least 1 error. So why exit with an error code?

Two initial ideas that could improve the behavior (either or both would work):
1) Use a return code that's unique to this kind of "failure but not really", so it can be handled and ignored / logged with the correct criticality
2) Add a new command line parameter that is a threshold for failure (percent using stats.fail_ratio and/or absolute compared against num_requests)

Is this bothering anyone else?
([ref](https://github.com/locustio/locust/blob/v0.8a1/locust/main.py#L443))


Discussion:
Hm, I agree. Maybe we could just make it so that one could set `locust.runners.locust_runner.exit_code`, and if it's set it'll be returned when the program exists. If it's no set, it'll use the current behaviour?
I wonder if something like `--failure-threshold=0.05` as a command line arg would be a good way to handle this?

I'm not sure a good name there (naming is hard) but it seems better to add that as an opt-in optional feature than to change the default exit behavior.

I would like the option to opt-out of this. It's fairly annoying that one error will post a non-zero error code.

Resolving patch:
diff --git a/locust/main.py b/locust/main.py
--- a/locust/main.py
+++ b/locust/main.py
@@ -282,6 +282,16 @@ def parse_options():
         help="show program's version number and exit"
     )
 
+    # set the exit code to post on errors
+    parser.add_option(
+        '--exit-code-on-error',
+        action='store',
+        type="int",
+        dest='exit_code_on_error',
+        default=1,
+        help="sets the exit code to post on error"
+    )
+
     # Finalize
     # Return three-tuple of parser + the output from parse_args (opt obj, args)
     opts, args = parser.parse_args()
@@ -554,7 +564,7 @@ def sig_term_handler():
         main_greenlet.join()
         code = 0
         if len(runners.locust_runner.errors):
-            code = 1
+            code = options.exit_code_on_error
         shutdown(code=code)
     except KeyboardInterrupt as e:
         shutdown(0)


Test patch:
Repository: locustio/locust

Issue:
FastHttpLocust is very slow at returning the response text
Getting resp.text when using FastHttpLocust does a string decode (fasthttp.py:257) and it is crazy slow for large responses (~12s for a 1.5MB payload).

        with self.client.request(method, f"https://{host}{url}", name, catch_response=True) as resp:
            t = resp.text: # this takes ages
            ....
            return resp


Discussion:
Oh, and also it doesnt cache the value, so every call to .text will need to decode the string again.

I'm looking in to it...
Oh, the actual problem is the character encoding detection in apparent_encoding()
I can solve this by using cchardet instead of chardet. Encoding detection time drops from 12 seconds to 0.03s for me.

@heyman ok to add this as a dependency?
Or we could just read the Content-Type: charset header?
> Or we could just read the Content-Type: charset header?

Yeah, maybe that's best. And assume ISO-8859-1 if none is specified. I guess it could cause problems when the server is setting incorrect chaset headers, though it shouldn't be that big of a problem since users of a load testing tool usually have control over both client and server side.
I'll have a go at it

Resolving patch:
diff --git a/locust/contrib/fasthttp.py b/locust/contrib/fasthttp.py
--- a/locust/contrib/fasthttp.py
+++ b/locust/contrib/fasthttp.py
@@ -1,6 +1,5 @@
 from __future__ import absolute_import
 
-import chardet
 import re
 import six
 import socket
@@ -252,9 +251,9 @@ def text(self):
         Returns the text content of the response as a decoded string
         (unicode on python2)
         """
-        # Decode unicode from detected encoding.
         try:
-            content = unicode(self.content, self.apparent_encoding, errors='replace')
+            charset = self.headers.get('content-type', '').partition("charset=")[2]
+            content = unicode(self.content, charset or 'utf-8', errors='replace')
         except (LookupError, TypeError):
             # A LookupError is raised if the encoding was not found which could
             # indicate a misspelling or similar mistake.
@@ -268,11 +267,6 @@ def text(self):
                 content = unicode(self.content, errors='replace')
         return content
     
-    @property
-    def apparent_encoding(self):
-        """The apparent encoding, provided by the chardet library."""
-        return chardet.detect(self.content)['encoding']
-    
     def raise_for_status(self):
         """Raise any connection errors that occured during the request"""
         if hasattr(self, 'error') and self.error:


Test patch:
Repository: locustio/locust

Issue:
set default Accept-Encoding to "gzip, deflate" in FastHttpLocust to match HttpLocust behaviour
I'm going to fix this right away, but I'm adding this ticket to make it clear that a change has been done.
set default Accept-Encoding to "gzip, deflate" in FastHttpLocust to match HttpLocust behaviour
I'm going to fix this right away, but I'm adding this ticket to make it clear that a change has been done.


Discussion:

Resolving patch:
diff --git a/locust/contrib/fasthttp.py b/locust/contrib/fasthttp.py
--- a/locust/contrib/fasthttp.py
+++ b/locust/contrib/fasthttp.py
@@ -167,13 +167,14 @@ def request(self, method, path, name=None, data=None, catch_response=False, stre
         request_meta["start_time"] = default_timer()
         request_meta["name"] = name or path
         
+        headers = headers or {}
         if auth:
-            headers = headers or {}
             headers['Authorization'] = _construct_basic_auth_str(auth[0], auth[1])
         elif self.auth_header:
-            headers = headers or {}
             headers['Authorization'] = self.auth_header
-        
+        if not "Accept-Encoding" in headers:
+            headers['Accept-Encoding'] = "gzip, deflate"
+
         # send request, and catch any exceptions
         response = self._send_request_safe_mode(method, url, payload=data, headers=headers, **kwargs)
         


Test patch: