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
6 changes: 3 additions & 3 deletions skills/deeppapernote/scripts/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ def json_input_sha256(value: str | Path) -> str:


def emit(payload: dict[str, Any], output_path: str | None = None) -> None:
text = json.dumps(payload, ensure_ascii=False, indent=2)
if output_path:
ensure_parent(output_path)
Path(output_path).write_text(text + "\n", encoding="utf-8")
Path(output_path).write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
else:
print(text)
# ASCII JSON reads back under any console encoding, such as cp1252 on Windows.
print(json.dumps(payload, indent=2))


def load_json_file(path: str | Path) -> dict[str, Any]:
Expand Down
9 changes: 8 additions & 1 deletion skills/deeppapernote/scripts/run_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,13 @@ def prepare_steps(
]


def print_outputs(outputs: list[tuple[str, object]]) -> None:
"""Print `name=path` lines as UTF-8, so paths survive a console encoding such as cp1252."""
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8")
print("\n".join(f"{name}={path}" for name, path in outputs))


def main() -> None:
argv = sys.argv[1:]
phase = argv[0] if argv and argv[0] in PHASES else ""
Expand Down Expand Up @@ -397,7 +404,7 @@ def main() -> None:
("figure_table_decisions", paths["figure_table_decisions"]),
("bundle", paths["bundle"]),
]
print("\n".join(f"{name}={path}" for name, path in outputs))
print_outputs(outputs)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion skills/deeppapernote/scripts/run_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,7 @@ def main() -> None:
report = inspect_run(args.workdir, args.prefix, bindings)
except (OSError, ValueError) as exc:
parser.error(str(exc))
print(json.dumps(report, ensure_ascii=False, indent=2))
print(json.dumps(report, indent=2)) # ASCII, readable under any console encoding


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion skills/deeppapernote/scripts/user_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def main() -> None:
result = exc.result
except ConfigurationWriteError as exc:
result = _blocked_result(user_config_path(), "write_failed", str(exc))
print(json.dumps(result, ensure_ascii=False, indent=2))
print(json.dumps(result, indent=2)) # ASCII, readable under any console encoding
if result["state"] != "ready":
raise SystemExit(2)

Expand Down
6 changes: 3 additions & 3 deletions skills/paper-glossary/scripts/configure_glossary.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ def parser() -> argparse.ArgumentParser:


def _emit(payload: dict[str, Any], output: str) -> None:
text = json.dumps(payload, ensure_ascii=False, indent=2) + "\n"
if output:
target = Path(output).expanduser()
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(text, encoding="utf-8")
target.write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
else:
print(text, end="")
# ASCII JSON reads back under any console encoding, such as cp1252 on Windows.
print(json.dumps(payload, indent=2))


def main() -> None:
Expand Down
6 changes: 3 additions & 3 deletions skills/paper-glossary/scripts/glossary_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ def maybe_load_json_record(value: str) -> dict[str, Any] | None:


def emit(payload: dict[str, Any], output: str = "") -> None:
text = json.dumps(payload, ensure_ascii=False, indent=2)
if output:
Path(output).expanduser().write_text(text + "\n", encoding="utf-8")
Path(output).expanduser().write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
else:
print(text)
# ASCII JSON reads back under any console encoding, such as cp1252 on Windows.
print(json.dumps(payload, indent=2))
6 changes: 3 additions & 3 deletions skills/research-memory/scripts/aggregate_reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ def main() -> None:
else:
language = args.language or located["language"]
payload = aggregate(Path(located["vault"]), located["papers_dir"], language=language, today=args.today)
text = json.dumps(payload, ensure_ascii=False, indent=2)
if args.output:
Path(args.output).write_text(text + "\n", encoding="utf-8")
Path(args.output).write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
else:
sys.stdout.write(text + "\n")
# ASCII JSON reads back under any console encoding, such as cp1252 on Windows.
sys.stdout.write(json.dumps(payload, indent=2) + "\n")


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions skills/research-memory/scripts/write_research_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ def main() -> None:
try:
result = run(args)
except (Refused, ValueError, OSError) as exc:
sys.stdout.write(json.dumps({"status": "refused", "reason": str(exc)}, ensure_ascii=False) + "\n")
sys.stdout.write(json.dumps({"status": "refused", "reason": str(exc)}) + "\n")
raise SystemExit(1) from None
sys.stdout.write(json.dumps(result, ensure_ascii=False, indent=2) + "\n")
sys.stdout.write(json.dumps(result, indent=2) + "\n")


