Can't pickle IntElement/StringElement objects

Asked by koba

Environment info:
{{{
Ubuntu 9.10
lxml.etree: (2, 2, 4, 0)
libxml used: (2, 7, 5)
libxml compiled: (2, 7, 5)
libxslt used: (1, 1, 24)
libxslt compiled: (1, 1, 24)
}}}

How to reproduce:
{{{
import pickle
from lxml import objectify
pickle.dumps(objectify.fromstring('<a><b><c/></b></a>').b) # Works.
pickle.dumps(objectify.fromstring('<a><b>1234</b></a>').b) # Fails.
pickle.dumps(objectify.fromstring('<a><b>abcd</b></a>').b) # Fails.
pickle.dumps(objectify.fromstring('<a><b/></a>').b) # Fails.
}}}

Example traceback:
{{{
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/pickle.py", line 1366, in dumps
    Pickler(file, protocol).dump(obj)
  File "/usr/lib/python2.6/pickle.py", line 224, in dump
    self.save(obj)
  File "/usr/lib/python2.6/pickle.py", line 306, in save
    rv = reduce(self.proto)
  File "/usr/lib/python2.6/copy_reg.py", line 70, in _reduce_ex
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle StringElement objects
}}}

I haven't filed a bug report since I don't know if this is the expected behavior or not.

Question information

Language:
English Edit question
Status:
Open
For:
lxml Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Jasper Op de Coul (jasper-infrae) said :
#1

On a possibly related note, ElementStringResult and ElementUnicodeResult can't be
pickled either. I tried this in the latest dev release:

>>> from lxml import etree; import pickle
>>> doc = etree.fromstring('<doc>spam</doc>')
>>> isinstance(doc.text, str)
True
>>> pickle.dumps(doc.text)
"S'spam'\np0\n."
>>> isinstance(doc.xpath('text()')[0], str)
True
>>> pickle.dumps(doc.xpath('text()')[0])
TypeError: can't pickle _Element objects

I understand that the result objects have an additional `parent` method, so they are not exactly strings,
but I would prefer if they could be pickled as strings. It's tedious to convert all xpath results to normal
str/unicode objects because someone might pickle them (zodb)

Can you help with this problem?

Provide an answer of your own, or ask koba for more information if necessary.

To post a message you must log in.