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

Issue:
werkzeug times out when exception is raised
I found this within a large Flask application but I managed to reproduce it.

SQLAlchemy's exceptions contain an `.orig` attribute with the original DBAPI exception. I only raised it to see it in the werkzeug debugger, and found that the debugger page never loads.

```python
from werkzeug.wrappers import Request, Response
from sqlalchemy import create_engine, text
from sqlalchemy.exc import DatabaseError

engine = create_engine('sqlite://')


@Request.application
def application(request):
    try:
        engine.execute(text('SELECT undefined_function()'))
    except DatabaseError as exc:
        # if we just re-raise, it works fine
        # raise

        # exc.orig is definitely an exception
        assert isinstance(exc.orig, Exception)

        # werkzeug doesn't return a response when this is raised
        raise exc.orig
    return Response('This is unreachable')


if __name__ == '__main__':
    from werkzeug.serving import run_simple
    run_simple('localhost', 4000, application, use_debugger=True)
```

The same happens for a PostgreSQL (psycopg2) connection.


Discussion:
Using git bisect I found https://github.com/pallets/werkzeug/commit/3245e585e467d93753a91e7d82dd27da75e64ac4

Resolving patch:
diff --git a/src/werkzeug/debug/tbtools.py b/src/werkzeug/debug/tbtools.py
--- a/src/werkzeug/debug/tbtools.py
+++ b/src/werkzeug/debug/tbtools.py
@@ -245,12 +245,14 @@ def __init__(self, exc_type, exc_value, tb):
         self.exception_type = exception_type
 
         self.groups = []
+        memo = set()
         while True:
             self.groups.append(Group(exc_type, exc_value, tb))
+            memo.add(exc_value)
             if PY2:
                 break
             exc_value = exc_value.__cause__ or exc_value.__context__
-            if exc_value is None:
+            if exc_value is None or exc_value in memo:
                 break
             exc_type = type(exc_value)
             tb = exc_value.__traceback__


Test patch:
diff --git a/tests/test_debug.py b/tests/test_debug.py
--- a/tests/test_debug.py
+++ b/tests/test_debug.py
@@ -354,3 +354,20 @@ def app(environ, start_response):
         assert "The debugger caught an exception in your WSGI application" in r.text
     else:
         assert r.text == "hello"
+
+
+@pytest.mark.skipif(PY2, reason="Python 2 doesn't have chained exceptions.")
+@pytest.mark.timeout(2)
+def test_chained_exception_cycle():
+    try:
+        try:
+            raise ValueError()
+        except ValueError:
+            raise TypeError()
+    except TypeError as e:
+        # create a cycle and make it available outside the except block
+        e.__context__.__context__ = error = e
+
+    # if cycles aren't broken, this will time out
+    tb = Traceback(TypeError, error, error.__traceback__)
+    assert len(tb.groups) == 2
Repository: pallets/werkzeug

Issue:
SyntaxError on python2.7.5 in 0.15.3
Traceback:

```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/site-packages/werkzeug/routing.py", line 948
    exec(code, globs, locs)
SyntaxError: unqualified exec is not allowed in function '_compile_builder' it contains a nested function with free variables
```

I haven't dug too deep into this, but I suspect it's the comprehensions which, in python2.7.5, are treated as subfunctions https://stackoverflow.com/a/52498591/8826764

python2.7.5 is important to support as it's the default python version shipped with centos7, and we're not at EOL quite yet.

I'll have time on the weekend to make an MR for this if that's OK with y'all.

If not, please change the setup python_requires to require a higher version of python. 


Discussion:
@asottile 
should be an easy fix, let me see what I can do here, I've never seen this error before though!
I recommend adding 2.7.5 to your tox.ini

On Tue, May 14, 2019, 20:17 Anthony Sottile <notifications@github.com>
wrote:

> should be an easy fix, let me see what I can do here, I've never seen this
> error before though!
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/pallets/werkzeug/issues/1544?email_source=notifications&email_token=AIQ6EKRWNORHHCPG3IJFHSLPVNQBFA5CNFSM4HM6OKBKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVNHJKI#issuecomment-492467369>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AIQ6EKQXWOD2YAUB6OIKH63PVNQBFANCNFSM4HM6OKBA>
> .
>


Resolving patch:
diff --git a/src/werkzeug/routing.py b/src/werkzeug/routing.py
--- a/src/werkzeug/routing.py
+++ b/src/werkzeug/routing.py
@@ -852,6 +852,12 @@ def match(self, path, method=None):
 
                 return result
 
