Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: jakkdl <11260241+jakkdl@users.noreply.github.com>
  • Loading branch information
CoolCat467 and jakkdl committed Dec 1, 2023
1 parent 1fc842a commit f89cd0a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 30 deletions.
6 changes: 2 additions & 4 deletions src/trio/_core/_tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,11 @@ async def test_child_crash_basic() -> None:
async def erroring() -> NoReturn:
raise exc

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


async def test_basic_interleave() -> None:
Expand Down
8 changes: 6 additions & 2 deletions src/trio/_tests/test_highlevel_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,13 @@ async def accept(self) -> tuple[SocketType, object]:
stream = await listener.accept()
assert stream.socket is fake_server_sock

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

Expand Down
24 changes: 10 additions & 14 deletions src/trio/_tests/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,19 +648,14 @@ async def res(
)
netlink_sock.close()

with pytest.raises(
ValueError,
match=r"^address should be a \(host, port(, \[flowinfo, \[scopeid\]\])*\) tuple$",
):
address = r"^address should be a \(host, port(, \[flowinfo, \[scopeid\]\])*\) tuple$"
with pytest.raises(ValueError, match=address):
await res("1.2.3.4") # type: ignore[arg-type]
with pytest.raises(
ValueError,
match=r"^address should be a \(host, port(, \[flowinfo, \[scopeid\]\])*\) tuple$",
):
with pytest.raises(ValueError, match=address):
await res(("1.2.3.4",)) # type: ignore[arg-type]
with pytest.raises( # noqa: PT012
ValueError,
match=r"^address should be a \(host, port(, \[flowinfo, \[scopeid\]\])*\) tuple$",
match=address,
):
if v6:
await res(("1.2.3.4", 80, 0, 0, 0)) # type: ignore[arg-type]
Expand Down Expand Up @@ -815,7 +810,7 @@ def connect(self, *args: Any, **kwargs: Any) -> None:
with tsocket.socket() as sock:
with pytest.raises(
OSError,
match=r"^.*Error connecting to \('127\.0\.0\.\d', \d+\): (Connection refused|Unknown error)$",
match=r"^\[\w+ \d+\] Error connecting to \('127\.0\.0\.\d', \d+\): (Connection refused|Unknown error)$",
):
# TCP port 2 is not assigned. Pretty sure nothing will be
# listening there. (We used to bind a port and then *not* call
Expand All @@ -831,11 +826,12 @@ def connect(self, *args: Any, **kwargs: Any) -> None:
async def test_address_in_socket_error() -> None:
address = "127.0.0.1"
with tsocket.socket() as sock:
try:
with pytest.raises(
OSError,
match=r"^\[\w+ \d+\] Error connecting to \('127\.0\.0\.1', 2\): (Connection refused|Unknown error)$",
) as excinfo:
await sock.connect((address, 2))
except OSError as e:
# the noqa is "Found assertion on exception `e` in `except` block"
assert any(address in str(arg) for arg in e.args) # noqa: PT017
assert any(address in str(arg) for arg in excinfo.value.args)


async def test_resolve_address_exception_in_connect_closes_socket() -> None:
Expand Down
15 changes: 5 additions & 10 deletions src/trio/_tests/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,19 +338,14 @@ async def test_run() -> None:
# invalid combinations
with pytest.raises(UnicodeError):
await run_process(CAT, stdin="oh no, it's text")
with pytest.raises(
ValueError,
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)*$",
):

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)*$"
with pytest.raises(ValueError, match=pipe_stdout_error):
await run_process(CAT, stdin=subprocess.PIPE)
with pytest.raises(
ValueError,
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)*$",
):
with pytest.raises(ValueError, match=pipe_stdout_error):
await run_process(CAT, stdout=subprocess.PIPE)
with pytest.raises(
ValueError,
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)*$",
ValueError, match=pipe_stdout_error.replace("stdout", "stderr", 1)
):
await run_process(CAT, stderr=subprocess.PIPE)
with pytest.raises(
Expand Down

0 comments on commit f89cd0a

Please sign in to comment.