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

Skip to content

Commit 7fd7e36

Browse files
committed
Change pyexpat test suite to exercise the .returns_unicode attribute,
parsing the sample data once with 8-bit strings and once with Unicode.
1 parent beba056 commit 7fd7e36

2 files changed

Lines changed: 121 additions & 20 deletions

File tree

Lib/test/output/test_pyexpat

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,91 @@
11
test_pyexpat
22
PI:
3-
xml-stylesheet href="stylesheet.css"
3+
'xml-stylesheet' 'href="stylesheet.css"'
44
Comment:
55
' comment data '
66
Notation declared: ('notation', None, 'notation.jpeg', None)
77
Unparsed entity decl:
88
('unparsed_entity', None, 'entity.file', None, 'notation')
99
Start element:
10-
root {}
10+
'root' {'attr1': 'value1', 'attr2': 'value2\341\275\200'}
1111
NS decl:
12-
myns http://www.python.org/namespace
12+
'myns' 'http://www.python.org/namespace'
1313
Start element:
14-
http://www.python.org/namespace!subelement {}
14+
'http://www.python.org/namespace!subelement' {}
1515
Character data:
1616
'Contents of subelements'
1717
End element:
18-
http://www.python.org/namespace!subelement
18+
'http://www.python.org/namespace!subelement'
1919
End of NS decl:
20-
myns
20+
'myns'
2121
Start element:
22-
sub2 {}
22+
'sub2' {}
2323
Start of CDATA section
2424
Character data:
2525
'contents of CDATA section'
2626
End of CDATA section
2727
End element:
28-
sub2
29-
External entity ref: http://www.python.org/namespace=http://www.w3.org/XML/1998/namespace external_entity None entity.file None
28+
'sub2'
29+
External entity ref: ('http://www.python.org/namespace=http://www.w3.org/XML/1998/namespace\014external_entity', None, 'entity.file', None)
3030
End element:
31-
root
31+
'root'
32+
PI:
33+
u'xml-stylesheet' u'href="stylesheet.css"'
34+
Comment:
35+
u' comment data '
36+
Notation declared: (u'notation', None, u'notation.jpeg', None)
37+
Unparsed entity decl:
38+
(u'unparsed_entity', None, u'entity.file', None, u'notation')
39+
Start element:
40+
u'root' {u'attr1': u'value1', u'attr2': u'value2\u1F40'}
41+
NS decl:
42+
u'myns' u'http://www.python.org/namespace'
43+
Start element:
44+
u'http://www.python.org/namespace!subelement' {}
45+
Character data:
46+
u'Contents of subelements'
47+
End element:
48+
u'http://www.python.org/namespace!subelement'
49+
End of NS decl:
50+
u'myns'
51+
Start element:
52+
u'sub2' {}
53+
Start of CDATA section
54+
Character data:
55+
u'contents of CDATA section'
56+
End of CDATA section
57+
End element:
58+
u'sub2'
59+
External entity ref: (u'http://www.python.org/namespace=http://www.w3.org/XML/1998/namespace\014external_entity', None, u'entity.file', None)
60+
End element:
61+
u'root'
62+
PI:
63+
u'xml-stylesheet' u'href="stylesheet.css"'
64+
Comment:
65+
u' comment data '
66+
Notation declared: (u'notation', None, u'notation.jpeg', None)
67+
Unparsed entity decl:
68+
(u'unparsed_entity', None, u'entity.file', None, u'notation')
69+
Start element:
70+
u'root' {u'attr1': u'value1', u'attr2': u'value2\u1F40'}
71+
NS decl:
72+
u'myns' u'http://www.python.org/namespace'
73+
Start element:
74+
u'http://www.python.org/namespace!subelement' {}
75+
Character data:
76+
u'Contents of subelements'
77+
End element:
78+
u'http://www.python.org/namespace!subelement'
79+
End of NS decl:
80+
u'myns'
81+
Start element:
82+
u'sub2' {}
83+
Start of CDATA section
84+
Character data:
85+
u'contents of CDATA section'
86+
End of CDATA section
87+
End element:
88+
u'sub2'
89+
External entity ref: (u'http://www.python.org/namespace=http://www.w3.org/XML/1998/namespace\014external_entity', None, u'entity.file', None)
90+
End element:
91+
u'root'

Lib/test/test_pyexpat.py

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
class Outputter:
1212
def StartElementHandler(self, name, attrs):
13-
print 'Start element:\n\t', name, attrs
13+
print 'Start element:\n\t', repr(name), attrs
1414

1515
def EndElementHandler(self, name):
16-
print 'End element:\n\t', name
16+
print 'End element:\n\t', repr(name)
1717

1818
def CharacterDataHandler(self, data):
1919
data = string.strip(data)
@@ -22,13 +22,13 @@ def CharacterDataHandler(self, data):
2222
print '\t', repr(data)
2323

