diff --git a/CHANGELOG.md b/CHANGELOG.md index f0f4a4fb62..9168975d6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## 0.27.34-dev3 +## 0.27.34-dev4 ### Enhancements @@ -7,6 +7,8 @@ ### Fixes +- **Drop empty email address-group entries.** An empty group such as `undisclosed-recipients:;` now produces no recipient instead of an empty string. Groups with members retain their addresses. + - **Read a PowerPoint line break as a newline.** python-pptx returns a line break (Shift+Enter, `a:br`) as a vertical tab, so `partition_pptx()` put `\x0b` into the text of titles, text boxes and slide notes (`"Quarterly\x0bresults"`). It now reads `"Quarterly\nresults"`. - **`partition_json()` and `partition_ndjson()` now honor `encoding=`.** Both read the source with a hard-coded UTF-8 (`open(filename, encoding="utf8")`, and a bare `.decode()` for a file-like object), while the `encoding` argument callers pass -- directly or through `partition()` -- was swallowed by `**kwargs`. A UTF-16 or otherwise non-UTF-8 document therefore raised `UnicodeDecodeError` even when its encoding was stated explicitly. Both partitioners now accept `encoding`, and `partition()` forwards it to them. An explicit encoding is authoritative; when none is given, valid UTF-8 is preserved and charset detection is used only for bytes that are not valid UTF-8, so a detector's confident guess cannot silently rewrite text that would have decoded correctly. The existing file-like contract is unchanged: the stream is read once from its current position, an already-decoded `str` passes through, and the object is never reopened. diff --git a/test_unstructured/partition/test_email.py b/test_unstructured/partition/test_email.py index 6fd0f82f38..1faa963699 100644 --- a/test_unstructured/partition/test_email.py +++ b/test_unstructured/partition/test_email.py @@ -256,6 +256,23 @@ def test_partition_email_accepts_non_ascii_addresses(): assert element.metadata.sent_to == ["Jürgen ", "alice@example.com"] +def test_partition_email_drops_the_empty_entry_of_an_address_group(): + # -- RFC 5322 group syntax: "undisclosed-recipients:;" names a group with no members -- + msg = EmailMessage() + msg["From"] = "alice@example.com" + msg["To"] = "undisclosed-recipients:;" + msg["Cc"] = "Team: bob@example.com, carol@example.com;" + msg["Bcc"] = "undisclosed-recipients:;" + msg.set_content("Hello") + + element = partition_email(file=io.BytesIO(msg.as_bytes()))[0] + + assert element.metadata.sent_from == ["alice@example.com"] + assert element.metadata.sent_to is None + assert element.metadata.cc_recipient == ["bob@example.com", "carol@example.com"] + assert element.metadata.bcc_recipient is None + + # -- .metadata.filename -------------------------------------------------------------------------- diff --git a/unstructured/__version__.py b/unstructured/__version__.py index 2d6e9090c8..4cf95c41c0 100644 --- a/unstructured/__version__.py +++ b/unstructured/__version__.py @@ -1 +1 @@ -__version__ = "0.27.34-dev3" # pragma: no cover +__version__ = "0.27.34-dev4" # pragma: no cover diff --git a/unstructured/partition/email.py b/unstructured/partition/email.py index 6a4c5252d5..d47941db79 100644 --- a/unstructured/partition/email.py +++ b/unstructured/partition/email.py @@ -53,6 +53,18 @@ def _format_address(name: str, address: str) -> str: return f"{quotes}{name}{quotes} <{address}>" +def _format_addresses(headers: list[str]) -> list[str] | None: + """The addresses in `headers`, formatted, or None when there are none. + + An empty group such as "undisclosed-recipients:;" holds no address, and `getaddresses()` + reports it as `("", "")`, so entries without an address are dropped. + """ + addrs = [ + _format_address(name, addr) for name, addr in email.utils.getaddresses(headers) if addr + ] + return addrs or None + + @partition_runtime_telemetry("eml") def partition_email( filename: str | None = None, @@ -151,8 +163,7 @@ def bcc_addresses(self) -> list[str] | None: bccs = self.msg.get_all("Bcc") if not bccs: return None - addrs = email.utils.getaddresses(bccs) - return [_format_address(*addr) for addr in addrs] + return _format_addresses(bccs) @cached_property def body_part(self) -> MIMEPart | None: @@ -169,8 +180,7 @@ def cc_addresses(self) -> list[str] | None: ccs = self.msg.get_all("Cc") if not ccs: return None - addrs = email.utils.getaddresses(ccs) - return [_format_address(*addr) for addr in addrs] + return _format_addresses(ccs) @cached_property def content_type_preference(self) -> tuple[str, ...]: @@ -204,9 +214,8 @@ def from_address(self) -> str | None: if not froms: # -- this should never occur because the From: header is mandatory per RFC 5322 -- return None - addrs = email.utils.getaddresses(froms) - formatted_addrs = [_format_address(*addr) for addr in addrs] - return formatted_addrs[0] + formatted_addrs = _format_addresses(froms) + return formatted_addrs[0] if formatted_addrs else None @cached_property def message_id(self) -> str | None: @@ -294,8 +303,7 @@ def to_addresses(self) -> list[str] | None: tos = self.msg.get_all("To") if not tos: return None - addrs = email.utils.getaddresses(tos) - return [_format_address(*addr) for addr in addrs] + return _format_addresses(tos) @cached_property def _filesystem_last_modified(self) -> str | None: