Files
unstructured-io--unstructured/test_unstructured/file_utils/test_encoding.py
T
wehub-resource-sync 461bf6fd40
CI / lint (3.11) (push) Has been cancelled
CI / lint (3.12) (push) Has been cancelled
CI / lint (3.13) (push) Has been cancelled
CI / shellcheck (push) Has been cancelled
CI / shfmt (push) Has been cancelled
CI / setup (3.11) (push) Has been cancelled
CI / setup (3.12) (push) Has been cancelled
CI / setup (3.13) (push) Has been cancelled
CI / check-licenses (3.12) (push) Has been cancelled
CI / test_unit (3.11) (push) Has been cancelled
CI / test_unit (3.12) (push) Has been cancelled
CI / test_unit (3.13) (push) Has been cancelled
CI / test_unit_no_extras (3.11) (push) Has been cancelled
CI / test_unit_no_extras (3.12) (push) Has been cancelled
CI / test_json_to_html (3.12) (push) Has been cancelled
CI / test_unit_no_extras (3.13) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.12, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.11, --extra xlsx) (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.12, --extra xlsx) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.11, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.13, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.11, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.12, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.13, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.11, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.12, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.13, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.11, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.12, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.13, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.11, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.12, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.13, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.11, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.12, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.13, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.11, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.12, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.13, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
Build And Push Docker Image / set-short-sha (push) Has been cancelled
Partition Benchmark / setup (push) Has been cancelled
Partition Benchmark / Measure and compare partition() runtime (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.13, --extra xlsx) (push) Has been cancelled
CI / test_ingest_src (3.12) (push) Has been cancelled
CI / test_json_to_markdown (3.12) (push) Has been cancelled
CI / changelog (push) Has been cancelled
CI / test_dockerfile (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Build And Push Docker Image / build-images (linux/amd64, opensource-linux-8core) (push) Has been cancelled
Build And Push Docker Image / build-images (linux/arm64, ubuntu-24.04-arm) (push) Has been cancelled
Build And Push Docker Image / publish-images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:33:56 +08:00

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