diff options
| author | scoder <none@none> | 2010-04-27 21:41:38 +0200 |
|---|---|---|
| committer | scoder <none@none> | 2010-04-27 21:41:38 +0200 |
| commit | 8f4837cb2168960ff72ec4083b32495b9f2f72eb (patch) | |
| tree | b58ff387127e786777ab16427c29a729d951d9fa /src/lxml/xpath.pxi | |
| parent | 376c9b769fff199cb49d6a826dc995256838e9da (diff) | |
| download | python-lxml-8f4837cb2168960ff72ec4083b32495b9f2f72eb.tar.gz | |
[svn r4402] r5569@lenny: sbehnel | 2010-04-27 21:41:20 +0200
API hardening against uninitialised proxies and missing __init__ calls
--HG--
branch : trunk
Diffstat (limited to 'src/lxml/xpath.pxi')
| -rw-r--r-- | src/lxml/xpath.pxi | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/src/lxml/xpath.pxi b/src/lxml/xpath.pxi index 60ca3f6b..dc9dffdb 100644 --- a/src/lxml/xpath.pxi +++ b/src/lxml/xpath.pxi @@ -130,6 +130,13 @@ cdef class _XPathEvaluatorBase: cdef _XPathContext _context cdef python.PyThread_type_lock _eval_lock cdef _ErrorLog _error_log + def __cinit__(self): + self._xpathCtxt = NULL + if config.ENABLE_THREADING: + self._eval_lock = python.PyThread_allocate_lock() + if self._eval_lock is NULL: + python.PyErr_NoMemory() + self._error_log = _ErrorLog() def __init__(self, namespaces, extensions, enable_regexp, smart_strings): @@ -139,17 +146,13 @@ cdef class _XPathEvaluatorBase: import warnings warnings.warn(u"This version of libxml2 has a known XPath bug. " + \ u"Use it at your own risk.") - self._error_log = _ErrorLog() self._context = _XPathContext(namespaces, extensions, enable_regexp, None, smart_strings) - if config.ENABLE_THREADING: - self._eval_lock = python.PyThread_allocate_lock() - if self._eval_lock is NULL: - python.PyErr_NoMemory() property error_log: def __get__(self): + assert self._error_log is not None, "XPath evaluator not initialised" return self._error_log.copy() def __dealloc__(self): @@ -195,7 +198,7 @@ cdef class _XPathEvaluatorBase: result = python.PyThread_acquire_lock( self._eval_lock, python.WAIT_LOCK) if result == 0: - raise ParserError, u"parser locking failed" + raise XPathError, u"XPath evaluator locking failed" return 0 cdef void _unlock(self): @@ -266,6 +269,8 @@ cdef class XPathElementEvaluator(_XPathEvaluatorBase): cdef xpath.xmlXPathContext* xpathCtxt cdef int ns_register_status cdef _Document doc + _assertValidNode(element) + _assertValidDoc(element._doc) self._element = element doc = element._doc _XPathEvaluatorBase.__init__(self, namespaces, extensions, @@ -300,6 +305,7 @@ cdef class XPathElementEvaluator(_XPathEvaluatorBase): cdef xpath.xmlXPathObject* xpathObj cdef _Document doc cdef char* c_path + assert self._xpathCtxt is not NULL, "XPath context not initialised" path = _utf8(_path) doc = self._element._doc @@ -351,6 +357,7 @@ cdef class XPathDocumentEvaluator(XPathElementEvaluator): cdef xmlDoc* c_doc cdef _Document doc cdef char* c_path + assert self._xpathCtxt is not NULL, "XPath context not initialised" path = _utf8(_path) doc = self._element._doc @@ -417,6 +424,8 @@ cdef class XPath(_XPathEvaluatorBase): """ cdef xpath.xmlXPathCompExpr* _xpath cdef bytes _path + def __cinit__(self): + self._xpath = NULL def __init__(self, path, *, namespaces=None, extensions=None, regexp=True, smart_strings=True): @@ -440,6 +449,7 @@ cdef class XPath(_XPathEvaluatorBase): cdef _Document document cdef _Element element + assert self._xpathCtxt is not NULL, "XPath context not initialised" document = _documentOrRaise(_etree_or_element) element = _rootNodeOrRaise(_etree_or_element) |
