Repository navigation
Use UTF-8 for Python code-execution subprocess I/O (#6489 class) - #6548
Conversation
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
There was a problem hiding this comment.
Code Review
This pull request ensures that Python and shell executions handle non-ASCII characters correctly by enforcing UTF-8 encoding during file writes, subprocess execution, and environment setup. It also introduces a test suite to verify that non-ASCII output round-trips successfully. The review feedback suggests ensuring that Windows cmd.exe outputs UTF-8 by changing the active code page to 65001, and refactoring the bash execution test to use a portable Python command instead of skipping it on Windows.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
8120281 to
7fbde55
Compare
Studio's code-execution tool already tells the child to emit UTF-8 (PYTHONIOENCODING=utf-8 in _build_safe_env), but _python_exec writes the temp script and decodes the subprocess pipe with the OS default codec. On Windows (cp1252), non-ASCII in model-written code or its output -- arrows, CJK, emoji -- raises UnicodeEncodeError / UnicodeDecodeError and breaks execution. Complete the UTF-8 wiring in core/inference/tools.py: - write the temp script with encoding="utf-8" - decode _python_exec stdout as utf-8, errors="replace" - set PYTHONIOENCODING=utf-8 in _build_bypass_env too (matches _build_safe_env, so the bypass path's child also emits utf-8) The child is python with PYTHONIOENCODING=utf-8, so it emits UTF-8 regardless of the console code page and the decode is always correct. Shell execution via cmd.exe has a separate console-code-page story and is left to a follow-up. Refs unslothai#6489
7fbde55 to
cbcd5c9
Compare
|
I Went through both points: 1. So rather than ship an unverifiable partial fix, I've scoped this PR to 2. Cross-platform bash test. Removed along with the Net diff is now 3 lines in |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 925c17b28d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| def test_bash_bypass_uses_bypass_preexec(captured_popen): | ||
| _bash_exec("echo hi", None, 5, "t", disable_sandbox = True) | ||
| assert captured_popen["kwargs"]["preexec_fn"] is tools._bypass_preexec | ||
| assert "PYTHONIOENCODING" not in captured_popen["kwargs"]["env"] |
There was a problem hiding this comment.
Control PYTHONIOENCODING in bash bypass test
When the test runner itself has PYTHONIOENCODING set, _build_bypass_env() intentionally preserves benign host variables for bypassed bash execution, so this new assertion fails even though _bash_exec did not add the variable. Please clear or explicitly set this env var in the test before asserting the bash-specific behavior.
Useful? React with 👍 / 👎.
…slothai#6548 Bypass mode preserves benign host env vars, so a host-set PYTHONIOENCODING was inherited into the bash bypass env and tripped the new assertion even though _bash_exec never adds it. Clear it in the test so the assertion checks _bash_exec, not the runner environment.
Extracted and narrowed from unslothai#6543 by @TheJagStudio. This keeps the startup/banner and text file encoding hardening separate from the already-merged Python code-exec UTF-8 fix in unslothai#6548. Co-authored-by: Jagrat Patel <[email protected]>
Extracted and narrowed from #6543 by @TheJagStudio. This keeps the startup/banner and text file encoding hardening separate from the already-merged Python code-exec UTF-8 fix in #6548. Co-authored-by: Jagrat Patel <[email protected]>

Summary
Studio's Python code-execution tool (
_python_exec) crashes on Windows when model-written code — or its output — contains non-ASCII (arrows, em-dashes, CJK, emoji), which is extremely common in LLM output. Same class of bug as #6489.Root cause
The path already tells the child to emit UTF-8 (
_build_safe_envsetsPYTHONIOENCODING=utf-8andLANG=C.UTF-8), but the rest of the I/O uses the OS default codec:.pyscript is written withos.fdopen(fd, "w")(noencoding);text=True(noencoding);_build_bypass_envdoesn't setPYTHONIOENCODINGat all.On Windows the default codec is cp1252, so a character it can't represent (
→,数,😀, …) raisesUnicodeEncodeError/UnicodeDecodeError.Fix (
core/inference/tools.py)encoding="utf-8";_python_execstdout withencoding="utf-8", errors="replace";PYTHONIOENCODING=utf-8in_build_bypass_envtoo.The child is
pythonwithPYTHONIOENCODING=utf-8, so it emits UTF-8 regardless of the console code page — the UTF-8 decode is always correct. On a UTF-8 host (Linux/macOS CI) the changes are no-ops.Scope
Deliberately limited to
_python_exec. The shell tool (_bash_exec) runscmd /con Windows, whose output uses the console code page rather than UTF-8; making that correct (e.g.chcp 65001) needs Windows validation and is better as its own change. See the review thread for details.Tests
studio/backend/tests/test_exec_utf8.py:_python_execround-trips"café — 数字 → ✓ 😀"(sandboxed and bypass paths). Passes on a UTF-8 host and guards against a regression to the OS default codec. The 247 existing sandbox/bypass tests still pass.