Issue 31467: cElementTree behaves differently compared to ElementTree (original) (raw)

I have started learning python and I see below snippet fails. I do not understand how to fix the issue and assume this might be a bug in multiprocessing library.

I use Python 2.7.12

My python snippet

import xml.etree.ElementTree as ET # WORKS import xml.etree.cElementTree as ET # DOES NOT WORK import multiprocessing

tree = ET.parse('country_data.xml') root = tree.getroot()

manager = multiprocessing.Manager() elems_saved = manager.dict()

elems_saved["1"]=root

/tmp/Python$ cat country_data.xml 1 2008 141100 /tmp/Python$

OUTPUT

/tmp/Python$ python testxml.py Traceback (most recent call last): File "testxml.py", line 10, in elems_saved["1"]=root File "", line 2, in setitem File "/usr/lib/python2.7/multiprocessing/managers.py", line 758, in _callmethod conn.send((self._id, methodname, args, kwds)) TypeError: expected string or Unicode object, NoneType found

Please let me know if I am wrong

This error is from cPickle.

import xml.etree.cElementTree as ET root = ET.XML('') import pickle pickle.dumps(root) Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/pickle.py", line 1380, in dumps Pickler(file, protocol).dump(obj) File "/usr/lib/python2.7/pickle.py", line 224, in dump self.save(obj) File "/usr/lib/python2.7/pickle.py", line 331, in save self.save_reduce(obj=obj, *rv) File "/usr/lib/python2.7/pickle.py", line 400, in save_reduce save(func) File "/usr/lib/python2.7/pickle.py", line 286, in save f(self, obj) # Call unbound method with explicit self File "/usr/lib/python2.7/pickle.py", line 754, in save_global (obj, module, name)) pickle.PicklingError: Can't pickle <function copyelement at 0x7f8acaf5b758>: it's not found as main.copyelement import cPickle cPickle.dumps(root) Traceback (most recent call last): File "", line 1, in TypeError: expected string or Unicode object, NoneType found

cElementTree.Element is not pickleable in Python 2. And I think it is too later to add this feature in 2.7. The obvious solution -- use Python 3.

Just now I have figured out that , in the documentation it was mentioned that cElementTree module is deprecated in Python 3.3 version.

"The xml.etree.ElementTree module implements a simple and efficient API for parsing and creating XML data.

Changed in version 3.3: This module will use a fast implementation whenever available. The xml.etree.cElementTree module is deprecated."

https://docs.python.org/3/library/xml.etree.elementtree.html

In that case, will your resolution comment still holds valid ?? If so then, how to use cElementTree in my usecase