+    @staticmethod
+    def _get_func_code(code, name):
+        globs, locs = {}, {}
+        exec(code, globs, locs)
+        return locs[name]
+
     def _compile_builder(self, append_unknown=True):
         defaults = self.defaults or {}
         dom_ops = []
@@ -943,11 +949,7 @@ def _join(parts):
 
         module = ast.fix_missing_locations(ast.Module([func_ast]))
         code = compile(module, "<werkzeug routing>", "exec")
-
-        globs, locs = {}, {}
-        exec(code, globs, locs)
-
-        return locs[func_ast.name]
+        return self._get_func_code(code, func_ast.name)
 
     def build(self, values, append_unknown=True):
         """Assembles the relative url for that rule and the subdomain.


Test patch:
Repository: pallets/werkzeug

Issue:
AssertionError on python 2.7.9 in Werkzeug>=0.15.3
When trying to create new flask object, I get following error:
```
>>> from flask import Flask
>>> flask_app = Flask(__name__)
python: /mnt/builds/jenkins-builds/workspace/pulsar-8/RC/icedge-apl-4/build-intel-apollolake/tmp/work/core2-64-wrs-linux/python/2.7.9-r1/Python-2.7.9/Python/symtable.c:1319: symtable_visit_params: Assertion `arg->v.Name.ctx == Param || (arg->v.Name.ctx == Store && !toplevel)' failed.
Aborted (core dumped)
```
This assertion error only shows up with Werkzeug version 0.15.3 and 0.15.4. It doesn't appear with version 0.15.2.

OS: Pulsar Linux 8.0.0.14
Python: 2.7.9
Flask: 1.0.2



Discussion:
@asottile :cry: 

It's ridiculous that direct bytecode manipulation appears to be a more stable API than the AST above it.
@DOlearczyk we'll try to fix this, but you're very far behind on patch updates to Python 2.7. There is no easy way we can test on different patch versions of Python, so you're always running the risk of weird bugs in this case.
I think I know how to fix this, but I'd like to reproduce it first, I tried with 2.7.9 but it doesn't trigger the assertion -- are you perchance using a debug build of python?

I guess I could [follow my own docs](https://github.com/asottile/scratch/wiki/virtualenv-from-cpython-source) for doing that 🤔 

Resolving patch:
diff --git a/src/werkzeug/routing.py b/src/werkzeug/routing.py
--- a/src/werkzeug/routing.py
+++ b/src/werkzeug/routing.py
@@ -939,9 +939,9 @@ def _join(parts):
                 func_ast.args.args.append(ast.arg(arg, None))
             func_ast.args.kwarg = ast.arg(".kwargs", None)
         else:
-            func_ast.args.args.append(ast.Name(".self", ast.Load()))
+            func_ast.args.args.append(ast.Name(".self", ast.Param()))
             for arg in pargs + kargs:
-                func_ast.args.args.append(ast.Name(arg, ast.Load()))
+                func_ast.args.args.append(ast.Name(arg, ast.Param()))
             func_ast.args.kwarg = ".kwargs"
         for _ in kargs:
             func_ast.args.defaults.append(ast.Str(""))
@@ -951,7 +951,16 @@ def _join(parts):
         # python3.8 changes the signature of `ast.Module`
         module = ast.parse("")
         module.body = [func_ast]
-        module = ast.fix_missing_locations(module)
+
+        # mark everything as on line 1, offset 0
+        # less error-prone than `ast.fix_missing_locations`
+        # bad line numbers cause an assert to fail in debug builds
+        for node in ast.walk(module):
+            if "lineno" in node._attributes:
+                node.lineno = 1
+            if "col_offset" in node._attributes:
+                node.col_offset = 0
+
         code = compile(module, "<werkzeug routing>", "exec")
         return self._get_func_code(code, func_ast.name)
 


Test patch:
Repository: pallets/werkzeug

Issue:
Detect opera as browser in user_agent
### Expected Behavior

`user_agent.browser` is reported as `opera` and `user_agent.version` as `60` when browsing using Opera 60 in MacOs 10.13.5.

```python
@web.route('/test')
def test():
    browser = request.user_agent.browser
    version = request.user_agent.version and int(request.user_agent.version.split('.')[0])
    platform = request.user_agent.platform
    uas = request.user_agent.string
    jsonify(dict(browser=browser, version=version, platform=platform, uas=uas))
```

### Actual Behavior

When using **Opera** the report is as follows (note the 'OPR/60'):

```
{
  "browser": "chrome", 
  "platform": "macos", 
  "uas": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36 OPR/60.0.3255.95", 
  "version": 73
}
```

When using **Chrome** the report is:

```
{
  "browser": "chrome", 
  "platform": "macos", 
  "uas": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36", 
  "version": 74
}
```

### Environment

* Python version: 3.7.0
* Flask version: 1.0.2
* Werkzeug version: 0.14.1



Discussion:

Resolving patch:
diff --git a/src/werkzeug/useragents.py b/src/werkzeug/useragents.py
--- a/src/werkzeug/useragents.py
+++ b/src/werkzeug/useragents.py
@@ -46,7 +46,7 @@ class UserAgentParser(object):
         ("yahoo", "yahoo"),
         ("ask jeeves", "ask"),
         (r"aol|america\s+online\s+browser", "aol"),
-        ("opera", "opera"),
+        (r"opera|opr", "opera"),
         ("edge", "edge"),
         ("chrome|crios", "chrome"),
         ("seamonkey", "seamonkey"),


Test patch:
diff --git a/tests/test_wrappers.py b/tests/test_wrappers.py
--- a/tests/test_wrappers.py
+++ b/tests/test_wrappers.py
@@ -616,6 +616,14 @@ def test_user_agent_mixin():
             "3.5.1",
             "de",
         ),
+        (
+            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36"
+            "(KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36 OPR/60.0.3255.95",
+            "opera",
+            "macos",
+            "60.0.3255.95",
+            None,
+        ),
     ]
     for ua, browser, platform, version, lang in user_agents:
         request = wrappers.Request({"HTTP_USER_AGENT": ua})
Repository: pallets/werkzeug

Issue:
suspicious first argument to super
The first line of `HTTPException.__init__()` is
https://github.com/pallets/werkzeug/blob/6e7c8bea0f307633e70d979fe0a89e046accc598/src/werkzeug/exceptions.py#L87

It is suspicious that this is not `super(HTTPException, ...)`. This should be changed if it is unintended or explained in a comment if it is.


Discussion:
`git blame` says that was me :sweat_smile: It's a typo, should be fixed.

Resolving patch:
diff --git a/src/werkzeug/exceptions.py b/src/werkzeug/exceptions.py
--- a/src/werkzeug/exceptions.py
+++ b/src/werkzeug/exceptions.py
@@ -84,7 +84,7 @@ class HTTPException(Exception):
     description = None
 
     def __init__(self, description=None, response=None):
-        super(Exception, self).__init__()
+        super(HTTPException, self).__init__()
         if description is not None:
             self.description = description
         self.response = response


Test patch:
Repository: pallets/werkzeug

Issue:
print warning when server_name and cur_server_name don't match
If the server name is misconfigured, the `subdomain` is `'<invalid>'` and no warning is printed.

https://github.com/pallets/werkzeug/blob/d637320cd174eec5cc1baa3aa3412bd5390e9442/werkzeug/routing.py#L1302

An improvement could be to print a warning if the names don't match, instead of just returning 404 errors for all pages.


Discussion:
I just spent multiple days trying to debug a Flask application that returned 404s for every request with no indication as to why the routes weren't being matched. It would help troubleshooting enormously if there were a visible warning when this mismatch occurs.
I had the same issue. This should probably be an error, but there must _atleast_ be a warning, as otherwise we have to dig through werkzeug source code just to find out that we have a config issue. 

Resolving patch:
diff --git a/src/werkzeug/routing.py b/src/werkzeug/routing.py
--- a/src/werkzeug/routing.py
+++ b/src/werkzeug/routing.py
@@ -100,6 +100,7 @@
 import posixpath
 import re
 import uuid
+import warnings
 from pprint import pformat
 from threading import Lock
 
@@ -1512,10 +1513,15 @@ def bind_to_environ(self, environ, server_name=None, subdomain=None):
             offset = -len(real_server_name)
             if cur_server_name[offset:] != real_server_name:
                 # This can happen even with valid configs if the server was
-                # accesssed directly by IP address under some situations.
+                # accessed directly by IP address under some situations.
                 # Instead of raising an exception like in Werkzeug 0.7 or
                 # earlier we go by an invalid subdomain which will result
                 # in a 404 error on matching.
+                warnings.warn(
+                    "Current server name '{}' doesn't match configured "
+                    "server name '{}'".format(wsgi_server_name, real_server_name),
+                    stacklevel=2,
+                )
                 subdomain = "<invalid>"
             else:
                 subdomain = ".".join(filter(None, cur_server_name[:offset]))


Test patch:
diff --git a/tests/test_routing.py b/tests/test_routing.py
--- a/tests/test_routing.py
+++ b/tests/test_routing.py
@@ -358,6 +358,16 @@ def test_http_host_before_server_name():
     assert adapter.build("index") == "http://wiki.example.com/"
 
 
+def test_invalid_subdomain_warning():
+    env = create_environ("/foo")
+    env["SERVER_NAME"] = env["HTTP_HOST"] = "foo.example.com"
+    m = r.Map([r.Rule("/foo", endpoint="foo")])
+    with pytest.warns(UserWarning) as record:
+        a = m.bind_to_environ(env, server_name="bar.example.com")
+    assert a.subdomain == "<invalid>"
+    assert len(record) == 1
+
+
 def test_adapter_url_parameter_sorting():
     map = r.Map(
         [r.Rule("/", endpoint="index")], sort_parameters=True, sort_key=lambda x: x[1]
Repository: pallets/werkzeug

Issue:
Reloader fails on setuptools script installed as an egg
We use werkzeug as router component of trytond. Everything worked on previous versions but since 0.15 (concretly after #1416 was merged), running the server from an package script and activating the reloader causes the following error: 

```
Traceback (most recent call last):
  File "/home/pokoli/.virtualenvs/kaialde/bin/trytond", line 4, in <module>
    __import__('pkg_resources').run_script('trytond==5.0.9', 'trytond')
  File "/home/pokoli/.virtualenvs/kaialde/lib/python3.7/site-packages/pkg_resources/__init__.py", line 666, in run_script
    self.require(requires)[0].run_script(script_name, ns)
  File "/home/pokoli/.virtualenvs/kaialde/lib/python3.7/site-packages/pkg_resources/__init__.py", line 1453, in run_script
    exec(code, namespace, namespace)
  File "/home/pokoli/.virtualenvs/kaialde/lib/python3.7/site-packages/trytond-5.0.9-py3.7.egg/EGG-INFO/scripts/trytond", line 47, in <module>
    use_reloader=options.dev)
  File "/home/pokoli/.virtualenvs/kaialde/lib/python3.7/site-packages/werkzeug/serving.py", line 1007, in run_simple
    run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
  File "/home/pokoli/.virtualenvs/kaialde/lib/python3.7/site-packages/werkzeug/_reloader.py", line 332, in run_with_reloader
    sys.exit(reloader.restart_with_reloader())
  File "/home/pokoli/.virtualenvs/kaialde/lib/python3.7/site-packages/werkzeug/_reloader.py", line 159, in restart_with_reloader
    args = _get_args_for_reloading()
  File "/home/pokoli/.virtualenvs/kaialde/lib/python3.7/site-packages/werkzeug/_reloader.py", line 76, in _get_args_for_reloading
    if __main__.__package__ is None:
AttributeError: module '__main__' has no attribute '__package__'
```



Discussion:

Resolving patch:
diff --git a/src/werkzeug/_reloader.py b/src/werkzeug/_reloader.py
--- a/src/werkzeug/_reloader.py
+++ b/src/werkzeug/_reloader.py
@@ -73,7 +73,9 @@ def _get_args_for_reloading():
     # Need to look at main module to determine how it was executed.
     __main__ = sys.modules["__main__"]
 
-    if __main__.__package__ is None:
+    # The value of __package__ indicates how Python was called. It may
+    # not exist if a setuptools script is installed as an egg.
+    if getattr(__main__, "__package__", None) is None:
         # Executed a file, like "python app.py".
         py_script = os.path.abspath(py_script)
 


Test patch:
Repository: pallets/werkzeug

Issue:
Reloader picks wrong module when Flask is run with the pydev debugger
This is a weird situation where the pallets/werkzeug#1416 fix to make `python -m` reloading more correct actually exposed an issue with PyDev. It rewrites `python -m flask` to `python flask` (which is clearly not correct), while Python itself rewrites it to `python /path/to/flask_entry_point.py`. Werkzeug still correctly detects that we were run as a module, but since `sys.argv[0]` is no longer a path but the module name, it incorrectly decides that there is a module named `flask.flask` in the current directory.

_Originally posted by @davidism in https://github.com/pallets/flask/issues/3297#issuecomment-510120836_


Discussion:

Resolving patch:
diff --git a/src/werkzeug/_reloader.py b/src/werkzeug/_reloader.py
--- a/src/werkzeug/_reloader.py
+++ b/src/werkzeug/_reloader.py
@@ -68,13 +68,15 @@ def _get_args_for_reloading():
     a program other than python)
     """
     rv = [sys.executable]
