Skip to content

Commit 9f1047f

Browse files
committed
fix: close progress bars on exception in io.py
Wrap progress bar usage in try/finally blocks in save_annotated_documents and download_text_from_url so progress_bar.close() is always called even when an exception is raised mid-iteration. Fixes #401
1 parent 6ec610a commit 9f1047f

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

langextract/io.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,19 @@ def save_annotated_documents(
117117
output_path=str(output_file), disable=not show_progress
118118
)
119119

120-
with open(output_file, 'w', encoding='utf-8') as f:
121-
for adoc in annotated_documents:
122-
if not adoc.document_id:
123-
continue
124-
125-
doc_dict = data_lib.annotated_document_to_dict(adoc)
126-
f.write(json.dumps(doc_dict, ensure_ascii=False) + '\n')
127-
has_data = True
128-
doc_count += 1
129-
progress_bar.update(1)
130-
131-
progress_bar.close()
120+
try:
121+
with open(output_file, 'w', encoding='utf-8') as f:
122+
for adoc in annotated_documents:
123+
if not adoc.document_id:
124+
continue
125+
126+
doc_dict = data_lib.annotated_document_to_dict(adoc)
127+
f.write(json.dumps(doc_dict, ensure_ascii=False) + '\n')
128+
has_data = True
129+
doc_count += 1
130+
progress_bar.update(1)
131+
finally:
132+
progress_bar.close()
132133

133134
if not has_data:
134135
raise InvalidDatasetError(f'No documents to save in: {output_file}')
@@ -305,12 +306,13 @@ def download_text_from_url(
305306
total_size=total_size, url=url
306307
)
307308

308-
for chunk in response.iter_content(chunk_size=chunk_size):
309-
if chunk:
310-
chunks.append(chunk)
311-
progress_bar.update(len(chunk))
312-
313-
progress_bar.close()
309+
try:
310+
for chunk in response.iter_content(chunk_size=chunk_size):
311+
if chunk:
312+
chunks.append(chunk)
313+
progress_bar.update(len(chunk))
314+
finally:
315+
progress_bar.close()
314316
else:
315317
# Download without progress bar
316318
for chunk in response.iter_content(chunk_size=chunk_size):

0 commit comments

Comments
 (0)