[Python-checkins] cpython: #18020: improve html.escape speed by an order of magnitude. Patch by Matt

ezio.melotti python-checkins at python.org
Sun Jul 7 11:11:37 CEST 2013


http://hg.python.org/cpython/rev/db5f2b74e369
changeset:   84481:db5f2b74e369
user:        Ezio Melotti <ezio.melotti at gmail.com>
date:        Sun Jul 07 11:11:24 2013 +0200
summary:
  #18020: improve html.escape speed by an order of magnitude.  Patch by Matt Bryant.

files:
  Lib/html/__init__.py |  13 ++++++-------
  Misc/ACKS            |   1 +
  Misc/NEWS            |   3 +++
  3 files changed, 10 insertions(+), 7 deletions(-)


diff --git a/Lib/html/__init__.py b/Lib/html/__init__.py
--- a/Lib/html/__init__.py
+++ b/Lib/html/__init__.py
@@ -2,11 +2,6 @@
 General functions for HTML manipulation.
 """
 
-
-_escape_map = {ord('&'): '&', ord('<'): '<', ord('>'): '>'}
-_escape_map_full = {ord('&'): '&', ord('<'): '<', ord('>'): '>',
-                    ord('"'): '"', ord('\''): '&#x27;'}
-
 # NB: this is a candidate for a bytes/string polymorphic interface
 
 def escape(s, quote=True):
@@ -16,6 +11,10 @@
     characters, both double quote (") and single quote (') characters are also
     translated.
     """
+    s = s.replace("&", "&") # Must be done first!
+    s = s.replace("<", "<")
+    s = s.replace(">", ">")
     if quote:
-        return s.translate(_escape_map_full)
-    return s.translate(_escape_map)
+        s = s.replace('"', """)
+        s = s.replace('\'', "&#x27;")
+    return s
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -172,6 +172,7 @@
 Francisco Martín Brugué
 Ian Bruntlett
 Floris Bruynooghe
+Matt Bryant
 Stan Bubrouski
 Erik de Bueger
 Jan-Hein Bührman
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -142,6 +142,9 @@
 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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list