internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: pallets/werkzeug

Issue:
Incorrect documentation for max_form_memory_size 
Hi,

The docs state that exceeding max_form_memory_size raises RequestURITooLarge, but it looks like it actually raises RequestEntityTooLarge. 

Adam



Discussion:
Positions of incorrect mention of `RequestURITooLarge`: 
- https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/formparser.py#L78
- https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/formparser.py#L121

Positions where `RequestEntityTooLarge` is raised from conditions relating to `max_form_memory_size`: 
- https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/formparser.py#L207
- https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/formparser.py#L339


Resolving patch:
diff --git a/werkzeug/formparser.py b/werkzeug/formparser.py
--- a/werkzeug/formparser.py
+++ b/werkzeug/formparser.py
@@ -75,7 +75,7 @@ def parse_form_data(environ, stream_factory=None, charset='utf-8',
     :param max_form_memory_size: the maximum number of bytes to be accepted for
                            in-memory stored form data.  If the data
                            exceeds the value specified an
-                           :exc:`~exceptions.RequestURITooLarge`
+                           :exc:`~exceptions.RequestEntityTooLarge`
                            exception is raised.
     :param max_content_length: If this is provided and the transmitted data
                                is longer than this value an
@@ -118,7 +118,7 @@ class FormDataParser(object):
     :param max_form_memory_size: the maximum number of bytes to be accepted for
                            in-memory stored form data.  If the data
                            exceeds the value specified an
-                           :exc:`~exceptions.RequestURITooLarge`
+                           :exc:`~exceptions.RequestEntityTooLarge`
                            exception is raised.
     :param max_content_length: If this is provided and the transmitted data
                                is longer than this value an


Test patch:
Repository: pallets/werkzeug

Issue:
non-latin urls
I encountered this bug while using the latest sprint code from Flask, but I assume it's Werkzeug-level.

If I register a URL that has non-latin characters (particularly, cyrillic), it doesn't work. The request seems to invariably go for ÑÐµÑÑ, whatever that is (not cyrillic), and therefore result in a 404 or something of the sort.

I've been staring at what I think might be relevant pieces of code, but I can't nail it down to where the issue might be.

In Python 2, if there is a route of the form /someurl/<name> where 'name' is cyrillic, it fetches the correct page. In Python 3, it gives me a 404.

I've also checked and found out that registering non-latin routes at top level (/некий-урл) crashes the Python 2 instance of Werkzeug completely, whereas the Python 3 instance doesn't register non-latin view functions at all (but doesn't crash).



Discussion:
Passing a unicode string into `app.route` makes Python 2 behave like 
Python 3, in the sense that it crashes on request time, not on server 
startup.


Resolving patch:
diff --git a/werkzeug/routing.py b/werkzeug/routing.py
--- a/werkzeug/routing.py
+++ b/werkzeug/routing.py
@@ -1230,7 +1230,7 @@ def __init__(self, map, server_name, script_name, subdomain,
         self.script_name = script_name
         self.subdomain = to_unicode(subdomain, 'ascii')
         self.url_scheme = to_unicode(url_scheme, 'ascii')
-        self.path_info = to_unicode(path_info or u'', 'ascii')
+        self.path_info = to_unicode(path_info or u'', 'utf-8')
         self.default_method = to_unicode(default_method, 'ascii')
         self.query_args = query_args
 
diff --git a/werkzeug/testsuite/routing.py b/werkzeug/testsuite/routing.py
--- a/werkzeug/testsuite/routing.py
+++ b/werkzeug/testsuite/routing.py
@@ -84,6 +84,17 @@ def test_environ_defaults(self):
         self.assert_strict_equal(a.match("/bar"), ('bar', {}))
         self.assert_raises(r.NotFound, a.match, "/bars")
 
+    def test_environ_nonascii_pathinfo(self):
+        environ = create_environ('/лошадь')
+        m = r.Map([
+            r.Rule('/', endpoint='index'),
+            r.Rule(u'/лошадь', endpoint='horse')
+        ])
+        a = m.bind_to_environ(environ)
+        self.assert_strict_equal(a.match('/'), ('index', {}))
+        self.assert_strict_equal(a.match('/лошадь'), ('horse', {}))
+        self.assert_raises(r.NotFound, a.match, '/барсук')
+
     def test_basic_building(self):
         map = r.Map([
             r.Rule('/', endpoint='index'),


Test patch:
Repository: pallets/werkzeug

Issue:
CombinedMultiDict can't be converted to bool
During check if `CombinedMultiDict` is `True` the following code is being executed:

```
def __len__(self):
    return len(self.keys())
```

TypeError is being raised:

```
TypeError: object of type 'set_iterator' has no len()
```

Easy to reproduce using flask by executing:

```
bool(request.values)
```

The above code works well with `MultiDict`:

```
bool(request.args)
```



Discussion:

Resolving patch:
diff --git a/werkzeug/datastructures.py b/werkzeug/datastructures.py
--- a/werkzeug/datastructures.py
+++ b/werkzeug/datastructures.py
@@ -1354,11 +1354,20 @@ def getlist(self, key, type=None):
             rv.extend(d.getlist(key, type))
         return rv
 
-    def keys(self):
+    def _keys_impl(self):
+        """This function exists so __len__ can be implemented more efficiently,
+        saving one list creation from an iterator.
+        
+        Using this for Python 2's ``dict.keys`` behavior would be useless since
+        `dict.keys` in Python 2 returns a list, while we have a set here.
+        """
         rv = set()
         for d in self.dicts:
-            rv.update(d.keys())
-        return iter(rv)
+            rv.update(iterkeys(d))
+        return rv
+
+    def keys(self):
+        return iter(self._keys_impl())
 
     __iter__ = keys
 
@@ -1406,7 +1415,7 @@ def to_dict(self, flat=True):
         return rv
 
     def __len__(self):
-        return len(self.keys())
+        return len(self._keys_impl())
 
     def __contains__(self, key):
         for d in self.dicts:
diff --git a/werkzeug/testsuite/datastructures.py b/werkzeug/testsuite/datastructures.py
--- a/werkzeug/testsuite/datastructures.py
+++ b/werkzeug/testsuite/datastructures.py
@@ -511,6 +511,16 @@ def test_basic_interface(self):
         x = self.storage_class((md1, md2))
         self.assert_equal(list(iterlists(x)), [('foo', ['bar', 'blafasel'])])
 
+    def test_length(self):
+        d1 = datastructures.MultiDict([('foo', '1')])
+        d2 = datastructures.MultiDict([('bar', '2')])
+        assert len(d1) == len(d2) == 1
+        d = self.storage_class([d1, d2])
+        assert len(d) == 2
+        d1.clear()
+        assert len(d1) == 0
+        assert len(d) == 1
+
 
 class HeadersTestCase(WerkzeugTestCase):
     storage_class = datastructures.Headers


Test patch:
Repository: pallets/werkzeug

Issue:
werkzeug.useragents parses IE11 default user agent strings incorrectly
werkzeug.useragents import UserAgent, will parse the default IE 11 user agent string browser as seamonkey.

See http://blogs.msdn.com/b/ieinternals/archive/2013/09/21/internet-explorer-11-user-agent-string-ua-string-sniffing-compatibility-with-gecko-webkit.aspx here for examples of the default IE11 user agent string.



Discussion:

Resolving patch:
diff --git a/werkzeug/testsuite/wrappers.py b/werkzeug/testsuite/wrappers.py
--- a/werkzeug/testsuite/wrappers.py
+++ b/werkzeug/testsuite/wrappers.py
@@ -347,7 +347,9 @@ def test_user_agent_mixin(self):
              'google', None, '2.1', None),
             ('Mozilla/5.0 (X11; CrOS armv7l 3701.81.0) AppleWebKit/537.31 '
              '(KHTML, like Gecko) Chrome/26.0.1410.57 Safari/537.31',
-             'chrome', 'chromeos', '26.0.1410.57', None)
+             'chrome', 'chromeos', '26.0.1410.57', None),
+            ('Mozilla/5.0 (Windows NT 6.3; Trident/7.0; .NET4.0E; rv:11.0) like Gecko',
+             'msie', 'windows', '11.0', None)
         ]
         for ua, browser, platform, version, lang in user_agents:
             request = wrappers.Request({'HTTP_USER_AGENT': ua})
diff --git a/werkzeug/useragents.py b/werkzeug/useragents.py
--- a/werkzeug/useragents.py
+++ b/werkzeug/useragents.py
@@ -51,7 +51,7 @@ class UserAgentParser(object):
         ('konqueror', 'konqueror'),
         ('k-meleon', 'kmeleon'),
         ('netscape', 'netscape'),
-        (r'msie|microsoft\s+internet\s+explorer', 'msie'),
+        (r'msie|microsoft\s+internet\s+explorer|trident/.+? rv:', 'msie'),
         ('lynx', 'lynx'),
         ('links', 'links'),
         ('seamonkey|mozilla', 'seamonkey')


Test patch:
Repository: pallets/werkzeug

Issue:
Add socket_timeout to RedisCache.
Close connection after detect redis-server is down.



Discussion:

Resolving patch:
diff --git a/werkzeug/contrib/cache.py b/werkzeug/contrib/cache.py
--- a/werkzeug/contrib/cache.py
+++ b/werkzeug/contrib/cache.py
@@ -476,6 +476,9 @@ class RedisCache(BaseCache):
     .. versionchanged:: 0.8.3
        This cache backend now supports password authentication.
 
+    .. versionchanged:: 0.10
+        ``**kwargs`` is now passed to the redis object.
+
     :param host: address of the Redis server or an object which API is
                  compatible with the official Python Redis client (redis-py).
     :param port: port number on which Redis server listens for connections.
@@ -484,18 +487,23 @@ class RedisCache(BaseCache):
     :param default_timeout: the default timeout that is used if no timeout is
                             specified on :meth:`~BaseCache.set`.
     :param key_prefix: A prefix that should be added to all keys.
+
+    Any additional keyword arguments will be passed to ``redis.Redis``.
     """
 
     def __init__(self, host='localhost', port=6379, password=None,
-                 db=0, default_timeout=300, key_prefix=None):
+                 db=0, default_timeout=300, key_prefix=None, **kwargs):
         BaseCache.__init__(self, default_timeout)
         if isinstance(host, string_types):
             try:
                 import redis
             except ImportError:
                 raise RuntimeError('no redis module found')
+            if kwargs.get('decode_responses', None):
+                raise ValueError('decode_responses is not supported by '
+                                 'RedisCache.')
             self._client = redis.Redis(host=host, port=port, password=password,
-                                       db=db)
+                                       db=db, **kwargs)
         else:
             self._client = host
         self.key_prefix = key_prefix or ''


Test patch:
Repository: pallets/werkzeug

Issue:
A solution to #360
The patch only take effect when PATH_INFO is an empty string.

It works, at least for me.

See also #360 



Discussion:
Can you add a testcase for this?

Oh, it was my fault that I forgot the testcase again ;) I'm sorry and working on it now.

It is ready now ;)


Resolving patch:
diff --git a/werkzeug/routing.py b/werkzeug/routing.py
--- a/werkzeug/routing.py
+++ b/werkzeug/routing.py
@@ -1400,8 +1400,10 @@ def match(self, path_info=None, method=None, return_rule=False,
             query_args = self.query_args
         method = (method or self.default_method).upper()
 
-        path = u'%s|/%s' % (self.map.host_matching and self.server_name or
-                            self.subdomain, path_info.lstrip('/'))
+        path = u'%s|%s' % (
+            self.map.host_matching and self.server_name or self.subdomain,
+            path_info and '/%s' % path_info.lstrip('/')
+        )
 
         have_match_for = set()
         for rule in self.map._rules:


Test patch:
diff --git a/tests/test_routing.py b/tests/test_routing.py
--- a/tests/test_routing.py
+++ b/tests/test_routing.py
@@ -670,6 +670,21 @@ def test_unicode_rules():
     url = a.build('foobar', {}, force_external=True)
     strict_eq(url, 'http://xn--n3h.example.com/foo+bar/')
 
+def test_empty_path_info():
+    m = r.Map([
+        r.Rule("/", endpoint="index"),
+    ])
+
+    b = m.bind("example.com", script_name="/approot")
+    with pytest.raises(r.RequestRedirect) as excinfo:
+        b.match("")
+    assert excinfo.value.new_url == "http://example.com/approot/"
+
+    a = m.bind("example.com")
+    with pytest.raises(r.RequestRedirect) as excinfo:
+        a.match("")
+    assert excinfo.value.new_url == "http://example.com/"
+
 def test_map_repr():
     m = r.Map([
         r.Rule(u'/wat', endpoint='enter'),
Repository: pallets/werkzeug

Issue:
www-authenticate qop= is not quoted
According to http://tools.ietf.org/html/rfc2617#section-3.2.1 the `qop` attribute of the `www-authenticate` header is always enquoted even when a single token.

But `werkzeug.WWWAuthenticate` leaves it unquoted when it is a single token:

``` python
>>> foo = werkzeug.Response()
>>> foo.www_authenticate.set_digest('REALM','NONCE',qop=("auth","auth-int"))
>>> print foo.headers['WWW-Authenticate']
Digest nonce="NONCE", realm="REALM", qop="auth, auth-int"
>>> foo.www_authenticate.set_digest('REALM','NONCE',qop=("auth",))
>>> print foo.headers['WWW-Authenticate']
Digest nonce="NONCE", realm="REALM", qop=auth
```

Some clients may be happy with this, but others surely are not.

To correct it I believe it is sufficent to add `'qop'` to `WWWAuthenticate._require_quoting`.



Discussion:

Resolving patch:
diff --git a/werkzeug/datastructures.py b/werkzeug/datastructures.py
--- a/werkzeug/datastructures.py
+++ b/werkzeug/datastructures.py
@@ -2357,7 +2357,7 @@ class WWWAuthenticate(UpdateDictMixin, dict):
     """Provides simple access to `WWW-Authenticate` headers."""
 
     #: list of keys that require quoting in the generated header
-    _require_quoting = frozenset(['domain', 'nonce', 'opaque', 'realm'])
+    _require_quoting = frozenset(['domain', 'nonce', 'opaque', 'realm', 'qop'])
 
     def __init__(self, auth_type=None, values=None, on_update=None):
         dict.__init__(self, values or ())
diff --git a/werkzeug/testsuite/wrappers.py b/werkzeug/testsuite/wrappers.py
--- a/werkzeug/testsuite/wrappers.py
+++ b/werkzeug/testsuite/wrappers.py
@@ -489,6 +489,21 @@ def test_authenticate_mixin(self):
         resp.www_authenticate.type = None
         assert 'WWW-Authenticate' not in resp.headers
 
+    def test_authenticate_mixin_quoted_qop(self):
+        # Example taken from https://github.com/mitsuhiko/werkzeug/issues/633
+        resp = wrappers.Response()
+        resp.www_authenticate.set_digest('REALM','NONCE',qop=("auth","auth-int"))
+
+        actual = set((resp.headers['WWW-Authenticate'] + ',').split())
+        expected = set('Digest nonce="NONCE", realm="REALM", qop="auth, auth-int",'.split())
+        self.assert_equal(actual, expected)
+
+        resp.www_authenticate.set_digest('REALM','NONCE',qop=("auth",))
+
+        actual = set((resp.headers['WWW-Authenticate'] + ',').split())
+        expected = set('Digest nonce="NONCE", realm="REALM", qop="auth",'.split())
+        self.assert_equal(actual, expected)
+
     def test_response_stream_mixin(self):
         response = wrappers.Response()
         response.stream.write('Hello ')


Test patch:
Repository: pallets/werkzeug

Issue:
Writing to response.stream does not invalidate/reset Content-Length
This is mostly an issue for "expensive" stream (e.g. images from disk resized on the fly), as the request's conditionality check will likely be performed before computing the payload, at least when it comes to `last_modified`/`if-modified-since`: the conditional response check is performed via `make_conditional` which will [compute a Content-Length if none is present](https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/wrappers.py#L1406)[0].

With an empty response (body not computed/set yet) this yields a `Content-Length` of 0. If one then uses `set_data`, by default (with `automatically_set_content_length=True`) it [will re-compute and re-set the `Content-Length` header](https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/wrappers.py#L903) whereas [`ResponseStream.write` will neither invalidate nor recompute an existing `Content-Lenght`](https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/wrappers.py#L1487).

As a result, if `make_conditional` was called before setting the response's content and the response's content was set using `response.stream` (e.g. by passing it to pillow's `image.save`) the browser will get a `Content-Length` of `0` and will thus ignore all content.

[0] Also note that it does not check for the `automatically_set_content_length` flag. Not an issue here, but still...



Discussion:

Resolving patch:
diff --git a/werkzeug/wrappers.py b/werkzeug/wrappers.py
--- a/werkzeug/wrappers.py
+++ b/werkzeug/wrappers.py
@@ -1494,6 +1494,7 @@ def write(self, value):
             raise ValueError('I/O operation on closed file')
         self.response._ensure_sequence(mutable=True)
         self.response.response.append(value)
+        self.response.headers.pop('Content-Length', None)
 
     def writelines(self, seq):
         for item in seq:


Test patch:
diff --git a/tests/test_wrappers.py b/tests/test_wrappers.py
--- a/tests/test_wrappers.py
+++ b/tests/test_wrappers.py
@@ -844,6 +844,20 @@ def test_auto_content_length():
     assert resp.content_length is None
     assert resp.get_wsgi_headers({})['Content-Length'] == '12'
 
+def test_stream_content_length():
+    resp = wrappers.Response()
+    resp.stream.writelines(['foo', 'bar', 'baz'])
+    assert resp.get_wsgi_headers({})['Content-Length'] == '9'
+
+    resp = wrappers.Response()
+    resp.make_conditional({'REQUEST_METHOD': 'GET'})
+    resp.stream.writelines(['foo', 'bar', 'baz'])
+    assert resp.get_wsgi_headers({})['Content-Length'] == '9'
+
+    resp = wrappers.Response('foo')
+    resp.stream.writelines(['bar', 'baz'])
+    assert resp.get_wsgi_headers({})['Content-Length'] == '9'
+
 def test_disabled_auto_content_length():
     class MyResponse(wrappers.Response):
         automatically_set_content_length = False
Repository: pallets/werkzeug

Issue:
management script utilities is broken in 0.10
In 46ff5e9, the `run_simple` function in `werkzeug/serving.py` adds a new parameter `reloader_type`, which breaks management script utilities. 

```
Traceback (most recent call last):
  File "manage.py", line 27, in <module>
    manager.run()
  File "/Users/leemars/.virtualenvs/meiping/lib/python2.7/site-packages/flaskext/actions/__init__.py", line 58, in run
    script.run(self._actions, '')
  File "/Users/leemars/.virtualenvs/meiping/lib/python2.7/site-packages/werkzeug/script.py", line 177, in run
    return func(**arguments)
  File "/Users/leemars/.virtualenvs/meiping/lib/python2.7/site-packages/werkzeug/script.py", line 314, in action
    static_files=static_files, ssl_context=ssl_context)
  File "/Users/leemars/.virtualenvs/meiping/lib/python2.7/site-packages/werkzeug/serving.py", line 622, in run_simple
    reloader_type)
  File "/Users/leemars/.virtualenvs/meiping/lib/python2.7/site-packages/werkzeug/_reloader.py", line 262, in run_with_reloader
    reloader = reloader_loops[reloader_type](extra_files, interval)
KeyError: True
```

I found that in `werkzeug/script.py`, when calling the `run_simple` function, it didn't use named parameters.

I have made a pull request to fix it.



Discussion:

Resolving patch:
diff --git a/werkzeug/script.py b/werkzeug/script.py
--- a/werkzeug/script.py
+++ b/werkzeug/script.py
@@ -309,7 +309,9 @@ def action(hostname=('h', hostname), port=('p', port),
         """Start a new development server."""
         from werkzeug.serving import run_simple
         app = app_factory()
-        run_simple(hostname, port, app, reloader, debugger, evalex,
-                   extra_files, 1, threaded, processes,
+        run_simple(hostname, port, app,
+                   use_reloader=reloader, use_debugger=debugger,
+                   use_evalex=evalex, extra_files=extra_files,
+                   reloader_interval=1, threaded=threaded, processes=processes,
                    static_files=static_files, ssl_context=ssl_context)
     return action


Test patch:
Repository: pallets/werkzeug

Issue:
All the Travis builds are broken...
Every new pull request fails due to stylecheck failures in existing code.



Discussion:

Resolving patch:
diff --git a/werkzeug/routing.py b/werkzeug/routing.py
--- a/werkzeug/routing.py
+++ b/werkzeug/routing.py
@@ -250,6 +250,7 @@ def __init__(self, matched_values):
         self.matched_values = matched_values
 
 
+@implements_to_string
 class BuildError(RoutingException, LookupError):
 
     """Raised if the build system cannot find a URL for an endpoint with the
@@ -280,7 +281,7 @@ def _score_rule(rule):
         if adapter and adapter.map._rules:
             return max(adapter.map._rules, key=_score_rule)
 
-    def __unicode__(self):
+    def __str__(self):
         message = []
         message.append('Could not build url for endpoint %r' % self.endpoint)
         if self.method:
@@ -308,9 +309,6 @@ def __unicode__(self):
                 )
         return u''.join(message)
 
-    def __str__(self):
-        return unicode(self).encode('utf-8')
-
 
 class ValidationError(ValueError):
 


Test patch:
Repository: pallets/werkzeug

Issue:
Check for environ before accessing attributes
Cleaner pull request than #838.



Discussion:
Could you add a test such that future errors like these will be avoided in the future? Thanks!


Resolving patch:
diff --git a/werkzeug/serving.py b/werkzeug/serving.py
--- a/werkzeug/serving.py
+++ b/werkzeug/serving.py
@@ -263,7 +263,10 @@ def version_string(self):
         return BaseHTTPRequestHandler.version_string(self).strip()
 
     def address_string(self):
-        return self.environ['REMOTE_ADDR']
+        if getattr(self, 'environ', None):
+            return self.environ['REMOTE_ADDR']
+        else:
+            return ''
 
     def log_request(self, code='-', size='-'):
         self.log('info', '"%s" %s %s', self.requestline, code, size)


Test patch:
diff --git a/tests/test_serving.py b/tests/test_serving.py
--- a/tests/test_serving.py
+++ b/tests/test_serving.py
@@ -196,3 +196,19 @@ def sleep(secs):
     ReloaderLoop()._sleep(0)
     '''))
     subprocess.check_call(['python', str(script)])
+
+
+def test_wrong_protocol(dev_server):
+    # Assert that sending HTTPS requests to a HTTP server doesn't show a
+    # traceback
+    # See https://github.com/mitsuhiko/werkzeug/pull/838
+
+    server = dev_server('''
+    def app(environ, start_response):
+        start_response('200 OK', [('Content-Type', 'text/html')])
+        return [b'hello']
+    ''')
+    with pytest.raises(requests.exceptions.ConnectionError):
+        requests.get('https://%s/' % server.addr)
+
+    assert 'Traceback' not in server.logfile.read()