Skip to content

The agent that turns websites into APIs!

fix(cli): retain UTF-8 when retrying a Python client

kalil0321d3016ebb52e2
diff --git a/src/reverse_api/cli.py b/src/reverse_api/cli.py
index ed4ddf9..801c028 100644
--- a/src/reverse_api/cli.py
+++ b/src/reverse_api/cli.py
@@ -3342,9 +3342,10 @@ def run_script(
# Execute with real-time stdout, capture stderr for import error detection
cmd = [python_path, str(script), *script_args]
+ launch_env = {**os.environ, "PYTHONIOENCODING": "utf-8"}
result = subprocess.run(
cmd, stderr=subprocess.PIPE, text=True, encoding="utf-8", errors="replace",
- env={**os.environ, "PYTHONIOENCODING": "utf-8"},
+ env=launch_env,
)
# Print stderr so the user sees it, then check for missing imports
@@ -3368,7 +3369,7 @@ def run_script(
if install:
subprocess.run([str(venv_pip), "install", "-q", missing], check=True)
console.print(f"Installed [green]{missing}[/green]. Retrying...")
- retry_result = subprocess.run(cmd)
+ retry_result = subprocess.run(cmd, env=launch_env)
raise SystemExit(retry_result.returncode)
raise SystemExit(result.returncode)
diff --git a/tests/test_windows_compatibility.py b/tests/test_windows_compatibility.py
index 1415959..7c2c5ea 100644
--- a/tests/test_windows_compatibility.py
+++ b/tests/test_windows_compatibility.py
@@ -170,6 +170,44 @@ def test_run_json_real_python_unicode_output(tmp_path, monkeypatch):
assert payload["stderr"] == "erreur — 中文\n"
+def test_interactive_dependency_retry_preserves_unicode(tmp_path, monkeypatch):
+ import venv
+
+ from click.testing import CliRunner
+
+ from reverse_api import cli
+
+ scripts = tmp_path / "scripts" / "retry-run"
+ scripts.mkdir(parents=True)
+ script = scripts / "api_client.py"
+ script.write_text("import rae_locale_dep\nprint('中文 — 🐍')\n", encoding="utf-8")
+ venv.EnvBuilder(with_pip=False).create(tmp_path / ".venv")
+ config = ConfigManager(tmp_path / "config.json")
+ config.set("output_dir", str(tmp_path))
+ session = SessionManager(tmp_path / "history.json")
+ session.add_run("retry-run", "retry output", paths={"script_path": str(script)})
+ monkeypatch.setattr(cli, "config_manager", config)
+ monkeypatch.setattr(cli, "session_manager", session)
+ monkeypatch.setenv("PYTHONIOENCODING", "cp1252")
+ real_run = subprocess.run
+ executions = []
+
+ def run_with_local_install(cmd, **kwargs):
+ if "install" in cmd:
+ assert cmd[-1] == "rae_locale_dep"
+ (scripts / "rae_locale_dep.py").write_text("", encoding="utf-8")
+ return subprocess.CompletedProcess(cmd, 0)
+ completed = real_run(cmd, stdout=subprocess.PIPE, **kwargs)
+ executions.append(completed)
+ return completed
+
+ monkeypatch.setattr(subprocess, "run", run_with_local_install)
+ result = CliRunner().invoke(cli.main, ["run", "retry-run", "--auto-install"])
+ assert result.exit_code == 0, result.output
+ assert [execution.returncode for execution in executions] == [1, 0]
+ assert executions[-1].stdout.decode("utf-8").strip() == "中文 — 🐍"
+
+
def test_windows_probe_resolves_cmd_launcher(monkeypatch):
from reverse_api import agent_browser