Skip to content

Commit 3272a3f

Browse files
authored
Merge branch 'main' into fix/improve-exception-handling-splitter
2 parents ee0f0e7 + af50222 commit 3272a3f

4 files changed

Lines changed: 44 additions & 23 deletions

File tree

bibtexparser/entrypoint.py

Lines changed: 14 additions & 7 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
@@ -20,9 +21,9 @@ def _build_parse_stack(
2021
) -> List[Middleware]:
2122
if parse_stack is not None and append_middleware is not None:
2223
raise ValueError(
23-
"Provided both parse_stack and append_middleware."
24-
"Only one should be provided."
25-
"(append_middleware should only be used with the default parse_stack,"
24+
"Provided both parse_stack and append_middleware. "
25+
"Only one should be provided. "
26+
"(append_middleware should only be used with the default parse_stack, "
2627
"i.e., when the passed parse_stack is None.)"
2728
)
2829

@@ -50,10 +51,10 @@ def _build_unparse_stack(
5051
) -> List[Middleware]:
5152
if unparse_stack is not None and prepend_middleware is not None:
5253
raise ValueError(
53-
"Provided both parse_stack and append_middleware."
54-
"Only one should be provided."
55-
"(prepend_middleware should only be used with the default parse_stack,"
56-
"i.e., when the passed parse_stack is None.)"
54+
"Provided both unparse_stack and prepend_middleware. "
55+
"Only one should be provided. "
56+
"(prepend_middleware should only be used with the default unparse_stack, "
57+
"i.e., when the passed unparse_stack is None.)"
5758
)
5859

5960
if unparse_stack is None:
@@ -125,7 +126,13 @@ 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+
try:
132+
codecs.lookup(encoding)
133+
except LookupError:
134+
raise LookupError(f"Unknown encoding: {encoding!r}")
135+
129136
with open(path, encoding=encoding) as f:
130137
bibtex_str = f.read()
131138
return parse_string(

bibtexparser/library.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,22 @@ def replace(self, old_block: Block, new_block: Block, fail_on_duplicate_key: boo
107107
def _cast_to_duplicate(
108108
prev_block_with_same_key: Union[Entry, String], duplicate: Union[Entry, String]
109109
):
110-
assert isinstance(prev_block_with_same_key, type(duplicate)) or isinstance(
111-
duplicate, type(prev_block_with_same_key)
112-
), (
113-
"Internal BibtexParser Error. Duplicate blocks share no common type."
114-
f"Found {type(prev_block_with_same_key)} and {type(duplicate)}, but both should be"
115-
f"either instance of String or instance of Entry."
116-
f"Please report this issue at the bibtexparser issue tracker.",
117-
)
118-
119-
assert (
120-
prev_block_with_same_key.key == duplicate.key
121-
), "Internal BibtexParser Error. Duplicate blocks have different keys."
110+
if not (
111+
isinstance(prev_block_with_same_key, type(duplicate))
112+
or isinstance(duplicate, type(prev_block_with_same_key))
113+
):
114+
raise ValueError(
115+
"Internal BibtexParser Error. Duplicate blocks share no common type. "
116+
f"Found {type(prev_block_with_same_key)} and {type(duplicate)}, but both should be "
117+
"either instance of String or instance of Entry. "
118+
"Please report this issue at the bibtexparser issue tracker."
119+
)
120+
121+
if prev_block_with_same_key.key != duplicate.key:
122+
raise ValueError(
123+
"Internal BibtexParser Error. Duplicate blocks have different keys. "
124+
"Please report this issue at the bibtexparser issue tracker."
125+
)
122126

123127
return DuplicateBlockKeyBlock(
124128
start_line=duplicate.start_line,

bibtexparser/middlewares/latex_encoding.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,15 @@ def __init__(
102102

103103
if encoder is not None and (keep_math is not None or enclose_urls is not None):
104104
raise ValueError(
105-
"Cannot specify both encoder and keep_math or enclose_urls."
105+
"Cannot specify both encoder and keep_math or enclose_urls. "
106106
"If you want to use a custom encoder, you have to specify it completely."
107107
)
108108

109+
if encoder is not None and not isinstance(encoder, UnicodeToLatexEncoder):
110+
raise TypeError(
111+
f"encoder must be a UnicodeToLatexEncoder instance, got {type(encoder).__name__}"
112+
)
113+
109114
# Defaults (not specified as defaults in args,
110115
# to make sure we can identify if they were specified)
111116
keep_math = keep_math if keep_math is not None else True
@@ -167,11 +172,16 @@ def __init__(
167172
if decoder is not None and (keep_braced_groups is not None or keep_math_mode is not None):
168173
raise ValueError(
169174
"Cannot specify both decoder and one of "
170-
"`keep_braced_groups` or `keep_math_mode`."
175+
"`keep_braced_groups` or `keep_math_mode`. "
171176
"If you want to use a custom decoder, "
172177
"you have to specify it completely."
173178
)
174179

180+
if decoder is not None and not isinstance(decoder, LatexNodes2Text):
181+
raise TypeError(
182+
f"decoder must be a LatexNodes2Text instance, got {type(decoder).__name__}"
183+
)
184+
175185
# Defaults (not specified as defaults in args,
176186
# to make sure we can identify if they were specified)
177187
keep_braced_groups = keep_braced_groups if keep_braced_groups is not None else False

bibtexparser/middlewares/names.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ def _transform_field_value(self, name) -> List[NameParts]:
164164
if not isinstance(name, list):
165165
raise ValueError(
166166
"Expected a list of strings, got {}. "
167-
"Make sure to use `SeparateCoAuthors` middleware"
168-
"before using `SplitNameParts` middleware".format(name)
167+
"Make sure to use `SeparateCoAuthors` middleware "
168+
"before using `SplitNameParts` middleware.".format(name)
169169
)
170170

171171
return [parse_single_name_into_parts(n) for n in name]

0 commit comments

Comments
 (0)