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

Update supported Python versions #108

Merged
merged 11 commits into from
Oct 7, 2023
22 changes: 14 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,27 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-20.04, windows-2019, macos-10.15]
python-version: [ '3.6', '3.7', '3.8', '3.9', '3.10-dev', 'pypy-3.6', 'pypy-3.7' ]
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: [ '3.8', '3.9', '3.10', '3.11', '3.12', 'pypy-3.8', 'pypy-3.9', 'pypy-3.10' ]
exclude:
- os: windows-2019
python-version: 3.10-dev
- os: macos-latest
python-version: 3.8
- os: macos-latest
python-version: 3.9
- os: macos-latest
python-version: pypy-3.8
- os: macos-latest
python-version: pypy-3.9
- os: macos-latest
python-version: pypy-3.10
steps:
- uses: actions/checkout@v2
with:
submodules: true
- uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install wheel
run: pip install wheel
run: pip install setuptools wheel
- name: Build package
run: python setup.py install
- name: Run tests
Expand Down
11 changes: 6 additions & 5 deletions .github/workflows/release-wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ on:
jobs:
build_wheels:
name: Build wheels
runs-on: ubuntu-20.04
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: actions/setup-python@v2
name: Install Python
with:
python-version: '3.8'
- run: pip install wheel
- run: pip install setuptools wheel
- name: Build wheels
run: python setup.py bdist_wheel
- uses: actions/upload-artifact@v2
Expand All @@ -25,13 +25,14 @@ jobs:

build_sdist:
name: Build source distribution
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
name: Install Python
with:
python-version: '3.8'
- run: pip install setuptools
- name: Build sdist
run: python setup.py sdist
- uses: actions/upload-artifact@v2
Expand All @@ -40,7 +41,7 @@ jobs:

upload_pypi:
needs: [build_wheels, build_sdist]
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
# upload to PyPI when a GitHub Release is created
if: github.event_name == 'release' && github.event.action == 'published'
steps:
Expand Down
15 changes: 15 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ The API is pretty simple, three functions are provided in the ``DNSResolver`` cl
``ARES_ECANCELLED`` errno.


Note for Windows users
======================

This library requires the asyncio loop to be a `SelectorEventLoop`, which is not the default on Windows since
Python 3.8.

The default can be changed as follows (do this very early in your application):

.. code:: python

asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

This may have other implications for the rest of your codebase, so make sure to test thoroughly.


Running the test suite
======================

Expand Down
16 changes: 10 additions & 6 deletions aiodns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import functools
import pycares
import socket
import sys

from typing import (
Any,
List,
Optional,
Set,
Union
Sequence
)

from . import error
Expand Down Expand Up @@ -45,11 +45,15 @@
}

class DNSResolver:
def __init__(self, nameservers: Optional[List[str]] = None,
def __init__(self, nameservers: Optional[Sequence[str]] = None,
loop: Optional[asyncio.AbstractEventLoop] = None,
**kwargs: Any) -> None:
self.loop = loop or asyncio.get_event_loop()
assert self.loop is not None
if sys.platform == 'win32':
if not isinstance(self.loop, asyncio.SelectorEventLoop):
raise RuntimeError(
'aiodns needs a SelectorEventLoop on Windows. See more: https://github.com/saghul/aiodns/issues/86')
kwargs.pop('sock_state_cb', None)
self._channel = pycares.Channel(sock_state_cb=self._sock_state_cb, **kwargs)
if nameservers:
Expand All @@ -59,11 +63,11 @@ def __init__(self, nameservers: Optional[List[str]] = None,
self._timer = None # type: Optional[asyncio.TimerHandle]

@property
def nameservers(self) -> List[Union[str, bytes]]:
def nameservers(self) -> Sequence[str]:
return self._channel.servers

@nameservers.setter
def nameservers(self, value: List[str]) -> None:
def nameservers(self, value: Sequence[str]) -> None:
self._channel.servers = value

@staticmethod
Expand All @@ -75,7 +79,7 @@ def _callback(fut: asyncio.Future, result: Any, errorno: int) -> None:
else:
fut.set_result(result)

def query(self, host: str, qtype: str, qclass: str=None) -> asyncio.Future:
def query(self, host: str, qtype: str, qclass: Optional[str]=None) -> asyncio.Future:
try:
qtype = query_type_map[qtype]
except KeyError:
Expand Down
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ def get_version():
"Operating System :: Microsoft :: Windows",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9"
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12"
]
)
11 changes: 7 additions & 4 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

class DNSTest(unittest.TestCase):
def setUp(self):
if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
self.loop = asyncio.new_event_loop()
self.addCleanup(self.loop.close)
self.resolver = aiodns.DNSResolver(loop=self.loop, timeout=5.0)
Expand Down Expand Up @@ -135,6 +137,7 @@ def test_gethostbyname(self):
result = self.loop.run_until_complete(f)
self.assertTrue(result)

@unittest.skipIf(sys.platform == 'win32', 'skipped on Windows')
def test_gethostbyaddr(self):
f = self.resolver.gethostbyaddr('127.0.0.1')
result = self.loop.run_until_complete(f)
Expand All @@ -150,10 +153,10 @@ def test_gethostbyname_bad_family(self):
with self.assertRaises(aiodns.error.DNSError):
self.loop.run_until_complete(f)

def test_query_bad_chars(self):
f = self.resolver.query('xn--cardeosapeluqueros-r0b.com', 'MX')
result = self.loop.run_until_complete(f)
self.assertTrue(result)
# def test_query_bad_chars(self):
# f = self.resolver.query('xn--cardeosapeluqueros-r0b.com', 'MX')
# result = self.loop.run_until_complete(f)
# self.assertTrue(result)


if __name__ == '__main__':
Expand Down