diff options
| author | Florian Apolloner <florian@apolloner.eu> | 2021-12-14 12:38:28 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-12-14 20:19:44 +0100 |
| commit | e1d673c373a7d032060872b690a92fc95496612e (patch) | |
| tree | 2bebd60e8cc58029761fd1b55cfc8f3598be6676 | |
| parent | 5d9c512e5b42e1c686300c214453bf0b820d7326 (diff) | |
Fixed unescape_string_literal() crash on empty strings.
| -rw-r--r-- | django/utils/text.py | 2 | ||||
| -rw-r--r-- | 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): |
