mirror of
https://github.com/DS4SD/docling.git
synced 2025-08-01 15:02:21 +00:00
fix: Fixes and tests for StopIteration on .convert()
Signed-off-by: Christoph Auer <cau@zurich.ibm.com>
This commit is contained in:
parent
6666d9ec07
commit
7df9527733
@ -159,7 +159,7 @@ class DocumentConverter:
|
|||||||
raises_on_error: bool = True,
|
raises_on_error: bool = True,
|
||||||
max_num_pages: int = sys.maxsize,
|
max_num_pages: int = sys.maxsize,
|
||||||
max_file_size: int = sys.maxsize,
|
max_file_size: int = sys.maxsize,
|
||||||
) -> ConversionResult:
|
) -> Optional[ConversionResult]:
|
||||||
|
|
||||||
all_res = self.convert_all(
|
all_res = self.convert_all(
|
||||||
source=[source],
|
source=[source],
|
||||||
@ -167,7 +167,7 @@ class DocumentConverter:
|
|||||||
max_num_pages=max_num_pages,
|
max_num_pages=max_num_pages,
|
||||||
max_file_size=max_file_size,
|
max_file_size=max_file_size,
|
||||||
)
|
)
|
||||||
return next(all_res)
|
return next(all_res, None)
|
||||||
|
|
||||||
@validate_call(config=ConfigDict(strict=True))
|
@validate_call(config=ConfigDict(strict=True))
|
||||||
def convert_all(
|
def convert_all(
|
||||||
@ -186,7 +186,10 @@ class DocumentConverter:
|
|||||||
limits=limits,
|
limits=limits,
|
||||||
)
|
)
|
||||||
conv_res_iter = self._convert(conv_input, raises_on_error=raises_on_error)
|
conv_res_iter = self._convert(conv_input, raises_on_error=raises_on_error)
|
||||||
|
|
||||||
|
had_result = False
|
||||||
for conv_res in conv_res_iter:
|
for conv_res in conv_res_iter:
|
||||||
|
had_result = True
|
||||||
if raises_on_error and conv_res.status not in {
|
if raises_on_error and conv_res.status not in {
|
||||||
ConversionStatus.SUCCESS,
|
ConversionStatus.SUCCESS,
|
||||||
ConversionStatus.PARTIAL_SUCCESS,
|
ConversionStatus.PARTIAL_SUCCESS,
|
||||||
@ -197,6 +200,14 @@ class DocumentConverter:
|
|||||||
else:
|
else:
|
||||||
yield conv_res
|
yield conv_res
|
||||||
|
|
||||||
|
if not had_result:
|
||||||
|
if raises_on_error:
|
||||||
|
raise RuntimeError(
|
||||||
|
f"Conversion failed because the provided file has no recognizable format or it wasn't in the list of allowed formats."
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
def _convert(
|
def _convert(
|
||||||
self, conv_input: _DocumentConversionInput, raises_on_error: bool
|
self, conv_input: _DocumentConversionInput, raises_on_error: bool
|
||||||
) -> Iterator[ConversionResult]:
|
) -> Iterator[ConversionResult]:
|
||||||
|
51
tests/test_invalid_input.py
Normal file
51
tests/test_invalid_input.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
from io import BytesIO
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from docling.backend.docling_parse_backend import DoclingParseDocumentBackend
|
||||||
|
from docling.datamodel.base_models import ConversionStatus, DocumentStream, InputFormat
|
||||||
|
from docling.datamodel.pipeline_options import PdfPipelineOptions
|
||||||
|
from docling.document_converter import DocumentConverter, PdfFormatOption
|
||||||
|
|
||||||
|
from .verify_utils import verify_conversion_result_v1, verify_conversion_result_v2
|
||||||
|
|
||||||
|
GENERATE = False
|
||||||
|
|
||||||
|
|
||||||
|
def get_pdf_path():
|
||||||
|
|
||||||
|
pdf_path = Path("./tests/data/2305.03393v1-pg9.pdf")
|
||||||
|
return pdf_path
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def converter():
|
||||||
|
converter = DocumentConverter()
|
||||||
|
|
||||||
|
return converter
|
||||||
|
|
||||||
|
|
||||||
|
def test_convert_invalid_doc(converter: DocumentConverter):
|
||||||
|
|
||||||
|
# Test with unrecognizable file format (xyz)
|
||||||
|
result = converter.convert(
|
||||||
|
DocumentStream(name="input.xyz", stream=BytesIO(b"xyz")), raises_on_error=False
|
||||||
|
)
|
||||||
|
assert result is None # No result comes back at all, since this file is skipped.
|
||||||
|
|
||||||
|
with pytest.raises(RuntimeError):
|
||||||
|
result = converter.convert(
|
||||||
|
DocumentStream(name="input.xyz", stream=BytesIO(b"xyz")),
|
||||||
|
raises_on_error=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Test with too small filesize limit
|
||||||
|
result = converter.convert(get_pdf_path(), max_file_size=1, raises_on_error=False)
|
||||||
|
assert result is not None
|
||||||
|
assert result.status == ConversionStatus.FAILURE
|
||||||
|
|
||||||
|
with pytest.raises(RuntimeError):
|
||||||
|
result = converter.convert(
|
||||||
|
get_pdf_path(), max_file_size=1, raises_on_error=True
|
||||||
|
)
|
Loading…
Reference in New Issue
Block a user