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

Skip to content

Commit 265a804

Browse files
committed
Revise the test case for pyexpat to avoid using asserts. Conform better
to the Python style guide, and remove unneeded imports.
1 parent 9e79a25 commit 265a804

2 files changed

Lines changed: 36 additions & 28 deletions

File tree

Lib/test/output/test_pyexpat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
test_pyexpat
2+
OK.
3+
OK.
4+
OK.
5+
OK.
26
PI:
37
'xml-stylesheet' 'href="stylesheet.css"'
48
Comment:

Lib/test/test_pyexpat.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# XXX TypeErrors on calling handlers, or on bad return values from a
44
# handler, are obscure and unhelpful.
55

6-
import sys, string
7-
import os
8-
96
import pyexpat
107

118
class Outputter:
@@ -16,7 +13,7 @@ def EndElementHandler(self, name):
1613
print 'End element:\n\t', repr(name)
1714

1815
def CharacterDataHandler(self, data):
19-
data = string.strip(data)
16+
data = data.strip()
2017
if data:
2118
print 'Character data:'
2219
print '\t', repr(data)
@@ -63,29 +60,37 @@ def DefaultHandlerExpand(self, userData):
6360
pass
6461

6562

63+
def confirm(ok):
64+
if ok:
65+
print "OK."
66+
else:
67+
print "Not OK."
68+
6669
out = Outputter()
6770
parser = pyexpat.ParserCreate(namespace_separator='!')
6871

6972
# 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',
76-
'CharacterDataHandler', 'ProcessingInstructionHandler',
77-
'UnparsedEntityDeclHandler', 'NotationDeclHandler',
78-
'StartNamespaceDeclHandler', 'EndNamespaceDeclHandler',
79-
'CommentHandler', 'StartCdataSectionHandler',
80-
'EndCdataSectionHandler',
81-
'DefaultHandler', 'DefaultHandlerExpand',
82-
#'NotStandaloneHandler',
83-
'ExternalEntityRefHandler'
84-
]
73+
parser.returns_unicode = 0; confirm(parser.returns_unicode == 0)
74+
parser.returns_unicode = 1; confirm(parser.returns_unicode == 1)
75+
parser.returns_unicode = 2; confirm(parser.returns_unicode == 1)
76+
parser.returns_unicode = 0; confirm(parser.returns_unicode == 0)
77+
78+
HANDLER_NAMES = [
79+
'StartElementHandler', 'EndElementHandler',
80+
'CharacterDataHandler', 'ProcessingInstructionHandler',
81+
'UnparsedEntityDeclHandler', 'NotationDeclHandler',
82+
'StartNamespaceDeclHandler', 'EndNamespaceDeclHandler',
83+
'CommentHandler', 'StartCdataSectionHandler',
84+
'EndCdataSectionHandler',
85+
'DefaultHandler', 'DefaultHandlerExpand',
86+
#'NotStandaloneHandler',
87+
'ExternalEntityRefHandler'
88+
]
8589
for name in HANDLER_NAMES:
86-
setattr(parser, name, getattr(out, name) )
90+
setattr(parser, name, getattr(out, name))
8791

88-
data = """<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
92+
data = '''\
93+
<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
8994
<?xml-stylesheet href="stylesheet.css"?>
9095
<!-- comment data -->
9196
<!DOCTYPE quotations SYSTEM "quotations.dtd" [
@@ -104,14 +109,14 @@ def DefaultHandlerExpand(self, userData):
104109
<sub2><![CDATA[contents of CDATA section]]></sub2>
105110
&external_entity;
106111
</root>
107-
"""
112+
'''
108113

109114
# Produce UTF-8 output
110115
parser.returns_unicode = 0
111116
try:
112117
parser.Parse(data, 1)
113118
except pyexpat.error:
114-
print '** Error', parser.ErrorCode, pyexpat.ErrorString( parser.ErrorCode)
119+
print '** Error', parser.ErrorCode, pyexpat.ErrorString(parser.ErrorCode)
115120
print '** Line', parser.ErrorLineNumber
116121
print '** Column', parser.ErrorColumnNumber
117122
print '** Byte', parser.ErrorByteIndex
@@ -121,11 +126,11 @@ def DefaultHandlerExpand(self, userData):
121126
parser.returns_unicode = 1
122127

123128
for name in HANDLER_NAMES:
124-
setattr(parser, name, getattr(out, name) )
129+
setattr(parser, name, getattr(out, name))
125130
try:
126131
parser.Parse(data, 1)
127132
except pyexpat.error:
128-
print '** Error', parser.ErrorCode, pyexpat.ErrorString( parser.ErrorCode)
133+
print '** Error', parser.ErrorCode, pyexpat.ErrorString(parser.ErrorCode)
129134
print '** Line', parser.ErrorLineNumber
130135
print '** Column', parser.ErrorColumnNumber
131136
print '** Byte', parser.ErrorByteIndex
@@ -135,14 +140,13 @@ def DefaultHandlerExpand(self, userData):
135140
parser.returns_unicode = 1
136141

137142
for name in HANDLER_NAMES:
138-
setattr(parser, name, getattr(out, name) )
143+
setattr(parser, name, getattr(out, name))
139144
import StringIO
140145
file = StringIO.StringIO(data)
141146
try:
142147
parser.ParseFile(file)
143148
except pyexpat.error:
144-
print '** Error', parser.ErrorCode, pyexpat.ErrorString( parser.ErrorCode)
149+
print '** Error', parser.ErrorCode, pyexpat.ErrorString(parser.ErrorCode)
145150
print '** Line', parser.ErrorLineNumber
146151
print '** Column', parser.ErrorColumnNumber
147152
print '** Byte', parser.ErrorByteIndex
148-

0 commit comments

Comments
 (0)