internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: aio-libs/aiohttp

Issue:
[0.21 regression] Broken support for handling ~/path in static file routes
That's regression introduced in #743



Discussion:

Resolving patch:
diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py
--- a/aiohttp/web_urldispatcher.py
+++ b/aiohttp/web_urldispatcher.py
@@ -440,7 +440,10 @@ def __init__(self, name, prefix, directory, *,
         self._prefix = prefix
         self._prefix_len = len(self._prefix)
         try:
-            directory = Path(directory).resolve()
+            directory = Path(directory)
+            if str(directory).startswith('~'):
+                directory = Path(os.path.expanduser(str(directory)))
+            directory = directory.resolve()
             if not directory.is_dir():
                 raise ValueError('Not a directory')
         except (FileNotFoundError, ValueError) as error:


Test patch:
diff --git a/tests/test_urldispatch.py b/tests/test_urldispatch.py
--- a/tests/test_urldispatch.py
+++ b/tests/test_urldispatch.py
@@ -874,3 +874,17 @@ def test_resources_abc(self):
         self.assertIsInstance(self.router.resources(), Sized)
         self.assertIsInstance(self.router.resources(), Iterable)
         self.assertIsInstance(self.router.resources(), Container)
+
+    def test_static_route_user_home(self):
+        here = pathlib.Path(aiohttp.__file__).parent
+        home = pathlib.Path(os.path.expanduser('~'))
+        if not str(here).startswith(str(home)):  # pragma: no cover
+            self.skipTest("aiohttp folder is not placed in user's HOME")
+        static_dir = '~/' + str(here.relative_to(home))
+        route = self.router.add_static('/st', static_dir)
+        self.assertEqual(here, route.get_info()['directory'])
+
+    def test_static_route_points_to_file(self):
+        here = pathlib.Path(aiohttp.__file__).parent / '__init__.py'
+        with self.assertRaises(ValueError):
+            self.router.add_static('/st', here)
Repository: aio-libs/aiohttp

Issue:
Add helper method to raise an exception for error status codes
## Long story short

aiohttp should provide a helper's method to check for errors in responses. Something like request's `raise_for_error`.



Discussion:
Something like:

```
ClientResponse:
    …
    def raise_for_status(self):
         http_error_msg = ''

        if 400 <= self.status< 500:
            http_error_msg = 'Client Error: {}'.format(self.reason)
        elif 500 <= self.status < 600:
            http_error_msg = 'Server Error: {}'.format(self.reason)

        if http_error_msg:
            raise aiohttp.HttpProcessingError(code=self.status, message=http_error_msg)
```

(borrowed from requests)

Good to have. 
Not sure about exact exception type though.


Resolving patch:
diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py
--- a/aiohttp/client_reqrep.py
+++ b/aiohttp/client_reqrep.py
@@ -691,6 +691,12 @@ def release(self):
                 self._connection = None
             self._cleanup_writer()
 
+    def raise_for_status(self):
+        if 400 <= self.status:
+            raise aiohttp.HttpProcessingError(
+                code=self.status,
+                message=self.reason)
+
     def _cleanup_writer(self):
         if self._writer is not None and not self._writer.done():
             self._writer.cancel()


Test patch:
diff --git a/tests/test_client_response.py b/tests/test_client_response.py
--- a/tests/test_client_response.py
+++ b/tests/test_client_response.py
@@ -274,3 +274,16 @@ def test_close_deprecated(self):
         with self.assertWarns(DeprecationWarning):
             self.response.close(force=False)
         self.assertIsNone(self.response._connection)
+
+    def test_raise_for_status_2xx(self):
+        self.response.status = 200
+        self.response.reason = 'OK'
+        self.response.raise_for_status()  # should not raise
+
+    def test_raise_for_status_4xx(self):
+        self.response.status = 409
+        self.response.reason = 'CONFLICT'
+        with self.assertRaises(aiohttp.HttpProcessingError) as cm:
+            self.response.raise_for_status()
+        self.assertEqual(str(cm.exception.code), '409')
+        self.assertEqual(str(cm.exception.message), "CONFLICT")
Repository: aio-libs/aiohttp

Issue:
Encoding is always UTF-8 in POST data
## Long story short

I'm doing a `POST` request via `client.post`:

```
data = aiohttp.FormData({
            'FindText': name,
        }, charset='windows-1251')

client.post(base_url, params={'RowFrom': offset}, data=data)
```

where `name` contains some none-latin text (`'хан'`)

## Expected behaviour

POST data should contain: `FindText=%D5%E0%ED`

## Actual behaviour

`FindText=%D1%85%D0%B0%D0%BD'`

## Steps to reproduce

Looking through the code of `formdata.py:99`

```
urlencode(data, doseq=True).encode(charset),
```

I noticed, that `data` is urlencoded in UTF-8 first and then encoded to `windows-1251` (and that has no effect on `%D1...`).

For now, I just manually do in my code:

```
data = urlencode({
            'FindText': name,
        }, encoding='windows-1251')
```

And I get the string that I need.

Is it a bug? Or am I doing it wrong?

## Your environment

```
Python 3.6.0 (default, Jan 16 2017, 12:12:55) 
[GCC 6.3.1 20170109] on linux
---
aiohttp==2.0.3
```



Discussion:

Resolving patch:
diff --git a/aiohttp/formdata.py b/aiohttp/formdata.py
--- a/aiohttp/formdata.py
+++ b/aiohttp/formdata.py
@@ -96,7 +96,7 @@ def _gen_form_urlencoded(self):
 
         charset = self._charset if self._charset is not None else 'utf-8'
         return payload.BytesPayload(
-            urlencode(data, doseq=True).encode(charset),
+            urlencode(data, doseq=True, encoding=charset).encode(),
             content_type='application/x-www-form-urlencoded')
 
     def _gen_form_data(self):


Test patch:
Repository: aio-libs/aiohttp

Issue:
Sub app middlewares are called before main app, 
## Long story short

According to http://aiohttp.readthedocs.io/en/stable/web.html#nested-applications,
`[I]f URL is '/admin/something' middlewares from [main application] are applied first and [sub application] middlewares are the next in the call chain.` 

It turns out it's actually the other way.

## Steps to reproduce

Run this code saved as foo.py:
```python
#!/usr/bin/python3 -tt

import aiohttp
import aiohttp.web

async def middleware1(app, next_handler):

    async def handler(request):
        print("middleware1")
        return await next_handler(request)

    return handler
    
async def middleware2(app, next_handler):

    async def handler(request):
        print("middleware2")
        return await next_handler(request)

    return handler

async def middleware3(app, next_handler):

    async def handler(request):
        print("middleware3")
        return await next_handler(request)

    return handler

app = aiohttp.web.Application(
    middlewares=[middleware1])

subapp1 = aiohttp.web.Application(middlewares=[middleware2])
app.add_subapp("/foo", subapp1)

subapp2 = aiohttp.web.Application(middlewares=[middleware3])
subapp1.add_subapp("/foo/bar", subapp2)

aiohttp.web.run_app(app, host='127.0.0.1', port=4096)
```

And the run `wget http://127.0.0.1:4096/foo/bar`.

### Current Result:
```
$ python3 foo.py
======== Running on http://127.0.0.1:4096 ========
(Press CTRL+C to quit)
middleware3
middleware2
middleware1
```

### Expected output:
```
$ python3 foo.py
======== Running on http://127.0.0.1:4096 ========
(Press CTRL+C to quit)
middleware1
middleware2
middleware3
```

## Your environment

Ubuntu Xenial (python 3.5.2) with aiohttp 2.0.7



Discussion:

Resolving patch:
diff --git a/aiohttp/web.py b/aiohttp/web.py
--- a/aiohttp/web.py
+++ b/aiohttp/web.py
@@ -288,7 +288,7 @@ def _handle(self, request):
 
         if resp is None:
             handler = match_info.handler
-            for app in match_info.apps:
+            for app in match_info.apps[::-1]:
                 for factory in app._middlewares:
                     handler = yield from factory(app, handler)
 


Test patch:
Repository: aio-libs/aiohttp

Issue:
In unit tests, Application comparisons can report false positive
Comparison between Application is performed at the MutableMapping level. MutableMapping says that, like dict objects, if all keys and matching values are the same 2 instances, then they are equals. This means that `web.Application() == web.Application()` will return True.

See:

```python
>>> a = aiohttp.web.Application()
>>> b = aiohttp.web.Application()
>>> a == b
True
>>> a["foo"] = "bar"
>>> a == b
False
>>> b["foo"] = "bar"
>>> a == b
True
```

I think those few unit tests are assuming a different behaviour:
* test_subapp_middlewares
* test_subapp_on_response_prepare
* test_subapp_on_startup
* test_subapp_on_shutdown
* test_subapp_on_cleanup

A change has been submitted for `test_subapp_middlewares` in #1854 to fix that. While the solution may or may not be accepted as is, all tests should be fixed.

Also, maybe an additional `test_application_equal` should be implemented, to be ensure expected behavior. Unless `web.Application.__eq__` special method gets implemented to change current behaviour, it should look like something like that:

```python
def test_application_equal():
    app1 = web.Application()
    app2 = web.Application()
    assert app1 == app2
    app1["foo"] = "bar"
    assert app1 != app2
```


Discussion:
You've raised an interesting problem.
1. We could leave apps comparison as is but this way will constantly produce many subtle bugs.
2. On other hand we could forbid applications comparison in the way that apps are equal if and only if they are the same (`app1 is app2`). Ordering should be fixed as well.

I'm inclining to option 2 (all potentially broken tests should be fixed as well).
I believe the change will not affect our users: most likely the only case for app comparison is test writing.
For the record (and as far as my opinion matters), I would agree with option 2.

Resolving patch:
diff --git a/aiohttp/web.py b/aiohttp/web.py
--- a/aiohttp/web.py
+++ b/aiohttp/web.py
@@ -81,6 +81,9 @@ def __init__(self, *,
 
     # MutableMapping API
 
+    def __eq__(self, other):
+        return self is other
+
     def __getitem__(self, key):
         return self._state[key]
 


Test patch:
diff --git a/tests/test_web_application.py b/tests/test_web_application.py
--- a/tests/test_web_application.py
+++ b/tests/test_web_application.py
@@ -219,3 +219,11 @@ def test_secure_proxy_ssl_header_init(loop):
     assert app._secure_proxy_ssl_header is hdr
     app.make_handler(loop=loop)
     assert app._secure_proxy_ssl_header is hdr
+
+
+def test_equality():
+    app1 = web.Application()
+    app2 = web.Application()
+
+    assert app1 == app1
+    assert app1 != app2
Repository: aio-libs/aiohttp

Issue:
Deprecate app.on_loop_available signal
From my understanding on `app.on_startup` the loop is already present.
Why do we need additional signal?
`app.on_loop_available` is not documented BTW
@fafhrd91 ?


Discussion:
I think this was for config and runtime separation. I am not sure if it is useful, we can deprecate any of those.
+1 to deprecate and remove `on_loop_available`, don't see how it could be useful. 
It looks like `on_loop_available` happens outside running the event loop, right? That's useful for doing blocking setup or interacting with third party packages that use the various run methods on the loop to do work. I've run into an issue where a package tries to use the loop to do something but it's already running so it just crashes. 
> It looks like on_loop_available happens outside running the event loop, right?

No, there is no such guarantee.

If you need to do something blocking etc -- please do it before `run_app()` call.
Ah, in that case, yeah it should probably go the way of the dodo. 

Resolving patch:
diff --git a/aiohttp/web.py b/aiohttp/web.py
--- a/aiohttp/web.py
+++ b/aiohttp/web.py
@@ -199,6 +199,8 @@ def add_subapp(self, prefix, subapp):
 
     @property
     def on_loop_available(self):
+        warnings.warn("on_loop_available is deprecated and will be removed",
+                      DeprecationWarning, stacklevel=2)
         return self._on_loop_available
 
     @property


Test patch:
diff --git a/tests/test_web_application.py b/tests/test_web_application.py
--- a/tests/test_web_application.py
+++ b/tests/test_web_application.py
@@ -50,7 +50,8 @@ def test_on_loop_available(loop):
     app = web.Application()
 
     cb = mock.Mock()
-    app.on_loop_available.append(cb)
+    with pytest.warns(DeprecationWarning):
+        app.on_loop_available.append(cb)
 
     app._set_loop(loop)
     cb.assert_called_with(app)
Repository: aio-libs/aiohttp

Issue:
Warnings should provide a stacklevel
## Long story short

Warnings have useless frame/position information.

## Expected behaviour

Warnings should point to *my* code so I know what to fix.

## Actual behaviour

Warnings point to aiohttp’s lines where the warnings are raised (like `aiohttp/helpers.py:129: DeprecationWarning: ClientSession.close() is a coroutine`) which makes them rather useless.  That’s because warnings.warn is called with the default stacklevel value of 1.  It’s a rather unknown option but makes the warnings incredibly more useful. :)  See also: https://docs.python.org/3/library/warnings.html#warnings.warn for an example.


Discussion:
would you create PR?

Resolving patch:
diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py
--- a/aiohttp/helpers.py
+++ b/aiohttp/helpers.py
@@ -106,6 +106,11 @@ def __await__(self):
 
 
 class _CoroGuard(_BaseCoroMixin):
+    """Only to be used with func:`deprecated_noop`.
+
+    Otherwise the stack information in the raised warning doesn't line up with
+    the user's code anymore.
+    """
     __slots__ = ('_msg', '_awaited')
 
     def __init__(self, coro, msg):
@@ -126,7 +131,7 @@ def __await__(self):
     def __del__(self):
         self._coro = None
         if not self._awaited:
-            warnings.warn(self._msg, DeprecationWarning)
+            warnings.warn(self._msg, DeprecationWarning, stacklevel=2)
 
 
 coroutines = asyncio.coroutines


Test patch:
diff --git a/tests/test_client_session.py b/tests/test_client_session.py
--- a/tests/test_client_session.py
+++ b/tests/test_client_session.py
@@ -64,9 +64,12 @@ def test_close_coro(create_session, loop):
 def test_close_deprecated(create_session):
     session = create_session()
 
-    with pytest.warns(DeprecationWarning):
+    with pytest.warns(DeprecationWarning) as ctx:
         session.close()
 
+    # Assert the warning points at us and not at _CoroGuard.
+    assert ctx.list[0].filename == __file__
+
 
 def test_init_headers_simple_dict(create_session):
     session = create_session(headers={"h1": "header1",
diff --git a/tests/test_helpers.py b/tests/test_helpers.py
--- a/tests/test_helpers.py
+++ b/tests/test_helpers.py
@@ -17,7 +17,12 @@
 def test_warn():
     with pytest.warns(DeprecationWarning) as ctx:
         helpers.deprecated_noop('Text')
-    assert str(ctx.list[0].message) == 'Text'
+
+    w = ctx.list[0]
+
+    assert str(w.message) == 'Text'
+    # Assert the warning points at us and not at _CoroGuard.
+    assert w.filename == __file__
 
 
 @asyncio.coroutine
Repository: aio-libs/aiohttp

Issue:
Server request's state is not cloned.
`req.clone()` call doesn't clone request state, changes made by `req['key'] = val` are not propagated to clone.

The solution is calling `req._state.copy()` in `clone`


Discussion:

Resolving patch:
diff --git a/aiohttp/web_request.py b/aiohttp/web_request.py
--- a/aiohttp/web_request.py
+++ b/aiohttp/web_request.py
@@ -62,7 +62,10 @@ class BaseRequest(collections.MutableMapping, HeadersMixin):
 
     def __init__(self, message, payload, protocol, writer, time_service, task,
                  loop,
-                 *, secure_proxy_ssl_header=None, client_max_size=1024**2):
+                 *, secure_proxy_ssl_header=None, client_max_size=1024**2,
+                 state=None):
+        if state is None:
+            state = {}
         self._message = message
         self._protocol = protocol
         self._transport = protocol.transport
@@ -78,7 +81,7 @@ def __init__(self, message, payload, protocol, writer, time_service, task,
 
         self._secure_proxy_ssl_header = secure_proxy_ssl_header
         self._time_service = time_service
-        self._state = {}
+        self._state = state
         self._cache = {}
         self._task = task
         self._client_max_size = client_max_size
@@ -120,7 +123,8 @@ def clone(self, *, method=sentinel, rel_url=sentinel,
             self._time_service,
             self._task,
             self._loop,
-            secure_proxy_ssl_header=self._secure_proxy_ssl_header)
+            secure_proxy_ssl_header=self._secure_proxy_ssl_header,
+            state=self._state.copy())
 
     @property
     def task(self):


Test patch:
diff --git a/tests/test_web_request.py b/tests/test_web_request.py
--- a/tests/test_web_request.py
+++ b/tests/test_web_request.py
@@ -620,3 +620,12 @@ def test_remote_peername_forwarded_overrides_x_forwarded():
         headers={'Forwarded': 'for=11.11.11.11, for=12.12.12.12',
                  'X-Forwarded-For': '13.13.13.13'})
     assert req.remote == '11.11.11.11'
+
+
+def test_save_state_on_clone():
+    req = make_mocked_request('GET', '/')
+    req['key'] = 'val'
+    req2 = req.clone()
+    req2['key'] = 'val2'
+    assert req['key'] == 'val'
+    assert req2['key'] == 'val2'
Repository: aio-libs/aiohttp

Issue:
Sync request() and ws_connect()
`ClientSession.request()` along with `.get()` etc. accepts SSL related params: `verify_ssl`, `fingerprint`, `ssl_context` and `proxy_headers` for per-request proxy support.
Unfortunately `.wc_connect()` doesn't support these params


Discussion:

Resolving patch:
diff --git a/aiohttp/client.py b/aiohttp/client.py
--- a/aiohttp/client.py
+++ b/aiohttp/client.py
@@ -372,6 +372,10 @@ def ws_connect(self, url, *,
                    headers=None,
                    proxy=None,
                    proxy_auth=None,
+                   verify_ssl=None,
+                   fingerprint=None,
+                   ssl_context=None,
+                   proxy_headers=None,
                    compress=0):
         """Initiate websocket connection."""
         return _WSRequestContextManager(
@@ -387,6 +391,10 @@ def ws_connect(self, url, *,
                              headers=headers,
                              proxy=proxy,
                              proxy_auth=proxy_auth,
+                             verify_ssl=verify_ssl,
+                             fingerprint=fingerprint,
+                             ssl_context=ssl_context,
+                             proxy_headers=proxy_headers,
                              compress=compress))
 
     @asyncio.coroutine
@@ -402,6 +410,10 @@ def _ws_connect(self, url, *,
                     headers=None,
                     proxy=None,
                     proxy_auth=None,
+                    verify_ssl=None,
+                    fingerprint=None,
+                    ssl_context=None,
+                    proxy_headers=None,
                     compress=0):
 
         if headers is None:
@@ -433,7 +445,11 @@ def _ws_connect(self, url, *,
                                    read_until_eof=False,
                                    auth=auth,
                                    proxy=proxy,
-                                   proxy_auth=proxy_auth)
+                                   proxy_auth=proxy_auth,
+                                   verify_ssl=verify_ssl,
+                                   fingerprint=fingerprint,
+                                   ssl_context=ssl_context,
+                                   proxy_headers=proxy_headers)
 
         try:
             # check handshake