-    py_script = os.path.abspath(sys.argv[0])
+    py_script = sys.argv[0]
     args = sys.argv[1:]
     # Need to look at main module to determine how it was executed.
     __main__ = sys.modules["__main__"]
 
     if __main__.__package__ is None:
         # Executed a file, like "python app.py".
+        py_script = os.path.abspath(py_script)
+
         if os.name == "nt":
             # Windows entry points have ".exe" extension and should be
             # called directly.
@@ -101,11 +103,16 @@ def _get_args_for_reloading():
             # TODO remove this once Flask no longer misbehaves
             args = sys.argv
         else:
-            py_module = __main__.__package__
-            name = os.path.splitext(os.path.basename(py_script))[0]
+            if os.path.isfile(py_script):
+                # Rewritten by Python from "-m script" to "/path/to/script.py".
+                py_module = __main__.__package__
+                name = os.path.splitext(os.path.basename(py_script))[0]
 
-            if name != "__main__":
-                py_module += "." + name
+                if name != "__main__":
+                    py_module += "." + name
+            else:
+                # Incorrectly rewritten by pydevd debugger from "-m script" to "script".
+                py_module = py_script
 
             rv.extend(("-m", py_module.lstrip(".")))
 


Test patch:
Repository: pallets/werkzeug

Issue:
Cannot import `secure_filename`
```python
In [1]: import werkzeug                                                                                                        

In [2]: werkzeug.secure_filename                                                                                               
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-1002393a11fd> in <module>
----> 1 werkzeug.secure_filename

~/pypath/werkzeug/__init__.py in __getattr__(self, name)
    186             module = __import__(object_origins[name], None, None, [name])
    187             for extra_name in all_by_module[module.__name__]:
--> 188                 setattr(self, extra_name, getattr(module, extra_name))
    189             return getattr(module, name)
    190         elif name in attribute_modules:

AttributeError: module 'werkzeug.utils' has no attribute 'dump_cookie'
```

