Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(scripts): run_performer_profiles 按 UTF-8 输出,生僻字名字不再打断一次补完

longmeidaoe4a3bc36befb
diff --git a/scripts/run_performer_profiles.py b/scripts/run_performer_profiles.py
index 438a9ac0c..e4ebfe308 100644
--- a/scripts/run_performer_profiles.py
+++ b/scripts/run_performer_profiles.py
@@ -72,6 +72,8 @@ def sweep(contract, items, run_id: str, *, out=sys.stdout,
def main(argv=None) -> int:
+ # 原地改编码:`sweep` 的默认输出在定义时就绑着这个对象。中文控制台是 GBK,名字里的生僻字会让打印抛错。
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
add_ledger_write_args(parser)
parser.add_argument("--limit", type=int, help="这一趟最多跑几位")
diff --git a/tests/test_performer_profile_followup.py b/tests/test_performer_profile_followup.py
index 03d5e4d48..8dcef3bbd 100644
--- a/tests/test_performer_profile_followup.py
+++ b/tests/test_performer_profile_followup.py
@@ -574,6 +574,19 @@ class SweepTests(unittest.TestCase):
pending.assert_not_called()
run.assert_not_called()
+ def test_a_name_outside_the_console_code_page_does_not_stop_the_sweep(self):
+ """中文 Windows 控制台是 GBK,名字里一个 `䌷` 就能让打印抛错;输出按 UTF-8 写。"""
+ sweep = load_sweep()
+ console = io.TextIOWrapper(io.BytesIO(), encoding="gbk")
+ item = SimpleNamespace(key=followup.followup_key(1), label="䌷野あい")
+ with mock.patch("sys.stdout", console), \
+ mock.patch.object(sweep, "open_readonly", return_value=contextlib.nullcontext()), \
+ mock.patch.object(sweep, "pending", return_value=[item]), \
+ mock.patch("peach.web_state.WebContract"):
+ self.assertEqual(sweep.main(["--db", "unused.db"]), 0)
+ console.flush()
+ self.assertIn("䌷野あい", console.buffer.getvalue().decode("utf-8"))
+
if __name__ == "__main__":
unittest.main()