Skip to content

知らない分野を、原文にたどりつくまで芋づる式に掘る Claude Code のスキル

Python

fetch: pdftoppm が途中で失敗したら OCR 全体を失敗にする(#14)

MaryCache7eec2a28c713
diff --git a/skills/imozuru/scripts/fetch.py b/skills/imozuru/scripts/fetch.py
index 2df5cf6..57ee8fd 100755
--- a/skills/imozuru/scripts/fetch.py
+++ b/skills/imozuru/scripts/fetch.py
@@ -477,6 +477,8 @@ def ocr_pdf(pdf: Path) -> tuple[str, str]:
if done is None:
return "", "PDF を画像にする途中で時間切れ"
# 途中で落ちると、書き出せたページの画像だけが残る。残りを OCR すると欠けた本文が成功扱いになる(#14)
+ if done.returncode != 0:
+ return "", f"PDF を画像にする途中で失敗(pdftoppm の終了コード {done.returncode})"
langs = ocr_langs()
pages, failed, weighted, chars = [], [], 0.0, 0
for img in sorted(tmp.glob("p-*.png")):
diff --git a/tests/test_security.py b/tests/test_security.py
index af3fe71..573ba92 100644
--- a/tests/test_security.py
+++ b/tests/test_security.py
@@ -756,6 +756,19 @@ class OcrLanguageTests(unittest.TestCase):
self.assertEqual(text, "")
self.assertIn("読めなかったページ", note)
+ def test_pdftoppm_failing_midway_fails_the_whole_ocr(self):
+ # #14: pdftoppm が1ページだけ書いて終了コード 1 で落ちても、残りの画像だけで成功扱いにしない
+ tools = self.fake_tools(conf=95)
+
+ def run(cmd, **kw):
+ if cmd[0] == "pdftoppm":
+ Path(f"{cmd[-1]}-1.png").write_bytes(b"png")
+ return subprocess.CompletedProcess(cmd, 1)
+ return tools(cmd, **kw)
+ text, note = self.ocr(run)
+ self.assertEqual(text, "")
+ self.assertIn("終了コード 1", note)
+
def test_fail_line_names_the_ocr_reason(self):
with tempfile.TemporaryDirectory() as tmp, \
patch.object(fetch, "download", return_value=(b"%PDF-1.4\nscan", "application/pdf")), \