My checkout is up-to-date as of today, 3454460b6231f9b690144f7706a246f78b485d9b

It looks like "dump_cookie" got moved to ".http" but wasn't updated in `__init__.py` of the module? Not sure.



Discussion:
This fixes it:

```diff
diff --git a/src/werkzeug/__init__.py b/src/werkzeug/__init__.py
index 952cd751..0a828bf2 100644
--- a/src/werkzeug/__init__.py
+++ b/src/werkzeug/__init__.py
@@ -66,8 +66,6 @@ all_by_module = {
         "redirect",
         "cached_property",
         "import_string",
-        "dump_cookie",
-        "parse_cookie",
         "unescape",
         "format_string",
         "find_modules",
@@ -121,6 +119,8 @@ all_by_module = {
     ],
     "werkzeug.useragents": ["UserAgent"],
     "werkzeug.http": [
+        "dump_cookie",
+        "parse_cookie",
         "parse_etags",
         "parse_date",
         "http_date",
```
Thanks, looks like an oversight while deprecating some old names. See #2 for getting rid of this whole import shim.
So what's the plan, are you pushing a fix?

Resolving patch:
diff --git a/src/werkzeug/__init__.py b/src/werkzeug/__init__.py
--- a/src/werkzeug/__init__.py
+++ b/src/werkzeug/__init__.py
@@ -66,8 +66,6 @@
         "redirect",
         "cached_property",
         "import_string",
-        "dump_cookie",
-        "parse_cookie",
         "unescape",
         "format_string",
         "find_modules",
@@ -146,6 +144,8 @@
         "unquote_header_value",
         "quote_header_value",
         "HTTP_STATUS_CODES",
+        "dump_cookie",
+        "parse_cookie",
     ],
     "werkzeug.wrappers": [
         "BaseResponse",


Test patch: