[Python-bugs-list] 1.5.2 & xmllib : runtime error (PR#29)
Sjoerd Mullender
sjoerd@oratrix.nl
Mon, 19 Jul 1999 13:13:22 +0200
You should call xmllib.XMLParser.__init__(self) from your __init__
method.
On Fri, Jul 16 1999 mkes@ra.rockwell.com wrote:
> Full_Name: Miroslav Kein s
> Version: 1.5.2
> OS: FreeBSD 3.2
> Submission from: (NULL) (205.175.223.11)
>
>
> In the 1.5.2 I have experienced following problem .
> I wrote a simple XML parser using xmllib (the source code and the XML example
> file is
> attached). When I run it it produces following runtime error:
>
>
> odysseus:/usr/home/mira/engine> python
> Python 1.5.2 (#2, May 11 1999, 17:14:37) [GCC 2.7.2.1] on freebsd3
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >>> import xmlparser
> >>> p = xmlparser.KBParser()
> >>> p.Run( '../xml/kb_test1.xml')
> Traceback (innermost last):
> File "<stdin>", line 1, in ?
> File "xmlparser.py", line 152, in Run
> self.close()
> File "/usr/local/lib/python1.5/xmllib.py", line 154, in close
> if self.__fixed:
> AttributeError: _XMLParser__fixed
> >>>
>
>
> The strange thing is that in Python 1.5.2b1 this runs OK !!!
>
> The source is here:
> --------------------------------------------------------------------
> import xmllib
> import sys
>
> ## class MyXMLParser( xmllib.XMLParser ):
>
> ## def unknown_starttag( self, tag, attributes ):
> ## print 'start tag: ' + tag + str( attributes )
>
> ## def unknown_endtag( self, tag ):
> ## print 'end tag: ' + tag
>
> ## def handle_data( self, data ):
> ## print data
>
> ## def run( self, filename ):
> ## self.reset()
> ## file = open( filename, 'r' )
> ## self.feed( file.read())
> ## self.close()
> ## file.close()
>
> class KBParser( xmllib.XMLParser ):
>
> KB = {}
> TagStack = []
> ReadData = 0
>
> def __init__( self ):
> self.attributes = { 'SYMPTOM_TEST': { 'IMPORTANCE' : 'MUST' }}
> self.elements = { 'KB': ( self.DictionaryTagStart, self.KBTagEnd ),
> 'QUESTION': ( self.DictionaryTagStart, self.AddDataAs_ListInDictionary_Item
> ),
> 'QUESTION_ID':( self.LeafTagStart, self.ValueSourceTagEnd ),
> 'TEXT':( self.LeafTagStart, self.GeneralTagEnd ),
> 'VALUE':( self.LeafTagStart, self.ValueTagEnd ),
> 'EXTERNAL_VARIABLE':( self.DictionaryTagStart,
> self.AddDataAs_ListInDictionary_Item ),
> 'EXTERNAL_VARIABLE_ID':( self.LeafTagStart, self.ValueSourceTagEnd ),
> 'DESCRIPTION':( self.LeafTagStart, self.GeneralTagEnd ),
> 'ATTRIBUTE':( self.DictionaryTagStart, self.AddDataAs_ListInDictionary_Item
> ),
> 'ATTRIBUTE_ID':( self.LeafTagStart, self.GeneralTagEnd ),
> 'SYMPTOM':( self.DictionaryTagStart, self.AddDataAs_ListInDictionary_Item
> ),
> 'SYMPTOM_ID':( self.LeafTagStart, self.SymptomIDTagEnd ),
> 'PARENT_ID':( self.LeafTagStart, self.GeneralTagEnd ),
> 'IDENTITY':( self.DictionaryTagStart, self.AddDataAs_ListInDictionary_Item
> ),
> 'DIAGNOSIS':( self.DictionaryTagStart, self.AddDataAs_ListInDictionary_Item
> ),
> 'DIAGNOSIS_ID':( self.LeafTagStart, self.GeneralTagEnd ),
> 'NAME':( self.LeafTagStart, self.GeneralTagEnd ),
> 'CASE':( self.ListTagStart, self.AddDataAs_ListInDictionary_Item ),
> 'SYMPTOM_TEST':( self.DictionaryTagStart, self.AddDataAs_ListInList_Item
> ),
> 'WEIGHT':( self.LeafTagStart, self.GeneralTagEnd ),
> 'CORRECTIVE_ACTION':( self.DictionaryTagStart,
> self.AddDataAs_ListInDictionary_Item ),
> 'CORRECTIVE_ACTION_ID':( self.LeafTagStart, self.GeneralTagEnd ) }
>
> def handle_starttag( self, tag, method, attributes ):
> method( tag, attributes )
>
> def handle_data( self, data ):
> if self.ReadData:
> self.TagStack[len( self.TagStack ) - 1]['dataStruct'] = data
> self.ReadData = 0
>
> def handle_endtag( self, tag, method ):
> method( tag )
>
> def DictionaryTagStart( self, tag, attributes ):
> self.TagStack.append( { 'tagName' : tag, 'dataStruct' : {}, 'attributes' :
> attributes } )
>
> def ListTagStart( self, tag, attributes ):
> self.TagStack.append( { 'tagName' : tag, 'dataStruct' : [], 'attributes' :
> attributes } )
>
> def LeafTagStart( self, tag, attributes ):
> self.TagStack.append( { 'tagName' : tag, 'dataStruct' : None, 'attributes' :
> attributes } )
> self.ReadData = 1
>
> def GeneralTagEnd( self, tag ):
> stackDepth = len( self.TagStack )
> tagStructure = self.TagStack[stackDepth - 1]
> container = self.TagStack[stackDepth - 2]
> containerDataStorage = container['dataStruct']
> if type(containerDataStorage) is type( [] ):
> containerDataStorage.append( { 'tag_value' : tagStructure['dataStruct'],
> 'tag_attribute' : tagStructure['attributes'] } )
> else:
> containerDataStorage[tagStructure['tagName']] = { 'tag_value' :
> tagStructure['dataStruct'], 'tag_attribute' : tagStructure['attributes'] }
> self.TagStack.remove( tagStructure )
>
> def AddDataAs_ListInDictionary_Item( self, tag ):
> stackDepth = len( self.TagStack )
> tagStructure = self.TagStack[stackDepth - 1]
> container = self.TagStack[stackDepth - 2]
> containerDataStorage = container['dataStruct']
> if containerDataStorage.has_key( tag ) == 0:
> containerDataStorage[tag] = []
> containerDataStorage[tag].append( { 'tag_value' : tagStructure['dataStruct'],
> 'tag_attribute' : tagStructure['attributes'] } )
> self.TagStack.remove( tagStructure )
>
> def AddDataAs_ListInList_Item( self, tag ):
> stackDepth = len( self.TagStack )
> tagStructure = self.TagStack[stackDepth - 1]
> container = self.TagStack[stackDepth - 2]
> containerDataStorage = container['dataStruct']
> containerDataStorage.append( { 'tag_value' : tagStructure['dataStruct'],
> 'tag_attribute' : tagStructure['attributes'] } )
> self.TagStack.remove( tagStructure )
>
> def KBTagEnd( self, tag ):
> self.KB = self.TagStack[0]['dataStruct']
> self.TagStack.remove( self.TagStack[0] )
>
> def ValueTagEnd( self, tag ):
> stackDepth = len( self.TagStack )
> container = self.TagStack[stackDepth - 2]
> containerName = container['tagName']
> if containerName == 'QUESTION' or containerName == 'EXTERNAL_VARIABLE':
> self.AddDataAs_ListInDictionary_Item( tag )
> elif containerName == 'IDENTITY':
> self.GeneralTagEnd( tag )
> else:
> pass
>
> def ValueSourceTagEnd( self, tag ):
> stackDepth = len( self.TagStack )
> container = self.TagStack[stackDepth - 2]
> containerName = container['tagName']
> if containerName == 'QUESTION' or containerName == 'EXTERNAL_VARIABLE' or
> containerName == 'IDENTITY':
> self.GeneralTagEnd( tag )
> elif containerName == 'SYMPTOM' or containerName == 'ATTRIBUTE':
> self.AddDataAs_ListInDictionary_Item( 'VALUE_SOURCE' )
> else:
> pass
>
> def SymptomIDTagEnd( self, tag ):
> stackDepth = len( self.TagStack )
> container = self.TagStack[stackDepth - 2]
> containerName = container['tagName']
> if containerName == 'SYMPTOM':
> self.GeneralTagEnd( tag )
> else:
> self.AddDataAs_ListInDictionary_Item( tag )
>
> def handle_doctype( self, tag, data, bla1, bla2 ):
> pass
>
> def unknown_starttag( self, tag, attributes ):
> print 'unknown start tag: ' + tag + str( attributes )
>
> def unknown_endtag( self, tag ):
> print 'unknown end tag: ' + tag
>
> def Run( self, filename ):
> self.reset()
> TagStack = []
> file = open( filename, 'r' )
> self.feed( file.read())
> self.close()
> file.close()
> return self.KB
>
> -------------------------------------------------------------------------
>
> Here is the XML file:
>
> -------------------------------------------------------
> <?xml version="1.0"?>
> <!-- DOCTYPE KB SYSTEM "kb.dtd" -->
>
> <KB>
> <QUESTION>
> <QUESTION_ID>Q1</QUESTION_ID>
> <TEXT>Are there any disturbancies in speed feedback ?</TEXT>
> <VALUE>Yes</VALUE>
> <VALUE>No</VALUE>
> </QUESTION>
>
> <QUESTION>
> <QUESTION_ID>Q2</QUESTION_ID>
> <TEXT>Are the disturbancies periodic ?</TEXT>
> <VALUE>Yes</VALUE>
> <VALUE>No</VALUE>
> </QUESTION>
>
> <QUESTION>
> <QUESTION_ID>Q3</QUESTION_ID>
> <TEXT>Is the frequency of oscillation proportional to the line speed ?</TEXT>
> <VALUE>Yes</VALUE>
> <VALUE>No</VALUE>
> </QUESTION>
>
> <QUESTION>
> <QUESTION_ID>Q4</QUESTION_ID>
> <TEXT>Does the problem disappear when the motor is disconnected from the
> gearbox?</TEXT>
> <VALUE>Yes</VALUE>
> <VALUE>No</VALUE>
> </QUESTION>
>
> <QUESTION>
> <QUESTION_ID>Q5</QUESTION_ID>
> <TEXT>What frequency matches the frequency of oscillation ?</TEXT>
> <VALUE>Motor speed</VALUE>
> <VALUE>Motor speed times 2</VALUE>
> <VALUE>Gerbox output speed</VALUE>
> <VALUE>Gerbox output speed times 2</VALUE>
> </QUESTION>
>
> <ATTRIBUTE>
> <ATTRIBUTE_ID>A1</ATTRIBUTE_ID>
> <QUESTION_ID>Q1</QUESTION_ID>
> <SYMPTOM>
> <SYMPTOM_ID>S1</SYMPTOM_ID>
> <PARENT_ID>A1</PARENT_ID>
> <IDENTITY>
> <QUESTION_ID>Q1</QUESTION_ID>
> <VALUE>Yes</VALUE>
> </IDENTITY>
> <QUESTION_ID>Q2</QUESTION_ID>
> </SYMPTOM>
> <SYMPTOM>
> <SYMPTOM_ID>S2</SYMPTOM_ID>
> <PARENT_ID>A1</PARENT_ID>
> <IDENTITY>
> <QUESTION_ID>Q1</QUESTION_ID>
> <VALUE>No</VALUE>
> </IDENTITY>
> </SYMPTOM>
> <SYMPTOM>
> <SYMPTOM_ID>S3</SYMPTOM_ID>
> <PARENT_ID>S1</PARENT_ID>
> <IDENTITY>
> <QUESTION_ID>Q2</QUESTION_ID>
> <VALUE>Yes</VALUE>
> </IDENTITY>
> <QUESTION_ID>Q3</QUESTION_ID>
> </SYMPTOM>
> <SYMPTOM>
> <SYMPTOM_ID>S4</SYMPTOM_ID>
> <PARENT_ID>S1</PARENT_ID>
> <IDENTITY>
> <QUESTION_ID>Q2</QUESTION_ID>
> <VALUE>No</VALUE>
> </IDENTITY>
> </SYMPTOM>
> <SYMPTOM>
> <SYMPTOM_ID>S5</SYMPTOM_ID>
> <PARENT_ID>S3</PARENT_ID>
> <IDENTITY>
> <QUESTION_ID>Q3</QUESTION_ID>
> <VALUE>Yes</VALUE>
> </IDENTITY>
> <QUESTION_ID>Q5</QUESTION_ID>
> </SYMPTOM>
> <SYMPTOM>
> <SYMPTOM_ID>S6</SYMPTOM_ID>
> <PARENT_ID>S5</PARENT_ID>
> <IDENTITY>
> <QUESTION_ID>Q5</QUESTION_ID>
> <VALUE>Motor speed</VALUE>
> </IDENTITY>
> </SYMPTOM>
> <SYMPTOM>
> <SYMPTOM_ID>S7</SYMPTOM_ID>
> <PARENT_ID>S5</PARENT_ID>
> <IDENTITY>
> <QUESTION_ID>Q5</QUESTION_ID>
> <VALUE>Motor speed times 2</VALUE>
> </IDENTITY>
> </SYMPTOM>
> <SYMPTOM>
> <SYMPTOM_ID>S8</SYMPTOM_ID>
> <PARENT_ID>S5</PARENT_ID>
> <IDENTITY>
> <QUESTION_ID>Q5</QUESTION_ID>
> <VALUE>Gerbox output speed</VALUE>
> </IDENTITY>
> </SYMPTOM>
> <SYMPTOM>
> <SYMPTOM_ID>S9</SYMPTOM_ID>
> <PARENT_ID>S5</PARENT_ID>
> <IDENTITY>
> <QUESTION_ID>Q5</QUESTION_ID>
> <VALUE>Gerbox output speed times 2</VALUE>
> </IDENTITY>
> </SYMPTOM>
> <SYMPTOM>
> <SYMPTOM_ID>S10</SYMPTOM_ID>
> <PARENT_ID>S3</PARENT_ID>
> <IDENTITY>
> <QUESTION_ID>Q3</QUESTION_ID>
> <VALUE>No</VALUE>
> </IDENTITY>
> </SYMPTOM>
> </ATTRIBUTE>
>
> <ATTRIBUTE TYPE="CONFIG">
> <ATTRIBUTE_ID>A2</ATTRIBUTE_ID>
> <QUESTION_ID>Q4</QUESTION_ID>
> <SYMPTOM>
> <SYMPTOM_ID>S1</SYMPTOM_ID>
> <PARENT_ID>A2</PARENT_ID>
> <IDENTITY>
> <QUESTION_ID>Q4</QUESTION_ID>
> <VALUE>Yes</VALUE>
> </IDENTITY>
> </SYMPTOM>
> <SYMPTOM>
> <SYMPTOM_ID>S2</SYMPTOM_ID>
> <PARENT_ID>A2</PARENT_ID>
> <IDENTITY>
> <QUESTION_ID>Q4</QUESTION_ID>
> <VALUE>No</VALUE>
> </IDENTITY>
> </SYMPTOM>
> </ATTRIBUTE>
>
> <DIAGNOSIS>
> <DIAGNOSIS_ID>D1</DIAGNOSIS_ID>
> <NAME>Incorrect alignment motor - speed sensor</NAME>
> <CASE>
> <SYMPTOM_TEST>
> <ATTRIBUTE_ID>A1</ATTRIBUTE_ID>
> <SYMPTOM_ID>S7</SYMPTOM_ID>
> <WEIGHT>0.5</WEIGHT>
> </SYMPTOM_TEST>
> <SYMPTOM_TEST>
> <ATTRIBUTE_ID>A2</ATTRIBUTE_ID>
> <SYMPTOM_ID>S1</SYMPTOM_ID>
> </SYMPTOM_TEST>
> </CASE>
> </DIAGNOSIS>
> <DIAGNOSIS>
> <DIAGNOSIS_ID>D2</DIAGNOSIS_ID>
> <NAME>Incorrect alignment motor - gearbox</NAME>
> <CASE>
> <SYMPTOM_TEST>
> <ATTRIBUTE_ID>A1</ATTRIBUTE_ID>
> <SYMPTOM_ID>S7</SYMPTOM_ID>
> <WEIGHT>0.5</WEIGHT>
> </SYMPTOM_TEST>
> <SYMPTOM_TEST>
> <ATTRIBUTE_ID>A2</ATTRIBUTE_ID>
> <SYMPTOM_ID>S2</SYMPTOM_ID>
> </SYMPTOM_TEST>
> </CASE>
> </DIAGNOSIS>
> </KB>
>
> -------------------------------------------------------------------------
>
>
>
>
>
-- Sjoerd Mullender <sjoerd.mullender@oratrix.com>