Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__repr__ only returns ascii encodable characters #899

Merged
merged 2 commits into from
Jun 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,17 @@ def __del__(self, _warnings=warnings):

def __repr__(self):
out = io.StringIO()
ascii_encodable_url = self.url.encode('ascii', 'backslashreplace') \
.decode('ascii')
if self.reason:
ascii_encodable_reason = self.reason.encode('ascii',
'backslashreplace') \
.decode('ascii')
else:
ascii_encodable_reason = self.reason
print('<ClientResponse({}) [{} {}]>'.format(
self.url, self.status, self.reason), file=out)
ascii_encodable_url, self.status, ascii_encodable_reason),
file=out)
print(self.headers, file=out)
return out.getvalue()

Expand Down
4 changes: 3 additions & 1 deletion aiohttp/web_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,10 @@ def copy(self):
raise NotImplementedError

def __repr__(self):
ascii_encodable_path = self.path.encode('ascii', 'backslashreplace') \
.decode('ascii')
return "<{} {} {} >".format(self.__class__.__name__,
self.method, self.path)
self.method, ascii_encodable_path)


############################################################
Expand Down
15 changes: 15 additions & 0 deletions tests/test_client_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ def test_repr(self):
'<ClientResponse(http://def-cl-resp.org) [200 Ok]>',
repr(self.response))

def test_repr_non_ascii_url(self):
response = ClientResponse('get', 'http://fake-host.org/\u03bb')
self.assertIn(
"<ClientResponse(http://fake-host.org/\\u03bb) [None None]>",
repr(response))
response.close()

def test_repr_non_ascii_reason(self):
response = ClientResponse('get', 'http://fake-host.org/path')
response.reason = '\u03bb'
self.assertIn(
"<ClientResponse(http://fake-host.org/path) [None \\u03bb]>",
repr(response))
response.close()

def test_read_and_release_connection(self):
def side_effect(*args, **kwargs):
fut = asyncio.Future(loop=self.loop)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ def test___repr__(make_request):
assert "<Request GET /path/to >" == repr(req)


def test___repr___non_ascii_path(make_request):
req = make_request('GET', '/path/\U0001f415\U0001f308')
assert "<Request GET /path/\\U0001f415\\U0001f308 >" == repr(req)


def test_http_scheme(make_request):
req = make_request('GET', '/')
assert "http" == req.scheme
Expand Down