classification
Title: cElementTree behaves differently compared to ElementTree
Type: behavior Stage: resolved
Components: Library (Lib), XML Versions: Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: eli.bendersky, poojariravi, scoder, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-09-14 10:06 by poojariravi, last changed 2018-05-28 16:08 by serhiy.storchaka. This issue is now closed.

Messages (5)
msg302164 - (view) Author: Ravikumar (poojariravi) Date: 2017-09-14 10:06
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 
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
</data>
/tmp/Python$


OUTPUT

/tmp/Python$ python testxml.py 
Traceback (most recent call last):
  File "testxml.py", line 10, in <module>
    elems_saved["1"]=root
  File "<string>", 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
msg302165 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-14 10:45
This error is from cPickle.

>>> import xml.etree.cElementTree as ET
>>> root = ET.XML('<data/>')
>>> import pickle
>>> pickle.dumps(root)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  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 "<stdin>", line 1, in <module>
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.
msg302166 - (view) Author: Ravikumar (poojariravi) Date: 2017-09-14 11:16
Thanks for the quick update. It helped us to understand the issue.

We will check of how to proceed further in our implementation.

/ Ravi
msg302167 - (view) Author: Ravikumar (poojariravi) Date: 2017-09-14 11:32
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
msg302168 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-14 11:43
Use ElementTree. In Python 3 ElementTree is accelerated and cElementTree is a deprecated alias of ElementTree.
History
Date User Action Args
2018-05-28 16:08:46serhiy.storchakasetpull_requests: - pull_request6786
2018-05-28 13:23:27twisteroid ambassadorsetpull_requests: + pull_request6786
2018-05-28 11:29:25serhiy.storchakasetpull_requests: - pull_request6783
2018-05-28 11:01:49twisteroid ambassadorsetpull_requests: + pull_request6783
2017-09-14 12:39:21serhiy.storchakasetresolution: wont fix
2017-09-14 12:24:41poojariravisetstatus: open -> closed
2017-09-14 11:43:11serhiy.storchakasetmessages: + msg302168
2017-09-14 11:32:55poojariravisetstatus: closed -> open

messages: + msg302167
2017-09-14 11:16:02poojariravisetstatus: open -> closed

messages: + msg302166
stage: resolved
2017-09-14 10:45:35serhiy.storchakasetnosy: + serhiy.storchaka, eli.bendersky, scoder
messages: + msg302165
2017-09-14 10:06:21poojariravicreate