python-bibtexparser icon indicating copy to clipboard operation
python-bibtexparser copied to clipboard

Exception caused by using both doi and convert_to_unicode customizations

Open jkkummerfeld opened this issue 6 years ago • 2 comments

I get a crash when I run the following code:

import io

import bibtexparser
from bibtexparser.bparser import BibTexParser
from bibtexparser.customization import *

example = """
@Article{key,
  doi = {0000},
}
"""

def customizations(record):
    record = doi(record)
    record = convert_to_unicode(record)
    return record

parser = BibTexParser()
parser.customization = customizations
bib_data = bibtexparser.load(io.StringIO(example), parser=parser)

The error is:

Traceback (most recent call last):
  File "./bug.py", line 24, in <module>
    bib_data = bibtexparser.load(io.StringIO(example), parser=parser)
  File "/usr/local/lib/python3.7/site-packages/bibtexparser/__init__.py", line 71, in load
    return parser.parse_file(bibtex_file)
  File "/usr/local/lib/python3.7/site-packages/bibtexparser/bparser.py", line 177, in parse_file
    return self.parse(file.read(), partial=partial)
  File "/usr/local/lib/python3.7/site-packages/bibtexparser/bparser.py", line 155, in parse
    self._expr.parseFile(bibtex_file_obj)
  File "/usr/local/lib/python3.7/site-packages/bibtexparser/bibtexexpression.py", line 286, in parseFile
    return self.main_expression.parseFile(file_obj, parseAll=True)
  File "/usr/local/lib/python3.7/site-packages/pyparsing.py", line 2362, in parseFile
    return self.parseString(file_contents, parseAll)
  File "/usr/local/lib/python3.7/site-packages/pyparsing.py", line 1804, in parseString
    loc, tokens = self._parse( instring, 0 )
  File "/usr/local/lib/python3.7/site-packages/pyparsing.py", line 1548, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/local/lib/python3.7/site-packages/pyparsing.py", line 4361, in parseImpl
    return super(ZeroOrMore, self).parseImpl(instring, loc, doActions)
  File "/usr/local/lib/python3.7/site-packages/pyparsing.py", line 4291, in parseImpl
    loc, tokens = self_expr_parse( instring, loc, doActions, callPreParse=False )
  File "/usr/local/lib/python3.7/site-packages/pyparsing.py", line 1548, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/local/lib/python3.7/site-packages/pyparsing.py", line 3871, in parseImpl
    ret = e._parse( instring, loc, doActions )
  File "/usr/local/lib/python3.7/site-packages/pyparsing.py", line 1581, in _parseNoCache
    tokens = fn( instring, tokensStart, retTokens )
  File "/usr/local/lib/python3.7/site-packages/pyparsing.py", line 1203, in wrapper
    ret = func(*args[limit[0]:])
  File "/usr/local/lib/python3.7/site-packages/bibtexparser/bparser.py", line 204, in <lambda>
    t.get('EntryType'), t.get('Key'), t.get('Fields'))
  File "/usr/local/lib/python3.7/site-packages/bibtexparser/bparser.py", line 299, in _add_entry
    d = self.customization(d)
  File "./bug.py", line 19, in customizations
    record = convert_to_unicode(record)
  File "/usr/local/lib/python3.7/site-packages/bibtexparser/customization.py", line 501, in convert_to_unicode
    latex_to_unicode(x) for x in record[val]
  File "/usr/local/lib/python3.7/site-packages/bibtexparser/customization.py", line 501, in <listcomp>
    latex_to_unicode(x) for x in record[val]
  File "/usr/local/lib/python3.7/site-packages/bibtexparser/latexenc.py", line 72, in latex_to_unicode
    string = string.replace("{", "").replace("}", "")
AttributeError: 'dict' object has no attribute 'replace'

This does not happen if either of the customizations is removed.

It looks like a bug in the doi customization because printing the string passed into latex_to_unicode gives the dictionary {'url': '0000', 'anchor': 'doi'}.

I am running on OS X 10.14.3 with Python 3.7.2. Thanks!

jkkummerfeld avatar Mar 05 '19 21:03 jkkummerfeld

Indeed. Thanks for reporting this. It is due to an incompatible design between some customization like link and doi and the rest of them. Anyway, it should at least be documented if not fixed.

omangin avatar Mar 06 '19 06:03 omangin

Got it, thanks for the follow up!

jkkummerfeld avatar Mar 07 '19 02:03 jkkummerfeld