From 4603487dc941f2e6de04664058689f21a2ff349f Mon Sep 17 00:00:00 2001 From: Ezio Melotti Date: Sun, 7 Jul 2013 11:11:24 +0200 Subject: [PATCH] #18020: improve html.escape speed by an order of magnitude. Patch by Matt Bryant. --- 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 index 02652ef73c3..2ad167f16af 100644 --- 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('\''): '''} - # 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("&", "&") # 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('\'', "'") + return s diff --git a/Misc/ACKS b/Misc/ACKS index ba7222ba22f..f0fefd5cdec 100644 --- 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 diff --git a/Misc/NEWS b/Misc/NEWS index c3fbcb96576..13694f5bf71 100644 --- 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.