Skip to content

_elementtree.c import can fail silently #47725

Closed

Description

@naufraghi
mannequin
BPO 3475
Nosy @loewis, @naufraghi, @florentx
Superseder
  • bpo-6472: Update ElementTree with upstream changes
  • Files
  • fix-celementtree.patch: check for exception from PyRun_String, else release the returned ref.
  • 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 2010-03-11.14:51:35.118>
    created_at = <Date 2008-07-31.07:13:10.009>
    labels = ['expert-XML', 'type-bug']
    title = '_elementtree.c import can fail silently'
    updated_at = <Date 2010-03-11.14:51:35.115>
    user = 'https://github.com/naufraghi'

    bugs.python.org fields:

    activity = <Date 2010-03-11.14:51:35.115>
    actor = 'flox'
    assignee = 'effbot'
    closed = True
    closed_date = <Date 2010-03-11.14:51:35.118>
    closer = 'flox'
    components = ['XML']
    creation = <Date 2008-07-31.07:13:10.009>
    creator = 'naufraghi'
    dependencies = []
    files = ['15290']
    hgrepos = []
    issue_num = 3475
    keywords = ['patch']
    message_count = 7.0
    messages = ['70488', '70489', '70656', '95038', '95043', '95044', '100860']
    nosy_count = 5.0
    nosy_names = ['loewis', 'effbot', 'ferringb', 'naufraghi', 'flox']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = '6472'
    type = 'behavior'
    url = 'https://bugs.python.org/issue3475'
    versions = ['Python 2.7', 'Python 3.2']

    Activity

    1. naufraghi commented on Jul 31, 2008

      naufraghimannequin
      MannequinAuthor

      Playing with PyInstaller I have found that the final part of
      _elementtree.c:

      Index: Modules/_elementtree.c
      ===================================================================
      --- Modules/_elementtree.c (revisione 59540)
      +++ Modules/_elementtree.c (copia locale)
      @@ -2780,7 +2780,10 @@

         );
      
      • PyRun_String(bootstrap, Py_file_input, g, NULL);
        + if (PyRun_String(bootstrap, Py_file_input, g, NULL) == NULL)
        + return;
          elementpath_obj = PyDict_GetItemString(g, "ElementPath");

      execute a bit of python code without checking the return value.
      That can lead to weird things playing with import hooks,
      for example an assert like this can fail:

      Index: Lib/test/test_elemettree.py
      ===================================================================

      --- Lib/test/test_elemettree.py (revisione 0)
      +++ Lib/test/test_elemettree.py (revisione 0)
      @@ -0,0 +1,21 @@
      +#! /usr/bin/env python
      +
      +def importHook(*args, **kwargs):
      +    if 'xml.etree' in args:
      +        raise ImportError
      +    else:
      +        return __real__import__(*args, **kwargs)
      +
      +import os
      +import __builtin__
      +__real__import__ = __builtin__.__import__
      +__builtin__.__import__ = importHook
      +
      +try:
      +    import xml.etree.cElementTree as cET
      +except ImportError:
      +    pass
      +else:
      +    out = os.popen("python -c 'import xml.etree.cElementTree as cET; 
      print dir(cET)'").read().strip()
      +    assert str(dir(cET)) == out, (str(dir(cET)), out)
      +
    2. loewis commented on Jul 31, 2008

      loewismannequin
      Mannequin

      Fredrik, can you take a look?

    3. effbot commented on Aug 3, 2008

      effbotmannequin
      Mannequin

      This is fixed in the ET 1.3-compatible codebase. Since it's too late to
      add ET 1.3 to 2.6, I guess it's time to make a new 1.2 bugfix release
      for 2.6.

    4. ferringb commented on Nov 8, 2009

      ferringbmannequin
      Mannequin

      At this point, this affects 2.5, 2.6, and 3.1 (and the normal 1.0.5
      release of cElementTree); what's required to get this fixed and queued
      up for micro/minor releases?

      Sidenote, the patch posted above still leaks a reference-

    5. loewis commented on Nov 8, 2009

      loewismannequin
      Mannequin

      For 2.5, this will not be fixed, as it is not security-critical.

    6. effbot commented on Nov 8, 2009

      effbotmannequin
      Mannequin

      Note that "fail silently" is a bit of a misnomer - if the embedded import
      doesn't work, portions of the library will fail pretty loudly. Feel free
      to use some variation of the suggested patch, or just wait until the next
      upstream release gets imported (if ever).

    7. florentx commented on Mar 11, 2010

      florentxmannequin
      Mannequin

      Fixed with latest xml.etree.

    8. 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

      topic-XMLtype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions