Updated for 2.1b2 distribution.
[python/dscho.git] / Lib / test / test_pyexpat.py
blob0c400b4d6d1a1bb892d0e1c8837b19bbab787d8c
1 # Very simple test - Parse a file and print what happens
3 # XXX TypeErrors on calling handlers, or on bad return values from a
4 # handler, are obscure and unhelpful.
6 from xml.parsers import expat
8 class Outputter:
9 def StartElementHandler(self, name, attrs):
10 print 'Start element:\n\t', repr(name), attrs
12 def EndElementHandler(self, name):
13 print 'End element:\n\t', repr(name)
15 def CharacterDataHandler(self, data):
16 data = data.strip()
17 if data:
18 print 'Character data:'
19 print '\t', repr(data)
21 def ProcessingInstructionHandler(self, target, data):
22 print 'PI:\n\t', repr(target), repr(data)
24 def StartNamespaceDeclHandler(self, prefix, uri):
25 print 'NS decl:\n\t', repr(prefix), repr(uri)
27 def EndNamespaceDeclHandler(self, prefix):
28 print 'End of NS decl:\n\t', repr(prefix)
30 def StartCdataSectionHandler(self):
31 print 'Start of CDATA section'
33 def EndCdataSectionHandler(self):
34 print 'End of CDATA section'
36 def CommentHandler(self, text):
37 print 'Comment:\n\t', repr(text)
39 def NotationDeclHandler(self, *args):
40 name, base, sysid, pubid = args
41 print 'Notation declared:', args
43 def UnparsedEntityDeclHandler(self, *args):
44 entityName, base, systemId, publicId, notationName = args
45 print 'Unparsed entity decl:\n\t', args
47 def NotStandaloneHandler(self, userData):
48 print 'Not standalone'
49 return 1
51 def ExternalEntityRefHandler(self, *args):
52 context, base, sysId, pubId = args
53 print 'External entity ref:', args[1:]
54 return 1
56 def DefaultHandler(self, userData):
57 pass
59 def DefaultHandlerExpand(self, userData):
60 pass
63 def confirm(ok):
64 if ok:
65 print "OK."
66 else:
67 print "Not OK."
69 out = Outputter()
70 parser = expat.ParserCreate(namespace_separator='!')
72 # Test getting/setting returns_unicode
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)
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'
89 for name in HANDLER_NAMES:
90 setattr(parser, name, getattr(out, name))
92 data = '''\
93 <?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
94 <?xml-stylesheet href="stylesheet.css"?>
95 <!-- comment data -->
96 <!DOCTYPE quotations SYSTEM "quotations.dtd" [
97 <!ELEMENT root ANY>
98 <!NOTATION notation SYSTEM "notation.jpeg">
99 <!ENTITY acirc "&#226;">
100 <!ENTITY external_entity SYSTEM "entity.file">
101 <!ENTITY unparsed_entity SYSTEM "entity.file" NDATA notation>
102 %unparsed_entity;
105 <root attr1="value1" attr2="value2&#8000;">
106 <myns:subelement xmlns:myns="http://www.python.org/namespace">
107 Contents of subelements
108 </myns:subelement>
109 <sub2><![CDATA[contents of CDATA section]]></sub2>
110 &external_entity;
111 </root>
114 # Produce UTF-8 output
115 parser.returns_unicode = 0
116 try:
117 parser.Parse(data, 1)
118 except expat.error:
119 print '** Error', parser.ErrorCode, expat.ErrorString(parser.ErrorCode)
120 print '** Line', parser.ErrorLineNumber
121 print '** Column', parser.ErrorColumnNumber
122 print '** Byte', parser.ErrorByteIndex
124 # Try the parse again, this time producing Unicode output
125 parser = expat.ParserCreate(namespace_separator='!')
126 parser.returns_unicode = 1
128 for name in HANDLER_NAMES:
129 setattr(parser, name, getattr(out, name))
130 try:
131 parser.Parse(data, 1)
132 except expat.error:
133 print '** Error', parser.ErrorCode, expat.ErrorString(parser.ErrorCode)
134 print '** Line', parser.ErrorLineNumber
135 print '** Column', parser.ErrorColumnNumber
136 print '** Byte', parser.ErrorByteIndex
138 # Try parsing a file
139 parser = expat.ParserCreate(namespace_separator='!')
140 parser.returns_unicode = 1
142 for name in HANDLER_NAMES:
143 setattr(parser, name, getattr(out, name))
144 import StringIO
145 file = StringIO.StringIO(data)
146 try:
147 parser.ParseFile(file)
148 except expat.error:
149 print '** Error', parser.ErrorCode, expat.ErrorString(parser.ErrorCode)
150 print '** Line', parser.ErrorLineNumber
151 print '** Column', parser.ErrorColumnNumber
152 print '** Byte', parser.ErrorByteIndex
155 # Tests that make sure we get errors when the namespace_separator value
156 # is illegal, and that we don't for good values:
157 print
158 print "Testing constructor for proper handling of namespace_separator values:"
159 expat.ParserCreate()
160 expat.ParserCreate(namespace_separator=None)
161 expat.ParserCreate(namespace_separator=' ')
162 print "Legal values tested o.k."
163 try:
164 expat.ParserCreate(namespace_separator=42)
165 except TypeError, e:
166 print "Caught expected TypeError:"
167 print e
168 else:
169 print "Failed to catch expected TypeError."
170 try:
171 expat.ParserCreate(namespace_separator='too long')
172 except ValueError, e:
173 print "Caught expected ValueError:"
174 print e
175 else:
176 print "Failed to catch expected ValueError."
177 try:
178 expat.ParserCreate(namespace_separator='') # too short
179 except ValueError, e:
180 print "Caught expected ValueError:"
181 print e
182 else:
183 print "Failed to catch expected ValueError."