fix: close pypdfium2 page handle on error + cover classifier/OCR edge cases

Follow-up to the tiered document processor (#858), landing the round-4 review
nits the reviewer approved without:

- pypdfium2_fast: free the page handle in an outer finally so a corrupt page
  that makes get_textpage() raise can't orphan it.
- test: classify_from_text junk-text-layer path (non-zero chars, low quality,
  high ocr_frac) flags bad_text_layer -- the hot-path coverage gap.
- test: build_ocr_backend raises ValueError when the gateway M2M client_id is
  set without its token_url/secret.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Chris Coutinho
2026-06-05 03:21:10 +02:00
co-authored by Claude Opus 4.8
parent ba3cb575fe
commit 9ffe0645b8
3 changed files with 37 additions and 3 deletions
+17
View File
@@ -185,3 +185,20 @@ def test_classify_from_text_no_pages_routes_fast():
assert c.recommended_tier == "fast"
assert c.ocr_page_fraction == pytest.approx(0.0)
assert c.flags == set()
def test_classify_from_text_junk_layer_flags_bad_text_layer():
# Non-zero chars but low quality on every page (high ocr_frac) -> ocr +
# bad_text_layer (gated on ocr_frac, matching classify_pdf).
text = "x1y2zx1y2z"
c = clf.classify_from_text(
text,
[
{"page": 1, "start_offset": 0, "end_offset": 5},
{"page": 2, "start_offset": 5, "end_offset": 10},
],
)
assert c.recommended_tier == "ocr"
assert c.total_chars > 0
assert "bad_text_layer" in c.flags
assert "no_text_layer" not in c.flags