diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2015-04-22 03:37:20 +0200 |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2015-04-22 03:37:20 +0200 |
commit | b19f4f27189a29d3a1c4cf036ae51d35db83ea5a (patch) | |
tree | 2f5cf8da68e1222998285300b1bad4d8d8027994 | |
parent | aadcd3e4f4ac5c8251db1a7899d96c8ee279e04d (diff) | |
download | paste-b19f4f27189a29d3a1c4cf036ae51d35db83ea5a.tar.gz |
Fix paste.util.html_quote(unicode): don't encode the string to escape it
-rw-r--r-- | docs/news.txt | 5 | ||||
-rw-r--r-- | paste/util/quoting.py | 26 | ||||
-rw-r--r-- | tests/test_util/test_quoting.py | 24 |
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!>') - '<hey!>' - >>> 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('<hey you>') - 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'<hey you>'), + 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!>'), + '<hey!>') + self.assertEqual(quoting.html_quote(u'<\u1029>'), + u'<\u1029>') |