2424
def ProcessingInstructionHandler(self, target, data):
25-
print 'PI:\n\t', target, data
25+
print 'PI:\n\t', repr(target), repr(data)
2626

2727
def StartNamespaceDeclHandler(self, prefix, uri):
28-
print 'NS decl:\n\t', prefix, uri
28+
print 'NS decl:\n\t', repr(prefix), repr(uri)
2929

3030
def EndNamespaceDeclHandler(self, prefix):
31-
print 'End of NS decl:\n\t', prefix
31+
print 'End of NS decl:\n\t', repr(prefix)
3232

3333
def StartCdataSectionHandler(self):
3434
print 'Start of CDATA section'
@@ -51,8 +51,9 @@ def NotStandaloneHandler(self, userData):
5151
print 'Not standalone'
5252
return 1
5353

54-
def ExternalEntityRefHandler(self, context, base, sysId, pubId):
55-
print 'External entity ref:', context, base, sysId, pubId
54+
def ExternalEntityRefHandler(self, *args):
55+
context, base, sysId, pubId = args
56+
print 'External entity ref:', args
5657
return 1
5758

5859
def DefaultHandler(self, userData):
@@ -64,7 +65,14 @@ def DefaultHandlerExpand(self, userData):
6465

6566
out = Outputter()
6667
parser = pyexpat.ParserCreate(namespace_separator='!')
67-
for name in ['StartElementHandler', 'EndElementHandler',
68+
69+
# Test getting/setting returns_unicode
70+
parser.returns_unicode = 0 ; assert parser.returns_unicode == 0
71+
parser.returns_unicode = 1 ; assert parser.returns_unicode == 1
72+
parser.returns_unicode = 2 ; assert parser.returns_unicode == 1
73+
parser.returns_unicode = 0 ; assert parser.returns_unicode == 0
74+
75+
HANDLER_NAMES = ['StartElementHandler', 'EndElementHandler',
6876
'CharacterDataHandler', 'ProcessingInstructionHandler',
6977
'UnparsedEntityDeclHandler', 'NotationDeclHandler',
7078
'StartNamespaceDeclHandler', 'EndNamespaceDeclHandler',
@@ -73,7 +81,8 @@ def DefaultHandlerExpand(self, userData):
7381
'DefaultHandler', 'DefaultHandlerExpand',
7482
#'NotStandaloneHandler',
7583
'ExternalEntityRefHandler'
76-
]:
84+
]
85+
for name in HANDLER_NAMES:
7786
setattr(parser, name, getattr(out, name) )
7887

7988
data = """<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
@@ -88,7 +97,7 @@ def DefaultHandlerExpand(self, userData):
8897
%unparsed_entity;
8998
]>
9099
91-
<root>
100+
<root attr1="value1" attr2="value2&#8000;">
92101
<myns:subelement xmlns:myns="http://www.python.org/namespace">
93102
Contents of subelements
94103
</myns:subelement>
@@ -97,6 +106,8 @@ def DefaultHandlerExpand(self, userData):
97106
</root>
98107
"""
99108

109+
# Produce UTF-8 output
110+
parser.returns_unicode = 0
100111
try:
101112
parser.Parse(data, 1)
102113
except pyexpat.error:
@@ -105,3 +116,33 @@ def DefaultHandlerExpand(self, userData):
105116
print '** Column', parser.ErrorColumnNumber
106117
print '** Byte', parser.ErrorByteIndex
107118

119+
# Try the parse again, this time producing Unicode output
120+
parser = pyexpat.ParserCreate(namespace_separator='!')
121+
parser.returns_unicode = 1
122+
123+
for name in HANDLER_NAMES:
124+
setattr(parser, name, getattr(out, name) )
125+
try:
126+
parser.Parse(data, 1)
127+
except pyexpat.error:
128+
print '** Error', parser.ErrorCode, pyexpat.ErrorString( parser.ErrorCode)
129+
print '** Line', parser.ErrorLineNumber
130+
print '** Column', parser.ErrorColumnNumber
131+
print '** Byte', parser.ErrorByteIndex
132+
133+
# Try parsing a file
134+
parser = pyexpat.ParserCreate(namespace_separator='!')
135+
parser.returns_unicode = 1
136+
137+
for name in HANDLER_NAMES:
138+
setattr(parser, name, getattr(out, name) )
139+
import StringIO
140+
file = StringIO.StringIO(data)
141+
try:
142+
parser.ParseFile(file)
143+
except pyexpat.error:
144+
print '** Error', parser.ErrorCode, pyexpat.ErrorString( parser.ErrorCode)
145+
print '** Line', parser.ErrorLineNumber
146+
print '** Column', parser.ErrorColumnNumber
147+
print '** Byte', parser.ErrorByteIndex
148+

0 commit comments

Comments
 (0)