Skip to content

Commit f89cd0a

Browse files
CoolCat467jakkdl
andcommitted
Apply suggestions from code review
Co-authored-by: jakkdl <[email protected]>
1 parent 1fc842a commit f89cd0a

File tree

4 files changed

+23
-30
lines changed

4 files changed

+23
-30
lines changed

src/trio/_core/_tests/test_run.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,11 @@ async def test_child_crash_basic() -> None:
135135
async def erroring() -> NoReturn:
136136
raise exc
137137

138-
try:
138+
with pytest.raises(ValueError, match="^uh oh$") as excinfo:
139139
# nursery.__aexit__ propagates exception from child back to parent
140140
async with _core.open_nursery() as nursery:
141141
nursery.start_soon(erroring)
142-
except ValueError as e:
143-
# the noqa is for "Found assertion on exception `multi_exc` in `except` block"
144-
assert e is exc # noqa: PT017
142+
assert excinfo is exc
145143

146144

147145
async def test_basic_interleave() -> None:

src/trio/_tests/test_highlevel_socket.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,13 @@ async def accept(self) -> tuple[SocketType, object]:
287287
stream = await listener.accept()
288288
assert stream.socket is fake_server_sock
289289

290-
for code in [errno.EMFILE, errno.EFAULT, errno.ENOBUFS]:
290+
for code, match in {
291+
errno.EMFILE: r"\[\w+ \d+\] Out of file descriptors$",
292+
errno.EFAULT: r"\[\w+ \d+\] attempt to write to read-only memory$",
293+
errno.ENOBUFS: r"\[\w+ \d+\] out of buffers$",
294+
}.items():
291295
with assert_checkpoints():
292-
with pytest.raises(OSError) as excinfo: # noqa: PT011 # missing `match`
296+
with pytest.raises(OSError, match=match) as excinfo:
293297
await listener.accept()
294298
assert excinfo.value.errno == code
295299

src/trio/_tests/test_socket.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -648,19 +648,14 @@ async def res(
648648
)
649649
netlink_sock.close()
650650

651-
with pytest.raises(
652-
ValueError,
653-
match=r"^address should be a \(host, port(, \[flowinfo, \[scopeid\]\])*\) tuple$",
654-
):
651+
address = r"^address should be a \(host, port(, \[flowinfo, \[scopeid\]\])*\) tuple$"
652+
with pytest.raises(ValueError, match=address):
655653
await res("1.2.3.4") # type: ignore[arg-type]
656-
with pytest.raises(
657-
ValueError,
658-
match=r"^address should be a \(host, port(, \[flowinfo, \[scopeid\]\])*\) tuple$",
659-
):
654+
with pytest.raises(ValueError, match=address):
660655
await res(("1.2.3.4",)) # type: ignore[arg-type]
661656
with pytest.raises( # noqa: PT012
662657
ValueError,
663-
match=r"^address should be a \(host, port(, \[flowinfo, \[scopeid\]\])*\) tuple$",
658+
match=address,
664659
):
665660
if v6:
666661
await res(("1.2.3.4", 80, 0, 0, 0)) # type: ignore[arg-type]
@@ -815,7 +810,7 @@ def connect(self, *args: Any, **kwargs: Any) -> None:
815810
with tsocket.socket() as sock:
816811
with pytest.raises(
817812
OSError,
818-
match=r"^.*Error connecting to \('127\.0\.0\.\d', \d+\): (Connection refused|Unknown error)$",
813+
match=r"^\[\w+ \d+\] Error connecting to \('127\.0\.0\.\d', \d+\): (Connection refused|Unknown error)$",
819814
):
820815
# TCP port 2 is not assigned. Pretty sure nothing will be
821816
# listening there. (We used to bind a port and then *not* call
@@ -831,11 +826,12 @@ def connect(self, *args: Any, **kwargs: Any) -> None:
831826
async def test_address_in_socket_error() -> None:
832827
address = "127.0.0.1"
833828
with tsocket.socket() as sock:
834-
try:
829+
with pytest.raises(
830+
OSError,
831+
match=r"^\[\w+ \d+\] Error connecting to \('127\.0\.0\.1', 2\): (Connection refused|Unknown error)$",
832+
) as excinfo:
835833
await sock.connect((address, 2))
836-
except OSError as e:
837-
# the noqa is "Found assertion on exception `e` in `except` block"
838-
assert any(address in str(arg) for arg in e.args) # noqa: PT017
834+
assert any(address in str(arg) for arg in excinfo.value.args)
839835

840836

841837
async def test_resolve_address_exception_in_connect_closes_socket() -> None:

src/trio/_tests/test_subprocess.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -338,19 +338,14 @@ async def test_run() -> None:
338338
# invalid combinations
339339
with pytest.raises(UnicodeError):
340340
await run_process(CAT, stdin="oh no, it's text")
341-
with pytest.raises(
342-
ValueError,
343-
match=r"^stdout=subprocess\.PIPE is only valid with nursery\.start, since that's the only way to access the pipe(; use nursery\.start or pass the data you want to write directly)*$",
344-
):
341+
342+
pipe_stdout_error = r"^stdout=subprocess\.PIPE is only valid with nursery\.start, since that's the only way to access the pipe(; use nursery\.start or pass the data you want to write directly)*$"
343+
with pytest.raises(ValueError, match=pipe_stdout_error):
345344
await run_process(CAT, stdin=subprocess.PIPE)
346-
with pytest.raises(
347-
ValueError,
348-
match=r"^stdout=subprocess\.PIPE is only valid with nursery\.start, since that's the only way to access the pipe(; use nursery\.start or pass the data you want to write directly)*$",
349-
):
345+
with pytest.raises(ValueError, match=pipe_stdout_error):
350346
await run_process(CAT, stdout=subprocess.PIPE)
351347
with pytest.raises(
352-
ValueError,
353-
match=r"^stderr=subprocess\.PIPE is only valid with nursery\.start, since that's the only way to access the pipe(; use nursery\.start or pass the data you want to write directly)*$",
348+
ValueError, match=pipe_stdout_error.replace("stdout", "stderr", 1)
354349
):
355350
await run_process(CAT, stderr=subprocess.PIPE)
356351
with pytest.raises(

0 commit comments

Comments
 (0)