Repository: sciunto-org/python-bibtexparser
Pull request #214 (2018-08-20 15:00:39)

Fixes for #212
- [X] Prevents failures on short strings with no BOM.
- [x] Add tests for short strings with no BOM.
- [x] Check string definitions after the full value is checked (otherwise may fail while pyparsing is probing whether the current chunk is formatted as a value).

coveralls: 
[![Coverage Status](https://coveralls.io/builds/18571346/badge)](https://coveralls.io/builds/18571346)

Coverage decreased (-0.3%) to 97.336% when pulling **64d0a553fcce7100257b5232c4424396b4ceab02 on omangin:fix/212** into **3344a1aa8e3059a8283aa96c99a68f272d686d9d on sciunto-org:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/18571346/badge)](https://coveralls.io/builds/18571346)

Coverage decreased (-0.3%) to 97.336% when pulling **64d0a553fcce7100257b5232c4424396b4ceab02 on omangin:fix/212** into **3344a1aa8e3059a8283aa96c99a68f272d686d9d on sciunto-org:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/18571346/badge)](https://coveralls.io/builds/18571346)

Coverage decreased (-0.3%) to 97.395% when pulling **64d0a553fcce7100257b5232c4424396b4ceab02 on omangin:fix/212** into **3344a1aa8e3059a8283aa96c99a68f272d686d9d on sciunto-org:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/18571346/badge)](https://coveralls.io/builds/18571346)

Coverage decreased (-0.3%) to 97.395% when pulling **64d0a553fcce7100257b5232c4424396b4ceab02 on omangin:fix/212** into **3344a1aa8e3059a8283aa96c99a68f272d686d9d on sciunto-org:master**.


Patch:
diff --git a/CHANGELOG b/CHANGELOG
index 8a8fd15..87914a1 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -6,12 +6,19 @@ v1.2.0
 v1.1.0
 ======
 
+* Handles declarations on lines starting with spaces after comments (#199)
+* accept extra characters in @string (#148)
+* Add support for BibLaTeX annotations (#208)
+* Strip the latex command str for latex_to_unicode() (#182)
+* Add comma_last to BibTexWriter (#219)
+* Feature: crossref Support (#216)
 * BUGFIX: Fix for pyparsing 2.3.1 (#226)
 * NEW: Add support for BibLaTeX annotations (#208)
 * NEW: Feature: crossref Support (#216)
 * ENH: Handles declarations on lines starting with spaces after comments (#199)
 * ENH: Checks for empty citekeys and whitespaces (#213)
 
+
 v1.0.1
 ======
 
diff --git a/bibtexparser/__init__.py b/bibtexparser/__init__.py
index 692bd90..2c75560 100644
--- a/bibtexparser/__init__.py
+++ b/bibtexparser/__init__.py
@@ -25,7 +25,7 @@ __all__ = [
     'loads', 'load', 'dumps', 'dump', 'bibdatabase',
     'bparser', 'bwriter', 'bibtexexpression', 'latexenc', 'customization',
 ]
-__version__ = '1.1.0'
+__version__ = '1.2.0'
 
 from . import bibdatabase, bibtexexpression, bparser, bwriter, latexenc, customization
 
diff --git a/bibtexparser/bibtexexpression.py b/bibtexparser/bibtexexpression.py
index cf8bdd1..855d2e8 100644
--- a/bibtexparser/bibtexexpression.py
+++ b/bibtexparser/bibtexexpression.py
@@ -99,6 +99,8 @@ class BibtexExpression(object):
     ParseException = pp.ParseException
 
     def __init__(self):
+        # Init parse action functions
+        self.set_string_name_parse_action(lambda s, l, t: None)
 
         # Bibtex keywords
 
@@ -141,7 +143,6 @@ class BibtexExpression(object):
         string_expr = pp.delimitedList(
             (quoted_value | braced_value | string_name), delim='#'
             )('StringExpression')
-        self.set_string_expression_parse_action(lambda s, l, t: None)
         string_expr.addParseAction(self._string_expr_parse_action)
 
         value = (integer | string_expr)('Value')
@@ -270,17 +271,8 @@ class BibtexExpression(object):
     def _string_name_parse_action(self, s, l, t):
         return self._string_name_parse_action_fun(s, l, t)
 
-    def set_string_expression_parse_action(self, fun):
-        """Set the parseAction for string_expression expression.
-
-        .. Note::
-
-            See set_string_name_parse_action.
-        """
-        self._string_expr_parse_action_fun = fun
-
     def _string_expr_parse_action(self, s, l, t):
-        return self._string_expr_parse_action_fun(s, l, t)
+        return BibDataStringExpression.expression_if_needed(t)
 
     def parseFile(self, file_obj):
         return self.main_expression.parseFile(file_obj, parseAll=True)
diff --git a/bibtexparser/bparser.py b/bibtexparser/bparser.py
index 6f946de..133b6cb 100644
--- a/bibtexparser/bparser.py
+++ b/bibtexparser/bparser.py
@@ -178,14 +178,6 @@ class BibTexParser(object):
         self._expr.set_string_name_parse_action(
             lambda s, l, t:
                 BibDataString(self.bib_database, t[0]))
-        if self.interpolate_strings:
-            maybe_interpolate = lambda expr: as_text(expr)
-        else:
-            maybe_interpolate = lambda expr: expr
-        self._expr.set_string_expression_parse_action(
-            lambda s, l, t:
-                maybe_interpolate(
-                    BibDataStringExpression.expression_if_needed(t)))
 
         # Add notice to logger
         self._expr.add_log_function(logger.debug)
@@ -212,12 +204,13 @@ class BibTexParser(object):
     def _bibtex_file_obj(self, bibtex_str):
         # Some files have Byte-order marks inserted at the start
         byte = b'\xef\xbb\xbf'
+
         if isinstance(bibtex_str, str):
             byte = str(byte, self.encoding, 'ignore')
-            if bibtex_str[0] == byte:
+            if len(bibtex_str) >= 1 and bibtex_str[0] == byte:
                 bibtex_str = bibtex_str[1:]
         else:
-            if bibtex_str[:3] == byte:
+            if len(bibtex_str) >= 3 and bibtex_str[:3] == byte:
                 bibtex_str = bibtex_str[3:]
             bibtex_str = bibtex_str.decode(encoding=self.encoding)
         return io.StringIO(bibtex_str)
@@ -231,7 +224,10 @@ class BibTexParser(object):
         """
         if not val or val == "{}":
             return ''
-        return val
+        elif self.interpolate_strings:
+            return as_text(val)
+        else:
+            return val
 
     def _clean_key(self, key):
         """ Lowercase a key and return as unicode.


Test patch:
diff --git a/bibtexparser/tests/test_bparser.py b/bibtexparser/tests/test_bparser.py
index 516411a..dd6f6e1 100644
--- a/bibtexparser/tests/test_bparser.py
+++ b/bibtexparser/tests/test_bparser.py
@@ -53,6 +53,13 @@ def customizations_latex(record):
 
 class TestBibtexParserList(unittest.TestCase):
 
+    def test_empty_string(self):
+        bib = BibTexParser("")
+        self.assertEqual(bib.entries, [])
+        self.assertEqual(bib.comments, [])
+        self.assertEqual(bib.preambles, [])
+        self.assertEqual(bib.strings, {})
+
     ###########
     # ARTICLE
     ###########
@@ -612,6 +619,21 @@ class TestBibtexParserList(unittest.TestCase):
         self.assertEqual(res_dict, expected_dict)
         self.assertEqual(bib.preambles, ["Blah blah"])
 
+    def test_does_not_fail_on_non_bibtex_with_partial(self):
+        bibraw = '''@misc{this looks,
+          like = a = bibtex file but
+              , is not a real one!
+        '''
+        parser = BibTexParser()
+        bib = parser.parse(bibraw, partial=False)
+        self.assertEqual(bib.entries, [])
+        self.assertEqual(bib.preambles, [])
+        self.assertEqual(bib.strings, {})
+        self.assertEqual(bib.comments, [
+            '@misc{this looks,\n'
+            '          like = a = bibtex file but\n'
+            '              , is not a real one!'])
+
     def test_no_citekey_parsed_as_comment(self):
         bib = BibTexParser('@BOOK{, title = "bla"}')
         self.assertEqual(bib.entries, [])