Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.27.3

### Fixes

- **Write LabelBox staging text files as UTF-8**: `stage_for_label_box()` opened each `<external-id>.txt` file in text mode without an explicit encoding, so the element text was encoded with the platform's locale codec. Any element text outside that codec raised `UnicodeEncodeError` (Windows `cp1252`, or a POSIX/`C` locale in a container), and where it did not raise, the staged file was written in the locale encoding while LabelBox reads it as UTF-8. The other staging serializers in `unstructured.staging.base` already pass an explicit encoding.

## 0.27.2

### Fixes
Expand Down
33 changes: 32 additions & 1 deletion test_unstructured/staging/test_label_box.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import builtins
import os

import pytest
Expand Down Expand Up @@ -139,5 +140,35 @@ def test_stage_for_label_box(
assert element_config["data"].endswith(f"{element_config['externalId']}.txt")

output_filepath = os.path.join(output_directory, f"{element_config['externalId']}.txt")
with open(output_filepath) as data_file:
with open(output_filepath, encoding="utf-8") as data_file:
assert data_file.read().strip() == element.text.strip()


def test_stage_for_label_box_writes_text_files_as_utf8(output_directory, url_prefix, monkeypatch):
"""Element text is staged as UTF-8 rather than in the platform's locale encoding.

LabelBox reads the staged files as UTF-8, and text outside the locale codec (for example
``cp1252`` on Windows, or ASCII under a POSIX/``C`` locale) otherwise raises
``UnicodeEncodeError``. Record the encoding each text-mode ``open()`` is given so the
assertion holds on a UTF-8 platform too.
"""
text = "Sales figures — 第一季度 café"
text_mode_encodings = []
real_open = builtins.open

def recording_open(file, mode="r", buffering=-1, encoding=None, *args, **kwargs):
if "b" not in mode:
text_mode_encodings.append(encoding)
return real_open(file, mode, buffering, encoding, *args, **kwargs)

monkeypatch.setattr(builtins, "open", recording_open)
config = label_box.stage_for_label_box(
[NarrativeText(text=text)], output_directory, url_prefix, create_directory=True
)
monkeypatch.undo()

assert text_mode_encodings == ["utf-8"]

output_filepath = os.path.join(output_directory, f"{config[0]['externalId']}.txt")
with open(output_filepath, "rb") as data_file:
assert data_file.read().decode("utf-8") == text
2 changes: 1 addition & 1 deletion unstructured/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.27.2" # pragma: no cover
__version__ = "0.27.3" # pragma: no cover
2 changes: 1 addition & 1 deletion unstructured/staging/label_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def stage_for_label_box(
output_filename = f"{element_id}.txt"
data_url = "/".join([url_prefix.rstrip("/"), output_filename])
output_filepath = os.path.join(output_directory, output_filename)
with open(output_filepath, "w+") as output_text_file:
with open(output_filepath, "w+", encoding="utf-8") as output_text_file:
output_text_file.write(element.text)

element_config: Dict[str, Any] = {
Expand Down