Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions newsfragments/3555.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`trio.Process` no longer emits an `EncodingWarning` on Linux when Python runs with
``-X warn_default_encoding`` (or ``PYTHONWARNDEFAULTENCODING=1``).
7 changes: 3 additions & 4 deletions src/trio/_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import warnings
from contextlib import ExitStack
from functools import partial
from io import FileIO
from typing import (
TYPE_CHECKING,
Final,
Expand All @@ -32,7 +33,6 @@
if TYPE_CHECKING:
import signal
from collections.abc import Awaitable, Callable, Iterable, Mapping, Sequence
from io import TextIOWrapper

from typing_extensions import Unpack

Expand Down Expand Up @@ -173,7 +173,7 @@ def __init__(

self._wait_lock: Lock = Lock()

self._pidfd: TextIOWrapper | None = None
self._pidfd: FileIO | None = None

@A5rocks A5rocks Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually surprised this explicit type hint is/was necessary. Maybe mypy has improved so it infers this now?

if can_try_pidfd_open:
try:
fd: int = pidfd_open(self._proc.pid, 0)
Expand All @@ -186,8 +186,7 @@ def __init__(
else:
# It worked! Wrap the raw fd up in a Python file object to
# make sure it'll get closed.
# SIM115: open-file-with-context-handler
self._pidfd = open(fd) # noqa: SIM115
self._pidfd = FileIO(fd)

self.args: StrOrBytesPath | Sequence[StrOrBytesPath] = self._proc.args
self.pid: int = self._proc.pid
Expand Down
20 changes: 20 additions & 0 deletions src/trio/_tests/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@ async def test_basic_no_pidfd(background_process: BackgroundProcessType) -> None
)


async def test_no_encoding_warning() -> None:
# Spawning a process must not open anything in text mode without an
# explicit encoding, which warns under -X warn_default_encoding.
code = "import sys, trio; trio.run(trio.run_process, [sys.executable, '-c', ''])"
result = await run_process(
[
sys.executable,
"-X",
"warn_default_encoding",
"-W",
"error::EncodingWarning",
"-c",
code,
],
capture_stderr=True,
check=False,
)
assert result.returncode == 0, result.stderr.decode()


@background_process_param
async def test_auto_update_returncode(
background_process: BackgroundProcessType,
Expand Down
Loading