From 167894661d3186b45b93743abdccbfb1c89f9408 Mon Sep 17 00:00:00 2001 From: Pablo Hoffman Date: Tue, 14 Jul 2009 12:41:25 -0300 Subject: [PATCH] some improvement to Libxml2Document cleanup: avoid noisy errors, and make sure both cleaning up functions are called --- scrapy/xpath/document.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/scrapy/xpath/document.py b/scrapy/xpath/document.py index f35ac8bc3..291a1f99b 100644 --- a/scrapy/xpath/document.py +++ b/scrapy/xpath/document.py @@ -12,11 +12,19 @@ class Libxml2Document(object): self.xpathContext = self.xmlDoc.xpathNewContext() def __del__(self): - if hasattr(self, 'xmlDoc'): + # we must call both cleanup functions, so we try/except all exceptions + # to make sure one doesn't prevent the other from being called + # this call sometimes raises a "NoneType is not callable" TypeError + # also, these calls sometimes raise a "NoneType is not callable" + # TypeError, so the try/except block silences them + try: self.xmlDoc.freeDoc() - if hasattr(self, 'xpathContext'): + except: + pass + try: self.xpathContext.xpathFreeContext() + except: + pass def __str__(self): return "" % self.xmlDoc.name -