[lxml-dev] inconsistency in the text nodes?
Hi, I've set up the following excerpt to reproduce it:
from lxml import etree s = etree._ElementStringResult("herve") u = etree._ElementUnicodeResult(u"Hervé") print s.getparent() Traceback (most recent call last): File "<input>", line 1, in <module> File "extensions.pxi", line 603, in lxml.etree._ElementStringResult.getparent (src/lxml/lxml.etree.c:99519) AttributeError: '_ElementStringResult' object has no attribute '_parent' print u.getparent() None
I'd like both of them to have the same behaviour, my personal preference being returning None. I guess it would look like that: --- lxml/extensions.pxi.orig 2009-11-13 09:46:27.760913564 +0100 +++ lxml/extensions.pxi 2009-11-13 09:46:34.869420791 +0100 @@ -597,6 +597,8 @@ return self._parent class _ElementStringResult(str): + cdef _Element _parent + cdef readonly object is_tail + cdef readonly object is_text + cdef readonly object is_attribute + # we need to use a Python class here, str cannot be C-subclassed # in Pyrex/Cython def getparent(self): I'm playing with these directly because I need to wrap them. Regards, Hervé
Hi, Hervé Cauwelier, 13.11.2009 09:57:
I've set up the following excerpt to reproduce it:
from lxml import etree s = etree._ElementStringResult("herve") u = etree._ElementUnicodeResult(u"Hervé") print s.getparent() Traceback (most recent call last): File "<input>", line 1, in <module> File "extensions.pxi", line 603, in lxml.etree._ElementStringResult.getparent (src/lxml/lxml.etree.c:99519) AttributeError: '_ElementStringResult' object has no attribute '_parent' print u.getparent() None
Note the leading underscore in the class names. They are not public.
I'd like both of them to have the same behaviour, my personal preference being returning None. I guess it would look like that:
--- lxml/extensions.pxi.orig 2009-11-13 09:46:27.760913564 +0100 +++ lxml/extensions.pxi 2009-11-13 09:46:34.869420791 +0100 @@ -597,6 +597,8 @@ return self._parent
class _ElementStringResult(str): + cdef _Element _parent + cdef readonly object is_tail + cdef readonly object is_text + cdef readonly object is_attribute + # we need to use a Python class here, str cannot be C-subclassed # in Pyrex/Cython def getparent(self):
That's not valid Cython code. "_ElementStringResult" is a normal Python class.
I'm playing with these directly because I need to wrap them.
Can you provide some details why you want to do that? Stefan
On 15/11/2009 13:52, Stefan Behnel wrote:
Note the leading underscore in the class names. They are not public.
Still, they are exposed in XPath results.
That's not valid Cython code. "_ElementStringResult" is a normal Python class.
Then it's: --- extensions.pxi.orig 2009-11-13 09:46:27.760913564 +0100 +++ extensions.pxi 2009-11-16 10:01:27.399150556 +0100 @@ -597,6 +597,11 @@ return self._parent class _ElementStringResult(str): + _parent = None + is_tail = None + is_text = None + is_attribute = None + # we need to use a Python class here, str cannot be C-subclassed # in Pyrex/Cython def getparent(self): But the idea is the same.
I'm playing with these directly because I need to wrap them.
Can you provide some details why you want to do that?
I'm abstracting the XML library in the library I design. Now I needed text node objects that are always unicode and with an API similar to the rest of the library. I inherit from the Python unicode class, and wrap _Element*Result's parent in my own element class.
participants (2)
-
Hervé Cauwelier -
Stefan Behnel