Skip to content

Write of manually created duplicate blocks fails when no raw bibtex was specified #400

Closed

Description

@claell

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.

Activity

  1. added and removed on Sep 20, 2023
  2. MiWeiss commented on Sep 20, 2023

    @MiWeiss
    Collaborator

    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 a DuplicateBlockKeyBlock is 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.

  3. changed the title [-]Write of files with duplicate blocks fails[/-] [+]Write of files with manually created duplicate blocks fails when no raw bibtex was specified[/+] on Sep 20, 2023
  4. changed the title [-]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[/+] on Sep 20, 2023
  5. claell commented on Sep 21, 2023

    @claell
    ContributorAuthor

    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).

  6. MiWeiss commented on Jun 12, 2026

    @MiWeiss
    Collaborator

    Fixed in #526

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions