Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
3cf4cc7f
Commit
3cf4cc7f
authored
Oct 26, 2002
by
Martin v. Löwis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Patch #613256: Add nescape method to xml.sax.saxutils.
parent
a9a7ac5f
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
58 additions
and
8 deletions
+58
-8
Doc/lib/xmlsaxutils.tex
Doc/lib/xmlsaxutils.tex
+11
-0
Lib/test/output/test_sax
Lib/test/output/test_sax
+4
-1
Lib/test/test_sax.py
Lib/test/test_sax.py
+13
-1
Lib/xml/sax/saxutils.py
Lib/xml/sax/saxutils.py
+26
-6
Misc/ACKS
Misc/ACKS
+1
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Doc/lib/xmlsaxutils.tex
View file @
3cf4cc7f
...
...
@@ -22,6 +22,17 @@ either in direct use, or as base classes.
strings; each key will be replaced with its corresponding value.
\end{funcdesc}
\begin{funcdesc}
{
unescape
}{
data
\optional
{
, entities
}}
Unescape
\character
{
\&
amp;
}
,
\character
{
\&
lt;
}
, and
\character
{
\&
gt;
}
in a string of data.
You can unescape other strings of data by passing a dictionary as the
optional
\var
{
entities
}
parameter. The keys and values must all be
strings; each key will be replaced with its corresponding value.
\versionadded
{
2.3
}
\end{funcdesc}
\begin{funcdesc}
{
quoteattr
}{
data
\optional
{
, entities
}}
Similar to
\function
{
escape()
}
, but also prepares
\var
{
data
}
to be
used as an attribute value. The return value is a quoted version of
...
...
Lib/test/output/test_sax
View file @
3cf4cc7f
...
...
@@ -29,6 +29,9 @@ Passed test_nsattrs_wattr
Passed test_quoteattr_basic
Passed test_single_double_quoteattr
Passed test_single_quoteattr
Passed test_unescape_all
Passed test_unescape_basic
Passed test_unescape_extra
Passed test_xmlgen_attr_escape
Passed test_xmlgen_basic
Passed test_xmlgen_content
...
...
@@ -36,4 +39,4 @@ Passed test_xmlgen_content_escape
Passed test_xmlgen_ignorable
Passed test_xmlgen_ns
Passed test_xmlgen_pi
37
tests, 0 failures
40
tests, 0 failures
Lib/test/test_sax.py
View file @
3cf4cc7f
...
...
@@ -8,7 +8,8 @@ try:
except
SAXReaderNotAvailable
:
# don't try to test this module if we cannot create a parser
raise
ImportError
(
"no XML parsers available"
)
from
xml.sax.saxutils
import
XMLGenerator
,
escape
,
quoteattr
,
XMLFilterBase
from
xml.sax.saxutils
import
XMLGenerator
,
escape
,
unescape
,
quoteattr
,
\
XMLFilterBase
from
xml.sax.expatreader
import
create_parser
from
xml.sax.xmlreader
import
InputSource
,
AttributesImpl
,
AttributesNSImpl
from
cStringIO
import
StringIO
...
...
@@ -70,6 +71,17 @@ def test_escape_all():
def
test_escape_extra
():
return
escape
(
"Hei p deg"
,
{
""
:
"å"
})
==
"Hei på deg"
# ===== unescape
def
test_unescape_basic
():
return
unescape
(
"Donald Duck & Co"
)
==
"Donald Duck & Co"
def
test_unescape_all
():
return
unescape
(
"<Donald Duck & Co>"
)
==
"<Donald Duck & Co>"
def
test_unescape_extra
():
return
unescape
(
"Hei p deg"
,
{
""
:
"å"
})
==
"Hei på deg"
# ===== quoteattr
def
test_quoteattr_basic
():
...
...
Lib/xml/sax/saxutils.py
View file @
3cf4cc7f
...
...
@@ -12,20 +12,40 @@ try:
except
AttributeError
:
_StringTypes
=
[
types
.
StringType
]
def
__dict_replace
(
s
,
d
):
"""Replace substrings of a string using a dictionary."""
for
key
,
value
in
d
.
items
():
s
=
s
.
replace
(
key
,
value
)
return
s
def
escape
(
data
,
entities
=
{}):
"""Escape &, <, and > in a string of data.
You can escape other strings of data by passing a dictionary as
the optional entities parameter. The keys and values must all be
strings; each key will be replaced with its corresponding value.
"""
# must do ampersand first
data
=
data
.
replace
(
"&"
,
"&"
)
data
=
data
.
replace
(
"<"
,
"<"
)
data
=
data
.
replace
(
">"
,
">"
)
for
chars
,
entity
in
entities
.
items
():
data
=
data
.
replace
(
chars
,
entity
)
return
data
data
=
__dict_replace
(
data
,
{
"<"
:
"<"
,
">"
:
">"
,
})
return
__dict_replace
(
data
,
entities
)
def
unescape
(
data
,
entities
=
{}):
"""Unescape &, <, and > in a string of data.
You can unescape other strings of data by passing a dictionary as
the optional entities parameter. The keys and values must all be
strings; each key will be replaced with its corresponding value.
"""
data
=
__dict_replace
(
data
,
{
"<"
:
"<"
,
">"
:
">"
,
})
# must do ampersand last
data
=
data
.
replace
(
"&"
,
"&"
)
return
__dict_replace
(
data
,
entities
)
def
quoteattr
(
data
,
entities
=
{}):
"""Escape and quote an attribute value.
...
...
Misc/ACKS
View file @
3cf4cc7f
...
...
@@ -56,6 +56,7 @@ Pablo Bleyer
Erik van Blokland
Finn Bock
Paul Boddie
Matthew Boedicker
David Bolen
Jurjen Bos
Peter Bosch
...
...
Misc/NEWS
View file @
3cf4cc7f
...
...
@@ -352,6 +352,9 @@ Extension modules
Library
-------
-
xml
.
sax
.
saxutils
.
unescape
has
been
added
,
to
replace
entity
references
with
their
entity
value
.
-
Queue
.
Queue
.{
put
,
get
}
now
support
an
optional
timeout
argument
.
-
Various
features
of
Tk
8.4
are
exposed
in
Tkinter
.
py
.
The
multiple
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment