Browse files

fixed libxml2 API usage by appropriately using 'const' and 'xmlChar*'

  • Loading branch information...
1 parent 5658b26 commit 290fdc52a30164a46f349dd6b7d2a94b45bdc15b @scoder scoder committed Jul 30, 2012
View
162 src/lxml/apihelpers.pxi
@@ -231,13 +231,11 @@ cdef int _initNodeNamespaces(xmlNode* c_node, _Document doc,
This only works for a newly created node!
"""
cdef xmlNs* c_ns
- cdef char* c_prefix
- cdef char* c_href
cdef list nsdefs
if not nsmap:
if node_ns_utf is not None:
_uriValidOrRaise(node_ns_utf)
- doc._setNodeNs(c_node, _cstr(node_ns_utf))
+ doc._setNodeNs(c_node, _xcstr(node_ns_utf))
return 0
nsdefs = list(nsmap.items())
@@ -255,25 +253,25 @@ cdef int _initNodeNamespaces(xmlNode* c_node, _Document doc,
for prefix, href in nsdefs:
href_utf = _utf8(href)
_uriValidOrRaise(href_utf)
- c_href = _cstr(href_utf)
+ c_href = _xcstr(href_utf)
if prefix is not None:
prefix_utf = _utf8(prefix)
_prefixValidOrRaise(prefix_utf)
- c_prefix = _cstr(prefix_utf)
+ c_prefix = _xcstr(prefix_utf)
else:
- c_prefix = NULL
+ c_prefix = <const_xmlChar*>NULL
# add namespace with prefix if it is not already known
c_ns = tree.xmlSearchNs(doc._c_doc, c_node, c_prefix)
if c_ns is NULL or \
c_ns.href is NULL or \
- cstring_h.strcmp(c_ns.href, c_href) != 0:
+ tree.xmlStrcmp(c_ns.href, c_href) != 0:
c_ns = tree.xmlNewNs(c_node, c_href, c_prefix)
if href_utf == node_ns_utf:
tree.xmlSetNs(c_node, c_ns)
node_ns_utf = None
if node_ns_utf is not None:
- doc._setNodeNs(c_node, _cstr(node_ns_utf))
+ doc._setNodeNs(c_node, _xcstr(node_ns_utf))
return 0
cdef _initNodeAttributes(xmlNode* c_node, _Document doc, attrib, extra):
@@ -298,12 +296,12 @@ cdef _initNodeAttributes(xmlNode* c_node, _Document doc, attrib, extra):
_attributeValidOrRaise(attr_name_utf)
value_utf = _utf8(value)
if attr_ns_utf is None:
- tree.xmlNewProp(c_node, _cstr(attr_name_utf), _cstr(value_utf))
+ tree.xmlNewProp(c_node, _xcstr(attr_name_utf), _xcstr(value_utf))
else:
_uriValidOrRaise(attr_ns_utf)
- c_ns = doc._findOrBuildNodeNs(c_node, _cstr(attr_ns_utf), NULL, 1)
+ c_ns = doc._findOrBuildNodeNs(c_node, _xcstr(attr_ns_utf), NULL, 1)
tree.xmlNewNsProp(c_node, c_ns,
- _cstr(attr_name_utf), _cstr(value_utf))
+ _xcstr(attr_name_utf), _xcstr(value_utf))
ctypedef struct _ns_node_ref:
xmlNs* ns
@@ -409,7 +407,7 @@ cdef int _removeUnusedNamespaceDeclarations(xmlNode* c_element) except -1:
stdlib.free(c_ns_list)
return 0
-cdef xmlNs* _searchNsByHref(xmlNode* c_node, char* c_href, bint is_attribute):
+cdef xmlNs* _searchNsByHref(xmlNode* c_node, const_xmlChar* c_href, bint is_attribute):
u"""Search a namespace declaration that covers a node (element or
attribute).
@@ -422,7 +420,7 @@ cdef xmlNs* _searchNsByHref(xmlNode* c_node, char* c_href, bint is_attribute):
cdef xmlNode* c_element
if c_href is NULL or c_node is NULL or c_node.type == tree.XML_ENTITY_REF_NODE:
return NULL
- if cstring_h.strcmp(c_href, tree.XML_XML_NAMESPACE) == 0:
+ if tree.xmlStrcmp(c_href, tree.XML_XML_NAMESPACE) == 0:
# no special cases here, let libxml2 handle this
return tree.xmlSearchNsByHref(c_node.doc, c_node, c_href)
if c_node.type == tree.XML_ATTRIBUTE_NODE:
@@ -434,7 +432,7 @@ cdef xmlNs* _searchNsByHref(xmlNode* c_node, char* c_href, bint is_attribute):
if c_node.type == tree.XML_ELEMENT_NODE:
c_ns = c_node.nsDef
while c_ns is not NULL:
- if c_ns.href is not NULL and cstring_h.strcmp(c_href, c_ns.href) == 0:
+ if c_ns.href is not NULL and tree.xmlStrcmp(c_href, c_ns.href) == 0:
if c_ns.prefix is NULL and is_attribute:
# for attributes, continue searching a named
# prefix, but keep the first default namespace
@@ -449,7 +447,7 @@ cdef xmlNs* _searchNsByHref(xmlNode* c_node, char* c_href, bint is_attribute):
if c_node is not c_element and c_node.ns is not NULL:
# optimise: the node may have the namespace itself
c_ns = c_node.ns
- if c_ns.href is not NULL and cstring_h.strcmp(c_href, c_ns.href) == 0:
+ if c_ns.href is not NULL and tree.xmlStrcmp(c_href, c_ns.href) == 0:
if c_ns.prefix is NULL and is_attribute:
# for attributes, continue searching a named
# prefix, but keep the first default namespace
@@ -508,8 +506,6 @@ cdef int _replaceNodeByChildren(_Document doc, xmlNode* c_node) except -1:
return 0
cdef object _attributeValue(xmlNode* c_element, xmlAttr* c_attrib_node):
- cdef char* value
- cdef char* c_href
c_href = _getNs(<xmlNode*>c_attrib_node)
value = tree.xmlGetNsProp(c_element, c_attrib_node.name, c_href)
try:
@@ -519,8 +515,8 @@ cdef object _attributeValue(xmlNode* c_element, xmlAttr* c_attrib_node):
return result
cdef object _attributeValueFromNsName(xmlNode* c_element,
- char* c_href, char* c_name):
- cdef char* c_result = tree.xmlGetNsProp(c_element, c_name, c_href)
+ const_xmlChar* c_href, const_xmlChar* c_name):
+ c_result = tree.xmlGetNsProp(c_element, c_name, c_href)
if c_result is NULL:
return None
try:
@@ -530,11 +526,9 @@ cdef object _attributeValueFromNsName(xmlNode* c_element,
return result
cdef object _getNodeAttributeValue(xmlNode* c_node, key, default):
- cdef char* c_result
- cdef char* c_href
ns, tag = _getNsTag(key)
- c_href = NULL if ns is None else _cstr(ns)
- c_result = tree.xmlGetNsProp(c_node, _cstr(tag), c_href)
+ c_href = <const_xmlChar*>NULL if ns is None else _xcstr(ns)
+ c_result = tree.xmlGetNsProp(c_node, _xcstr(tag), c_href)
if c_result is NULL:
# XXX free namespace that is not in use..?
return default
@@ -549,35 +543,30 @@ cdef inline object _getAttributeValue(_Element element, key, default):
cdef int _setAttributeValue(_Element element, key, value) except -1:
cdef xmlNs* c_ns
- cdef char* c_value
- cdef char* c_tag
ns, tag = _getNsTag(key)
if not element._doc._parser._for_html:
_attributeValidOrRaise(tag)
- c_tag = _cstr(tag)
+ c_tag = _xcstr(tag)
if isinstance(value, QName):
value = _resolveQNameText(element, value)
else:
value = _utf8(value)
- c_value = _cstr(value)
+ c_value = _xcstr(value)
if ns is None:
c_ns = NULL
else:
- c_ns = element._doc._findOrBuildNodeNs(element._c_node,
- _cstr(ns), NULL, 1)
+ c_ns = element._doc._findOrBuildNodeNs(element._c_node, _xcstr(ns), NULL, 1)
tree.xmlSetNsProp(element._c_node, c_ns, c_tag, c_value)
return 0
cdef int _delAttribute(_Element element, key) except -1:
- cdef char* c_href
ns, tag = _getNsTag(key)
- c_href = NULL if ns is None else _cstr(ns)
- if _delAttributeFromNsName(element._c_node, c_href, _cstr(tag)):
+ c_href = <const_xmlChar*>NULL if ns is None else _xcstr(ns)
+ if _delAttributeFromNsName(element._c_node, c_href, _xcstr(tag)):
raise KeyError, key
return 0
-cdef int _delAttributeFromNsName(xmlNode* c_node, char* c_href, char* c_name):
- cdef xmlAttr* c_attr
+cdef int _delAttributeFromNsName(xmlNode* c_node, const_xmlChar* c_href, const_xmlChar* c_name):
c_attr = tree.xmlHasNsProp(c_node, c_name, c_href)
if c_attr is NULL:
# XXX free namespace that is not in use..?
@@ -590,15 +579,14 @@ cdef list _collectAttributes(xmlNode* c_node, int collecttype):
it collects either the name (1), the value (2) or the name-value tuples.
"""
cdef Py_ssize_t count
- cdef xmlAttr* c_attr
c_attr = c_node.properties
count = 0
while c_attr is not NULL:
if c_attr.type == tree.XML_ATTRIBUTE_NODE:
- count = count + 1
+ count += 1
c_attr = c_attr.next
- if count == 0:
+ if not count:
return []
attributes = python.PyList_New(count)
@@ -616,7 +604,7 @@ cdef list _collectAttributes(xmlNode* c_node, int collecttype):
python.Py_INCREF(item)
python.PyList_SET_ITEM(attributes, count, item)
- count = count + 1
+ count += 1
c_attr = c_attr.next
return attributes
@@ -664,7 +652,7 @@ cdef _collectText(xmlNode* c_node):
If there was no text to collect, return None
"""
cdef Py_ssize_t scount
- cdef char* c_text
+ cdef xmlChar* c_text
cdef xmlNode* c_node_cur
# check for multiple text nodes
scount = 0
@@ -685,9 +673,9 @@ cdef _collectText(xmlNode* c_node):
# the rest is not performance critical anymore
result = b''
while c_node is not NULL:
- result = result + c_node.content
+ result += <unsigned char*>c_node.content
c_node = _textNodeOrSkip(c_node.next)
- return funicode(result)
+ return funicode(<const_xmlChar*><unsigned char*>result)
cdef void _removeText(xmlNode* c_node):
u"""Remove all text nodes.
@@ -711,10 +699,10 @@ cdef int _setNodeText(xmlNode* c_node, value) except -1:
# now add new text node with value at start
if python._isString(value):
text = _utf8(value)
- c_text_node = tree.xmlNewDocText(c_node.doc, _cstr(text))
+ c_text_node = tree.xmlNewDocText(c_node.doc, _xcstr(text))
elif isinstance(value, CDATA):
c_text_node = tree.xmlNewCDataBlock(
- c_node.doc, _cstr((<CDATA>value)._utf8_data),
+ c_node.doc, _xcstr((<CDATA>value)._utf8_data),
python.PyBytes_GET_SIZE((<CDATA>value)._utf8_data))
else:
# this will raise the right error
@@ -733,7 +721,7 @@ cdef int _setTailText(xmlNode* c_node, value) except -1:
if value is None:
return 0
text = _utf8(value)
- c_text_node = tree.xmlNewDocText(c_node.doc, _cstr(text))
+ c_text_node = tree.xmlNewDocText(c_node.doc, _xcstr(text))
# XXX what if we're the top element?
tree.xmlAddNextSibling(c_node, c_text_node)
return 0
@@ -745,7 +733,7 @@ cdef bytes _resolveQNameText(_Element element, value):
return tag
else:
c_ns = element._doc._findOrBuildNodeNs(
- element._c_node, _cstr(ns), NULL, 0)
+ 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):
@@ -757,7 +745,7 @@ cdef inline Py_ssize_t _countElements(xmlNode* c_node):
count = 0
while c_node is not NULL:
if _isElement(c_node):
- count = count + 1
+ count += 1
c_node = c_node.next
return count
@@ -903,7 +891,7 @@ cdef inline xmlNode* _parentElement(xmlNode* c_node):
return NULL
return c_node
-cdef inline bint _tagMatches(xmlNode* c_node, char* c_href, char* c_name):
+cdef inline bint _tagMatches(xmlNode* c_node, const_xmlChar* c_href, const_xmlChar* c_name):
u"""Tests if the node matches namespace URI and tag name.
A node matches if it matches both c_href and c_name.
@@ -917,7 +905,6 @@ cdef inline bint _tagMatches(xmlNode* c_node, char* c_href, char* c_name):
* c_name is NULL
* its name string equals the c_name string
"""
- cdef char* c_node_href
if c_node is NULL:
return 0
if c_node.type != tree.XML_ELEMENT_NODE:
@@ -932,17 +919,17 @@ cdef inline bint _tagMatches(xmlNode* c_node, char* c_href, char* c_name):
if c_node_href is NULL:
return c_href[0] == c'\0'
else:
- return cstring_h.strcmp(c_node_href, c_href) == 0
+ return tree.xmlStrcmp(c_node_href, c_href) == 0
elif c_href is NULL:
if _getNs(c_node) is not NULL:
return 0
- return c_node.name == c_name or cstring_h.strcmp(c_node.name, c_name) == 0
- elif c_node.name == c_name or cstring_h.strcmp(c_node.name, c_name) == 0:
+ return c_node.name == c_name or tree.xmlStrcmp(c_node.name, c_name) == 0
+ elif c_node.name == c_name or tree.xmlStrcmp(c_node.name, c_name) == 0:
c_node_href = _getNs(c_node)
if c_node_href is NULL:
return c_href[0] == c'\0'
else:
- return cstring_h.strcmp(c_node_href, c_href) == 0
+ return tree.xmlStrcmp(c_node_href, c_href) == 0
else:
return 0
@@ -964,7 +951,6 @@ cdef inline bint _tagMatchesExactly(xmlNode* c_node, qname* c_qname):
* c_name is NULL
* its name string points to the same address (!) as c_name
"""
- cdef char* c_node_href
if c_qname.c_name is not NULL and c_qname.c_name is not c_node.name:
return 0
c_node_href = _getNs(c_node)
@@ -973,7 +959,7 @@ cdef inline bint _tagMatchesExactly(xmlNode* c_node, qname* c_qname):
elif c_node_href is NULL:
return 0
else:
- return cstring_h.strcmp(python.__cstr(c_qname.href), c_node_href) == 0
+ return tree.xmlStrcmp(<const_xmlChar*>python.__cstr(c_qname.href), c_node_href) == 0
cdef size_t _mapTagsToQnameMatchArray(xmlDoc* c_doc, list ns_tags,
qname* c_ns_tags, bint force_into_dict) except -1:
@@ -984,20 +970,19 @@ cdef size_t _mapTagsToQnameMatchArray(xmlDoc* c_doc, list ns_tags,
if it is not NULL.
"""
cdef size_t count = 0, i
- cdef char* c_tag
cdef bytes ns, tag
for ns, tag in ns_tags:
if tag is None:
- c_tag = NULL
+ c_tag = <const_xmlChar*>NULL
elif force_into_dict:
- c_tag = tree.xmlDictLookup(c_doc.dict, _cstr(tag), len(tag))
+ c_tag = tree.xmlDictLookup(c_doc.dict, _xcstr(tag), len(tag))
if c_tag is NULL:
# clean up before raising the error
for i in xrange(count):
cpython.ref.Py_XDECREF(c_ns_tags[i].href)
raise MemoryError()
else:
- c_tag = tree.xmlDictExists(c_doc.dict, _cstr(tag), len(tag))
+ c_tag = tree.xmlDictExists(c_doc.dict, _xcstr(tag), len(tag))
if c_tag is NULL:
# not in the dict => not in the document
continue
@@ -1322,12 +1307,12 @@ cdef int _prependSibling(_Element element, _Element sibling) except -1:
# parent element has moved; change them too..
moveNodeToDocument(element._doc, c_source_doc, c_node)
-cdef inline int isutf8(char* s):
- cdef char c = s[0]
+cdef inline int isutf8(const_xmlChar* s):
+ cdef xmlChar c = s[0]
while c != c'\0':
if c & 0x80:
return 1
- s = s + 1
+ s += 1
c = s[0]
return 0
@@ -1336,8 +1321,8 @@ cdef int check_string_utf8(bytes pystring):
for ASCII, 1 for UTF-8 and -1 in the case of errors, such as NULL
bytes or ASCII control characters.
"""
- cdef char* s = _cstr(pystring)
- cdef char* c_end = s + len(pystring)
+ cdef const_xmlChar* s = _xcstr(pystring)
+ cdef const_xmlChar* c_end = s + len(pystring)
cdef bint is_non_ascii = 0
while s < c_end:
if s[0] & 0x80:
@@ -1350,29 +1335,28 @@ cdef int check_string_utf8(bytes pystring):
s += 1
return is_non_ascii
-cdef inline object funicodeOrNone(char* s):
+cdef inline object funicodeOrNone(const_xmlChar* s):
return funicode(s) if s is not NULL else None
-cdef inline object funicodeOrEmpty(char* s):
+cdef inline object funicodeOrEmpty(const_xmlChar* s):
return funicode(s) if s is not NULL else ''
-cdef object funicode(char* s):
+cdef object funicode(const_xmlChar* s):
cdef Py_ssize_t slen
- cdef char* spos
+ cdef const_xmlChar* spos
cdef bint is_non_ascii
if python.IS_PYTHON3:
- slen = cstring_h.strlen(s)
- return s[:slen].decode('UTF-8')
+ return s.decode('UTF-8')
spos = s
is_non_ascii = 0
while spos[0] != c'\0':
if spos[0] & 0x80:
is_non_ascii = 1
break
spos += 1
- while spos[0] != c'\0':
- spos += 1
slen = spos - s
+ if spos[0] != c'\0':
+ slen += tree.xmlStrlen(spos)
if is_non_ascii:
return s[:slen].decode('UTF-8')
return <bytes>s[:slen]
@@ -1400,9 +1384,9 @@ cdef bytes _utf8(object s):
u"All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters"
return utf8_string
-cdef bint _isFilePath(char* c_path):
+cdef bint _isFilePath(const_xmlChar* c_path):
u"simple heuristic to see if a path is a filename"
- cdef char c
+ cdef xmlChar c
# test if it looks like an absolute Unix path or a Windows network path
if c_path[0] == c'/':
return 1
@@ -1433,7 +1417,7 @@ cdef object _encodeFilename(object filename):
elif python.PyUnicode_Check(filename):
filename8 = python.PyUnicode_AsEncodedString(
filename, 'UTF-8', NULL)
- if _isFilePath(filename8):
+ if _isFilePath(<unsigned char*>filename8):
try:
return python.PyUnicode_AsEncodedString(
filename, _C_FILENAME_ENCODING, NULL)
@@ -1443,21 +1427,21 @@ cdef object _encodeFilename(object filename):
else:
raise TypeError, u"Argument must be string or unicode."
-cdef object _decodeFilename(char* c_path):
+cdef object _decodeFilename(const_xmlChar* c_path):
u"""Make the filename a unicode string if we are in Py3.
"""
- cdef Py_ssize_t c_len = cstring_h.strlen(c_path)
+ c_len = tree.xmlStrlen(c_path)
if _isFilePath(c_path):
try:
return python.PyUnicode_Decode(
- c_path, c_len, _C_FILENAME_ENCODING, NULL)
+ <const_char*>c_path, c_len, _C_FILENAME_ENCODING, NULL)
except UnicodeDecodeError:
pass
try:
- return c_path[:c_len].decode('UTF-8')
+ return (<unsigned char*>c_path)[:c_len].decode('UTF-8')
except UnicodeDecodeError:
# this is a stupid fallback, but it might still work...
- return c_path[:c_len].decode('latin-1', 'replace')
+ return (<unsigned char*>c_path)[:c_len].decode('latin-1', 'replace')
cdef object _encodeFilenameUTF8(object filename):
u"""Recode filename as UTF-8. Tries ASCII, local filesystem encoding and
@@ -1531,27 +1515,27 @@ cdef tuple __getNsTag(tag, bint empty_ns):
return ns, tag
cdef inline int _pyXmlNameIsValid(name_utf8):
- return _xmlNameIsValid(_cstr(name_utf8))
+ return _xmlNameIsValid(_xcstr(name_utf8))
cdef inline int _pyHtmlNameIsValid(name_utf8):
- return _htmlNameIsValid(_cstr(name_utf8))
+ return _htmlNameIsValid(_xcstr(name_utf8))
-cdef inline int _xmlNameIsValid(char* c_name):
+cdef inline int _xmlNameIsValid(const_xmlChar* c_name):
return tree.xmlValidateNCName(c_name, 0) == 0
-cdef int _htmlNameIsValid(char* c_name):
- cdef char c
+cdef int _htmlNameIsValid(const_xmlChar* c_name):
+ cdef xmlChar c
if c_name is NULL or c_name[0] == c'\0':
return 0
while c_name[0] != c'\0':
c = c_name[0]
if c in (c'&', c'<', c'>', c'/', c'"', c"'",
c'\t', c'\n', c'\x0B', c'\x0C', c'\r', c' '):
return 0
- c_name = c_name + 1
+ c_name += 1
return 1
-cdef bint _characterReferenceIsValid(char* c_name):
+cdef bint _characterReferenceIsValid(const_xmlChar* c_name):
cdef bint is_hex
if c_name[0] == c'x':
c_name += 1
@@ -1564,8 +1548,8 @@ cdef bint _characterReferenceIsValid(char* c_name):
if c_name[0] < c'0' or c_name[0] > c'9':
if not is_hex:
return 0
- if not (c_name[0] >= c'a' and c_name[0] <= c'f'):
- if not (c_name[0] >= c'A' and c_name[0] <= c'F'):
+ if not (c'a' <= c_name[0] <= c'f'):
+ if not (c'A' <= c_name[0] <= c'F'):
return 0
c_name += 1
return 1
@@ -1605,7 +1589,7 @@ cdef int _uriValidOrRaise(uri_utf) except -1:
cdef inline object _namespacedName(xmlNode* c_node):
return _namespacedNameFromNsName(_getNs(c_node), c_node.name)
-cdef object _namespacedNameFromNsName(char* href, char* name):
+cdef object _namespacedNameFromNsName(const_xmlChar* href, const_xmlChar* name):
if href is NULL:
return funicode(name)
elif python.IS_PYTHON3:
View
27 src/lxml/classlookup.pxi
@@ -116,7 +116,7 @@ cdef class CommentBase(_Comment):
text = _utf8(text)
c_doc = _newXMLDoc()
doc = _documentFactory(c_doc, None)
- self._c_node = _createComment(c_doc, _cstr(text))
+ self._c_node = _createComment(c_doc, _xcstr(text))
tree.xmlAddChild(<xmlNode*>c_doc, self._c_node)
cdef class PIBase(_ProcessingInstruction):
@@ -143,7 +143,7 @@ cdef class PIBase(_ProcessingInstruction):
text = _utf8(text)
c_doc = _newXMLDoc()
doc = _documentFactory(c_doc, None)
- self._c_node = _createPI(c_doc, _cstr(target), _cstr(text))
+ self._c_node = _createPI(c_doc, _xcstr(target), _xcstr(text))
tree.xmlAddChild(<xmlNode*>c_doc, self._c_node)
cdef class EntityBase(_Entity):
@@ -161,9 +161,8 @@ cdef class EntityBase(_Entity):
def __init__(self, name):
cdef _Document doc
cdef xmlDoc* c_doc
- cdef char* c_name
name_utf = _utf8(name)
- c_name = _cstr(name_utf)
+ c_name = _xcstr(name_utf)
if c_name[0] == c'#':
if not _characterReferenceIsValid(c_name + 1):
raise ValueError, u"Invalid character reference: '%s'" % name
@@ -296,9 +295,9 @@ cdef object _lookupDefaultElementClass(state, _Document _doc, xmlNode* c_node):
if state is None or (<ElementDefaultClassLookup>state).pi_class is None:
# special case XSLT-PI
if c_node.name is not NULL and c_node.content is not NULL:
- if cstring_h.strcmp(c_node.name, "xml-stylesheet") == 0:
- if cstring_h.strstr(c_node.content, "text/xsl") is not NULL or \
- cstring_h.strstr(c_node.content, "text/xml") is not NULL:
+ if tree.xmlStrcmp(c_node.name, <unsigned char*>"xml-stylesheet") == 0:
+ if tree.xmlStrstr(c_node.content, <unsigned char*>"text/xsl") is not NULL or \
+ tree.xmlStrstr(c_node.content, <unsigned char*>"text/xml") is not NULL:
return _XSLTProcessingInstruction
return _ProcessingInstruction
else:
@@ -325,8 +324,8 @@ cdef class AttributeBasedElementClassLookup(FallbackElementClassLookup):
"""
cdef object _class_mapping
cdef object _pytag
- cdef char* _c_ns
- cdef char* _c_name
+ cdef const_xmlChar* _c_ns
+ cdef const_xmlChar* _c_name
def __cinit__(self):
self._lookup_function = _attribute_class_lookup
@@ -337,8 +336,8 @@ cdef class AttributeBasedElementClassLookup(FallbackElementClassLookup):
if ns is None:
self._c_ns = NULL
else:
- self._c_ns = _cstr(ns)
- self._c_name = _cstr(name)
+ self._c_ns = _xcstr(ns)
+ self._c_name = _xcstr(name)
self._class_mapping = dict(class_mapping)
FallbackElementClassLookup.__init__(self, fallback)
@@ -402,7 +401,6 @@ cdef class CustomElementClassLookup(FallbackElementClassLookup):
cdef object _custom_class_lookup(state, _Document doc, xmlNode* c_node):
cdef CustomElementClassLookup lookup
- cdef char* c_str
lookup = <CustomElementClassLookup>state
@@ -421,10 +419,7 @@ cdef object _custom_class_lookup(state, _Document doc, xmlNode* c_node):
else:
name = funicode(c_node.name)
c_str = tree._getNs(c_node)
- if c_str is NULL:
- ns = None
- else:
- ns = funicode(c_str)
+ ns = funicode(c_str) if c_str is not NULL else None
cls = lookup.lookup(element_type, doc, ns, name)
if cls is not None:
View
27 src/lxml/extensions.pxi
@@ -183,7 +183,7 @@ cdef class _BaseContext:
self._namespaces = namespaces
if self._xpathCtxt is not NULL:
xpath.xmlXPathRegisterNs(
- self._xpathCtxt, _cstr(prefix_utf), _cstr(ns_uri_utf))
+ self._xpathCtxt, _xcstr(prefix_utf), _xcstr(ns_uri_utf))
cdef registerNamespace(self, prefix, ns_uri):
if prefix is None:
@@ -192,33 +192,33 @@ cdef class _BaseContext:
ns_uri_utf = self._to_utf(ns_uri)
self._global_namespaces.append(prefix_utf)
xpath.xmlXPathRegisterNs(self._xpathCtxt,
- _cstr(prefix_utf), _cstr(ns_uri_utf))
+ _xcstr(prefix_utf), _xcstr(ns_uri_utf))
cdef registerLocalNamespaces(self):
if self._namespaces is None:
return
for prefix_utf, ns_uri_utf in self._namespaces:
xpath.xmlXPathRegisterNs(
- self._xpathCtxt, _cstr(prefix_utf), _cstr(ns_uri_utf))
+ self._xpathCtxt, _xcstr(prefix_utf), _xcstr(ns_uri_utf))
cdef registerGlobalNamespaces(self):
cdef list ns_prefixes = _find_all_extension_prefixes()
if python.PyList_GET_SIZE(ns_prefixes) > 0:
for prefix_utf, ns_uri_utf in ns_prefixes:
self._global_namespaces.append(prefix_utf)
xpath.xmlXPathRegisterNs(
- self._xpathCtxt, _cstr(prefix_utf), _cstr(ns_uri_utf))
+ self._xpathCtxt, _xcstr(prefix_utf), _xcstr(ns_uri_utf))
cdef unregisterGlobalNamespaces(self):
if python.PyList_GET_SIZE(self._global_namespaces) > 0:
for prefix_utf in self._global_namespaces:
xpath.xmlXPathRegisterNs(self._xpathCtxt,
- _cstr(prefix_utf), NULL)
+ _xcstr(prefix_utf), NULL)
del self._global_namespaces[:]
cdef void _unregisterNamespace(self, prefix_utf):
xpath.xmlXPathRegisterNs(self._xpathCtxt,
- _cstr(prefix_utf), NULL)
+ _xcstr(prefix_utf), NULL)
# extension functions
@@ -279,7 +279,7 @@ cdef class _BaseContext:
unreg_func(ctxt, name_utf, ns_utf)
@cython.final
- cdef _find_cached_function(self, char* c_ns_uri, char* c_name):
+ cdef _find_cached_function(self, const_xmlChar* c_ns_uri, const_xmlChar* c_name):
u"""Lookup an extension function in the cache and return it.
Parameters: c_ns_uri may be NULL, c_name must not be NULL
@@ -290,7 +290,7 @@ cdef class _BaseContext:
self._function_cache, None if c_ns_uri is NULL else c_ns_uri)
if c_dict is not NULL:
dict_result = python.PyDict_GetItem(
- <object>c_dict, c_name)
+ <object>c_dict, <unsigned char*>c_name)
if dict_result is not NULL:
return <object>dict_result
return None
@@ -458,7 +458,6 @@ cdef class _ExsltRegExp:
self._compile_map = {}
cdef _make_string(self, value):
- cdef char* c_text
if _isString(value):
return value
elif python.PyList_Check(value):
@@ -592,12 +591,12 @@ cdef xpath.xmlXPathObject* _wrapXPathObject(object obj, _Document doc,
context._hold(fake_node)
else:
# append a comment node to keep the text nodes separate
- c_node = tree.xmlNewDocComment(doc._c_doc, "")
+ c_node = tree.xmlNewDocComment(doc._c_doc, <unsigned char*>"")
if c_node is NULL:
raise MemoryError()
tree.xmlAddChild(fake_node._c_node, c_node)
context._hold(value)
- c_node = tree.xmlNewDocText(doc._c_doc, _cstr(value))
+ c_node = tree.xmlNewDocText(doc._c_doc, _xcstr(value))
if c_node is NULL:
raise MemoryError()
tree.xmlAddChild(fake_node._c_node, c_node)
@@ -767,7 +766,6 @@ cdef object _buildElementStringResult(_Document doc, xmlNode* c_node,
cdef _Element parent = None
cdef object attrname = None
cdef xmlNode* c_element
- cdef char* s
cdef bint is_tail
if c_node.type == tree.XML_ATTRIBUTE_NODE:
@@ -843,9 +841,10 @@ cdef void _xpath_function_call(xpath.xmlXPathParserContext* ctxt,
_extension_function_call(context, function, ctxt, nargs)
else:
if rctxt.functionURI is not NULL:
- fref = u"{%s}%s" % (rctxt.functionURI, rctxt.function)
+ fref = u"{%s}%s" % ((<unsigned char*>rctxt.functionURI).decode('UTF-8'),
+ (<unsigned char*>rctxt.function).decode('UTF-8'))
else:
- fref = rctxt.function
+ fref = (<unsigned char*>rctxt.function).decode('UTF-8')
xpath.xmlXPathErr(ctxt, xpath.XPATH_UNKNOWN_FUNC_ERROR)
context._exc._store_exception(
XPathFunctionError(u"XPath function '%s' not found" % fref))
View
4 src/lxml/includes/c14n.pxd
@@ -1,4 +1,4 @@
-from tree cimport xmlDoc, xmlOutputBuffer
+from tree cimport xmlDoc, xmlOutputBuffer, xmlChar
from xpath cimport xmlNodeSet
cdef extern from "libxml/c14n.h":
@@ -7,7 +7,7 @@ cdef extern from "libxml/c14n.h":
int exclusive,
char** inclusive_ns_prefixes,
int with_comments,
- char** doc_txt_ptr) nogil
+ xmlChar** doc_txt_ptr) nogil
cdef int xmlC14NDocSave(xmlDoc* doc,
xmlNodeSet* nodes,
View
15 src/lxml/includes/etreepublic.pxd
@@ -1,6 +1,7 @@
# public Cython/C interface to lxml.etree
cimport tree
+from tree cimport const_xmlChar
cdef extern from "lxml-version.h":
cdef char* LXML_VERSION_STRING
@@ -10,7 +11,7 @@ cdef extern from "etree_defs.h":
cdef bint _isElement(tree.xmlNode* c_node) nogil
# return the namespace URI of the node or NULL
- cdef char* _getNs(tree.xmlNode* node) nogil
+ cdef const_xmlChar* _getNs(tree.xmlNode* node) nogil
# pair of macros for tree traversal
cdef void BEGIN_FOR_EACH_ELEMENT_FROM(tree.xmlNode* tree_top,
@@ -102,7 +103,7 @@ cdef extern from "lxml.etree_api.h":
# return the value of the attribute with 'ns' and 'name' (or None)
cdef object attributeValueFromNsName(tree.xmlNode* c_element,
- char* c_ns, char* c_name)
+ const_xmlChar* c_ns, const_xmlChar* c_name)
# return the value of attribute "{ns}name", or the default value
cdef object getAttributeValue(_Element element, key, default)
@@ -125,7 +126,7 @@ cdef extern from "lxml.etree_api.h":
# delete an attribute based on name and namespace URI
# returns -1 if the attribute was not found (no exception)
cdef int delAttributeFromNsName(tree.xmlNode* c_element,
- char* c_href, char* c_name)
+ const_xmlChar* c_href, const_xmlChar* c_name)
##########################################################################
# XML node helper functions
@@ -175,10 +176,10 @@ cdef extern from "lxml.etree_api.h":
# check if a C node matches a tag name and namespace
# (NULL allowed for each => always matches)
- cdef int tagMatches(tree.xmlNode* c_node, char* c_href, char* c_name)
+ cdef int tagMatches(tree.xmlNode* c_node, const_xmlChar* c_href, const_xmlChar* c_name)
# convert a UTF-8 char* to a Python string or unicode string
- cdef object pyunicode(char* s)
+ cdef object pyunicode(const_xmlChar* s)
# convert the string to UTF-8 using the normal lxml.etree semantics
cdef bytes utf8(object s)
@@ -193,7 +194,7 @@ cdef extern from "lxml.etree_api.h":
cdef object namespacedName(tree.xmlNode* c_node)
# get the "{ns}tag" string for a href/tagname pair (c_ns may be NULL)
- cdef object namespacedNameFromNsName(char* c_ns, char* c_tag)
+ cdef object namespacedNameFromNsName(const_xmlChar* c_ns, const_xmlChar* c_tag)
# check if the node has a text value (which may be '')
cdef bint hasText(tree.xmlNode* c_node) nogil
@@ -218,7 +219,7 @@ cdef extern from "lxml.etree_api.h":
# recursively lookup a namespace in element or ancestors, or create it
cdef tree.xmlNs* findOrBuildNodeNsPrefix(
- _Document doc, tree.xmlNode* c_node, char* href, char* prefix)
+ _Document doc, tree.xmlNode* c_node, const_xmlChar* href, const_xmlChar* prefix)
# find the Document of an Element, ElementTree or Document (itself!)
cdef _Document documentOrRaise(object input)
View
4 src/lxml/includes/htmlparser.pxd
@@ -1,6 +1,6 @@
from tree cimport xmlDoc, xmlDict
from tree cimport xmlInputReadCallback, xmlInputCloseCallback
-from xmlparser cimport xmlParserCtxt, xmlSAXHandler
+from xmlparser cimport xmlParserCtxt, xmlSAXHandler, xmlSAXHandlerV1
from xmlerror cimport xmlError
cdef extern from "libxml/HTMLparser.h":
@@ -14,7 +14,7 @@ cdef extern from "libxml/HTMLparser.h":
HTML_PARSE_RECOVER # Relaxed parsing
HTML_PARSE_COMPACT # compact small text nodes
- xmlSAXHandler htmlDefaultSAXHandler
+ xmlSAXHandlerV1 htmlDefaultSAXHandler
cdef xmlParserCtxt* htmlCreateMemoryParserCtxt(
char* buffer, int size) nogil
View
151 src/lxml/includes/tree.pxd
@@ -1,13 +1,25 @@
from libc cimport stdio
+from libc.string cimport const_char
cdef extern from "lxml-version.h":
# deprecated declaration, use etreepublic.pxd instead
cdef char* LXML_VERSION_STRING
cdef extern from "libxml/xmlversion.h":
- cdef char* xmlParserVersion
+ cdef const_char* xmlParserVersion
cdef int LIBXML_VERSION
+cdef extern from "libxml/xmlstring.h":
+ ctypedef unsigned char xmlChar
+ ctypedef unsigned char const_xmlChar "const xmlChar"
+ cdef int xmlStrlen(const_xmlChar* str) nogil
+ cdef xmlChar* xmlStrdup(const_xmlChar* cur) nogil
+ cdef int xmlStrncmp(const_xmlChar* str1, const_xmlChar* str2, int length) nogil
+ cdef int xmlStrcmp(const_xmlChar* str1, const_xmlChar* str2) nogil
+ cdef const_xmlChar* xmlStrstr(const_xmlChar* str1, const_xmlChar* str2) nogil
+ cdef const_xmlChar* xmlStrchr(const_xmlChar* str1, xmlChar ch) nogil
+ cdef const_xmlChar* _xcstr "(const xmlChar*)PyBytes_AS_STRING" (object s)
+
cdef extern from "libxml/encoding.h":
ctypedef enum xmlCharEncoding:
XML_CHAR_ENCODING_ERROR = -1 # No char encoding detected
@@ -41,7 +53,7 @@ cdef extern from "libxml/encoding.h":
xmlCharEncoding enc) nogil
cdef int xmlCharEncCloseFunc(xmlCharEncodingHandler* handler) nogil
cdef xmlCharEncoding xmlDetectCharEncoding(char* text, int len) nogil
- cdef char* xmlGetCharEncodingName(xmlCharEncoding enc) nogil
+ cdef const_char* xmlGetCharEncodingName(xmlCharEncoding enc) nogil
cdef xmlCharEncoding xmlParseCharEncoding(char* name) nogil
cdef extern from "libxml/chvalid.h":
@@ -56,9 +68,9 @@ cdef extern from "libxml/hash.h":
cdef extern from *: # actually "libxml/dict.h"
# libxml/dict.h appears to be broken to include in C
ctypedef struct xmlDict
- cdef char* xmlDictLookup(xmlDict* dict, char* name, int len) nogil
- cdef char* xmlDictExists(xmlDict* dict, char* name, int len) nogil
- cdef int xmlDictOwns(xmlDict* dict, char* name) nogil
+ cdef const_xmlChar* xmlDictLookup(xmlDict* dict, const_xmlChar* name, int len) nogil
+ cdef const_xmlChar* xmlDictExists(xmlDict* dict, const_xmlChar* name, int len) nogil
+ cdef int xmlDictOwns(xmlDict* dict, const_xmlChar* name) nogil
cdef int xmlDictSize(xmlDict* dict) nogil
cdef extern from "libxml/tree.h":
@@ -134,21 +146,21 @@ cdef extern from "libxml/tree.h":
XML_INTERNAL_PREDEFINED_ENTITY= 6
ctypedef struct xmlNs:
- char* href
- char* prefix
+ const_xmlChar* href
+ const_xmlChar* prefix
xmlNs* next
ctypedef struct xmlNode:
void* _private
xmlElementType type
- char* name
+ const_xmlChar* name
xmlNode* children
xmlNode* last
xmlNode* parent
xmlNode* next
xmlNode* prev
xmlDoc* doc
- char* content
+ xmlChar* content
xmlAttr* properties
xmlNs* ns
xmlNs* nsDef
@@ -157,20 +169,20 @@ cdef extern from "libxml/tree.h":
ctypedef struct xmlElementContent:
xmlElementContentType type
xmlElementContentOccur ocur
- char *name
+ const_xmlChar *name
xmlElementContent *c1
xmlElementContent *c2
xmlElementContent *parent
- char *prefix
+ const_xmlChar *prefix
ctypedef struct xmlEnumeration:
xmlEnumeration *next
- char *name
+ const_xmlChar *name
ctypedef struct xmlAttribute:
void* _private
xmlElementType type
- char* name
+ const_xmlChar* name
xmlNode* children
xmlNode* last
xmlDtd* parent
@@ -180,15 +192,15 @@ cdef extern from "libxml/tree.h":
xmlAttribute* nexth
xmlAttributeType atype
xmlAttributeDefault def_ "def"
- char* defaultValue
+ const_xmlChar* defaultValue
xmlEnumeration* tree
- char* prefix
- char* elem
+ const_xmlChar* prefix
+ const_xmlChar* elem
ctypedef struct xmlElement:
void* _private
xmlElementType type
- char* name
+ const_xmlChar* name
xmlNode* children
xmlNode* last
xmlNode* parent
@@ -198,34 +210,34 @@ cdef extern from "libxml/tree.h":
xmlElementTypeVal etype
xmlElementContent* content
xmlAttribute* attributes
- char *prefix
+ const_xmlChar* prefix
void *contModel
ctypedef struct xmlEntity:
void* _private
xmlElementType type
- char* name
+ const_xmlChar* name
xmlNode* children
xmlNode* last
xmlDtd* parent
xmlNode* next
xmlNode* prev
xmlDoc* doc
- char* orig
- char* content
+ xmlChar* orig
+ xmlChar* content
int length
xmlEntityType etype
- char* ExternalID
- char* SystemID
+ const_xmlChar* ExternalID
+ const_xmlChar* SystemID
xmlEntity* nexte
- char* URI
+ const_xmlChar* URI
int owner
int checked
ctypedef struct xmlDtd:
- char* name
- char* ExternalID
- char* SystemID
+ const_xmlChar* name
+ const_xmlChar* ExternalID
+ const_xmlChar* SystemID
void* notations
void* entities
void* pentities
@@ -247,17 +259,17 @@ cdef extern from "libxml/tree.h":
xmlDict* dict
xmlHashTable* ids
int standalone
- char* version
- char* encoding
- char* URL
+ const_xmlChar* version
+ const_xmlChar* encoding
+ const_xmlChar* URL
void* _private
xmlDtd* intSubset
xmlDtd* extSubset
ctypedef struct xmlAttr:
void* _private
xmlElementType type
- char* name
+ const_xmlChar* name
xmlNode* children
xmlNode* last
xmlNode* parent
@@ -267,7 +279,7 @@ cdef extern from "libxml/tree.h":
xmlNs* ns
ctypedef struct xmlID:
- char* value
+ const_xmlChar* value
xmlAttr* attr
xmlDoc* doc
@@ -278,40 +290,40 @@ cdef extern from "libxml/tree.h":
xmlBuffer* conv
int error
- char* XML_XML_NAMESPACE
+ const_xmlChar* XML_XML_NAMESPACE
cdef void xmlFreeDoc(xmlDoc* cur) nogil
cdef void xmlFreeDtd(xmlDtd* cur) nogil
cdef void xmlFreeNode(xmlNode* cur) nogil
cdef void xmlFreeNsList(xmlNs* ns) nogil
cdef void xmlFreeNs(xmlNs* ns) nogil
- cdef void xmlFree(char* buf) nogil
+ cdef void xmlFree(void* buf) nogil
- cdef xmlNode* xmlNewNode(xmlNs* ns, char* name) nogil
- cdef xmlNode* xmlNewDocText(xmlDoc* doc, char* content) nogil
- cdef xmlNode* xmlNewDocComment(xmlDoc* doc, char* content) nogil
- cdef xmlNode* xmlNewDocPI(xmlDoc* doc, char* name, char* content) nogil
- cdef xmlNode* xmlNewReference(xmlDoc* doc, char* name) nogil
- cdef xmlNode* xmlNewCDataBlock(xmlDoc* doc, char* text, int len) nogil
- cdef xmlNs* xmlNewNs(xmlNode* node, char* href, char* prefix) nogil
+ cdef xmlNode* xmlNewNode(xmlNs* ns, const_xmlChar* name) nogil
+ cdef xmlNode* xmlNewDocText(xmlDoc* doc, const_xmlChar* content) nogil
+ cdef xmlNode* xmlNewDocComment(xmlDoc* doc, const_xmlChar* content) nogil
+ cdef xmlNode* xmlNewDocPI(xmlDoc* doc, const_xmlChar* name, const_xmlChar* content) nogil
+ cdef xmlNode* xmlNewReference(xmlDoc* doc, const_xmlChar* name) nogil
+ cdef xmlNode* xmlNewCDataBlock(xmlDoc* doc, const_xmlChar* text, int len) nogil
+ cdef xmlNs* xmlNewNs(xmlNode* node, const_xmlChar* href, const_xmlChar* prefix) nogil
cdef xmlNode* xmlAddChild(xmlNode* parent, xmlNode* cur) nogil
cdef xmlNode* xmlReplaceNode(xmlNode* old, xmlNode* cur) nogil
cdef xmlNode* xmlAddPrevSibling(xmlNode* cur, xmlNode* elem) nogil
cdef xmlNode* xmlAddNextSibling(xmlNode* cur, xmlNode* elem) nogil
cdef xmlNode* xmlNewDocNode(xmlDoc* doc, xmlNs* ns,
- char* name, char* content) nogil
- cdef xmlDoc* xmlNewDoc(char* version) nogil
- cdef xmlAttr* xmlNewProp(xmlNode* node, char* name, char* value) nogil
+ const_xmlChar* name, const_xmlChar* content) nogil
+ cdef xmlDoc* xmlNewDoc(const_xmlChar* version) nogil
+ cdef xmlAttr* xmlNewProp(xmlNode* node, const_xmlChar* name, const_xmlChar* value) nogil
cdef xmlAttr* xmlNewNsProp(xmlNode* node, xmlNs* ns,
- char* name, char* value) nogil
- cdef char* xmlGetNoNsProp(xmlNode* node, char* name) nogil
- cdef char* xmlGetNsProp(xmlNode* node, char* name, char* nameSpace) nogil
+ const_xmlChar* name, const_xmlChar* value) nogil
+ cdef xmlChar* xmlGetNoNsProp(xmlNode* node, const_xmlChar* name) nogil
+ cdef xmlChar* xmlGetNsProp(xmlNode* node, const_xmlChar* name, const_xmlChar* nameSpace) nogil
cdef void xmlSetNs(xmlNode* node, xmlNs* ns) nogil
- cdef xmlAttr* xmlSetProp(xmlNode* node, char* name, char* value) nogil
+ cdef xmlAttr* xmlSetProp(xmlNode* node, const_xmlChar* name, const_xmlChar* value) nogil
cdef xmlAttr* xmlSetNsProp(xmlNode* node, xmlNs* ns,
- char* name, char* value) nogil
+ const_xmlChar* name, const_xmlChar* value) nogil
cdef int xmlRemoveProp(xmlAttr* cur) nogil
- cdef char* xmlGetNodePath(xmlNode* node) nogil
+ cdef xmlChar* xmlGetNodePath(xmlNode* node) nogil
cdef void xmlDocDumpMemory(xmlDoc* cur, char** mem, int* size) nogil
cdef void xmlDocDumpMemoryEnc(xmlDoc* cur, char** mem, int* size,
char* encoding) nogil
@@ -322,20 +334,20 @@ cdef extern from "libxml/tree.h":
cdef xmlNode* xmlDocSetRootElement(xmlDoc* doc, xmlNode* root) nogil
cdef xmlNode* xmlDocGetRootElement(xmlDoc* doc) nogil
cdef void xmlSetTreeDoc(xmlNode* tree, xmlDoc* doc) nogil
- cdef xmlAttr* xmlHasProp(xmlNode* node, char* name) nogil
- cdef xmlAttr* xmlHasNsProp(xmlNode* node, char* name, char* nameSpace) nogil
- cdef char* xmlNodeGetContent(xmlNode* cur) nogil
+ cdef xmlAttr* xmlHasProp(xmlNode* node, const_xmlChar* name) nogil
+ cdef xmlAttr* xmlHasNsProp(xmlNode* node, const_xmlChar* name, const_xmlChar* nameSpace) nogil
+ cdef xmlChar* xmlNodeGetContent(xmlNode* cur) nogil
cdef int xmlNodeBufGetContent(xmlBuffer* buffer, xmlNode* cur) nogil
- cdef xmlNs* xmlSearchNs(xmlDoc* doc, xmlNode* node, char* prefix) nogil
- cdef xmlNs* xmlSearchNsByHref(xmlDoc* doc, xmlNode* node, char* href) nogil
+ cdef xmlNs* xmlSearchNs(xmlDoc* doc, xmlNode* node, const_xmlChar* prefix) nogil
+ cdef xmlNs* xmlSearchNsByHref(xmlDoc* doc, xmlNode* node, const_xmlChar* href) nogil
cdef int xmlIsBlankNode(xmlNode* node) nogil
cdef long xmlGetLineNo(xmlNode* node) nogil
cdef void xmlElemDump(stdio.FILE* f, xmlDoc* doc, xmlNode* cur) nogil
cdef void xmlNodeDumpOutput(xmlOutputBuffer* buf,
xmlDoc* doc, xmlNode* cur, int level,
- int format, char* encoding) nogil
- cdef void xmlNodeSetName(xmlNode* cur, char* name) nogil
- cdef void xmlNodeSetContent(xmlNode* cur, char* content) nogil
+ int format, const_char* encoding) nogil
+ cdef void xmlNodeSetName(xmlNode* cur, const_xmlChar* name) nogil
+ cdef void xmlNodeSetContent(xmlNode* cur, const_xmlChar* content) nogil
cdef xmlDtd* xmlCopyDtd(xmlDtd* dtd) nogil
cdef xmlDoc* xmlCopyDoc(xmlDoc* doc, int recursive) nogil
cdef xmlNode* xmlCopyNode(xmlNode* node, int extended) nogil
@@ -345,31 +357,31 @@ cdef extern from "libxml/tree.h":
cdef xmlBuffer* xmlBufferCreate() nogil
cdef void xmlBufferWriteChar(xmlBuffer* buf, char* string) nogil
cdef void xmlBufferFree(xmlBuffer* buf) nogil
- cdef char* xmlBufferContent(xmlBuffer* buf) nogil
+ cdef const_xmlChar* xmlBufferContent(xmlBuffer* buf) nogil
cdef int xmlBufferLength(xmlBuffer* buf) nogil
cdef int xmlKeepBlanksDefault(int val) nogil
- cdef char* xmlNodeGetBase(xmlDoc* doc, xmlNode* node) nogil
- cdef void xmlNodeSetBase(xmlNode* node, char* uri) nogil
- cdef int xmlValidateNCName(char* value, int space) nogil
+ cdef xmlChar* xmlNodeGetBase(xmlDoc* doc, xmlNode* node) nogil
+ cdef void xmlNodeSetBase(xmlNode* node, const_xmlChar* uri) nogil
+ cdef int xmlValidateNCName(const_xmlChar* value, int space) nogil
cdef extern from "libxml/uri.h":
- cdef char* xmlBuildURI(char* href, char* base) nogil
+ cdef const_xmlChar* xmlBuildURI(const_xmlChar* href, const_xmlChar* base) nogil
cdef extern from "libxml/HTMLtree.h":
cdef void htmlNodeDumpFormatOutput(xmlOutputBuffer* buf,
xmlDoc* doc, xmlNode* cur,
char* encoding, int format) nogil
- cdef xmlDoc* htmlNewDoc(char* uri, char* externalID) nogil
+ cdef xmlDoc* htmlNewDoc(const_xmlChar* uri, const_xmlChar* externalID) nogil
cdef extern from "libxml/valid.h":
- cdef xmlAttr* xmlGetID(xmlDoc* doc, char* ID) nogil
+ cdef xmlAttr* xmlGetID(xmlDoc* doc, const_xmlChar* ID) nogil
cdef void xmlDumpNotationTable(xmlBuffer* buffer,
xmlNotationTable* table) nogil
cdef extern from "libxml/xmlIO.h":
- cdef int xmlOutputBufferWriteString(xmlOutputBuffer* out, char* str) nogil
+ cdef int xmlOutputBufferWriteString(xmlOutputBuffer* out, const_char* str) nogil
cdef int xmlOutputBufferWrite(xmlOutputBuffer* out,
- int len, char* str) nogil
+ int len, const_char* str) nogil
cdef int xmlOutputBufferFlush(xmlOutputBuffer* out) nogil
cdef int xmlOutputBufferClose(xmlOutputBuffer* out) nogil
@@ -421,17 +433,14 @@ cdef extern from "libxml/globals.h":
cdef int xmlThrDefLineNumbersDefaultValue(int onoff) nogil
cdef int xmlThrDefIndentTreeOutput(int onoff) nogil
-cdef extern from "libxml/xmlstring.h":
- cdef char* xmlStrdup(char* cur) nogil
-
cdef extern from "libxml/xmlmemory.h":
cdef void* xmlMalloc(size_t size) nogil
cdef int xmlMemBlocks() nogil
cdef extern from "etree_defs.h":
cdef bint _isElement(xmlNode* node) nogil
cdef bint _isElementOrXInclude(xmlNode* node) nogil
- cdef char* _getNs(xmlNode* node) nogil
+ cdef const_xmlChar* _getNs(xmlNode* node) nogil
cdef void BEGIN_FOR_EACH_ELEMENT_FROM(xmlNode* tree_top,
xmlNode* start_node,
bint inclusive) nogil
View
54 src/lxml/includes/xmlparser.pxd
@@ -1,64 +1,70 @@
-from tree cimport xmlDoc, xmlNode, xmlDict, xmlDtd
+from libc.string cimport const_char
+
+from tree cimport xmlDoc, xmlNode, xmlDict, xmlDtd, const_xmlChar
from tree cimport xmlInputReadCallback, xmlInputCloseCallback
from xmlerror cimport xmlError, xmlStructuredErrorFunc
cdef extern from "libxml/parser.h":
ctypedef void (*startElementNsSAX2Func)(void* ctx,
- char* localname,
- char* prefix,
- char* URI,
+ const_xmlChar* localname,
+ const_xmlChar* prefix,
+ const_xmlChar* URI,
int nb_namespaces,
- char** namespaces,
+ const_xmlChar** namespaces,
int nb_attributes,
int nb_defaulted,
- char** attributes)
+ const_xmlChar** attributes)
ctypedef void (*endElementNsSAX2Func)(void* ctx,
- char* localname,
- char* prefix,
- char* URI)
+ const_xmlChar* localname,
+ const_xmlChar* prefix,
+ const_xmlChar* URI)
- ctypedef void (*startElementSAXFunc)(void* ctx, char* name, char** atts)
+ ctypedef void (*startElementSAXFunc)(void* ctx, const_xmlChar* name, const_xmlChar** atts)
- ctypedef void (*endElementSAXFunc)(void* ctx, char* name)
+ ctypedef void (*endElementSAXFunc)(void* ctx, const_xmlChar* name)
- ctypedef void (*charactersSAXFunc)(void* ctx, char* ch, int len)
+ ctypedef void (*charactersSAXFunc)(void* ctx, const_xmlChar* ch, int len)
- ctypedef void (*cdataBlockSAXFunc)(void* ctx, char* value, int len)
+ ctypedef void (*cdataBlockSAXFunc)(void* ctx, const_xmlChar* value, int len)
- ctypedef void (*commentSAXFunc)(void* ctx, char* value)
+ ctypedef void (*commentSAXFunc)(void* ctx, const_xmlChar* value)
ctypedef void (*processingInstructionSAXFunc)(void* ctx,
- char* target,
- char* data)
+ const_xmlChar* target,
+ const_xmlChar* data)
ctypedef void (*internalSubsetSAXFunc)(void* ctx,
- char* name,
- char* externalID,
- char* systemID)
+ const_xmlChar* name,
+ const_xmlChar* externalID,
+ const_xmlChar* systemID)
ctypedef void (*endDocumentSAXFunc)(void* ctx)
ctypedef void (*startDocumentSAXFunc)(void* ctx)
- ctypedef void (*referenceSAXFunc)(void * ctx, char* name)
+ ctypedef void (*referenceSAXFunc)(void * ctx, const_xmlChar* name)
cdef int XML_SAX2_MAGIC
cdef extern from "libxml/tree.h":
ctypedef struct xmlParserInput:
int line
int length
- char* base
- char* cur
- char* end
+ const_xmlChar* base
+ const_xmlChar* cur
+ const_xmlChar* end
ctypedef struct xmlParserInputBuffer:
void* context
xmlInputReadCallback readcallback
xmlInputCloseCallback closecallback
+ ctypedef struct xmlSAXHandlerV1:
+ # same as xmlSAXHandler, but without namespaces
+ pass
+
ctypedef struct xmlSAXHandler:
internalSubsetSAXFunc internalSubset
startElementNsSAX2Func startElementNs
@@ -185,7 +191,7 @@ cdef extern from "libxml/parser.h":
# entity loaders:
ctypedef xmlParserInput* (*xmlExternalEntityLoader)(
- char * URL, char * ID, xmlParserCtxt* context) nogil
+ const_char * URL, const_char * ID, xmlParserCtxt* context) nogil
cdef xmlExternalEntityLoader xmlGetExternalEntityLoader() nogil
cdef void xmlSetExternalEntityLoader(xmlExternalEntityLoader f) nogil
View
41 src/lxml/includes/xpath.pxd
@@ -1,6 +1,9 @@
cimport tree
cimport xmlerror
+from libc.string cimport const_char
+from tree cimport xmlChar, const_xmlChar
+
cdef extern from "libxml/xpath.h":
ctypedef enum xmlXPathObjectType:
XPATH_UNDEFINED = 0
@@ -49,15 +52,15 @@ cdef extern from "libxml/xpath.h":
xmlNodeSet* nodesetval
bint boolval
double floatval
- char* stringval
+ xmlChar* stringval
ctypedef struct xmlXPathContext:
tree.xmlDoc* doc
tree.xmlNode* node
tree.xmlDict* dict
tree.xmlHashTable* nsHash
- char* function
- char* functionURI
+ const_xmlChar* function
+ const_xmlChar* functionURI
xmlerror.xmlStructuredErrorFunc error
xmlerror.xmlError lastError
void* userData
@@ -72,54 +75,54 @@ cdef extern from "libxml/xpath.h":
ctypedef void (*xmlXPathFunction)(xmlXPathParserContext* ctxt, int nargs) nogil
ctypedef xmlXPathFunction (*xmlXPathFuncLookupFunc)(void* ctxt,
- char* name,
- char* ns_uri) nogil
+ const_xmlChar* name,
+ const_xmlChar* ns_uri) nogil
cdef xmlXPathContext* xmlXPathNewContext(tree.xmlDoc* doc) nogil
- cdef xmlXPathObject* xmlXPathEvalExpression(char* str,
+ cdef xmlXPathObject* xmlXPathEvalExpression(const_xmlChar* str,
xmlXPathContext* ctxt) nogil
cdef xmlXPathObject* xmlXPathCompiledEval(xmlXPathCompExpr* comp,
xmlXPathContext* ctxt) nogil
- cdef xmlXPathCompExpr* xmlXPathCompile(char* str) nogil
+ cdef xmlXPathCompExpr* xmlXPathCompile(const_xmlChar* str) nogil
cdef xmlXPathCompExpr* xmlXPathCtxtCompile(xmlXPathContext* ctxt,
- char* str) nogil
+ const_xmlChar* str) nogil
cdef void xmlXPathFreeContext(xmlXPathContext* ctxt) nogil
cdef void xmlXPathFreeCompExpr(xmlXPathCompExpr* comp) nogil
cdef void xmlXPathFreeObject(xmlXPathObject* obj) nogil
cdef int xmlXPathRegisterNs(xmlXPathContext* ctxt,
- char* prefix, char* ns_uri) nogil
+ const_xmlChar* prefix, const_xmlChar* ns_uri) nogil
cdef xmlNodeSet* xmlXPathNodeSetCreate(tree.xmlNode* val) nogil
cdef void xmlXPathFreeNodeSet(xmlNodeSet* val) nogil
cdef extern from "libxml/xpathInternals.h":
cdef int xmlXPathRegisterFunc(xmlXPathContext* ctxt,
- char* name,
+ const_xmlChar* name,
xmlXPathFunction f) nogil
cdef int xmlXPathRegisterFuncNS(xmlXPathContext* ctxt,
- char* name,
- char* ns_uri,
+ const_xmlChar* name,
+ const_xmlChar* ns_uri,
xmlXPathFunction f) nogil
cdef void xmlXPathRegisterFuncLookup(xmlXPathContext *ctxt,
xmlXPathFuncLookupFunc f,
void *funcCtxt) nogil
cdef int xmlXPathRegisterVariable(xmlXPathContext *ctxt,
- char* name,
+ const_xmlChar* name,
xmlXPathObject* value) nogil
cdef int xmlXPathRegisterVariableNS(xmlXPathContext *ctxt,
- char* name,
- char* ns_uri,
+ const_xmlChar* name,
+ const_xmlChar* ns_uri,
xmlXPathObject* value) nogil
cdef void xmlXPathRegisteredVariablesCleanup(xmlXPathContext *ctxt) nogil
cdef void xmlXPathRegisteredNsCleanup(xmlXPathContext *ctxt) nogil
cdef xmlXPathObject* valuePop (xmlXPathParserContext *ctxt) nogil
cdef int valuePush(xmlXPathParserContext* ctxt, xmlXPathObject *value) nogil
- cdef xmlXPathObject* xmlXPathNewCString(char *val) nogil
- cdef xmlXPathObject* xmlXPathWrapCString(char * val) nogil
- cdef xmlXPathObject* xmlXPathNewString(char *val) nogil
- cdef xmlXPathObject* xmlXPathWrapString(char * val) nogil
+ cdef xmlXPathObject* xmlXPathNewCString(const_char *val) nogil
+ cdef xmlXPathObject* xmlXPathWrapCString(const_char * val) nogil
+ cdef xmlXPathObject* xmlXPathNewString(const_xmlChar *val) nogil
+ cdef xmlXPathObject* xmlXPathWrapString(const_xmlChar * val) nogil
cdef xmlXPathObject* xmlXPathNewFloat(double val) nogil
cdef xmlXPathObject* xmlXPathNewBoolean(int val) nogil
cdef xmlXPathObject* xmlXPathNewNodeSet(tree.xmlNode* val) nogil
View
12 src/lxml/includes/xslt.pxd
@@ -1,6 +1,8 @@
-from tree cimport xmlDoc, xmlNode, xmlDict
+from tree cimport xmlDoc, xmlNode, xmlDict, xmlChar, const_xmlChar
from xpath cimport xmlXPathContext, xmlXPathFunction
+from libc.string cimport const_char
+
cdef extern from "libxslt/xslt.h":
cdef int xsltLibxsltVersion
cdef int xsltMaxDepth
@@ -69,7 +71,7 @@ cdef extern from "libxslt/documents.h":
XSLT_LOAD_STYLESHEET
XSLT_LOAD_DOCUMENT
- ctypedef xmlDoc* (*xsltDocLoaderFunc)(char* URI, xmlDict* dict,
+ ctypedef xmlDoc* (*xsltDocLoaderFunc)(const_xmlChar* URI, xmlDict* dict,
int options,
void* ctxt,
xsltLoadType type) nogil
@@ -78,9 +80,9 @@ cdef extern from "libxslt/documents.h":
cdef extern from "libxslt/transform.h":
cdef xmlDoc* xsltApplyStylesheet(xsltStylesheet* style, xmlDoc* doc,
- char** params) nogil
+ const_char** params) nogil
cdef xmlDoc* xsltApplyStylesheetUser(xsltStylesheet* style, xmlDoc* doc,
- char** params, char* output,
+ const_char** params, const_char* output,
void* profile,
xsltTransformContext* context) nogil
cdef void xsltProcessOneNode(xsltTransformContext* ctxt,
@@ -95,7 +97,7 @@ cdef extern from "libxslt/transform.h":
xsltStackElem* params) nogil
cdef extern from "libxslt/xsltutils.h":
- cdef int xsltSaveResultToString(char** doc_txt_ptr,
+ cdef int xsltSaveResultToString(xmlChar** doc_txt_ptr,
int* doc_txt_len,
xmlDoc* result,
xsltStylesheet* style) nogil
View
19 src/lxml/iterparse.pxi
@@ -237,10 +237,10 @@ cdef void _iterparseSaxStartDocument(void* ctxt):
c_ctxt.myDoc.dict = c_ctxt.dict
_pushSaxStartDocument(context, c_ctxt.myDoc)
-cdef void _iterparseSaxStart(void* ctxt, char* localname, char* prefix,
- char* URI, int nb_namespaces, char** namespaces,
+cdef void _iterparseSaxStart(void* ctxt, const_xmlChar* localname, const_xmlChar* prefix,
+ const_xmlChar* URI, int nb_namespaces, const_xmlChar** namespaces,
int nb_attributes, int nb_defaulted,
- char** attributes):
+ const_xmlChar** attributes):
cdef xmlparser.xmlParserCtxt* c_ctxt
cdef _IterparseContext context
c_ctxt = <xmlparser.xmlParserCtxt*>ctxt
@@ -251,31 +251,32 @@ cdef void _iterparseSaxStart(void* ctxt, char* localname, char* prefix,
nb_attributes, nb_defaulted, attributes)
_pushSaxStartEvent(context, c_ctxt.node)
-cdef void _iterparseSaxEnd(void* ctxt, char* localname, char* prefix, char* URI):
+cdef void _iterparseSaxEnd(void* ctxt, const_xmlChar* localname, const_xmlChar* prefix,
+ const_xmlChar* URI):
cdef xmlparser.xmlParserCtxt* c_ctxt
cdef _IterparseContext context
c_ctxt = <xmlparser.xmlParserCtxt*>ctxt
context = <_IterparseContext>c_ctxt._private
_pushSaxEndEvent(context, c_ctxt.node)
context._origSaxEnd(ctxt, localname, prefix, URI)
-cdef void _iterparseSaxStartNoNs(void* ctxt, char* name, char** attributes):
+cdef void _iterparseSaxStartNoNs(void* ctxt, const_xmlChar* name, const_xmlChar** attributes):
cdef xmlparser.xmlParserCtxt* c_ctxt
cdef _IterparseContext context
c_ctxt = <xmlparser.xmlParserCtxt*>ctxt
context = <_IterparseContext>c_ctxt._private
context._origSaxStartNoNs(ctxt, name, attributes)
_pushSaxStartEvent(context, c_ctxt.node)
-cdef void _iterparseSaxEndNoNs(void* ctxt, char* name):
+cdef void _iterparseSaxEndNoNs(void* ctxt, const_xmlChar* name):
cdef xmlparser.xmlParserCtxt* c_ctxt
cdef _IterparseContext context
c_ctxt = <xmlparser.xmlParserCtxt*>ctxt
context = <_IterparseContext>c_ctxt._private
_pushSaxEndEvent(context, c_ctxt.node)
context._origSaxEndNoNs(ctxt, name)
-cdef void _iterparseSaxComment(void* ctxt, char* text):
+cdef void _iterparseSaxComment(void* ctxt, const_xmlChar* text):
cdef xmlNode* c_node
cdef xmlparser.xmlParserCtxt* c_ctxt
cdef _IterparseContext context
@@ -286,7 +287,7 @@ cdef void _iterparseSaxComment(void* ctxt, char* text):
if c_node is not NULL:
_pushSaxEvent(context, u"comment", c_node)
-cdef void _iterparseSaxPI(void* ctxt, char* target, char* data):
+cdef void _iterparseSaxPI(void* ctxt, const_xmlChar* target, const_xmlChar* data):
cdef xmlNode* c_node
cdef xmlparser.xmlParserCtxt* c_ctxt
cdef _IterparseContext context
@@ -361,7 +362,7 @@ cdef class iterparse(_BaseParser):
cdef object _source
cdef object _buffer
cdef int (*_parse_chunk)(xmlparser.xmlParserCtxt* ctxt,
- char* chunk, int size, int terminate) nogil
+ const_char* chunk, int size, int terminate) nogil
cdef bint _close_source_after_read
def __init__(self, source, events=(u"end",), *, tag=None,
View
71 src/lxml/lxml.etree.pyx
@@ -42,6 +42,7 @@ cimport cython
from lxml cimport python
from lxml.includes cimport tree, config
from lxml.includes.tree cimport xmlDoc, xmlNode, xmlAttr, xmlNs, _isElement, _getNs
+from lxml.includes.tree cimport const_xmlChar, xmlChar, _xcstr
from lxml.python cimport callable, _cstr, _isString
from lxml.includes cimport xpath
from lxml.includes cimport c14n
@@ -51,6 +52,7 @@ cimport cpython.mem
cimport cpython.ref
from libc cimport limits, stdio, stdlib
from libc cimport string as cstring_h # not to be confused with stdlib 'string'
+from libc.string cimport const_char
try:
import __builtin__
@@ -114,7 +116,7 @@ DEBUG = __DEBUG
# PyPy requires us to store a Python reference for the
# namespace in order to keep the byte buffer alive.
cdef struct qname:
- char* c_name
+ const_xmlChar* c_name
python.PyObject* href
# global per-thread setup
@@ -234,9 +236,9 @@ cdef __unpackIntVersion(int c_version):
cdef int _LIBXML_VERSION_INT
try:
_LIBXML_VERSION_INT = int(
- re.match(u'[0-9]+', (tree.xmlParserVersion).decode("ascii")).group(0))
+ re.match(u'[0-9]+', (<unsigned char*>tree.xmlParserVersion).decode("ascii")).group(0))
except Exception:
- print u"Unknown libxml2 version: %s" % (tree.xmlParserVersion).decode("ascii")
+ print u"Unknown libxml2 version: %s" % (<unsigned char*>tree.xmlParserVersion).decode("ascii")
_LIBXML_VERSION_INT = 0
LIBXML_VERSION = __unpackIntVersion(_LIBXML_VERSION_INT)
@@ -410,7 +412,7 @@ cdef public class _Document [ type LxmlDocumentType, object LxmlDocument ]:
@cython.final
cdef xmlNs* _findOrBuildNodeNs(self, xmlNode* c_node,
- char* c_href, char* c_prefix,
+ const_xmlChar* c_href, const_xmlChar* c_prefix,
bint is_attribute) except NULL:
u"""Get or create namespace structure for a node. Reuses the prefix if
possible.
@@ -435,17 +437,17 @@ cdef public class _Document [ type LxmlDocumentType, object LxmlDocument ]:
# none found => determine a suitable new prefix
if c_prefix is NULL:
dict_result = python.PyDict_GetItem(
- _DEFAULT_NAMESPACE_PREFIXES, c_href)
+ _DEFAULT_NAMESPACE_PREFIXES, <unsigned char*>c_href)
if dict_result is not NULL:
prefix = <object>dict_result
else:
prefix = self.buildNewPrefix()
- c_prefix = _cstr(prefix)
+ c_prefix = _xcstr(prefix)
# make sure the prefix is not in use already
while tree.xmlSearchNs(self._c_doc, c_node, c_prefix) is not NULL:
prefix = self.buildNewPrefix()
- c_prefix = _cstr(prefix)
+ c_prefix = _xcstr(prefix)
# declare the namespace and return it
c_ns = tree.xmlNewNs(c_node, c_href, c_prefix)
@@ -454,10 +456,9 @@ cdef public class _Document [ type LxmlDocumentType, object LxmlDocument ]:
return c_ns
@cython.final
- cdef int _setNodeNs(self, xmlNode* c_node, char* href) except -1:
+ cdef int _setNodeNs(self, xmlNode* c_node, const_xmlChar* c_href) except -1:
u"Lookup namespace structure and set it for the node."
- cdef xmlNs* c_ns
- c_ns = self._findOrBuildNodeNs(c_node, href, NULL, 0)
+ c_ns = self._findOrBuildNodeNs(c_node, c_href, NULL, 0)
tree.xmlSetNs(c_node, c_ns)
cdef tuple __initPrefixCache():
@@ -538,15 +539,14 @@ cdef class DocInfo:
return None
return _decodeFilename(self._doc._c_doc.URL)
def __set__(self, url):
- cdef char* c_oldurl
url = _encodeFilename(url)
c_oldurl = self._doc._c_doc.URL
if url is None:
self._doc._c_doc.URL = NULL
else:
- self._doc._c_doc.URL = tree.xmlStrdup(_cstr(url))
+ self._doc._c_doc.URL = tree.xmlStrdup(_xcstr(url))
if c_oldurl is not NULL:
- tree.xmlFree(c_oldurl)
+ tree.xmlFree(<void*>c_oldurl)
property doctype:
u"Returns a DOCTYPE declaration string for the document."
@@ -890,11 +890,11 @@ cdef public class _Element [ type LxmlElementType, object LxmlElement ]:
else:
_tagValidOrRaise(name)
self._tag = value
- tree.xmlNodeSetName(self._c_node, _cstr(name))
+ tree.xmlNodeSetName(self._c_node, _xcstr(name))
if ns is None:
self._c_node.ns = NULL
else:
- self._doc._setNodeNs(self._c_node, _cstr(ns))
+ self._doc._setNodeNs(self._c_node, _xcstr(ns))
property attrib:
u"""Element attribute dictionary. Where possible, use get(), set(),
@@ -1007,7 +1007,6 @@ cdef public class _Element [ type LxmlElementType, object LxmlElement ]:
Element, regardless of the document type (XML or HTML).
"""
def __get__(self):
- cdef char* c_base
_assertValidNode(self)
c_base = tree.xmlNodeGetBase(self._doc._c_doc, self._c_node)
if c_base is NULL:
@@ -1019,13 +1018,12 @@ cdef public class _Element [ type LxmlElementType, object LxmlElement ]:
return base
def __set__(self, url):
- cdef char* c_base
_assertValidNode(self)
if url is None:
- c_base = NULL
+ c_base = <const_xmlChar*>NULL
else:
url = _encodeFilename(url)
- c_base = _cstr(url)
+ c_base = _xcstr(url)
tree.xmlNodeSetBase(self._c_node, c_base)
# ACCESSORS
@@ -1544,13 +1542,12 @@ cdef class __ContentOnlyElement(_Element):
def __set__(self, value):
cdef tree.xmlDict* c_dict
- cdef char* c_text
_assertValidNode(self)
if value is None:
- c_text = NULL
+ c_text = <const_xmlChar*>NULL
else:
value = _utf8(value)
- c_text = _cstr(value)
+ c_text = _xcstr(value)
tree.xmlNodeSetContent(self._c_node, c_text)
# ACCESSORS
@@ -1603,7 +1600,7 @@ cdef class _ProcessingInstruction(__ContentOnlyElement):
def __set__(self, value):
_assertValidNode(self)
value = _utf8(value)
- c_text = _cstr(value)
+ c_text = _xcstr(value)
tree.xmlNodeSetName(self._c_node, c_text)
def __repr__(self):
@@ -1655,7 +1652,7 @@ cdef class _Entity(__ContentOnlyElement):
value_utf = _utf8(value)
assert u'&' not in value and u';' not in value, \
u"Invalid entity name '%s'" % value
- tree.xmlNodeSetName(self._c_node, _cstr(value_utf))
+ tree.xmlNodeSetName(self._c_node, _xcstr(value_utf))
property text:
# FIXME: should this be None or '&[VALUE];' or the resolved
@@ -1915,7 +1912,6 @@ cdef public class _ElementTree [ type LxmlElementTreeType,
cdef _Document doc
cdef _Element root
cdef xmlDoc* c_doc
- cdef char* c_path
_assertValidNode(element)
if self._context_node is not None:
root = self._context_node
@@ -2289,11 +2285,10 @@ cdef class _Attrib:
def __contains__(self, key):
cdef xmlNode* c_node
- cdef char* c_href
ns, tag = _getNsTag(key)
c_node = self._element._c_node
- c_href = NULL if ns is None else _cstr(ns)
- return 1 if tree.xmlHasNsProp(c_node, _cstr(tag), c_href) else 0
+ c_href = <const_xmlChar*>NULL if ns is None else _xcstr(ns)
+ return 1 if tree.xmlHasNsProp(c_node, _xcstr(tag), c_href) else 0
def __richcmp__(one, other, int op):
if not isinstance(one, dict):
@@ -2394,7 +2389,7 @@ cdef public class _ElementIterator(_ElementTagMatcher) [
while c_node is not NULL and \
self._node_type != 0 and \
(self._node_type != c_node.type or
- not _tagMatches(c_node, self._href, self._name)):
+ not _tagMatches(c_node, <const_xmlChar*>self._href, <const_xmlChar*>self._name)):
c_node = self._next_element(c_node)
if c_node is NULL:
self._node = None
@@ -2536,7 +2531,6 @@ cdef class _MultiTagMatcher:
not care about node types and href NULL values are not wildcards
but match only unnamespaced attributes.
"""
- cdef char* c_href
cdef qname* c_qname
for c_qname in self._cached_tags[:self._tag_count]:
if c_qname.c_name is NULL or c_qname.c_name is c_attr.name:
@@ -2545,7 +2539,7 @@ cdef class _MultiTagMatcher:
if c_href is NULL:
return True
elif c_href is not NULL:
- if cstring_h.strcmp(c_href, python.__cstr(c_qname.href)) == 0:
+ if tree.xmlStrcmp(c_href, <const_xmlChar*>python.__cstr(c_qname.href)) == 0:
return True
return False
@@ -2732,20 +2726,20 @@ cdef class ElementTextIterator:
cdef xmlNode* _createElement(xmlDoc* c_doc, object name_utf) except NULL:
cdef xmlNode* c_node
- c_node = tree.xmlNewDocNode(c_doc, NULL, _cstr(name_utf), NULL)
+ c_node = tree.xmlNewDocNode(c_doc, NULL, _xcstr(name_utf), NULL)
return c_node
-cdef xmlNode* _createComment(xmlDoc* c_doc, char* text):
+cdef xmlNode* _createComment(xmlDoc* c_doc, const_xmlChar* text):
cdef xmlNode* c_node
c_node = tree.xmlNewDocComment(c_doc, text)
return c_node
-cdef xmlNode* _createPI(xmlDoc* c_doc, char* target, char* text):
+cdef xmlNode* _createPI(xmlDoc* c_doc, const_xmlChar* target, const_xmlChar* text):
cdef xmlNode* c_node
c_node = tree.xmlNewDocPI(c_doc, target, text)
return c_node
-cdef xmlNode* _createEntity(xmlDoc* c_doc, char* name):
+cdef xmlNode* _createEntity(xmlDoc* c_doc, const_xmlChar* name):
cdef xmlNode* c_node
c_node = tree.xmlNewReference(c_doc, name)
return c_node
@@ -2780,7 +2774,7 @@ def Comment(text=None):
text = _utf8(text)
c_doc = _newXMLDoc()
doc = _documentFactory(c_doc, None)
- c_node = _createComment(c_doc, _cstr(text))
+ c_node = _createComment(c_doc, _xcstr(text))
tree.xmlAddChild(<xmlNode*>c_doc, c_node)
return _elementFactory(doc, c_node)
@@ -2800,7 +2794,7 @@ def ProcessingInstruction(target, text=None):
text = _utf8(text)
c_doc = _newXMLDoc()
doc = _documentFactory(c_doc, None)
- c_node = _createPI(c_doc, _cstr(target), _cstr(text))
+ c_node = _createPI(c_doc, _xcstr(target), _xcstr(text))
tree.xmlAddChild(<xmlNode*>c_doc, c_node)
return _elementFactory(doc, c_node)
@@ -2832,9 +2826,8 @@ def Entity(name):
cdef _Document doc
cdef xmlNode* c_node
cdef xmlDoc* c_doc
- cdef char* c_name
name_utf = _utf8(name)
- c_name = _cstr(name_utf)
+ c_name = _xcstr(name_utf)
if c_name[0] == c'#':
if not _characterReferenceIsValid(c_name + 1):
raise ValueError, u"Invalid character reference: '%s'" % name
View
100 src/lxml/lxml.objectify.pyx
@@ -5,10 +5,13 @@ XML. It is based on `lxml.etree`.
from lxml.includes.etreepublic cimport _Document, _Element, ElementBase, ElementClassLookup
from lxml.includes.etreepublic cimport elementFactory, import_lxml__etree, textOf, pyunicode
from lxml.python cimport callable, _cstr
-cimport lxml.includes.etreepublic as cetree
+from lxml.tree cimport xmlChar, const_xmlChar, _xcstr
from lxml cimport python
from lxml.includes cimport tree
+
+cimport lxml.includes.etreepublic as cetree
cimport libc.string as cstring_h # not to be confused with stdlib 'string'
+from libc.string cimport const_char
__all__ = [u'BoolElement', u'DataElement', u'E', u'Element', u'ElementMaker',
u'FloatElement', u'IntElement', u'LongElement', u'NoneElement',
@@ -34,22 +37,21 @@ cdef tuple IGNORABLE_ERRORS = (ValueError, TypeError)
cdef object is_special_method = re.compile(u'__.*__$').match
cdef object _typename(object t):
- cdef char* c_name
cdef char* s
c_name = python._fqtypename(t)
s = cstring_h.strrchr(c_name, c'.')
if s is not NULL:
c_name = s + 1
- return pyunicode(c_name)
+ return pyunicode(<const_xmlChar*>c_name)
# namespace/name for "pytype" hint attribute
cdef object PYTYPE_NAMESPACE
cdef object PYTYPE_NAMESPACE_UTF8
-cdef char* _PYTYPE_NAMESPACE
+cdef const_xmlChar* _PYTYPE_NAMESPACE
cdef object PYTYPE_ATTRIBUTE_NAME
cdef object PYTYPE_ATTRIBUTE_NAME_UTF8
-cdef char* _PYTYPE_ATTRIBUTE_NAME
+cdef const_xmlChar* _PYTYPE_ATTRIBUTE_NAME
PYTYPE_ATTRIBUTE = None
@@ -86,8 +88,8 @@ def set_pytype_attribute_tag(attribute_tag=None):
PYTYPE_ATTRIBUTE_NAME = python.PyUnicode_FromEncodedObject(
PYTYPE_ATTRIBUTE_NAME_UTF8, 'UTF-8', NULL)
- _PYTYPE_NAMESPACE = _cstr(PYTYPE_NAMESPACE_UTF8)
- _PYTYPE_ATTRIBUTE_NAME = _cstr(PYTYPE_ATTRIBUTE_NAME_UTF8)
+ _PYTYPE_NAMESPACE = _xcstr(PYTYPE_NAMESPACE_UTF8)
+ _PYTYPE_ATTRIBUTE_NAME = _xcstr(PYTYPE_ATTRIBUTE_NAME_UTF8)
PYTYPE_ATTRIBUTE = cetree.namespacedNameFromNsName(
_PYTYPE_NAMESPACE, _PYTYPE_ATTRIBUTE_NAME)
@@ -98,19 +100,15 @@ set_pytype_attribute_tag()
cdef object XML_SCHEMA_NS, XML_SCHEMA_NS_UTF8
XML_SCHEMA_NS, XML_SCHEMA_NS_UTF8 = \
_unicodeAndUtf8(u"http://www.w3.org/2001/XMLSchema")
-cdef char* _XML_SCHEMA_NS
-_XML_SCHEMA_NS = _cstr(XML_SCHEMA_NS_UTF8)
+cdef const_xmlChar* _XML_SCHEMA_NS = _xcstr(XML_SCHEMA_NS_UTF8)
cdef object XML_SCHEMA_INSTANCE_NS, XML_SCHEMA_INSTANCE_NS_UTF8
XML_SCHEMA_INSTANCE_NS, XML_SCHEMA_INSTANCE_NS_UTF8 = \
_unicodeAndUtf8(u"http://www.w3.org/2001/XMLSchema-instance")
-cdef char* _XML_SCHEMA_INSTANCE_NS
-_XML_SCHEMA_INSTANCE_NS = _cstr(XML_SCHEMA_INSTANCE_NS_UTF8)
+cdef const_xmlChar* _XML_SCHEMA_INSTANCE_NS = _xcstr(XML_SCHEMA_INSTANCE_NS_UTF8)
-cdef object XML_SCHEMA_INSTANCE_NIL_ATTR
-XML_SCHEMA_INSTANCE_NIL_ATTR = u"{%s}nil" % XML_SCHEMA_INSTANCE_NS
-cdef object XML_SCHEMA_INSTANCE_TYPE_ATTR
-XML_SCHEMA_INSTANCE_TYPE_ATTR = u"{%s}type" % XML_SCHEMA_INSTANCE_NS
+cdef object XML_SCHEMA_INSTANCE_NIL_ATTR = u"{%s}nil" % XML_SCHEMA_INSTANCE_NS
+cdef object XML_SCHEMA_INSTANCE_TYPE_ATTR = u"{%s}type" % XML_SCHEMA_INSTANCE_NS
# Forward declaration
@@ -161,15 +159,10 @@ cdef class ObjectifiedElement(ElementBase):
Note that this only considers the first child with a given name.
"""
def __get__(self):
- cdef char* c_ns
- cdef char* c_child_ns
cdef _Element child
cdef dict children
c_ns = tree._getNs(self._c_node)
- if c_ns is NULL:
- tag = None<