From e1d673c373a7d032060872b690a92fc95496612e Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Tue, 14 Dec 2021 12:38:28 +0100 Subject: [PATCH] Fixed unescape_string_literal() crash on empty strings. --- django/utils/text.py | 2 +- tests/utils_tests/test_text.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/django/utils/text.py b/django/utils/text.py index 3e4199a1d9..fa337650af 100644 --- a/django/utils/text.py +++ b/django/utils/text.py @@ -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'\\', '\\') diff --git a/tests/utils_tests/test_text.py b/tests/utils_tests/test_text.py index 5d6c53afdd..820a890bc5 100644 --- a/tests/utils_tests/test_text.py +++ b/tests/utils_tests/test_text.py @@ -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):