TypeError: can't pickle StringElement objects

Bug #509504 reported by scoder
12
This bug affects 2 people
Affects Status Importance Assigned to Milestone
lxml
Fix Released
Medium
Holger Joukl

Bug Description

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
}}}

scoder (scoder)
Changed in lxml:
assignee: nobody → Holger Joukl (jholg)
importance: Undecided → Medium
status: New → Confirmed
Revision history for this message
Holger Joukl (jholg) wrote :
Download full text (5.3 KiB)

Fixed on the trunk:

Committed revision 71085.

Changes:

$ svn diff -rCOMMITTED
Index: src/lxml/tests/test_objectify.py
===================================================================
--- src/lxml/tests/test_objectify.py (revision 71046)
+++ src/lxml/tests/test_objectify.py (working copy)
@@ -2328,6 +2328,43 @@
             etree.tostring(new_tree),
             etree.tostring(tree))

+ def test_pickle_intelement(self):
+ self._test_pickle('<x>42</x>')
+ self._test_pickle(objectify.DataElement(42))
+
+ def test_pickle_floattelement(self):
+ self._test_pickle('<x>42.0</x>')
+ self._test_pickle(objectify.DataElement(42.0))
+
+ def test_pickle_strelement(self):
+ self._test_pickle('<x>Pickle me!</x>')
+ self._test_pickle(objectify.DataElement('Pickle me!'))
+
+ def test_pickle_boolelement(self):
+ self._test_pickle('<x>true</x>')
+ self._test_pickle('<x>false</x>')
+ self._test_pickle(objectify.DataElement(True))
+ self._test_pickle(objectify.DataElement(False))
+
+ def test_pickle_noneelement(self):
+ self._test_pickle('''
+<x xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>''')
+ self._test_pickle(objectify.DataElement(None))
+
+ def _test_pickle(self, stringOrElt):
+ import pickle
+ if isinstance(stringOrElt, (etree._Element, etree._ElementTree)):
+ elt = stringOrElt
+ else:
+ elt = self.XML(stringOrElt)
+ out = BytesIO()
+ pickle.dump(elt, out)
+
+ new_elt = pickle.loads(out.getvalue())
+ self.assertEquals(
+ etree.tostring(new_elt),
+ etree.tostring(elt)) ...

Read more...

scoder (scoder)
Changed in lxml:
status: Confirmed → Fix Committed
milestone: none → 2.3
Revision history for this message
scoder (scoder) wrote :

Fixed in lxml 2.3alpha1.

Changed in lxml:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.