Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e9cc5c8

Browse filesBrowse files
authored
chore: prepare _engine.py to be able to be cythonized (#1219)
1 parent 12560a7 commit e9cc5c8
Copy full SHA for e9cc5c8

File tree

Expand file treeCollapse file tree

2 files changed

+31
-6
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+31
-6
lines changed

‎src/zeroconf/_engine.py

Copy file name to clipboardExpand all lines: src/zeroconf/_engine.py
+15-4Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ async def _async_create_endpoints(self) -> None:
150150
sender_sockets.append(s)
151151

152152
for s in reader_sockets:
153-
transport, protocol = await loop.create_datagram_endpoint(lambda: AsyncListener(self.zc), sock=s)
153+
transport, protocol = await loop.create_datagram_endpoint(
154+
lambda: AsyncListener(self.zc), sock=s # type: ignore[arg-type, return-value]
155+
)
154156
self.protocols.append(cast(AsyncListener, protocol))
155157
self.readers.append(_make_wrapped_transport(cast(asyncio.DatagramTransport, transport)))
156158
if s in sender_sockets:
@@ -198,7 +200,7 @@ def close(self) -> None:
198200
run_coro_with_timeout(self._async_close(), self.loop, _CLOSE_TIMEOUT)
199201

200202

201-
class AsyncListener(asyncio.Protocol, QuietLogger):
203+
class AsyncListener:
202204

203205
"""A Listener is used by this module to listen on the multicast
204206
group to which DNS messages are sent, allowing the implementation
@@ -207,7 +209,16 @@ class AsyncListener(asyncio.Protocol, QuietLogger):
207209
It requires registration with an Engine object in order to have
208210
the read() method called when a socket is available for reading."""
209211

210-
__slots__ = ('zc', 'data', 'last_time', 'transport', 'sock_description', '_deferred', '_timers')
212+
__slots__ = (
213+
'zc',
214+
'data',
215+
'last_time',
216+
'last_message',
217+
'transport',
218+
'sock_description',
219+
'_deferred',
220+
'_timers',
221+
)
211222

212223
def __init__(self, zc: 'Zeroconf') -> None:
213224
self.zc = zc
@@ -357,7 +368,7 @@ def error_received(self, exc: Exception) -> None:
357368
# different socket in case there are problems with multiple
358369
# sockets
359370
msg_str = f"Error with socket {self.sock_description}): %s"
360-
self.log_exception_once(exc, msg_str, exc)
371+
QuietLogger.log_exception_once(exc, msg_str, exc)
361372

362373
def connection_made(self, transport: asyncio.BaseTransport) -> None:
363374
wrapped_transport = _make_wrapped_transport(cast(asyncio.DatagramTransport, transport))

‎tests/test_engine.py

Copy file name to clipboardExpand all lines: tests/test_engine.py
+16-2Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
import logging
99
import unittest
1010
import unittest.mock
11-
from typing import Set
11+
from typing import Set, Tuple, Union
1212
from unittest.mock import MagicMock, patch
1313

1414
import pytest
1515

1616
import zeroconf as r
1717
from zeroconf import Zeroconf, _engine, const, current_time_millis
1818
from zeroconf._protocol import outgoing
19+
from zeroconf._protocol.incoming import DNSIncoming
1920
from zeroconf.asyncio import AsyncZeroconf
2021

2122
log = logging.getLogger('zeroconf')
@@ -188,7 +189,20 @@ def test_guard_against_duplicate_packets():
188189
These packets can quickly overwhelm the system.
189190
"""
190191
zc = Zeroconf(interfaces=['127.0.0.1'])
191-
listener = _engine.AsyncListener(zc)
192+
193+
class SubListener(_engine.AsyncListener):
194+
def handle_query_or_defer(
195+
self,
196+
msg: DNSIncoming,
197+
addr: str,
198+
port: int,
199+
transport: _engine._WrappedTransport,
200+
v6_flow_scope: Union[Tuple[()], Tuple[int, int]] = (),
201+
) -> None:
202+
"""Handle a query or defer it for later processing."""
203+
super().handle_query_or_defer(msg, addr, port, transport, v6_flow_scope)
204+
205+
listener = SubListener(zc)
192206
listener.transport = MagicMock()
193207

194208
query = r.DNSOutgoing(const._FLAGS_QR_QUERY, multicast=True)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.