summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-04-22 03:37:20 +0200
committerVictor Stinner <victor.stinner@gmail.com>2015-04-22 03:37:20 +0200
commitb19f4f27189a29d3a1c4cf036ae51d35db83ea5a (patch)
tree2f5cf8da68e1222998285300b1bad4d8d8027994
parentaadcd3e4f4ac5c8251db1a7899d96c8ee279e04d (diff)
downloadpaste-b19f4f27189a29d3a1c4cf036ae51d35db83ea5a.tar.gz
Fix paste.util.html_quote(unicode): don't encode the string to escape it
-rw-r--r--docs/news.txt5
-rw-r--r--paste/util/quoting.py26
-rw-r--r--tests/test_util/test_quoting.py24
3 files changed, 30 insertions, 25 deletions
diff --git a/docs/news.txt b/docs/news.txt
index 9f90a39..d80d84b 100644
--- a/docs/news.txt
+++ b/docs/news.txt
@@ -3,6 +3,11 @@ News
.. contents::
+development version
+-------------------
+
+* Fix paste.util.html_quote(unicode): don't encode the string to escape it
+
2.0
---
diff --git a/paste/util/quoting.py b/paste/util/quoting.py
index 2e26434..9bd50c8 100644
--- a/paste/util/quoting.py
+++ b/paste/util/quoting.py
@@ -17,15 +17,6 @@ def html_quote(v, encoding=None):
r"""
Quote the value (turned to a string) as HTML. This quotes <, >,
and quotes:
-
- >>> html_quote(1)
- '1'
- >>> html_quote(None)
- ''
- >>> html_quote('<hey!>')
- '&lt;hey!&gt;'
- >>> html_quote(u'\u1029')
- '\xe1\x80\xa9'
"""
encoding = encoding or default_encoding
if v is None:
@@ -33,10 +24,7 @@ def html_quote(v, encoding=None):
elif isinstance(v, six.binary_type):
return cgi.escape(v, 1)
elif isinstance(v, six.text_type):
- if six.PY3:
- return cgi.escape(v, 1)
- else:
- return cgi.escape(v.encode(encoding), 1)
+ return cgi.escape(v, 1)
else:
if six.PY3:
return cgi.escape(six.text_type(v), 1)
@@ -55,20 +43,8 @@ def html_unquote(s, encoding=None):
r"""
Decode the value.
- >>> html_unquote('&lt;hey&nbsp;you&gt;')
- u'<hey\xa0you>'
- >>> html_unquote('')
- u''
- >>> html_unquote('&blahblah;')
- u'&blahblah;'
- >>> html_unquote('\xe1\x80\xa9')
- u'\u1029'
"""
if isinstance(s, six.binary_type):
- if s == '':
- # workaround re.sub('', '', u'') returning '' < 2.5.2
- # instead of u'' >= 2.5.2
- return u''
s = s.decode(encoding or default_encoding)
return _unquote_re.sub(_entity_subber, s)
diff --git a/tests/test_util/test_quoting.py b/tests/test_util/test_quoting.py
new file mode 100644
index 0000000..28790a4
--- /dev/null
+++ b/tests/test_util/test_quoting.py
@@ -0,0 +1,24 @@
+from paste.util import quoting
+import six
+import unittest
+
+class TestQuoting(unittest.TestCase):
+ def test_html_unquote(self):
+ self.assertEqual(quoting.html_unquote(b'&lt;hey&nbsp;you&gt;'),
+ u'<hey\xa0you>')
+ self.assertEqual(quoting.html_unquote(b''),
+ u'')
+ self.assertEqual(quoting.html_unquote(b'&blahblah;'),
+ u'&blahblah;')
+ self.assertEqual(quoting.html_unquote(b'\xe1\x80\xa9'),
+ u'\u1029')
+
+ def test_html_quote(self):
+ self.assertEqual(quoting.html_quote(1),
+ '1')
+ self.assertEqual(quoting.html_quote(None),
+ '')
+ self.assertEqual(quoting.html_quote('<hey!>'),
+ '&lt;hey!&gt;')
+ self.assertEqual(quoting.html_quote(u'<\u1029>'),
+ u'&lt;\u1029&gt;')