Test patch:
Repository: aio-libs/aiohttp

Issue:
Drop resovle param from TCPConnector
It was deprecated in aiohttp 1.x


Discussion:
I do this!

Resolving patch:
diff --git a/aiohttp/connector.py b/aiohttp/connector.py
--- a/aiohttp/connector.py
+++ b/aiohttp/connector.py
@@ -563,8 +563,6 @@ class TCPConnector(BaseConnector):
         digest of the expected certificate in DER format to verify
         that the certificate the server presents matches. See also
         https://en.wikipedia.org/wiki/Transport_Layer_Security#Certificate_pinning
-    resolve - (Deprecated) Set to True to do DNS lookup for
-        host name.
     resolver - Enable DNS lookups and use this
         resolver
     use_dns_cache - Use memory cache for DNS lookups.
@@ -581,7 +579,7 @@ class TCPConnector(BaseConnector):
     """
 
     def __init__(self, *, verify_ssl=True, fingerprint=None,
-                 resolve=sentinel, use_dns_cache=True, ttl_dns_cache=10,
+                 use_dns_cache=True, ttl_dns_cache=10,
                  family=0, ssl_context=None, local_addr=None,
                  resolver=None, keepalive_timeout=sentinel,
                  force_close=False, limit=100, limit_per_host=0,


Test patch:
diff --git a/tests/test_client_functional.py b/tests/test_client_functional.py
--- a/tests/test_client_functional.py
+++ b/tests/test_client_functional.py
@@ -744,7 +744,7 @@ async def test_HTTP_200_OK_METHOD_connector(loop, test_client):
     async def handler(request):
         return web.Response(text=request.method)
 
-    conn = aiohttp.TCPConnector(resolve=True, loop=loop)
+    conn = aiohttp.TCPConnector(loop=loop)
     conn.clear_dns_cache()
 
     app = web.Application()
Repository: aio-libs/aiohttp

Issue:
Drop deprecated request.GET property
For sake of `request.query`


Discussion:

Resolving patch:
diff --git a/aiohttp/web_request.py b/aiohttp/web_request.py
--- a/aiohttp/web_request.py
+++ b/aiohttp/web_request.py
@@ -356,13 +356,6 @@ def query(self):
         """A multidict with all the variables in the query string."""
         return self._rel_url.query
 
-    @property
-    def GET(self):
-        """A multidict with all the variables in the query string."""
-        warnings.warn("GET property is deprecated, use .query instead",
-                      DeprecationWarning)
-        return self._rel_url.query
-
     @property
     def query_string(self):
         """The query string in the URL.


Test patch:
diff --git a/tests/test_web_request.py b/tests/test_web_request.py
--- a/tests/test_web_request.py
+++ b/tests/test_web_request.py
@@ -47,9 +47,6 @@ def test_ctor():
     assert req.raw_headers == ((b'FOO', b'bar'),)
     assert req.task is req._task
 
-    with pytest.warns(DeprecationWarning):
-        assert req.GET is req.query
-
 
 def test_doubleslashes():
     # NB: //foo/bar is an absolute URL with foo netloc and /bar path