From af09029649637916d4df01592eba7f0aa1e8af48 Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 8 Oct 2009 16:32:42 +0100 Subject: [PATCH] trackref Libxml2Document objects and do not silence xml exceptions on __del__ --- scrapy/selector/document.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/scrapy/selector/document.py b/scrapy/selector/document.py index bc4bb4615..55b2cbd2b 100644 --- a/scrapy/selector/document.py +++ b/scrapy/selector/document.py @@ -5,9 +5,10 @@ garbage collection to libxml2 documents (xmlDoc). import weakref +from scrapy.utils.trackref import object_ref from .factories import xmlDoc_from_html -class Libxml2Document(object): +class Libxml2Document(object_ref): cache = weakref.WeakKeyDictionary() __slots__ = ['xmlDoc', 'xpathContext', '__weakref__'] @@ -15,7 +16,7 @@ class Libxml2Document(object): def __new__(cls, response, factory=xmlDoc_from_html): cache = cls.cache.setdefault(response, {}) if factory not in cache: - obj = object.__new__(cls) + obj = object_ref.__new__(cls) obj.xmlDoc = factory(response) obj.xpathContext = obj.xmlDoc.xpathNewContext() cache[factory] = obj @@ -29,12 +30,8 @@ class Libxml2Document(object): # TypeError, so the try/except block silences them try: self.xmlDoc.freeDoc() - except: - pass - try: + finally: self.xpathContext.xpathFreeContext() - except: - pass def __str__(self): return "" % self.xmlDoc.name