Skip to content

Commit f049ab0

Browse files
MiWeissclaude
andcommitted
Add encoding validation in parse_file() and fix write_file parameter bug
- Validate the encoding parameter before attempting to open the file. This provides a clearer error message when an invalid encoding is specified, rather than letting the error propagate from the open() call. - Fix bug from PR #489 where write_file() referenced undefined variables (parse_stack/append_middleware) instead of the renamed parameters (unparse_stack/prepend_middleware). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 6f5f161 commit f049ab0

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

bibtexparser/entrypoint.py

Lines changed: 10 additions & 2 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(
@@ -151,8 +159,8 @@ def write_file(
151159
:param bibtex_format: Customized BibTeX format to use (optional)."""
152160
bibtex_str = write_string(
153161
library=library,
154-
unparse_stack=parse_stack,
155-
prepend_middleware=append_middleware,
162+
unparse_stack=unparse_stack,
163+
prepend_middleware=prepend_middleware,
156164
bibtex_format=bibtex_format,
157165
)
158166
if isinstance(file, str):

0 commit comments

Comments
 (0)