Skip to content

xml.etree.ElementTree.Element is no longer pickleable #60280

Closed

Description

@einarfd
mannequin
BPO 16076
Nosy @birkenfeld, @jcea, @larryhastings, @ezio-melotti, @skrah, @florentx
Files
  • i16076-cpatch.diff
  • i16076-v3-combined.diff
  • i16076-v4-combined.diff
  • i16076-v5.diff
  • transcript-test___all__.txt
  • i16076-v6.diff
  • i16076-v7.diff
  • i16076-v8.diff
  • memleak-v1.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2013-01-12.13:21:35.399>
    created_at = <Date 2012-09-28.08:54:59.477>
    labels = ['extension-modules', 'expert-XML', 'type-bug', 'library', 'release-blocker']
    title = 'xml.etree.ElementTree.Element is no longer pickleable'
    updated_at = <Date 2013-01-12.13:43:31.998>
    user = 'https://bugs.python.org/einarfd'

    bugs.python.org fields:

    activity = <Date 2013-01-12.13:43:31.998>
    actor = 'python-dev'
    assignee = 'none'
    closed = True
    closed_date = <Date 2013-01-12.13:21:35.399>
    closer = 'python-dev'
    components = ['Extension Modules', 'Library (Lib)', 'XML']
    creation = <Date 2012-09-28.08:54:59.477>
    creator = 'einarfd'
    dependencies = []
    files = ['28199', '28252', '28261', '28363', '28484', '28502', '28627', '28639', '28691']
    hgrepos = []
    issue_num = 16076
    keywords = ['patch', '3.3regression']
    message_count = 50.0
    messages = ['171418', '171737', '172297', '172613', '172665', '176854', '177134', '177136', '177141', '177170', '177590', '177766', '178498', '178499', '178500', '178509', '178546', '178578', '178579', '178627', '178632', '178721', '178736', '178745', '178770', '178776', '178783', '178788', '178946', '179002', '179011', '179032', '179243', '179244', '179314', '179315', '179354', '179356', '179386', '179393', '179395', '179400', '179544', '179546', '179553', '179642', '179651', '179667', '179790', '179792']
    nosy_count = 12.0
    nosy_names = ['georg.brandl', 'jcea', 'larry', 'ezio.melotti', 'Arfrever', 'eli.bendersky', 'skrah', 'flox', 'santoso.wijaya', 'python-dev', 'einarfd', 'danielsh']
    pr_nums = []
    priority = 'release blocker'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue16076'
    versions = ['Python 3.3', 'Python 3.4']

    Activity

    1. einarfd commented on Sep 28, 2012

      einarfdmannequin
      MannequinAuthor

      The xml.etree.ElementTree.Element class is no longer pickleable in Python 3.3.0rc3 .

      This is a regression from Python 3.2 where the Element class was pickleable, while the xml.etree.cElementTree.Element was not. So this is probably related to switching the ElementTree implementation in Python 3.3.

      I've looked at the "what's new" documentation in Python 3.3, and there was nothing that indicated that the ElemenTree switchover would give this kind of issues.

      I've run into this issues because I use the multiprocessing module to pass parsed XML documents between processes, and when I can't pickle Element objects. This stops working.

      You can reproduce the issue with the following code:
      import pickle
      from xml.etree.ElementTree import Element

      print(pickle.dumps(Element("foo")))
    2. added
      stdlib
      type-bug
      on Sep 28, 2012
    3. removed
      stdlib
      on Sep 28, 2012
    4. serhiy-storchaka commented on Oct 1, 2012

      @serhiy-storchaka
      Member

      In 3.2 repr(xml.etree.ElementTree.Element) is "<class 'xml.etree.ElementTree.Element'>".
      In 3.3 repr(xml.etree.ElementTree.Element) is "<class 'Element'>".

    5. elibendersky commented on Oct 7, 2012

      elibenderskymannequin
      Mannequin

      Will look into it, thanks for the report.

    6. santosowijaya commented on Oct 11, 2012

      santosowijayamannequin
      Mannequin

      This is because, in Python 3.3, xml.etree.ElementTree.Element is overridden by the C accelerator version, _elementtree.Element.

      If you remove the statement,

      1708 from _elementtree import *

      the behavior from Python 3.2 comes back.

    7. santosowijaya commented on Oct 11, 2012

      santosowijayamannequin
      Mannequin

      OTOH, xml.etree.cElementTree.Element in Python 3.2 and earlier has never been pickleable, either.

      Python 3.2.3+ (3.2:24499eebbc2f, Oct 10 2012, 13:54:45) 
      [GCC 4.6.3] on linux2
      Type "help", "copyright", "credits" or "license" for more information.
      >>> from xml.etree.cElementTree import Element
      >>> repr(Element)
      '<built-in function Element>'
      >>> import pickle
      >>> pickle.dumps(Element('foo'))
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      _pickle.PicklingError: Can't pickle <class 'Element'>: attribute lookup builtins.Element failed
    8. added
      stdlib
      on Oct 11, 2012
    9. danielsh commented on Dec 3, 2012

      danielshmannequin
      Mannequin

      Attached patch for your consideration.

      I've tested pickling/unpickling and comparing the resulting object attribute by attribute (.tag, .attrib, .text, .tail for equality; and recursively .getchildren()), and 'make test' --- all seems to work.

      If the approach is sound, I can submit a revised patch that also updates documentation, regression tests, and potententially updates other type objects in the same manner as this patch updates _elementtree.Element .

    10. added
      extension-modules
      on Dec 3, 2012
    11. danielsh commented on Dec 7, 2012

      danielshmannequin
      Mannequin

      TreeBuilder was also pickleable in 3.2 but now isn't so; updating summary accordingly.

      Attaching a checkpoint patch. It addresses both Element and TreeBuilder, and adds tests. The added tests fail if test___all__ is included in the test run, so the patch should not be applied as-is.

      Thanks to Ezio Melotti for preliminary review and suggestions on IRC.

    12. changed the title [-]xml.etree.ElementTree.Element is no longer pickleable[/-] [+]xml.etree.ElementTree.Element and xml.etree.ElementTree.TreeBuilder are no longer pickleable[/+] on Dec 7, 2012
    13. danielsh commented on Dec 7, 2012

      danielshmannequin
      Mannequin

      Reattaching without the unrelated Python-ast.c change.

    14. 33 remaining items

    15. danielsh commented on Jan 8, 2013

      danielshmannequin
      Mannequin

      Dissociating TreeBuilder from this issue per recent comments.

    16. changed the title [-]xml.etree.ElementTree.Element and xml.etree.ElementTree.TreeBuilder are no longer pickleable[/-] [+]xml.etree.ElementTree.Element is no longer pickleable[/+] on Jan 8, 2013
    17. danielsh commented on Jan 8, 2013

      danielshmannequin
      Mannequin

      v8 removes TreeBuilder handling (code + tests) and removes memcpy per review. (Outstanding issues in the review can be fixed post-v8, if needed.)

    18. python-dev commented on Jan 10, 2013

      python-devmannequin
      Mannequin

      New changeset 8d6dadfecf22 by Eli Bendersky in branch '3.3':
      Issue bpo-16076: make _elementtree.Element pickle-able in a way that is compatible
      http://hg.python.org/cpython/rev/8d6dadfecf22

      New changeset 4c268b7c86e6 by Eli Bendersky in branch 'default':
      Issue bpo-16076: make _elementtree.Element pickle-able in a way that is compatible
      http://hg.python.org/cpython/rev/4c268b7c86e6

    19. elibendersky commented on Jan 10, 2013

      elibenderskymannequin
      Mannequin

      Fixed in 3.3 and default. Thanks for the good work, Daniel. A separate issue can be opened for TreeBuilder.

    20. python-dev commented on Jan 10, 2013

      python-devmannequin
      Mannequin

      New changeset c46054b49b6c by Eli Bendersky in branch '3.3':
      Update Misc/NEWS for issue bpo-16076
      http://hg.python.org/cpython/rev/c46054b49b6c

    21. ezio-melotti commented on Jan 11, 2013

      @ezio-melotti
      Member

      The fix introduced some refleaks:
      $ ./python -m test -R3:2 test_xml_etree_c
      test_xml_etree_c leaked [56, 56] references, sum=112

      One seems to be in __getstate__:
      diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
      --- a/Modules/_elementtree.c
      +++ b/Modules/_elementtree.c
      @@ -859,8 +859,10 @@
                                            PICKLED_ATTRIB, self->extra->attrib,
                                            PICKLED_TEXT, self->text,
                                            PICKLED_TAIL, self->tail);
      -    if (instancedict)
      +    if (instancedict) {
      +        Py_DECREF(children);
               return instancedict;
      +    }
           else {
               for (i = 0; i < PyList_GET_SIZE(children); i++)
                   Py_DECREF(PyList_GET_ITEM(children, i));

      I'm still looking for the other.

    22. ezio-melotti commented on Jan 11, 2013

      @ezio-melotti
      Member

      This is what I found out.
      I used an easily copy/pastable one-liner that creates 3 variables: e (no children), e2 (3 children), e3 (5 children).

      Original leaky code (test_xml_etree_c leaked [56, 56] references, sum=112):
      >>> from xml.etree import ElementTree as ET; e = ET.Element('foo'); SAMPLE_XML = "<body><tag class='a'>text</tag><tag class='b' /><section><tag class='b' id='inner'>subtext</tag></section></body>"; e2 = ET.XML(SAMPLE_XML); SAMPLE_XML = "<body><tag class='a'>text</tag><tag class='b' /><tag class='b' /><tag class='b' /><section><tag class='b' id='inner'>subtext</tag></section></body>"; e3 = ET.XML(SAMPLE_XML)
      [76773 refs]
      >>> ### e has no children and leaks 1 ref:
      >>> e.__getstate__()
      {'tag': 'foo', 'attrib': {}, 'text': None, 'tail': None, '_children': []}
      [76791 refs]
      >>> e.__getstate__()
      {'tag': 'foo', 'attrib': {}, 'text': None, 'tail': None, '_children': []}
      [76792 refs]
      >>> e.__getstate__()
      {'tag': 'foo', 'attrib': {}, 'text': None, 'tail': None, '_children': []}
      [76793 refs]
      >>> ### e2 has 3 children and leaks 4 refs:
      >>> e2.__getstate__()
      {'tag': 'body', 'attrib': {}, 'text': None, 'tail': None, '_children': [<Element 'tag' at 0xb735cef4>, <Element 'tag' at 0xb7368034>, <Element 'section' at 0xb73688f4>]}
      [76798 refs]
      >>> e2.__getstate__()
      {'tag': 'body', 'attrib': {}, 'text': None, 'tail': None, '_children': [<Element 'tag' at 0xb735cef4>, <Element 'tag' at 0xb7368034>, <Element 'section' at 0xb73688f4>]}
      [76802 refs]

      The leaked refs seems to be 1 for the children list + 1 for each children.
      The diff I pasted in the previous *seems* to fix this (i.e. leaks in __gestate__ are gone, tests pass), but I had it crash once (couldn't reproduce after that, so it might be unrelated*), and I'm not sure it's correct.

      With that patch applied we go down to test_xml_etree_c leaked [6, 6] references, sum=12.
      The remaining leaks seem to be in __setstate__.
      Patched code:
      >>> from xml.etree import ElementTree as ET; e = ET.Element('foo'); SAMPLE_XML = "<body><tag class='a'>text</tag><tag class='b' /><section><tag class='b' id='inner'>subtext</tag></section></body>"; e2 = ET.XML(SAMPLE_XML); SAMPLE_XML = "<body><tag class='a'>text</tag><tag class='b' /><tag class='b' /><tag class='b' /><section><tag class='b' id='inner'>subtext</tag></section></body>"; e3 = ET.XML(SAMPLE_XML)
      [76773 refs]
      >>> ### no more leaks for getstate:
      >>> p = e.__getstate__()
      [76787 refs]
      >>> p = e.__getstate__()
      [76787 refs]
      >>> ### also no leaks when there are no child:
      >>> e.__setstate__(p)
      [76788 refs]
      >>> e.__setstate__(p)
      [76788 refs]
      >>> ### no more leaks for getstate with children:
      >>> p2 = e2.__getstate__()
      [76807 refs]
      >>> p2 = e2.__getstate__()
      [76807 refs]
      >>> ### one ref leaked for every child in __setstate__:
      >>> e2.__setstate__(p2)
      [76810 refs]
      >>> e2.__setstate__(p2)
      [76813 refs]
      >>> e2.__setstate__(p2)
      [76816 refs]

      I'm not working on this anymore now, so someone more familiar with the code can take a look, see if my patch is correct, and fix the remaining leaks.

      • maybe I'm doing something wrong, but ISTM that make -j2 doesn't always work right away, and sometimes I get different results if I do it again without touching the code.
    23. danielsh commented on Jan 11, 2013

      danielshmannequin
      Mannequin
      Ezio Melotti wrote on Fri, Jan 11, 2013 at 08:44:43 +0000:
      > >>> ### one ref leaked for every child in __setstate__:
      > >>> e2.__setstate__(p2)
      > [76810 refs]
      > >>> e2.__setstate__(p2)
      > [76813 refs]
      > >>> e2.__setstate__(p2)
      > [76816 refs]
      > 
      > I'm not working on this anymore now, so someone more familiar with the
      > code can take a look, see if my patch is correct, and fix the
      > remaining leaks.
      > 

      Thank for narrowing it down so much. Attached patch incorporates your
      getstate fix and a fix for setstate. It passes the -R 3:2 tests.

    24. python-dev commented on Jan 12, 2013

      python-devmannequin
      Mannequin

      New changeset 5b36768b9a11 by Eli Bendersky in branch '3.3':
      Issue bpo-16076: fix refleak in pickling of Element.
      http://hg.python.org/cpython/rev/5b36768b9a11

      New changeset 848738d3c40f by Eli Bendersky in branch 'default':
      Close bpo-16076: fix refleak in pickling of Element.
      http://hg.python.org/cpython/rev/848738d3c40f

    25. python-dev commented on Jan 12, 2013

      python-devmannequin
      Mannequin

      New changeset 4501813ea676 by Eli Bendersky in branch '3.3':
      Issue bpo-16076: check for return value of PyTuple_New for args (following
      http://hg.python.org/cpython/rev/4501813ea676

      New changeset 7313096e0bad by Eli Bendersky in branch 'default':
      Issue bpo-16076: check for return value of PyTuple_New for args (following
      http://hg.python.org/cpython/rev/7313096e0bad

    26. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      extension-modulesrelease-blockerstdlibtopic-XMLtype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions