[Lxml-checkins] r46712 - lxml/trunk/src/lxml

scoder at codespeak.net scoder at codespeak.net
Tue Sep 18 10:53:23 CEST 2007


Author: scoder
Date: Tue Sep 18 10:53:13 2007
New Revision: 46712

Modified:
   lxml/trunk/src/lxml/apihelpers.pxi
   lxml/trunk/src/lxml/etree.pyx
   lxml/trunk/src/lxml/etreepublic.pxd
   lxml/trunk/src/lxml/objectify.pyx
   lxml/trunk/src/lxml/public-api.pxi
Log:
new C-API function hasChild(), some cleanup to use it

Modified: lxml/trunk/src/lxml/apihelpers.pxi
==============================================================================
--- lxml/trunk/src/lxml/apihelpers.pxi	(original)
+++ lxml/trunk/src/lxml/apihelpers.pxi	Tue Sep 18 10:53:13 2007
@@ -426,6 +426,9 @@
             element._c_node, _cstr(ns), NULL)
         return '%s:%s' % (c_ns.prefix, tag)
 
+cdef int _hasChild(xmlNode* c_node):
+    return c_node is not NULL and _findChildForwards(c_node, 0) is not NULL
+
 cdef xmlNode* _findChild(xmlNode* c_node, Py_ssize_t index):
     if index < 0:
         return _findChildBackwards(c_node, -index - 1)

Modified: lxml/trunk/src/lxml/etree.pyx
==============================================================================
--- lxml/trunk/src/lxml/etree.pyx	(original)
+++ lxml/trunk/src/lxml/etree.pyx	Tue Sep 18 10:53:13 2007
@@ -859,8 +859,7 @@
             FutureWarning
             )
         # emulate old behaviour
-        c_node = _findChildBackwards(self._c_node, 0)
-        return c_node != NULL
+        return bool(_hasChild(self._c_node))
 
     def __contains__(self, element):
         cdef xmlNode* c_node

Modified: lxml/trunk/src/lxml/etreepublic.pxd
==============================================================================
--- lxml/trunk/src/lxml/etreepublic.pxd	(original)
+++ lxml/trunk/src/lxml/etreepublic.pxd	Tue Sep 18 10:53:13 2007
@@ -128,6 +128,9 @@
     ##########################################################################
     # XML node helper functions
 
+    # check if the element has at least one child
+    cdef int hasChild(tree.xmlNode* c_node)
+
     # find child element number 'index' (supports negative indexes)
     cdef tree.xmlNode* findChild(tree.xmlNode* c_node,
                                  python.Py_ssize_t index)

Modified: lxml/trunk/src/lxml/objectify.pyx
==============================================================================
--- lxml/trunk/src/lxml/objectify.pyx	(original)
+++ lxml/trunk/src/lxml/objectify.pyx	Tue Sep 18 10:53:13 2007
@@ -1238,7 +1238,7 @@
     cdef python.PyObject* dict_result
     lookup = <ObjectifyElementClassLookup>state
     # if element has children => no data class
-    if cetree.findChildForwards(c_node, 0) is not NULL:
+    if cetree.hasChild(c_node):
         return lookup.tree_class
 
     # if element is defined as xsi:nil, return NoneElement class
@@ -1448,7 +1448,7 @@
                 c_node, _PYTYPE_NAMESPACE, _PYTYPE_ATTRIBUTE_NAME)
             if old_pytypename is not None:
                 if old_pytypename == TREE_PYTYPE_NAME:
-                    if cetree.findChild(c_node, 0) is NULL:
+                    if not cetree.hasChild(c_node):
                         # only case where we should keep it,
                         # everything else is clear enough
                         pytype = TREE_PYTYPE
@@ -1468,7 +1468,7 @@
 
         if pytype is None:
             # try to guess type
-            if cetree.findChildForwards(c_node, 0) is NULL:
+            if not cetree.hasChild(c_node):
                 # element has no children => data class
                 pytype = _guessPyType(textOf(c_node), StrType)
             else:

Modified: lxml/trunk/src/lxml/public-api.pxi
==============================================================================
--- lxml/trunk/src/lxml/public-api.pxi	(original)
+++ lxml/trunk/src/lxml/public-api.pxi	Tue Sep 18 10:53:13 2007
@@ -106,6 +106,9 @@
                                        char* c_href, char* c_name):
     return _delAttributeFromNsName(c_element, c_href, c_name)
 
+cdef public int hasChild(xmlNode* c_node):
+    return _hasChild(c_node)
+
 cdef public xmlNode* findChild(xmlNode* c_node, Py_ssize_t index):
     return _findChild(c_node, index)
 


More information about the lxml-checkins mailing list