if __name__ == "__main__":
Expand Down
10 changes: 10 additions & 0 deletions skills/research-memory/tests/test_research_memory_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,13 @@ def test_cli_without_a_vault_reports_needs_vault(tmp_path: Path) -> None:
result = subprocess.run([sys.executable, str(SCRIPT)], env=env, capture_output=True, text=True)
assert result.returncode == 0
assert json.loads(result.stdout)["status"] == "needs_vault"


def test_cli_prints_json_a_windows_console_can_read(tmp_path: Path, library: Path) -> None:
env = {
**os.environ, "PYTHONIOENCODING": "cp1252", "PYTHONUTF8": "0",
"DEEPPAPERNOTE_CONFIG_PATH": str(write_config(tmp_path, {"obsidian_vault": str(library), "papers_dir": "Papers"})),
}
result = subprocess.run([sys.executable, str(SCRIPT), "--today", "2026-11-06"], env=env, capture_output=True, text=True)
assert result.returncode == 0, result.stderr
assert json.loads(result.stdout)["paper_count"] == 4
3 changes: 3 additions & 0 deletions skills/research-memory/tests/test_research_memory_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import hashlib
import json
import os
import subprocess
import sys
from pathlib import Path
Expand Down Expand Up @@ -34,6 +35,8 @@ def write(tmp_path: Path, vault: Path, mode: str, payload: dict, expected: str,
[sys.executable, str(SCRIPT), "--vault", str(vault), f"--{mode}", str(data),
"--expected-sha256", expected, "--today", "2026-11-06", *extra],
capture_output=True, text=True,
# cp1252 stands in for a Windows console, where non-ASCII output fails.
env={**os.environ, "PYTHONIOENCODING": "cp1252", "PYTHONUTF8": "0"},
)


Expand Down
56 changes: 56 additions & 0 deletions tests/test_console_encoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""Scripts print to a console whose encoding may not cover Chinese, such as cp1252 on Windows."""

from __future__ import annotations

import json
import os
import subprocess
import sys
from pathlib import Path

PROJECT_ROOT = Path(__file__).resolve().parents[1]
SCRIPTS = PROJECT_ROOT / "skills" / "deeppapernote" / "scripts"
CP1252 = {**os.environ, "PYTHONIOENCODING": "cp1252", "PYTHONUTF8": "0"}


def run_python(code: str, scripts: Path, *args: str) -> subprocess.CompletedProcess:
return subprocess.run(
[sys.executable, "-c", code, *args], capture_output=True, text=True, env=CP1252, cwd=scripts,
)


def test_emit_prints_json_a_cp1252_console_can_read():
result = run_python("import common; common.emit({'domain': '长视频理解'})", SCRIPTS)
assert result.returncode == 0, result.stderr
assert json.loads(result.stdout) == {"domain": "长视频理解"}


def test_glossary_emit_prints_json_a_cp1252_console_can_read():
glossary = PROJECT_ROOT / "skills" / "paper-glossary" / "scripts"
for code in (
"import glossary_common; glossary_common.emit({'term': '注意力'})",
"import configure_glossary; configure_glossary._emit({'term': '注意力'}, '')",
):
result = run_python(code, glossary)
assert result.returncode == 0, result.stderr
assert json.loads(result.stdout) == {"term": "注意力"}


def test_run_state_prints_json_a_cp1252_console_can_read(tmp_path):
workdir = tmp_path / "运行目录"
workdir.mkdir()
result = subprocess.run(
[sys.executable, str(SCRIPTS / "run_state.py"), "status", "--workdir", str(workdir), "--prefix", "run"],
capture_output=True, text=True, env=CP1252,
)
assert result.returncode == 0, result.stderr
assert json.loads(result.stdout)["workdir"] == str(workdir.resolve())


def test_pipeline_paths_print_as_utf8():
result = subprocess.run(
[sys.executable, "-c", "import run_pipeline; run_pipeline.print_outputs([('workdir', '/tmp/运行')])"],
capture_output=True, env=CP1252, cwd=SCRIPTS,
)
assert result.returncode == 0, result.stderr.decode("utf-8", "replace")
assert result.stdout.decode("utf-8").strip() == "workdir=/tmp/运行"
Loading