Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4603487

Browse files
committed
#18020: improve html.escape speed by an order of magnitude. Patch by Matt Bryant.
1 parent 071029f commit 4603487

3 files changed

Lines changed: 10 additions & 7 deletions

File tree

Lib/html/__init__.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
General functions for HTML manipulation.
33
"""
44

5-
6-
_escape_map = {ord('&'): '&amp;', ord('<'): '&lt;', ord('>'): '&gt;'}
7-
_escape_map_full = {ord('&'): '&amp;', ord('<'): '&lt;', ord('>'): '&gt;',
8-
ord('"'): '&quot;', ord('\''): '&#x27;'}
9-
105
# NB: this is a candidate for a bytes/string polymorphic interface
116

127
def escape(s, quote=True):
@@ -16,6 +11,10 @@ def escape(s, quote=True):
1611
characters, both double quote (") and single quote (') characters are also
1712
translated.
1813
"""
14+
s = s.replace("&", "&amp;") # Must be done first!
15+
s = s.replace("<", "&lt;")
16+
s = s.replace(">", "&gt;")
1917
if quote:
20-
return s.translate(_escape_map_full)
21-
return s.translate(_escape_map)
18+
s = s.replace('"', "&quot;")
19+
s = s.replace('\'', "&#x27;")
20+
return s

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ Dave Brueck
172172
Francisco Martín Brugué
173173
Ian Bruntlett
174174
Floris Bruynooghe
175+
Matt Bryant
175176
Stan Bubrouski
176177
Erik de Bueger
177178
Jan-Hein Bührman

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ Core and Builtins
142142
Library
143143
-------
144144

145+
- Issue #18020: improve html.escape speed by an order of magnitude.
146+
Patch by Matt Bryant.
147+
145148
- Issue #18347: ElementTree's html serializer now preserves the case of
146149
closing tags.
147150

0 commit comments

Comments
 (0)