Skip to content

Commit 1181b22

Browse files
committed
Apply mypy config override to blocked_signals
1 parent 7d27b9c commit 1181b22

File tree

2 files changed

+36
-34
lines changed

2 files changed

+36
-34
lines changed

tests/lib/compat.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
# mypy: no-warn-unused-ignores
2+
13
import contextlib
2-
from typing import Iterator
4+
import signal
5+
from typing import Iterable, Iterator
36

47

58
@contextlib.contextmanager
@@ -18,3 +21,34 @@ def nullcontext() -> Iterator[None]:
1821
support.
1922
"""
2023
yield
24+
25+
26+
# Applies on Windows.
27+
if not hasattr(signal, "pthread_sigmask"):
28+
# We're not relying on this behavior anywhere currently, it's just best
29+
# practice.
30+
blocked_signals = nullcontext
31+
else:
32+
33+
@contextlib.contextmanager
34+
def blocked_signals() -> Iterator[None]:
35+
"""Block all signals for e.g. starting a worker thread."""
36+
# valid_signals() was added in Python 3.8 (and not using it results
37+
# in a warning on pthread_sigmask() call)
38+
mask: Iterable[int]
39+
try:
40+
mask = signal.valid_signals()
41+
except AttributeError:
42+
mask = set(range(1, signal.NSIG))
43+
44+
old_mask = signal.pthread_sigmask( # type: ignore[attr-defined]
45+
signal.SIG_SETMASK, # type: ignore[attr-defined]
46+
mask,
47+
)
48+
try:
49+
yield
50+
finally:
51+
signal.pthread_sigmask( # type: ignore[attr-defined]
52+
signal.SIG_SETMASK, # type: ignore[attr-defined]
53+
old_mask,
54+
)

tests/lib/server.py

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import signal
32
import ssl
43
import threading
54
from base64 import b64encode
@@ -11,7 +10,7 @@
1110
from werkzeug.serving import BaseWSGIServer, WSGIRequestHandler
1211
from werkzeug.serving import make_server as _make_server
1312

14-
from .compat import nullcontext
13+
from .compat import blocked_signals
1514

1615
if TYPE_CHECKING:
1716
from _typeshed.wsgi import StartResponse, WSGIApplication, WSGIEnvironment
@@ -23,37 +22,6 @@ class MockServer(BaseWSGIServer):
2322
mock: Mock = Mock()
2423

2524

26-
# Applies on Windows.
27-
if not hasattr(signal, "pthread_sigmask"):
28-
# We're not relying on this behavior anywhere currently, it's just best
29-
# practice.
30-
blocked_signals = nullcontext
31-
else:
32-
33-
@contextmanager
34-
def blocked_signals() -> Iterator[None]:
35-
"""Block all signals for e.g. starting a worker thread."""
36-
# valid_signals() was added in Python 3.8 (and not using it results
37-
# in a warning on pthread_sigmask() call)
38-
mask: Iterable[int]
39-
try:
40-
mask = signal.valid_signals()
41-
except AttributeError:
42-
mask = set(range(1, signal.NSIG))
43-
44-
old_mask = signal.pthread_sigmask( # type: ignore[attr-defined]
45-
signal.SIG_SETMASK, # type: ignore[attr-defined]
46-
mask,
47-
)
48-
try:
49-
yield
50-
finally:
51-
signal.pthread_sigmask( # type: ignore[attr-defined]
52-
signal.SIG_SETMASK, # type: ignore[attr-defined]
53-
old_mask,
54-
)
55-
56-
5725
class _RequestHandler(WSGIRequestHandler):
5826
def make_environ(self) -> Dict[str, Any]:
5927
environ = super().make_environ()

0 commit comments

Comments
 (0)