Skip to content

Commit 5abf7b5

Browse files
remove until_not_interrupted() (#612)
* remove until_not_interrupted Retry is not needed since Python 3.5. https://peps.python.org/pep-0475/ * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 1456456 commit 5abf7b5

File tree

4 files changed

+9
-84
lines changed

4 files changed

+9
-84
lines changed

src/structlog/_output.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
from sys import stderr, stdout
1818
from typing import IO, Any, BinaryIO, TextIO
1919

20-
from structlog._utils import until_not_interrupted
21-
2220

2321
WRITE_LOCKS: dict[IO[Any], threading.Lock] = {}
2422

@@ -109,7 +107,7 @@ def msg(self, message: str) -> None:
109107
"""
110108
f = self._file if self._file is not stdout else None
111109
with self._lock:
112-
until_not_interrupted(print, message, file=f, flush=True)
110+
print(message, file=f, flush=True)
113111

114112
log = debug = info = warn = warning = msg
115113
fatal = failure = err = error = critical = exception = msg
@@ -216,8 +214,8 @@ def msg(self, message: str) -> None:
216214
Write and flush *message*.
217215
"""
218216
with self._lock:
219-
until_not_interrupted(self._write, message + "\n")
220-
until_not_interrupted(self._flush)
217+
self._write(message + "\n")
218+
self._flush()
221219

222220
log = debug = info = warn = warning = msg
223221
fatal = failure = err = error = critical = exception = msg
@@ -320,8 +318,8 @@ def msg(self, message: bytes) -> None:
320318
Write *message*.
321319
"""
322320
with self._lock:
323-
until_not_interrupted(self._write, message + b"\n")
324-
until_not_interrupted(self._flush)
321+
self._write(message + b"\n")
322+
self._flush()
325323

326324
log = debug = info = warn = warning = msg
327325
fatal = failure = err = error = critical = exception = msg

src/structlog/_utils.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,10 @@
99

1010
from __future__ import annotations
1111

12-
import errno
1312
import sys
1413

1514
from contextlib import suppress
16-
from typing import Any, Callable, TypeVar
17-
18-
19-
T = TypeVar("T")
20-
21-
22-
def until_not_interrupted(
23-
f: Callable[..., T], *args: object, **kw: object
24-
) -> T:
25-
"""
26-
Retry until *f* succeeds or an exception that isn't caused by EINTR occurs.
27-
28-
Args:
29-
f: A callable like a function.
30-
31-
*args: Positional arguments for *f*.
32-
33-
**kw: Keyword arguments for *f*.
34-
35-
Returns:
36-
Whatever *f* returns.
37-
"""
38-
while True:
39-
try:
40-
return f(*args, **kw)
41-
except OSError as e: # noqa: PERF203
42-
if e.args[0] == errno.EINTR:
43-
continue
44-
raise
15+
from typing import Any
4516

4617

4718
def get_processname() -> str:

src/structlog/twisted.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
from ._base import BoundLoggerBase
2626
from ._config import _BUILTIN_DEFAULT_PROCESSORS
27-
from ._utils import until_not_interrupted
2827
from .processors import JSONRenderer as GenericJSONRenderer
2928
from .typing import EventDict, WrappedLogger
3029

@@ -215,12 +214,11 @@ def __init__(self, file: TextIO) -> None:
215214
self._flush = file.flush
216215

217216
def __call__(self, eventDict: EventDict) -> None:
218-
until_not_interrupted(
219-
self._write,
217+
self._write(
220218
textFromEventDict(eventDict) # type: ignore[arg-type, operator]
221219
+ "\n",
222220
)
223-
until_not_interrupted(self._flush)
221+
self._flush()
224222

225223

226224
@implementer(ILogObserver)

tests/test_utils.py

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,12 @@
33
# 2.0, and the MIT License. See the LICENSE file in the root of this
44
# repository for complete details.
55

6-
import errno
76
import multiprocessing
87
import sys
98

109
import pytest
1110

12-
from pretend import raiser
13-
14-
from structlog._utils import get_processname, until_not_interrupted
15-
16-
17-
class TestUntilNotInterrupted:
18-
def test_passes_arguments_and_returns_return_value(self):
19-
"""
20-
until_not_interrupted() passes arguments to the wrapped function and
21-
returns its return value.
22-
"""
23-
24-
def returner(*args, **kw):
25-
assert (42,) == args
26-
assert {"x": 23} == kw
27-
28-
return "foo"
29-
30-
assert "foo" == until_not_interrupted(returner, 42, x=23)
31-
32-
def test_leaves_unrelated_exceptions_through(self):
33-
"""
34-
Exceptions that are not an EINTR OSError are not intercepted/retried.
35-
"""
36-
exc = IOError
37-
with pytest.raises(exc):
38-
until_not_interrupted(raiser(exc("not EINTR")))
39-
40-
def test_retries_on_EINTR(self):
41-
"""
42-
Wrapped functions that raise EINTR OSErrors are retried.
43-
"""
44-
calls = [0]
45-
46-
def raise_on_first_three():
47-
if calls[0] < 3:
48-
calls[0] += 1
49-
raise OSError(errno.EINTR)
50-
51-
until_not_interrupted(raise_on_first_three)
52-
53-
assert 3 == calls[0]
11+
from structlog._utils import get_processname
5412

5513

5614
class TestGetProcessname:

0 commit comments

Comments
 (0)