Skip to content

Commit

Permalink
Add exception value markers for Cython 3.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
scoder committed Oct 7, 2023
1 parent 350beb0 commit 0e7d7e5
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 101 deletions.
58 changes: 29 additions & 29 deletions src/lxml/apihelpers.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from lxml.includes cimport uri


cdef void displayNode(xmlNode* c_node, indent):
cdef void displayNode(xmlNode* c_node, indent) noexcept:
# to help with debugging
cdef xmlNode* c_child
try:
Expand Down Expand Up @@ -72,7 +72,7 @@ cdef _Element _rootNodeOrRaise(object input):
_assertValidNode(node)
return node

cdef bint _isAncestorOrSame(xmlNode* c_ancestor, xmlNode* c_node):
cdef bint _isAncestorOrSame(xmlNode* c_ancestor, xmlNode* c_node) noexcept:
while c_node:
if c_node is c_ancestor:
return True
Expand Down Expand Up @@ -450,7 +450,7 @@ cdef int _removeUnusedNamespaceDeclarations(xmlNode* c_element, set prefixes_to_
python.lxml_free(c_ns_list)
return 0

cdef xmlNs* _searchNsByHref(xmlNode* c_node, const_xmlChar* c_href, bint is_attribute):
cdef xmlNs* _searchNsByHref(xmlNode* c_node, const_xmlChar* c_href, bint is_attribute) noexcept:
u"""Search a namespace declaration that covers a node (element or
attribute).
Expand Down Expand Up @@ -614,7 +614,7 @@ cdef int _delAttribute(_Element element, key) except -1:
raise KeyError, key
return 0

cdef int _delAttributeFromNsName(xmlNode* c_node, const_xmlChar* c_href, const_xmlChar* c_name):
cdef int _delAttributeFromNsName(xmlNode* c_node, const_xmlChar* c_href, const_xmlChar* c_name) noexcept:
c_attr = tree.xmlHasNsProp(c_node, c_name, c_href)
if c_attr is NULL:
# XXX free namespace that is not in use..?
Expand Down Expand Up @@ -668,16 +668,16 @@ cdef bint _hasEncodingDeclaration(object xml_string) except -1:
# check if a (unicode) string has an XML encoding declaration
return __HAS_XML_ENCODING(xml_string) is not None

cdef inline bint _hasText(xmlNode* c_node):
cdef inline bint _hasText(xmlNode* c_node) noexcept:
return c_node is not NULL and _textNodeOrSkip(c_node.children) is not NULL

cdef inline bint _hasTail(xmlNode* c_node):
cdef inline bint _hasTail(xmlNode* c_node) noexcept:
return c_node is not NULL and _textNodeOrSkip(c_node.next) is not NULL

cdef inline bint _hasNonWhitespaceTail(xmlNode* c_node):
cdef inline bint _hasNonWhitespaceTail(xmlNode* c_node) noexcept:
return _hasNonWhitespaceText(c_node, tail=True)

cdef bint _hasNonWhitespaceText(xmlNode* c_node, bint tail=False):
cdef bint _hasNonWhitespaceText(xmlNode* c_node, bint tail=False) noexcept:
c_text_node = c_node and _textNodeOrSkip(c_node.next if tail else c_node.children)
if c_text_node is NULL:
return False
Expand Down Expand Up @@ -720,7 +720,7 @@ cdef _collectText(xmlNode* c_node):
c_node = _textNodeOrSkip(c_node.next)
return funicode(<const_xmlChar*><unsigned char*>result)

cdef void _removeText(xmlNode* c_node):
cdef void _removeText(xmlNode* c_node) noexcept:
u"""Remove all text nodes.
Start removing at c_node.
Expand Down Expand Up @@ -779,10 +779,10 @@ cdef bytes _resolveQNameText(_Element element, value):
element._c_node, _xcstr(ns), NULL, 0)
return python.PyBytes_FromFormat('%s:%s', c_ns.prefix, _cstr(tag))

cdef inline bint _hasChild(xmlNode* c_node):
cdef inline bint _hasChild(xmlNode* c_node) noexcept:
return c_node is not NULL and _findChildForwards(c_node, 0) is not NULL

cdef inline Py_ssize_t _countElements(xmlNode* c_node):
cdef inline Py_ssize_t _countElements(xmlNode* c_node) noexcept:
u"Counts the elements within the following siblings and the node itself."
cdef Py_ssize_t count
count = 0
Expand Down Expand Up @@ -846,13 +846,13 @@ cdef _collectChildren(_Element element):
c_node = _nextElement(c_node)
return result

