Skip to content

Commit

Permalink
Fix proper extraction of tesseract version
Browse files Browse the repository at this point in the history
  • Loading branch information
int3l committed Jan 25, 2022
1 parent 1827fe0 commit 64e7c2f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
12 changes: 7 additions & 5 deletions pytesseract/pytesseract.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from tempfile import NamedTemporaryFile
from time import sleep

from packaging.version import InvalidVersion
from packaging.version import parse
from packaging.version import Version
from PIL import Image
Expand Down Expand Up @@ -387,13 +388,14 @@ def get_tesseract_version():
raise TesseractNotFoundError()

raw_version = output.decode(DEFAULT_ENCODING)
strip_version = raw_version.lstrip(string.printable[10:])
str_version, *_ = raw_version.lstrip(string.printable[10:]).partition(' ')
str_version, *_ = str_version.partition('-')

try:
version = parse(strip_version)
assert version > TESSERACT_MIN_VERSION
except AttributeError:
raise SystemExit(f'Invalid tesseract version: "{strip_version}"')
version = parse(str_version)
assert version >= TESSERACT_MIN_VERSION
except (AssertionError, InvalidVersion):
raise SystemExit(f'Invalid tesseract version: "{raw_version}"')

return version

Expand Down
6 changes: 3 additions & 3 deletions tests/pytesseract_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,14 +448,14 @@ def test_file_to_dict(input_args, expected):
('tesseract_version', 'expected'),
(
(b'3.5.0', '3.5.0'),
(b'4.1-a8s6f8d3f', '4.1-a8s6f8d3f'),
(b'v4.0.0-beta1.9', '4.0.0-beta1.9'),
(b'4.1-a8s6f8d3f', '4.1'),
(b'v4.0.0-beta1.9', '4.0.0'),
),
)
def test_get_tesseract_version(tesseract_version, expected):
with mock.patch('subprocess.check_output', spec=True) as output_mock:
output_mock.return_value = tesseract_version
assert get_tesseract_version.__wrapped__() == expected
assert get_tesseract_version.__wrapped__().public == expected


@pytest.mark.parametrize(
Expand Down

0 comments on commit 64e7c2f

Please sign in to comment.