3

I have a session in an instance running ZODB, which parses a page and then stores the lxml object. It later throws:

AssertionError: invalid Element proxy at 4495778632 

It's not easy to reproduce in my particular case, but this code also does it:

from lxml import etree tree = etree.fromstring("<html><body>test</body></html>" , etree.HTMLParser()) c=[ x for x in tree.iter() ][0] print(c.__class__()) 

What is going on?

2 Answers 2

5

I got this AssertionError when I was trying to do operations on an element node that I had passed as an argument to a celery @shared_task on it's .delay call. To fix the error, rather than passing in the Element, I passed in the xml_string and did a fresh ET.fromstring(xml_string) within the @shared_task. With the fresh doc there all the etree operations worked fine. Must have been something to do with the serialization of the Element as it went into the celery queues.

Sign up to request clarification or add additional context in comments.

Comments

1

Your error message says that there does not exist any element proxy. Proxy means the according C representation of the node, which is missing.

With c.__class__() you try to call the constructor of the _Element class. The documentation of lxml says:

It is important to know that every proxy in lxml has a factory function that properly sets up C level members. Proxy objects must never be instantiated outside of that factory. For example, to instantiate an _Element object or its subclasses, you must always call its factory function::

cdef xmlNode* c_node cdef _Document doc cdef _Element element ... element = _elementFactory(doc, c_node) 

Without using the factory pattern and passing the c_node, the constructor will fail because of the assertions:

lxml/src/lxml/apihelpers.pxi:

cdef inline int _assertValidNode(_Element element) except -1: assert element._c_node is not NULL, u"invalid Element proxy at %s" % id(element) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.