Fixed unescape_string_literal() crash on empty strings.

This commit is contained in:
Florian Apolloner 2021-12-14 12:38:28 +01:00 committed by Mariusz Felisiak
parent 5d9c512e5b
commit e1d673c373
2 changed files with 2 additions and 2 deletions

View File

@ -375,7 +375,7 @@ def unescape_string_literal(s):
>>> unescape_string_literal("'\'ab\' c'")
"'ab' c"
"""
if s[0] not in "\"'" or s[-1] != s[0]:
if not s or s[0] not in "\"'" or s[-1] != s[0]:
raise ValueError("Not a string literal: %r" % s)
quote = s[0]
return s[1:-1].replace(r'\%s' % quote, quote).replace(r'\\', '\\')

View File

@ -226,7 +226,7 @@ class TestUtilsText(SimpleTestCase):
self.assertEqual(text.unescape_string_literal(lazystr(value)), output)
def test_unescape_string_literal_invalid_value(self):
items = ['abc', "'abc\""]
items = ['', 'abc', "'abc\""]
for item in items:
msg = f'Not a string literal: {item!r}'
with self.assertRaisesMessage(ValueError, msg):