cdef inline xmlNode* _findChild(xmlNode* c_node, Py_ssize_t index):
cdef inline xmlNode* _findChild(xmlNode* c_node, Py_ssize_t index) noexcept:
if index < 0:
return _findChildBackwards(c_node, -index - 1)
else:
return _findChildForwards(c_node, index)

cdef inline xmlNode* _findChildForwards(xmlNode* c_node, Py_ssize_t index):
cdef inline xmlNode* _findChildForwards(xmlNode* c_node, Py_ssize_t index) noexcept:
u"""Return child element of c_node with index, or return NULL if not found.
"""
cdef xmlNode* c_child
Expand All @@ -867,7 +867,7 @@ cdef inline xmlNode* _findChildForwards(xmlNode* c_node, Py_ssize_t index):
c_child = c_child.next
return NULL

cdef inline xmlNode* _findChildBackwards(xmlNode* c_node, Py_ssize_t index):
cdef inline xmlNode* _findChildBackwards(xmlNode* c_node, Py_ssize_t index) noexcept:
u"""Return child element of c_node with index, or return NULL if not found.
Search from the end.
"""
Expand All @@ -883,7 +883,7 @@ cdef inline xmlNode* _findChildBackwards(xmlNode* c_node, Py_ssize_t index):
c_child = c_child.prev
return NULL

