-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: improve type safety and error handling #12254
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
Open
N3XT3R1337
wants to merge
1
commit into
aio-libs:master
Choose a base branch
from
N3XT3R1337:fix/improve-code-quality
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Fixed an off-by-one error in :class:`~aiohttp.resolver.AsyncResolver` that caused | ||
| an :exc:`IndexError` when handling :exc:`aiodns.error.DNSError` exceptions with | ||
| a single argument. Also fixed the resolver not extracting the port from | ||
| ``getnameinfo()`` results for link-local IPv6 addresses, which could lead to | ||
| incorrect port values in resolved addresses. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -105,9 +105,9 @@ def __init__(self, hosts: Collection[str]) -> None: | |||||
|
|
||||||
|
|
||||||
| class FakeAIODNSNameInfoIPv6Result: | ||||||
| def __init__(self, host: str) -> None: | ||||||
| def __init__(self, host: str, service: str = "0") -> None: | ||||||
| self.node = host | ||||||
| self.service = None | ||||||
| self.service = service | ||||||
|
|
||||||
|
|
||||||
| async def fake_aiodns_getaddrinfo_ipv4_result( | ||||||
|
|
@@ -123,9 +123,9 @@ async def fake_aiodns_getaddrinfo_ipv6_result( | |||||
|
|
||||||
|
|
||||||
| async def fake_aiodns_getnameinfo_ipv6_result( | ||||||
| host: str, | ||||||
| host: str, service: str = "0" | ||||||
| ) -> FakeAIODNSNameInfoIPv6Result: | ||||||
| return FakeAIODNSNameInfoIPv6Result(host) | ||||||
| return FakeAIODNSNameInfoIPv6Result(host, service=service) | ||||||
|
|
||||||
|
|
||||||
| def fake_addrinfo(hosts: Collection[str]) -> Callable[..., Awaitable[_AddrInfo4]]: | ||||||
|
|
@@ -209,6 +209,27 @@ async def test_async_resolver_positive_link_local_ipv6_lookup( | |||||
| type=socket.SOCK_STREAM, | ||||||
| ) | ||||||
| mock().getnameinfo.assert_called_with(("fe80::1", 0, 0, 3), _NAME_SOCKET_FLAGS) | ||||||
| assert real[0]["port"] == 0 | ||||||
| await resolver.close() | ||||||
|
|
||||||
|
|
||||||
| @pytest.mark.skipif(not getaddrinfo, reason="aiodns >=3.2.0 required") | ||||||
| @pytest.mark.usefixtures("check_no_lingering_resolvers") | ||||||
| async def test_async_resolver_link_local_ipv6_port_from_getnameinfo( | ||||||
| loop: asyncio.AbstractEventLoop, | ||||||
| ) -> None: | ||||||
| """Ensure the port is correctly extracted from getnameinfo for link-local IPv6.""" | ||||||
| with patch("aiodns.DNSResolver") as mock: | ||||||
| mock().getaddrinfo.return_value = fake_aiodns_getaddrinfo_ipv6_result( | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can probably skip this by patching the AsyncResolver instance below, but generally we don't want to add any calls to a mock, so something like this would be:
Suggested change
|
||||||
| ["fe80::1"] | ||||||
| ) | ||||||
| mock().getnameinfo.return_value = fake_aiodns_getnameinfo_ipv6_result( | ||||||
| "fe80::1%eth0", service="8080" | ||||||
| ) | ||||||
| resolver = AsyncResolver() | ||||||
| real = await resolver.resolve("www.python.org") | ||||||
| assert real[0]["port"] == 8080 | ||||||
| assert real[0]["host"] == "fe80::1%eth0" | ||||||
| await resolver.close() | ||||||
|
|
||||||
|
|
||||||
|
|
@@ -400,6 +421,22 @@ async def test_async_resolver_error_messages_passed( | |||||
| await resolver.close() | ||||||
|
|
||||||
|
|
||||||
| @pytest.mark.skipif(not getaddrinfo, reason="aiodns >=3.2.0 required") | ||||||
| @pytest.mark.usefixtures("check_no_lingering_resolvers") | ||||||
| async def test_async_resolver_error_single_arg_dns_error( | ||||||
| loop: asyncio.AbstractEventLoop, | ||||||
| ) -> None: | ||||||
| """Ensure DNSError with a single arg does not cause IndexError.""" | ||||||
| with patch("aiodns.DNSResolver", autospec=True, spec_set=True) as mock: | ||||||
| mock().getaddrinfo.side_effect = aiodns.error.DNSError(1) | ||||||
| resolver = AsyncResolver() | ||||||
| with pytest.raises(OSError, match="DNS lookup failed") as excinfo: | ||||||
| await resolver.resolve("x.org") | ||||||
|
|
||||||
| assert excinfo.value.strerror == "DNS lookup failed" | ||||||
| await resolver.close() | ||||||
|
|
||||||
|
|
||||||
| @pytest.mark.skipif(not getaddrinfo, reason="aiodns >=3.2.0 required") | ||||||
| @pytest.mark.usefixtures("check_no_lingering_resolvers") | ||||||
| async def test_async_resolver_error_messages_passed_no_hosts( | ||||||
|
|
||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, it might make more sense to patch directly on the AsyncResolver instance?