]> granicus.if.org Git - python/commitdiff
#18020: improve html.escape speed by an order of magnitude. Patch by Matt Bryant.
authorEzio Melotti <ezio.melotti@gmail.com>
Sun, 7 Jul 2013 09:11:24 +0000 (11:11 +0200)
committerEzio Melotti <ezio.melotti@gmail.com>
Sun, 7 Jul 2013 09:11:24 +0000 (11:11 +0200)
Lib/html/__init__.py
Misc/ACKS
Misc/NEWS

index 02652ef73c312c9263a3394350c428a1bcbc09fb..2ad167f16af531c8279f985585effa6119b26167 100644 (file)
@@ -2,11 +2,6 @@
 General functions for HTML manipulation.
 """
 
-
-_escape_map = {ord('&'): '&amp;', ord('<'): '&lt;', ord('>'): '&gt;'}
-_escape_map_full = {ord('&'): '&amp;', ord('<'): '&lt;', ord('>'): '&gt;',
-                    ord('"'): '&quot;', ord('\''): '&#x27;'}
-
 # NB: this is a candidate for a bytes/string polymorphic interface
 
 def escape(s, quote=True):
@@ -16,6 +11,10 @@ def escape(s, quote=True):
     characters, both double quote (") and single quote (') characters are also
     translated.
     """
+    s = s.replace("&", "&amp;") # Must be done first!
+    s = s.replace("<", "&lt;")
+    s = s.replace(">", "&gt;")
     if quote:
-        return s.translate(_escape_map_full)
-    return s.translate(_escape_map)
+        s = s.replace('"', "&quot;")
+        s = s.replace('\'', "&#x27;")
+    return s
index ba7222ba22f328045100e2386d511b5709f46f7a..f0fefd5cdec3a90fca0d1b7d6e641168b278f25a 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -172,6 +172,7 @@ Dave Brueck
 Francisco Martín Brugué
 Ian Bruntlett
 Floris Bruynooghe
+Matt Bryant
 Stan Bubrouski
 Erik de Bueger
 Jan-Hein Bührman
index c3fbcb96576c49d50ad227646ee296918b27abe1..13694f5bf71e4c40408fa0bc1109841165419f89 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -142,6 +142,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #18020: improve html.escape speed by an order of magnitude.
+  Patch by Matt Bryant.
+
 - Issue #18347: ElementTree's html serializer now preserves the case of
   closing tags.