Skip to content

Commit 9840964

Browse files
committed
Fix #2562: avoid crash with newlines in templates
Turns out! The $ character in Python regexes also matches before the last newline at the end of a string, not just at the end of a string. The \Z entity does what we really want: the *real* end of the string.
1 parent d0522d8 commit 9840964

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

beets/util/functemplate.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ def __init__(self, string, in_argument=False):
325325
# Common parsing resources.
326326
special_chars = (SYMBOL_DELIM, FUNC_DELIM, GROUP_OPEN, GROUP_CLOSE,
327327
ESCAPE_CHAR)
328-
special_char_re = re.compile(r'[%s]|$' %
328+
special_char_re = re.compile(r'[%s]|\Z' %
329329
u''.join(re.escape(c) for c in special_chars))
330330
escapable_chars = (SYMBOL_DELIM, FUNC_DELIM, GROUP_CLOSE, ARG_SEP)
331331
terminator_chars = (GROUP_CLOSE,)
@@ -343,8 +343,11 @@ def parse_expression(self):
343343
if self.in_argument:
344344
extra_special_chars = (ARG_SEP,)
345345
special_char_re = re.compile(
346-
r'[%s]|$' % u''.join(re.escape(c) for c in
347-
self.special_chars + extra_special_chars))
346+
r'[%s]|\Z' % u''.join(
347+
re.escape(c) for c in
348+
self.special_chars + extra_special_chars
349+
)
350+
)
348351

349352
text_parts = []
350353

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Fixes:
106106
error message. Thanks to :user:`Mary011196`. :bug:`1676` :bug:`2508`
107107
* :doc:`/plugins/web`: Avoid a crash when sending binary data, such as
108108
Chromaprint fingerprints, in music attributes. :bug:`2542` :bug:`2532`
109+
* Fix a hang when parsing templates that end in newlines. :bug:`2562`
109110

110111
Two plugins had backends removed due to bitrot:
111112

test/test_template.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ def test_sep_with_symbols(self):
227227
self.assertEqual(parts[2], u',')
228228
self._assert_symbol(parts[3], u"bar")
229229

230+
def test_newline_at_end(self):
231+
parts = list(_normparse(u'foo\n'))
232+
self.assertEqual(len(parts), 1)
233+
self.assertEqual(parts[0], u'foo\n')
234+
230235

231236
class EvalTest(unittest.TestCase):
232237
def _eval(self, template):

0 commit comments

Comments
 (0)