Skip to content

Return None from GetChoiceIterator when not positioned on a word - #396

Merged
sirfz merged 2 commits into
sirfz:masterfrom
TonyTech404:fix/choice-iterator-no-word
Nov 1, 2026
Merged

sirfz merged 2 commits into
sirfz:masterfrom
TonyTech404:fix/choice-iterator-no-word

Conversation

@TonyTech404

Copy link
Copy Markdown
Contributor

Fixes #325.

Problem

PyLTRResultIterator.GetChoiceIterator() constructs a Tesseract ChoiceIterator unconditionally. When the result iterator is not on a word (for example an image without any text, or a non-text block), the constructor hits ASSERT_HOST(result_it.it_->word() != nullptr) and Tesseract aborts the entire Python process (exit 134), so it cannot be caught from Python:

from PIL import Image
from tesserocr import RIL, PyTessBaseAPI, iterate_level

with PyTessBaseAPI() as api:
    api.SetImage(Image.new('RGB', (100, 100), (255, 255, 255)))
    api.Recognize()
    for r in iterate_level(api.GetIterator(), RIL.SYMBOL):
        r.GetChoiceIterator()
# result_it.it_->word() != nullptr:Error:Assert failed:in file src/ccmain/ltrresultiterator.cpp, line 369

Fix

Check whether the iterator has a current word before constructing the ChoiceIterator (GetUTF8Text(RIL_SYMBOL) returns NULL exactly when word() is null) and return None otherwise. The docstring already states "Returns None on failure"; the existing citer == NULL check could never trigger. The docstring now mentions this case.

Tests

  • test_choice_iterator_without_word: blank image, GetChoiceIterator() returns None (before the fix this aborts the test process).
  • test_choice_iterator: on eurotext.png the first symbol returns a choice iterator whose first choice is T.

Verified locally (macOS arm64, Tesseract 5.5.3, Leptonica 1.87, Python 3.12): the new tests pass; tests.test_signals and tests.test_threads pass. In tests.test_api, test_detect_os aborts the interpreter and test_init errors (no legacy-engine traineddata in the Homebrew tessdata); both behave the same on unmodified master, so they are unrelated to this change. All other test_api tests pass.

Note: other iterator methods I probed at a word-less position (WordFontAttributes, GetBestLSTMSymbolChoices, etc.) behave fine; GetBlamerDebug segfaults there but is a debug-only API, so I left it out of scope.

TonyTech404 and others added 2 commits November 1, 2026 14:15
sirfz#325)

Creating a ChoiceIterator without a current word trips ASSERT_HOST in Tesseract, which aborts the whole Python process (e.g. when iterating RIL.SYMBOL on an image without text). Check for a word first and return None, as the docstring already documents for failures.
@sirfz
sirfz merged commit 729281e into sirfz:master Nov 1, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

in loop GetChoiceIterator crashs python if result is empty

2 participants