-
-
Notifications
You must be signed in to change notification settings - Fork 73
Add pycares 5.0 support with backward-compatible result types #218
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
bdraco
wants to merge
43
commits into
master
Choose a base branch
from
pycares_5
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.
+1,568
−182
Open
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
5142603
Add support for pycares 5
bdraco 9d8a68e
Add support for pycares 5
bdraco 361fb62
Add support for pycares 5
bdraco 212aa30
fixes
bdraco db65ca3
fixes
bdraco 78e350d
fixes
bdraco ce2d3f4
fixes
bdraco 1cb6720
fixes
bdraco b264b62
fixes
bdraco dc2aab9
fixes
bdraco 5419903
compat tests
bdraco 4dd4f80
fixes
bdraco 741a22f
fixes
bdraco 2801db7
minimize change
bdraco bf68807
minimize change
bdraco f218c46
minimize change
bdraco 4a29cad
minimize change
bdraco 5061ac0
minimize change
bdraco f119bce
more typing fixes
bdraco a5b24b8
more typing fixes
bdraco 42b7419
more typing fixes
bdraco 3dc5199
more typing fixes
bdraco e5d7690
more typing fixes
bdraco 2e0af6f
socket state fallback path test
bdraco 824ea45
fix win32 test
bdraco 3a9fce0
more typing
bdraco a5ef8f7
add missing coverage
bdraco 84c4ed8
more windows test fixes
bdraco f8b5ce3
pycares no longer works with pypy
bdraco d3f7f69
make sure it works on 3.14t as well
bdraco ea162cd
fix missing coverage
bdraco b299d69
restore some lost coverage
bdraco fad285d
typing fixes
bdraco 6540001
restore some missing coverage
bdraco e98a218
restore some missing coverage
bdraco 450166d
missing classifer
bdraco 108ac66
tweaks
bdraco 2fc55b1
revert
bdraco 6f88df0
back compat for nameservers
bdraco 9428d46
deprecate query, add query_dns
bdraco c76550c
update docs
bdraco 68cb7a0
add some more cover
bdraco 2858193
add some more cover
bdraco 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
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 |
|---|---|---|
|
|
@@ -19,14 +19,13 @@ Example | |
| import asyncio | ||
| import aiodns | ||
|
|
||
| loop = asyncio.get_event_loop() | ||
| resolver = aiodns.DNSResolver(loop=loop) | ||
| async def main(): | ||
| resolver = aiodns.DNSResolver() | ||
| result = await resolver.query_dns('google.com', 'A') | ||
| for record in result.answer: | ||
| print(record.data.addr) | ||
|
|
||
| async def query(name, query_type): | ||
| return await resolver.query(name, query_type) | ||
|
|
||
| coro = query('google.com', 'A') | ||
| result = loop.run_until_complete(coro) | ||
| asyncio.run(main()) | ||
|
|
||
|
|
||
| The following query types are supported: A, AAAA, ANY, CAA, CNAME, MX, NAPTR, NS, PTR, SOA, SRV, TXT. | ||
|
|
@@ -37,27 +36,66 @@ API | |
|
|
||
| The API is pretty simple, the following functions are provided in the ``DNSResolver`` class: | ||
|
|
||
| * ``query(host, type)``: Do a DNS resolution of the given type for the given hostname. It returns an | ||
| instance of ``asyncio.Future``. The actual result of the DNS query is taken directly from pycares. | ||
| As of version 1.0.0 of aiodns (and pycares, for that matter) results are always namedtuple-like | ||
| objects with different attributes. Please check the `documentation | ||
| <http://pycares.readthedocs.org/latest/channel.html#pycares.Channel.query>`_ | ||
| for the result fields. | ||
| * ``query_dns(host, type)``: Do a DNS resolution of the given type for the given hostname. It returns an | ||
| instance of ``asyncio.Future``. The result is a ``pycares.DNSResult`` object with ``answer``, | ||
| ``authority``, and ``additional`` attributes containing lists of ``pycares.DNSRecord`` objects. | ||
| Each record has ``type``, ``ttl``, and ``data`` attributes. Check the `pycares documentation | ||
| <https://pycares.readthedocs.io/>`_ for details on the data attributes for each record type. | ||
| * ``query(host, type)``: **Deprecated** - use ``query_dns()`` instead. This method returns results | ||
| in a legacy format compatible with aiodns 3.x for backward compatibility. | ||
| * ``gethostbyname(host, socket_family)``: Do a DNS resolution for the given | ||
| hostname and the desired type of address family (i.e. ``socket.AF_INET``). | ||
| While ``query()`` always performs a request to a DNS server, | ||
| While ``query_dns()`` always performs a request to a DNS server, | ||
| ``gethostbyname()`` first looks into ``/etc/hosts`` and thus can resolve | ||
| local hostnames (such as ``localhost``). Please check `the documentation | ||
| <http://pycares.readthedocs.io/latest/channel.html#pycares.Channel.gethostbyname>`_ | ||
| for the result fields. The actual result of the call is a ``asyncio.Future``. | ||
| local hostnames (such as ``localhost``). The actual result of the call is a ``asyncio.Future``. | ||
| * ``gethostbyaddr(name)``: Make a reverse lookup for an address. | ||
| * ``getaddrinfo(host, family, port, proto, type, flags)``: Resolve a host and port into a list of | ||
| address info entries. | ||
| * ``getnameinfo(sockaddr, flags)``: Resolve a socket address to a host and port. | ||
| * ``cancel()``: Cancel all pending DNS queries. All futures will get ``DNSError`` exception set, with | ||
| ``ARES_ECANCELLED`` errno. | ||
| * ``close()``: Close the resolver. This releases all resources and cancels any pending queries. It must be called | ||
| when the resolver is no longer needed (e.g., application shutdown). The resolver should only be closed from the | ||
| event loop that created the resolver. | ||
|
|
||
|
|
||
| Migrating from aiodns 3.x | ||
| ========================= | ||
|
|
||
| aiodns 4.x introduces a new ``query_dns()`` method that returns native pycares 5.x result types. | ||
|
Contributor
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. Perhaps a link to the pycares docs where the result types are described would help? |
||
| The old ``query()`` method is deprecated but continues to work for backward compatibility. | ||
|
|
||
| .. code:: python | ||
|
|
||
| # Old API (deprecated) | ||
| result = await resolver.query('example.com', 'MX') | ||
| for record in result: | ||
| print(record.host, record.priority) | ||
|
|
||
| # New API (recommended) | ||
| result = await resolver.query_dns('example.com', 'MX') | ||
| for record in result.answer: | ||
| print(record.data.exchange, record.data.priority) | ||
|
|
||
|
|
||
| Future migration to aiodns 5.x | ||
| ------------------------------ | ||
|
|
||
| The temporary ``query_dns()`` naming allows gradual migration without breaking changes: | ||
|
|
||
| +-----------+---------------------------------------+--------------------------------------------+ | ||
| | Version | ``query()`` | ``query_dns()`` | | ||
| +===========+=======================================+============================================+ | ||
| | **4.x** | Deprecated, returns compat types | New API, returns pycares 5.x types | | ||
| +-----------+---------------------------------------+--------------------------------------------+ | ||
| | **5.x** | New API, returns pycares 5.x types | Alias to ``query()`` for back compat | | ||
| +-----------+---------------------------------------+--------------------------------------------+ | ||
|
|
||
| In aiodns 5.x, ``query()`` will become the primary API returning native pycares 5.x types, | ||
| and ``query_dns()`` will remain as an alias for backward compatibility. This allows downstream | ||
| projects to migrate at their own pace. | ||
|
|
||
|
|
||
| Async Context Manager Support | ||
| ============================= | ||
|
|
||
|
|
@@ -67,7 +105,7 @@ for scenarios where automatic cleanup is desired: | |
| .. code:: python | ||
|
|
||
| async with aiodns.DNSResolver() as resolver: | ||
| result = await resolver.query('example.com', 'A') | ||
| result = await resolver.query_dns('example.com', 'A') | ||
| # resolver.close() is called automatically when exiting the context | ||
|
|
||
| **Important**: This pattern is discouraged for most applications because ``DNSResolver`` instances | ||
|
|
@@ -101,7 +139,7 @@ This may have other implications for the rest of your codebase, so make sure to | |
| Running the test suite | ||
| ====================== | ||
|
|
||
| To run the test suite: ``python tests.py`` | ||
| To run the test suite: ``python -m pytest tests/`` | ||
|
|
||
|
|
||
| Author | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.