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

added bytes conversion #582

Merged
merged 4 commits into from
Oct 5, 2021
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
1 change: 1 addition & 0 deletions CHANGES/582.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `__bytes__()` magic method so that `bytes(url)` will work and use optimal ASCII encoding.
8 changes: 8 additions & 0 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,14 @@ def test_from_non_ascii_path():
) == str(url)


def test_bytes():
url = URL("http://example.com/путь/туда")
assert (
b"http://example.com/%D0%BF%D1%83%D1%82%D1%8C/%D1%82%D1%83%D0%B4%D0%B0"
== bytes(url)
)


def test_from_ascii_query_parts():
url = URL(
"http://example.com/"
Expand Down
3 changes: 3 additions & 0 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ def __str__(self):
def __repr__(self):
return "{}('{}')".format(self.__class__.__name__, str(self))

def __bytes__(self):
return str(self).encode("ascii")
asvetlov marked this conversation as resolved.
Show resolved Hide resolved

def __eq__(self, other):
if not type(other) is URL:
return NotImplemented
Expand Down