Repository navigation
Write of manually created duplicate blocks fails when no raw bibtex was specified #400
Copy link
Copy link
Closed
Labels
Description
Describe the bug
Writing a library with duplicate blocks leads to:
WARNING:root:Unknown block type <class 'bibtexparser.model.DuplicateBlockKeyBlock'>
Traceback (most recent call last):
File "n:\Gruppenplatte\Veröffentlichungen\2023\Systems Engineering Journal (INCOSE)\Scopus retrieval\test.py", line 24, in <module>
bibtexparser.write_file("my_new_file.bib", bib_library)
File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\entrypoint.py", line 136, in write_file
bibtex_str = write_string(
^^^^^^^^^^^^^
File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\entrypoint.py", line 168, in write_string
return write(library, bibtex_format=bibtex_format)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\writer.py", line 106, in write
string_block_pieces = _treat_block(bibtex_format, block)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\writer.py", line 127, in _treat_block
string_block_pieces = _treat_failed_block(block, bibtex_format)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\writer.py", line 71, in _treat_failed_block
lines = len(block.raw.splitlines())
^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'splitlines'
PS N:\Gruppenplatte\Veröffentlichungen\2023\Systems Engineering Journal (INCOSE)> & C:/Users/Ellsel/AppData/Local/Programs/Python/Python311/python.exe "n:/Gruppenplatte/Veröffentlichungen/2023/Systems Engineering Journal (INCOSE)/Scopus retrieval/test.py"
{'test': Entry(entry_type=`article`, key=`test`, fields=`[Field(key=`author`, value=`test`, start_line=None)]`, start_line=None)}
WARNING:root:Unknown block type <class 'bibtexparser.model.DuplicateBlockKeyBlock'>
Traceback (most recent call last):
File "n:\Gruppenplatte\Veröffentlichungen\2023\Systems Engineering Journal (INCOSE)\Scopus retrieval\test.py", line 24, in <module>
bibtexparser.write_file("my_new_file.bib", bib_library)
File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\entrypoint.py", line 136, in write_file
bibtex_str = write_string(
^^^^^^^^^^^^^
File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\entrypoint.py", line 168, in write_string
return write(library, bibtex_format=bibtex_format)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\writer.py", line 106, in write
string_block_pieces = _treat_block(bibtex_format, block)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\writer.py", line 127, in _treat_block
string_block_pieces = _treat_failed_block(block, bibtex_format)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\writer.py", line 71, in _treat_failed_block
lines = len(block.raw.splitlines())
^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'splitlines'
Reproducing
Version: Latest from PyPI
import bibtexparser
from bibtexparser import *
bib_library = bibtexparser.Library()
fields = []
fields.append(bibtexparser.model.Field("author", "test"))
entry = bibtexparser.model.Entry("article", "test", fields)
entry1 = bibtexparser.model.Entry("article", "test", fields)
bib_library.add(entry)
bib_library.add(entry1)
print(bib_library.entries_dict)
bibtexparser.write_file("my_new_file.bib", bib_library)
Workaround
Checking for duplicate entries beforehand.
Remaining Questions (Optional)
Please tick all that apply:
- I would be willing to to contribute a PR to fix this issue.
- This issue is a blocker, I'd be greatful for an early fix.
Reactions are currently unavailable
Activity
MiWeiss commentedon Sep 20, 2023
When writing any ParsingFailedBlock, including duplicate key blocks, the writer defaults to writing the raw bibtex back which was observed when parsing. In your case, as you manually created the entries, you did not specify such raw bibtex (which is an optional constructor parameter). In that sense, the library is working as intended.
In principle, changing this would be rather easy, as we could have the writer write the
block.ignore_error_block(which is the block with the duplicate key) whenever aDuplicateBlockKeyBlockis faced. However, I'd be reluctant to merge such a change as the current rule "failed block => print raw" is nice and easy to understand and deviating from this will make things harder to grasp for most users.I need to think about this for a bit and will get back asap.
[-]Write of files with duplicate blocks fails[/-][+]Write of files with manually created duplicate blocks fails when no raw bibtex was specified[/+][-]Write of files with manually created duplicate blocks fails when no raw bibtex was specified[/-][+]Write of manually created duplicate blocks fails when no raw bibtex was specified[/+]claell commentedon Sep 21, 2023
How about only writing the block in case there is no raw content? One could also add some sanity check for the block, before writing it.
That's what I can currently think of (apart from changing the failed blocks logic).
MiWeiss commentedon Jun 12, 2026
Fixed in #526
🦖 Make Library.add fail on duplicate keys by default (#400)
🐛 Raise descriptive error when writing failed blocks without raw bibt…
🦖 Make Library.add fail on duplicate keys by default (#527)