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

Skip to content

Commit f55222d

Browse files
committed
Avoid calling __dict_replace() if we don't need to -- the call is much
more expensive than just doing to work needed, and these things seem to always turn into a bottleneck eventually.
1 parent 15941e6 commit f55222d

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

Lib/xml/sax/saxutils.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ def escape(data, entities={}):
2828

2929
# must do ampersand first
3030
data = data.replace("&", "&")
31-
data = __dict_replace(data, {"<" : "&lt;",
32-
">" : "&gt;",
33-
})
34-
return __dict_replace(data, entities)
31+
data = data.replace(">", "&gt;")
32+
data = data.replace("<", "&lt;")
33+
if entities:
34+
data = __dict_replace(data, entities)
35+
return data
3536

3637
def unescape(data, entities={}):
3738
"""Unescape &amp;, &lt;, and &gt; in a string of data.
@@ -40,12 +41,13 @@ def unescape(data, entities={}):
4041
the optional entities parameter. The keys and values must all be
4142
strings; each key will be replaced with its corresponding value.
4243
"""
43-
data = __dict_replace(data, {"&lt;" : "<",
44-
"&gt;" : ">",
45-
})
44+
data = data.replace("&lt;", "<")
45+
data = data.replace("&gt;", ">")
4646
# must do ampersand last
4747
data = data.replace("&amp;", "&")
48-
return __dict_replace(data, entities)
48+
if entities:
49+
data = __dict_replace(data, entities)
50+
return data
4951

5052
def quoteattr(data, entities={}):
5153
"""Escape and quote an attribute value.

0 commit comments

Comments
 (0)