From becd3e0d7a97bf0d64a4b77c0ff2befab8565c07 Mon Sep 17 00:00:00 2001 From: Bob Swift Date: Thu, 27 Feb 2020 16:30:00 -0700 Subject: [PATCH] Add IndexError check to $firstwords function --- picard/script.py | 9 ++++++--- test/test_script.py | 2 ++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/picard/script.py b/picard/script.py index 7acd6361b..822a8f9aa 100644 --- a/picard/script.py +++ b/picard/script.py @@ -863,9 +863,12 @@ def func_firstwords(parser, text, length): if len(text) <= length: return text else: - if text[length] == ' ': - return text[:length] - return text[:length].rsplit(' ', 1)[0] + try: + if text[length] == ' ': + return text[:length] + return text[:length].rsplit(' ', 1)[0] + except IndexError: + return '' @script_function() diff --git a/test/test_script.py b/test/test_script.py index b6e329801..af5cb2cfc 100644 --- a/test/test_script.py +++ b/test/test_script.py @@ -456,6 +456,8 @@ class ScriptParserTest(PicardTestCase): self.assertScriptResultEquals("$firstwords(Abc Def Ghi,0)", "") self.assertScriptResultEquals("$firstwords(Abc Def Ghi,NaN)", "") self.assertScriptResultEquals("$firstwords(Abc Def Ghi,)", "") + self.assertScriptResultEquals("$firstwords(Abc Def Ghi,-2)", "Abc Def") + self.assertScriptResultEquals("$firstwords(Abc Def Ghi,-50)", "") def test_cmd_startswith(self): self.assertScriptResultEquals("$startswith(abc,a)", "1")