From b198e04645804aa0cd07bce12b491533a9b439ec Mon Sep 17 00:00:00 2001 From: Michael Weiss Date: Wed, 14 Jan 2026 20:51:40 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20encoding=20validation=20in=20?= =?UTF-8?q?parse=5Ffile()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bibtexparser/entrypoint.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bibtexparser/entrypoint.py b/bibtexparser/entrypoint.py index 3024784..761bac9 100644 --- a/bibtexparser/entrypoint.py +++ b/bibtexparser/entrypoint.py @@ -1,3 +1,4 @@ +import codecs import warnings from typing import Iterable from typing import List @@ -125,7 +126,13 @@ def parse_file( :param encoding: Encoding of the .bib file. Default encoding is ``"UTF-8"``. :return: Library: Parsed BibTeX library + :raises LookupError: If the specified encoding is not recognized. """ + try: + codecs.lookup(encoding) + except LookupError: + raise LookupError(f"Unknown encoding: {encoding!r}") + with open(path, encoding=encoding) as f: bibtex_str = f.read() return parse_string(