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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Fixes

- **Update README.md**: readme-only changes; added a link to Unstructured Pipelines to the README. No library behavior changes.
- **`_get_indexed_match` raises `ValueError` instead of `UnboundLocalError` on zero matches**: `extract_text_before()`/`extract_text_after()`'s underlying helper looped `for i, result in enumerate(re.finditer(pattern, text))` and referenced `i` in its "not found" error message; when the pattern matched zero times, the loop body never ran, so `i` was never bound, raising `UnboundLocalError` instead of the intended `ValueError`. `i` is now initialized to `-1` before the loop.

## 0.25.0

Expand Down
10 changes: 10 additions & 0 deletions test_unstructured/cleaners/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ def test_get_indexed_match_raises_with_index_too_high():
extract._get_indexed_match("BLAH BLAH BLAH", "BLAH", 4)


def test_get_indexed_match_raises_value_error_with_zero_matches():
"""Regression test: when the pattern has zero matches at all (not just an
out-of-range index on an otherwise-matching pattern), the "not found" branch
referenced the loop variable `i`, which Python never binds when a for-loop's
iterable is empty -- raising UnboundLocalError instead of the intended
ValueError."""
with pytest.raises(ValueError):
extract._get_indexed_match("no digits here", r"[0-9]+")


def test_extract_text_before():
text = "Teacher: BLAH BLAH BLAH; Student: BLAH BLAH BLAH!"
assert extract.extract_text_before(text, "BLAH", 1) == "Teacher: BLAH"
Expand Down
1 change: 1 addition & 0 deletions unstructured/cleaners/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def _get_indexed_match(text: str, pattern: str, index: int = 0) -> re.Match:
raise ValueError(f"The index is {index}. Index must be a non-negative integer.")

regex_match = None
i = -1
for i, result in enumerate(re.finditer(pattern, text)):
if i == index:
regex_match = result
Expand Down