Skip to content
Merged
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 0.27.34-dev3
## 0.27.34-dev4

### Enhancements

Expand All @@ -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.

Expand Down
17 changes: 17 additions & 0 deletions test_unstructured/partition/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,23 @@ def test_partition_email_accepts_non_ascii_addresses():
assert element.metadata.sent_to == ["Jürgen <jürgen@beispiel.de>", "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 --------------------------------------------------------------------------


Expand Down
2 changes: 1 addition & 1 deletion unstructured/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.27.34-dev3" # pragma: no cover
__version__ = "0.27.34-dev4" # pragma: no cover
26 changes: 17 additions & 9 deletions unstructured/partition/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand All @@ -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, ...]:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
Loading