Skip to content

Commit 6d4e517

Browse files
committed
Replace the html.escape() function from Py3.3 with the one from Py3.4
The newer function happens to be a polymorphic bytes/unicode interface on Py2. This should fix issue #108: TypeError: character mapping must return integer, None or unicode
1 parent 1eee2cf commit 6d4e517

File tree

2 files changed

+13
-36
lines changed

2 files changed

+13
-36
lines changed

src/future/moves/html/__init__.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
from __future__ import absolute_import, unicode_literals
1+
from __future__ import absolute_import
22
from future.utils import PY3
33
__future_module__ = True
44

55
if PY3:
66
from html import *
77
else:
8-
98
# cgi.escape isn't good enough for the single Py3.3 html test to pass.
10-
# Define it inline here instead. From the Py3.3 stdlib
9+
# Define it inline here instead. From the Py3.4 stdlib. Note that the
10+
# html.escape() function from the Py3.3 stdlib is not suitable for use on
11+
# Py2.x.
1112
"""
1213
General functions for HTML manipulation.
1314
"""
1415

15-
16-
_escape_map = {ord('&'): '&amp;', ord('<'): '&lt;', ord('>'): '&gt;'}
17-
_escape_map_full = {ord('&'): '&amp;', ord('<'): '&lt;', ord('>'): '&gt;',
18-
ord('"'): '&quot;', ord('\''): '&#x27;'}
19-
20-
# NB: this is a candidate for a bytes/string polymorphic interface
21-
2216
def escape(s, quote=True):
2317
"""
2418
Replace special characters "&", "<" and ">" to HTML-safe sequences.
2519
If the optional flag quote is true (the default), the quotation mark
2620
characters, both double quote (") and single quote (') characters are also
2721
translated.
2822
"""
23+
s = s.replace("&", "&amp;") # Must be done first!
24+
s = s.replace("<", "&lt;")
25+
s = s.replace(">", "&gt;")
2926
if quote:
30-
return s.translate(_escape_map_full)
31-
return s.translate(_escape_map)
27+
s = s.replace('"', "&quot;")
28+
s = s.replace('\'', "&#x27;")
29+
return s
30+
31+
__all__ = ['escape']

src/html/__init__.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,5 @@
44

55
if sys.version_info[0] == 3:
66
raise ImportError('Cannot import module from python-future source folder')
7-
87
else:
9-
# cgi.escape isn't good enough for the single Py3.3 html test to pass.
10-
# Define it inline here instead. From the Py3.3 stdlib
11-
"""
12-
General functions for HTML manipulation.
13-
"""
14-
15-
16-
_escape_map = {ord('&'): '&amp;', ord('<'): '&lt;', ord('>'): '&gt;'}
17-
_escape_map_full = {ord('&'): '&amp;', ord('<'): '&lt;', ord('>'): '&gt;',
18-
ord('"'): '&quot;', ord('\''): '&#x27;'}
19-
20-
# NB: this is a candidate for a bytes/string polymorphic interface
21-
22-
def escape(s, quote=True):
23-
"""
24-
Replace special characters "&", "<" and ">" to HTML-safe sequences.
25-
If the optional flag quote is true (the default), the quotation mark
26-
characters, both double quote (") and single quote (') characters are also
27-
translated.
28-
"""
29-
if quote:
30-
return s.translate(_escape_map_full)
31-
return s.translate(_escape_map)
8+
from future.moves.html import *

0 commit comments

Comments
 (0)