cdef inline xmlNode* _textNodeOrSkip(xmlNode* c_node) nogil:
cdef inline xmlNode* _textNodeOrSkip(xmlNode* c_node) noexcept nogil:
u"""Return the node if it's a text node. Skip over ignorable nodes in a
series of text nodes. Return NULL if a non-ignorable node is found.
Expand All @@ -901,7 +901,7 @@ cdef inline xmlNode* _textNodeOrSkip(xmlNode* c_node) nogil:
return NULL
return NULL

cdef inline xmlNode* _nextElement(xmlNode* c_node):
cdef inline xmlNode* _nextElement(xmlNode* c_node) noexcept:
u"""Given a node, find the next sibling that is an element.
"""
if c_node is NULL:
Expand All @@ -913,7 +913,7 @@ cdef inline xmlNode* _nextElement(xmlNode* c_node):
c_node = c_node.next
return NULL

cdef inline xmlNode* _previousElement(xmlNode* c_node):
cdef inline xmlNode* _previousElement(xmlNode* c_node) noexcept:
u"""Given a node, find the next sibling that is an element.
"""
if c_node is NULL:
Expand All @@ -925,7 +925,7 @@ cdef inline xmlNode* _previousElement(xmlNode* c_node):
c_node = c_node.prev
return NULL

cdef inline xmlNode* _parentElement(xmlNode* c_node):
cdef inline xmlNode* _parentElement(xmlNode* c_node) noexcept:
u"Given a node, find the parent element."
if c_node is NULL or not _isElement(c_node):
return NULL
Expand All @@ -934,7 +934,7 @@ cdef inline xmlNode* _parentElement(xmlNode* c_node):
return NULL
return c_node

cdef inline bint _tagMatches(xmlNode* c_node, const_xmlChar* c_href, const_xmlChar* c_name):
cdef inline bint _tagMatches(xmlNode* c_node, const_xmlChar* c_href, const_xmlChar* c_name) noexcept:
u"""Tests if the node matches namespace URI and tag name.
A node matches if it matches both c_href and c_name.
Expand Down Expand Up @@ -976,7 +976,7 @@ cdef inline bint _tagMatches(xmlNode* c_node, const_xmlChar* c_href, const_xmlCh
else:
return 0

cdef inline bint _tagMatchesExactly(xmlNode* c_node, qname* c_qname):
cdef inline bint _tagMatchesExactly(xmlNode* c_node, qname* c_qname) noexcept:
u"""Tests if the node matches namespace URI and tag name.
This differs from _tagMatches() in that it does not consider a
Expand All @@ -998,7 +998,7 @@ cdef inline bint _tagMatchesExactly(xmlNode* c_node, qname* c_qname):

cdef inline bint _nsTagMatchesExactly(const_xmlChar* c_node_href,
const_xmlChar* c_node_name,
qname* c_qname):
qname* c_qname) noexcept:
u"""Tests if name and namespace URI match those of c_qname.
This differs from _tagMatches() in that it does not consider a
Expand Down Expand Up @@ -1099,7 +1099,7 @@ cdef int _removeSiblings(xmlNode* c_element, tree.xmlElementType node_type, bint
c_node = c_next
return 0

cdef void _moveTail(xmlNode* c_tail, xmlNode* c_target):
cdef void _moveTail(xmlNode* c_tail, xmlNode* c_target) noexcept:
cdef xmlNode* c_next
# tail support: look for any text nodes trailing this node and
# move them too
Expand Down Expand Up @@ -1422,7 +1422,7 @@ cdef int _addSibling(_Element element, _Element sibling, bint as_next) except -1
moveNodeToDocument(element._doc, c_source_doc, c_node)
return 0

cdef inline bint isutf8(const_xmlChar* s):
cdef inline bint isutf8(const_xmlChar* s) noexcept:
cdef xmlChar c = s[0]
while c != c'\0':
if c & 0x80:
Expand All @@ -1431,7 +1431,7 @@ cdef inline bint isutf8(const_xmlChar* s):
c = s[0]
return False

cdef bint isutf8l(const_xmlChar* s, size_t length):
cdef bint isutf8l(const_xmlChar* s, size_t length) noexcept:
"""
Search for non-ASCII characters in the string, knowing its length in advance.
"""
Expand Down Expand Up @@ -1477,7 +1477,7 @@ cdef int _is_valid_xml_ascii(bytes pystring):
return 0
return 1

cdef bint _is_valid_xml_utf8(bytes pystring):
cdef bint _is_valid_xml_utf8(bytes pystring) except -1:
u"""Check if a string is like valid UTF-8 XML content."""
cdef const_xmlChar* s = _xcstr(pystring)
cdef const_xmlChar* c_end = s + len(pystring)
Expand Down Expand Up @@ -1576,7 +1576,7 @@ cdef enum:
REL_FILE_PATH = 3


cdef bint _isFilePath(const_xmlChar* c_path):
cdef bint _isFilePath(const_xmlChar* c_path) noexcept:
u"simple heuristic to see if a path is a filename"
cdef xmlChar c
# test if it looks like an absolute Unix path or a Windows network path
Expand Down Expand Up @@ -1733,10 +1733,10 @@ cdef inline int _pyXmlNameIsValid(name_utf8):
cdef inline int _pyHtmlNameIsValid(name_utf8):
return _htmlNameIsValid(_xcstr(name_utf8))

cdef inline int _xmlNameIsValid(const_xmlChar* c_name):
cdef inline int _xmlNameIsValid(const_xmlChar* c_name) noexcept:
return tree.xmlValidateNameValue(c_name)

cdef int _htmlNameIsValid(const_xmlChar* c_name):
cdef int _htmlNameIsValid(const_xmlChar* c_name) noexcept:
if c_name is NULL or c_name[0] == c'\0':
return 0
while c_name[0] != c'\0':
Expand All @@ -1745,7 +1745,7 @@ cdef int _htmlNameIsValid(const_xmlChar* c_name):
c_name += 1
return 1

cdef bint _characterReferenceIsValid(const_xmlChar* c_name):
cdef bint _characterReferenceIsValid(const_xmlChar* c_name) noexcept:
cdef bint is_hex
if c_name[0] == c'x':
c_name += 1
Expand Down
4 changes: 2 additions & 2 deletions src/lxml/dtd.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ cdef tree.xmlDtd* _copyDtd(tree.xmlDtd* c_orig_dtd) except NULL:
return c_dtd


cdef void _linkDtdAttribute(tree.xmlDtd* c_dtd, tree.xmlAttribute* c_attr):
cdef void _linkDtdAttribute(tree.xmlDtd* c_dtd, tree.xmlAttribute* c_attr) noexcept:
"""
Create the link to the DTD attribute declaration from the corresponding
element declaration.
Expand Down Expand Up @@ -469,7 +469,7 @@ cdef void _linkDtdAttribute(tree.xmlDtd* c_dtd, tree.xmlAttribute* c_attr):
c_pos.nexth = c_attr


cdef bint _isDtdNsDecl(tree.xmlAttribute* c_attr):
cdef bint _isDtdNsDecl(tree.xmlAttribute* c_attr) noexcept:
if cstring_h.strcmp(<const_char*>c_attr.name, "xmlns") == 0:
return True
if (c_attr.prefix is not NULL and
Expand Down
30 changes: 15 additions & 15 deletions src/lxml/etree.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ cdef class _ExceptionContext:
self._exc_info = None
return 0

cdef void _store_raised(self):
cdef void _store_raised(self) noexcept:
try:
self._exc_info = sys.exc_info()
except BaseException as e:
Expand Down Expand Up @@ -378,7 +378,7 @@ cdef public class _Document [ type LxmlDocumentType, object LxmlDocument ]:
return _elementFactory(self, c_node)

@cython.final
cdef bint hasdoctype(self):
cdef bint hasdoctype(self) noexcept:
# DOCTYPE gets parsed into internal subset (xmlDTD*)
return self._c_doc is not NULL and self._c_doc.intSubset is not NULL

Expand Down Expand Up @@ -2700,23 +2700,23 @@ cdef class _MultiTagMatcher:
def __dealloc__(self):
self._clear()

cdef bint rejectsAll(self):
cdef bint rejectsAll(self) noexcept:
return not self._tag_count and not self._node_types

cdef bint rejectsAllAttributes(self):
cdef bint rejectsAllAttributes(self) noexcept:
return not self._tag_count

cdef bint matchesType(self, int node_type):
cdef bint matchesType(self, int node_type) noexcept:
if node_type == tree.XML_ELEMENT_NODE and self._tag_count:
return True
return self._node_types & (1 << node_type)

cdef void _clear(self):
cdef void _clear(self) noexcept:
cdef size_t i, count
count = self._tag_count
self._tag_count = 0
if self._cached_tags:
for i in xrange(count):
for i in range(count):
cpython.ref.Py_XDECREF(self._cached_tags[i].href)
python.lxml_free(self._cached_tags)
self._cached_tags = NULL
Expand Down Expand Up @@ -2791,7 +2791,7 @@ cdef class _MultiTagMatcher:
self._cached_size = dict_size
return 0

cdef inline bint matches(self, xmlNode* c_node):
cdef inline bint matches(self, xmlNode* c_node) noexcept:
cdef qname* c_qname
if self._node_types & (1 << c_node.type):
return True
Expand All @@ -2802,7 +2802,7 @@ cdef class _MultiTagMatcher:
return False

cdef inline bint matchesNsTag(self, const_xmlChar* c_href,
const_xmlChar* c_name):
const_xmlChar* c_name) noexcept:
cdef qname* c_qname
if self._node_types & (1 << tree.XML_ELEMENT_NODE):
return True
Expand All @@ -2811,7 +2811,7 @@ cdef class _MultiTagMatcher:
return True
return False

cdef inline bint matchesAttribute(self, xmlAttr* c_attr):
cdef inline bint matchesAttribute(self, xmlAttr* c_attr) noexcept:
"""Attribute matches differ from Element matches in that they do
not care about node types.
"""
Expand Down Expand Up @@ -2953,7 +2953,7 @@ cdef class ElementDepthFirstIterator:
return current_node

@cython.final
cdef xmlNode* _nextNodeAnyTag(self, xmlNode* c_node):
cdef xmlNode* _nextNodeAnyTag(self, xmlNode* c_node) noexcept:
cdef int node_types = self._matcher._node_types
if not node_types:
return NULL
Expand All @@ -2964,7 +2964,7 @@ cdef class ElementDepthFirstIterator:
return NULL

@cython.final
cdef xmlNode* _nextNodeMatchTag(self, xmlNode* c_node):
cdef xmlNode* _nextNodeMatchTag(self, xmlNode* c_node) noexcept:
tree.BEGIN_FOR_EACH_ELEMENT_FROM(self._top_node._c_node, c_node, 0)
if self._matcher.matches(c_node):
return c_node
Expand Down Expand Up @@ -3011,17 +3011,17 @@ cdef xmlNode* _createElement(xmlDoc* c_doc, object name_utf) except NULL:
c_node = tree.xmlNewDocNode(c_doc, NULL, _xcstr(name_utf), NULL)
return c_node

cdef xmlNode* _createComment(xmlDoc* c_doc, const_xmlChar* text):
cdef xmlNode* _createComment(xmlDoc* c_doc, const_xmlChar* text) noexcept:
cdef xmlNode* c_node
c_node = tree.xmlNewDocComment(c_doc, text)
return c_node

cdef xmlNode* _createPI(xmlDoc* c_doc, const_xmlChar* target, const_xmlChar* text):
cdef xmlNode* _createPI(xmlDoc* c_doc, const_xmlChar* target, const_xmlChar* text) noexcept:
cdef xmlNode* c_node
c_node = tree.xmlNewDocPI(c_doc, target, text)
return c_node

cdef xmlNode* _createEntity(xmlDoc* c_doc, const_xmlChar* name):
cdef xmlNode* _createEntity(xmlDoc* c_doc, const_xmlChar* name) noexcept:
cdef xmlNode* c_node
c_node = tree.xmlNewReference(c_doc, name)
return c_node
Expand Down

0 comments on commit 0e7d7e5

Please sign in to comment.