461bf6fd40
CI / lint (3.11) (push) Blocked by required conditions
CI / lint (3.12) (push) Blocked by required conditions
CI / lint (3.13) (push) Blocked by required conditions
CI / shellcheck (push) Waiting to run
CI / shfmt (push) Waiting to run
CI / setup (3.11) (push) Waiting to run
CI / setup (3.12) (push) Waiting to run
CI / setup (3.13) (push) Waiting to run
CI / check-licenses (3.12) (push) Blocked by required conditions
CI / test_unit (3.11) (push) Blocked by required conditions
CI / test_unit (3.12) (push) Blocked by required conditions
CI / test_unit (3.13) (push) Blocked by required conditions
CI / test_unit_no_extras (3.11) (push) Blocked by required conditions
CI / test_unit_no_extras (3.12) (push) Blocked by required conditions
CI / test_json_to_html (3.12) (push) Blocked by required conditions
CI / test_unit_no_extras (3.13) (push) Blocked by required conditions
CI / test_unit_dependency_extras (csv, 3.11, --extra csv) (push) Blocked by required conditions
CI / test_unit_dependency_extras (csv, 3.12, --extra csv) (push) Blocked by required conditions
CI / test_unit_dependency_extras (csv, 3.13, --extra csv) (push) Blocked by required conditions
CI / test_unit_dependency_extras (docx, 3.11, --extra docx) (push) Blocked by required conditions
CI / test_unit_dependency_extras (docx, 3.12, --extra docx) (push) Blocked by required conditions
CI / test_unit_dependency_extras (docx, 3.13, --extra docx) (push) Blocked by required conditions
CI / test_unit_dependency_extras (markdown, 3.11, --extra md) (push) Blocked by required conditions
CI / test_unit_dependency_extras (markdown, 3.12, --extra md) (push) Blocked by required conditions
CI / test_unit_dependency_extras (markdown, 3.13, --extra md) (push) Blocked by required conditions
CI / test_unit_dependency_extras (odt, 3.11, --extra odt) (push) Blocked by required conditions
CI / test_unit_dependency_extras (odt, 3.12, --extra odt) (push) Blocked by required conditions
CI / test_unit_dependency_extras (pdf-image, 3.12, --extra pdf --extra image --extra paddleocr) (push) Blocked by required conditions
CI / test_unit_dependency_extras (pdf-image, 3.13, --extra pdf --extra image --extra paddleocr) (push) Blocked by required conditions
CI / test_unit_dependency_extras (pptx, 3.13, --extra pptx) (push) Blocked by required conditions
CI / test_unit_dependency_extras (odt, 3.13, --extra odt) (push) Blocked by required conditions
CI / test_unit_dependency_extras (pdf-image, 3.11, --extra pdf --extra image --extra paddleocr) (push) Blocked by required conditions
CI / test_unit_dependency_extras (pptx, 3.11, --extra pptx) (push) Blocked by required conditions
CI / test_unit_dependency_extras (pptx, 3.12, --extra pptx) (push) Blocked by required conditions
CI / test_unit_dependency_extras (pypandoc, 3.11, --extra epub --extra org --extra rtf --extra rst) (push) Blocked by required conditions
CI / test_unit_dependency_extras (pypandoc, 3.12, --extra epub --extra org --extra rtf --extra rst) (push) Blocked by required conditions
CI / test_unit_dependency_extras (pypandoc, 3.13, --extra epub --extra org --extra rtf --extra rst) (push) Blocked by required conditions
CI / test_unit_dependency_extras (xlsx, 3.11, --extra xlsx) (push) Blocked by required conditions
CI / test_unit_dependency_extras (xlsx, 3.12, --extra xlsx) (push) Blocked by required conditions
CI / test_unit_dependency_extras (xlsx, 3.13, --extra xlsx) (push) Blocked by required conditions
CI / test_ingest_src (3.12) (push) Blocked by required conditions
CI / test_json_to_markdown (3.12) (push) Blocked by required conditions
CI / changelog (push) Waiting to run
CI / test_dockerfile (push) Blocked by required conditions
CodeQL / Analyze (python) (push) Waiting to run
Build And Push Docker Image / set-short-sha (push) Waiting to run
Build And Push Docker Image / build-images (linux/amd64, opensource-linux-8core) (push) Blocked by required conditions
Build And Push Docker Image / build-images (linux/arm64, ubuntu-24.04-arm) (push) Blocked by required conditions
Build And Push Docker Image / publish-images (push) Blocked by required conditions
Partition Benchmark / setup (push) Waiting to run
Partition Benchmark / Measure and compare partition() runtime (push) Blocked by required conditions
72 lines
2.8 KiB
Python
72 lines
2.8 KiB
Python
"""Test encoding detection error handling (PR #4071)."""
|
|
|
|
import os
|
|
import pickle
|
|
import sys
|
|
import tempfile
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
from unstructured.errors import UnprocessableEntityError
|
|
from unstructured.file_utils.encoding import detect_file_encoding
|
|
|
|
|
|
def test_charset_detection_failure():
|
|
"""Test encoding detection failure with memory safety checks."""
|
|
large_data = b"\x80\x81\x82\x83" * 250_000 # 1MB of invalid UTF-8
|
|
|
|
with tempfile.NamedTemporaryFile(mode="wb", delete=False) as f:
|
|
f.write(large_data)
|
|
temp_file_path = f.name
|
|
|
|
try:
|
|
detect_result = {"encoding": None, "confidence": None}
|
|
with patch("unstructured.file_utils.encoding.detect", return_value=detect_result):
|
|
with patch("unstructured.file_utils.encoding.COMMON_ENCODINGS", ["utf_8"]): # Will fail
|
|
with pytest.raises(UnprocessableEntityError) as exc_info:
|
|
detect_file_encoding(filename=temp_file_path)
|
|
|
|
exception = exc_info.value
|
|
|
|
assert "Unable to determine file encoding" in str(exception)
|
|
|
|
# Ensure no .object attribute that would store file content (prevents memory bloat)
|
|
# See: https://docs.python.org/3/library/exceptions.html#UnicodeError.object
|
|
assert not hasattr(exception, "object")
|
|
|
|
# Exception should be lightweight regardless of file size
|
|
exception_memory = sys.getsizeof(exception)
|
|
serialized_size = len(pickle.dumps(exception))
|
|
|
|
assert exception_memory < 10_000 # Small in-memory footprint
|
|
assert serialized_size < 10_000 # Small serialization footprint
|
|
finally:
|
|
os.unlink(temp_file_path)
|
|
|
|
|
|
def test_decode_failure():
|
|
"""Test decode failure with memory safety checks."""
|
|
# Invalid UTF-16: BOM followed by odd number of bytes
|
|
invalid_utf16 = b"\xff\xfe" + b"A\x00B\x00" + b"\x00"
|
|
|
|
detect_result = {"encoding": "utf-16", "confidence": 0.95}
|
|
with patch("unstructured.file_utils.encoding.detect", return_value=detect_result):
|
|
with pytest.raises(UnprocessableEntityError) as exc_info:
|
|
detect_file_encoding(file=invalid_utf16)
|
|
|
|
exception = exc_info.value
|
|
|
|
assert "detected 'utf-16' but decode failed" in str(exception)
|
|
|
|
# Ensure no .object attribute that would store file content (prevents memory bloat)
|
|
# See: https://docs.python.org/3/library/exceptions.html#UnicodeError.object
|
|
assert not hasattr(exception, "object")
|
|
|
|
# Exception should be lightweight
|
|
exception_memory = sys.getsizeof(exception)
|
|
serialized_size = len(pickle.dumps(exception))
|
|
|
|
assert exception_memory < 10_000 # Small in-memory footprint
|
|
assert serialized_size < 10_000 # Small serialization footprint
|