Skip to content

Commit 26bca9c

Browse files
committed
✨ Add encoding validation in parse_file()
1 parent fc6748d commit 26bca9c

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

bibtexparser/entrypoint.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import codecs
12
import warnings
23
from typing import Iterable
34
from typing import List
@@ -125,7 +126,14 @@ def parse_file(
125126
126127
:param encoding: Encoding of the .bib file. Default encoding is ``"UTF-8"``.
127128
:return: Library: Parsed BibTeX library
129+
:raises LookupError: If the specified encoding is not recognized.
128130
"""
131+
# Validate encoding before opening file to provide clearer error message
132+
try:
133+
codecs.lookup(encoding)
134+
except LookupError:
135+
raise LookupError(f"Unknown encoding: {encoding!r}")
136+
129137
with open(path, encoding=encoding) as f:
130138
bibtex_str = f.read()
131139
return parse_string(

0 commit comments

Comments
 (0)