[Python-checkins] CVS: python/dist/src/Lib/xml/sax expatreader.py,1.25,1.25.16.1

Fred L. Drake fdrake@users.sourceforge.net
Thu, 04 Apr 2002 09:58:58 -0800


Update of /cvsroot/python/python/dist/src/Lib/xml/sax
In directory usw-pr-cvs1:/tmp/cvs-serv10291/Lib/xml/sax

Modified Files:
      Tag: release22-maint
	expatreader.py 
Log Message:
Avoid creating circular references between the ExpatParser and the
ContentHandler.  While GC will eventually clean up, it can take longer than
normal for applications that create a lot of strings (or other immutables)
rather without creating many containers.
This closes SF bug #535474.


Index: expatreader.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/xml/sax/expatreader.py,v
retrieving revision 1.25
retrieving revision 1.25.16.1
diff -C2 -d -r1.25 -r1.25.16.1
*** expatreader.py	30 Jul 2001 22:41:23 -0000	1.25
--- expatreader.py	4 Apr 2002 17:58:53 -0000	1.25.16.1
***************
*** 27,30 ****
--- 27,67 ----
  
  import string
+ import weakref
+ 
+ # --- ExpatLocator
+ 
+ class ExpatLocator(xmlreader.Locator):
+     """Locator for use with the ExpatParser class.
+ 
+     This uses a weak reference to the parser object to avoid creating
+     a circular reference between the parser and the content handler.
+     """
+     def __init__(self, parser):
+         self._ref = weakref.ref(parser)
+ 
+     def getColumnNumber(self):
+         parser = self._ref()
+         if parser is None or parser._parser is None:
+             return None
+         return parser._parser.ErrorColumnNumber
+ 
+     def getLineNumber(self):
+         parser = self._ref()
+         if parser is None or parser._parser is None:
+             return 1
+         return self._parser.ErrorLineNumber
+ 
+     def getPublicId(self):
+         parser = self._ref()
+         if parser is None:
+             return None
+         return parser._source.getPublicId()
+ 
+     def getSystemId(self):
+         parser = self._ref()
+         if parser is None:
+             return None
+         return parser._source.getSystemId()
+ 
  
  # --- ExpatParser
***************
*** 50,54 ****
          self._source = source
          self.reset()
!         self._cont_handler.setDocumentLocator(self)
          xmlreader.IncrementalParser.parse(self, source)
  
--- 87,91 ----
          self._source = source
          self.reset()
!         self._cont_handler.setDocumentLocator(ExpatLocator(self))
          xmlreader.IncrementalParser.parse(self, source)