(original) (raw)

changeset: 84481:db5f2b74e369 user: Ezio Melotti ezio.melotti@gmail.com date: Sun Jul 07 11:11:24 2013 +0200 files: Lib/html/__init__.py Misc/ACKS Misc/NEWS description: #18020: improve html.escape speed by an order of magnitude. Patch by Matt Bryant. diff -r d7a59e6f48df -r db5f2b74e369 Lib/html/__init__.py --- a/Lib/html/__init__.py Sun Jul 07 09:54:08 2013 +0200 +++ b/Lib/html/__init__.py Sun Jul 07 11:11:24 2013 +0200 @@ -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 @@ 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 -r d7a59e6f48df -r db5f2b74e369 Misc/ACKS --- a/Misc/ACKS Sun Jul 07 09:54:08 2013 +0200 +++ b/Misc/ACKS Sun Jul 07 11:11:24 2013 +0200 @@ -172,6 +172,7 @@ Francisco Martín Brugué Ian Bruntlett Floris Bruynooghe +Matt Bryant Stan Bubrouski Erik de Bueger Jan-Hein Bührman diff -r d7a59e6f48df -r db5f2b74e369 Misc/NEWS --- a/Misc/NEWS Sun Jul 07 09:54:08 2013 +0200 +++ b/Misc/NEWS Sun Jul 07 11:11:24 2013 +0200 @@ -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. /ezio.melotti@gmail.com