From 5e739219de63328c13e14d60732ced4f28c033b9 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Fri, 30 Mar 2007 11:57:50 +0000 Subject: [PATCH] Fixed #3733 -- Fixed up quote parsing in smart_split(). Thanks, Ivan Chelubeev. git-svn-id: http://code.djangoproject.com/svn/django/trunk@4870 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + django/utils/text.py | 9 +++++---- tests/regressiontests/text/__init__.py | 0 tests/regressiontests/text/models.py | 0 tests/regressiontests/text/tests.py | 17 +++++++++++++++++ 5 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 tests/regressiontests/text/__init__.py create mode 100644 tests/regressiontests/text/models.py create mode 100644 tests/regressiontests/text/tests.py diff --git a/AUTHORS b/AUTHORS index 900335efbc..ad269d3f3b 100644 --- a/AUTHORS +++ b/AUTHORS @@ -64,6 +64,7 @@ answer newbie questions, and generally made Django that much better: Chris Chamberlin Amit Chakradeo ChaosKCW + ivan.chelubeev@gmail.com Ian Clelland crankycoder@gmail.com Matt Croydon diff --git a/django/utils/text.py b/django/utils/text.py index faf8705fa9..e75abc5638 100644 --- a/django/utils/text.py +++ b/django/utils/text.py @@ -191,14 +191,15 @@ def smart_split(text): Supports both single and double quotes, and supports escaping quotes with backslashes. In the output, strings will keep their initial and trailing quote marks. - >>> list(smart_split('This is "a person\'s" test.')) - ['This', 'is', '"a person\'s"', 'test.'] + + >>> list(smart_split('This is "a person\'s" test.')) + ['This', 'is', '"a person\'s"', 'test.'] """ for bit in smart_split_re.finditer(text): bit = bit.group(0) - if bit[0] == '"': + if bit[0] == '"' and bit[-1] == '"': yield '"' + bit[1:-1].replace('\\"', '"').replace('\\\\', '\\') + '"' - elif bit[0] == "'": + elif bit[0] == "'" and bit[-1] == "'": yield "'" + bit[1:-1].replace("\\'", "'").replace("\\\\", "\\") + "'" else: yield bit diff --git a/tests/regressiontests/text/__init__.py b/tests/regressiontests/text/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/text/models.py b/tests/regressiontests/text/models.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/text/tests.py b/tests/regressiontests/text/tests.py new file mode 100644 index 0000000000..f758ecaf90 --- /dev/null +++ b/tests/regressiontests/text/tests.py @@ -0,0 +1,17 @@ +""" +# Tests for stuff in django.utils.text. + +>>> from django.utils.text import * + +### smart_split ########################################################### +>>> list(smart_split(r'''This is "a person" test.''')) +['This', 'is', '"a person"', 'test.'] +>>> print list(smart_split(r'''This is "a person's" test.'''))[2] +"a person's" +>>> print list(smart_split(r'''This is "a person\\"s" test.'''))[2] +"a person"s" +>>> list(smart_split('''"a 'one''')) +['"a', "'one"] +>>> print list(smart_split(r'''all friends' tests'''))[1] +friends' +"""