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

Skip to content

Commit 74b51ac

Browse files
committed
Patch #613256: Add nescape method to xml.sax.saxutils.
1 parent edb6bff commit 74b51ac

6 files changed

Lines changed: 58 additions & 8 deletions

File tree

Doc/lib/xmlsaxutils.tex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ \section{\module{xml.sax.saxutils} ---
2222
strings; each key will be replaced with its corresponding value.
2323
\end{funcdesc}
2424

25+
\begin{funcdesc}{unescape}{data\optional{, entities}}
26+
Unescape \character{\&}, \character{\<}, and \character{\>}
27+
in a string of data.
28+
29+
You can unescape other strings of data by passing a dictionary as the
30+
optional \var{entities} parameter. The keys and values must all be
31+
strings; each key will be replaced with its corresponding value.
32+
33+
\versionadded{2.3}
34+
\end{funcdesc}
35+
2536
\begin{funcdesc}{quoteattr}{data\optional{, entities}}
2637
Similar to \function{escape()}, but also prepares \var{data} to be
2738
used as an attribute value. The return value is a quoted version of

Lib/test/output/test_sax

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ Passed test_nsattrs_wattr
2929
Passed test_quoteattr_basic
3030
Passed test_single_double_quoteattr
3131
Passed test_single_quoteattr
32+
Passed test_unescape_all
33+
Passed test_unescape_basic
34+
Passed test_unescape_extra
3235
Passed test_xmlgen_attr_escape
3336
Passed test_xmlgen_basic
3437
Passed test_xmlgen_content
3538
Passed test_xmlgen_content_escape
3639
Passed test_xmlgen_ignorable
3740
Passed test_xmlgen_ns
3841
Passed test_xmlgen_pi
39-
37 tests, 0 failures
42+
40 tests, 0 failures

Lib/test/test_sax.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
except SAXReaderNotAvailable:
99
# don't try to test this module if we cannot create a parser
1010
raise ImportError("no XML parsers available")
11-
from xml.sax.saxutils import XMLGenerator, escape, quoteattr, XMLFilterBase
11+
from xml.sax.saxutils import XMLGenerator, escape, unescape, quoteattr, \
12+
XMLFilterBase
1213
from xml.sax.expatreader import create_parser
1314
from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl
1415
from cStringIO import StringIO
@@ -70,6 +71,17 @@ def test_escape_all():
7071
def test_escape_extra():
7172
return escape("Hei på deg", {"å" : "å"}) == "Hei på deg"
7273

74+
# ===== unescape
75+
76+
def test_unescape_basic():
77+
return unescape("Donald Duck & Co") == "Donald Duck & Co"
78+
79+
def test_unescape_all():
80+
return unescape("&lt;Donald Duck &amp; Co&gt;") == "<Donald Duck & Co>"
81+
82+
def test_unescape_extra():
83+
return unescape("Hei på deg", {"å" : "&aring;"}) == "Hei p&aring; deg"
84+
7385
# ===== quoteattr
7486

7587
def test_quoteattr_basic():

Lib/xml/sax/saxutils.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,40 @@
1212
except AttributeError:
1313
_StringTypes = [types.StringType]
1414

15+
def __dict_replace(s, d):
16+
"""Replace substrings of a string using a dictionary."""
17+
for key, value in d.items():
18+
s = s.replace(key, value)
19+
return s
1520

1621
def escape(data, entities={}):
1722
"""Escape &, <, and > in a string of data.
18-
23+
1924
You can escape other strings of data by passing a dictionary as
2025
the optional entities parameter. The keys and values must all be
2126
strings; each key will be replaced with its corresponding value.
2227
"""
28+
29+
# must do ampersand first
2330
data = data.replace("&", "&amp;")
24-
data = data.replace("<", "&lt;")
25-
data = data.replace(">", "&gt;")
26-
for chars, entity in entities.items():
27-
data = data.replace(chars, entity)
28-
return data
31+
data = __dict_replace(data, {"<" : "&lt;",
32+
">" : "&gt;",
33+
})
34+
return __dict_replace(data, entities)
35+
36+
def unescape(data, entities={}):
37+
"""Unescape &amp;, &lt;, and &gt; in a string of data.
38+
39+
You can unescape other strings of data by passing a dictionary as
40+
the optional entities parameter. The keys and values must all be
41+
strings; each key will be replaced with its corresponding value.
42+
"""
43+
data = __dict_replace(data, {"&lt;" : "<",
44+
"&gt;" : ">",
45+
})
46+
# must do ampersand last
47+
data = data.replace("&amp;", "&")
48+
return __dict_replace(data, entities)
2949

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

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Pablo Bleyer
5656
Erik van Blokland
5757
Finn Bock
5858
Paul Boddie
59+
Matthew Boedicker
5960
David Bolen
6061
Jurjen Bos
6162
Peter Bosch

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ Extension modules
352352
Library
353353
-------
354354

355+
- xml.sax.saxutils.unescape has been added, to replace entity references
356+
with their entity value.
357+
355358
- Queue.Queue.{put,get} now support an optional timeout argument.
356359

357360
- Various features of Tk 8.4 are exposed in Tkinter.py. The multiple

0 commit comments

Comments
 (0)