Skip to content

DeprecationWarning for doctype() method when subclassing _elementtree.XMLParser #63375

Closed

Description

@vadmium
BPO 19176
Nosy @ezio-melotti, @berkerpeksag, @vadmium, @serhiy-storchaka
Files
  • inherit-doctype.patch
  • doctype-remove.patch
  • doctype-remove.v2.patch: Remove instead of fixing
  • inherit-doctype.v2.patch: Fix without removing
  • doctype-remove.v3.patch: Remove instead of fixing
  • doctype-remove.v4.patch
  • inherit-doctype.v3.patch: For 3.4
  • 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 = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2015-06-29.20:50:13.950>
    created_at = <Date 2013-10-06.06:42:30.384>
    labels = ['extension-modules', 'expert-XML', 'type-bug']
    title = 'DeprecationWarning for doctype() method when subclassing _elementtree.XMLParser'
    updated_at = <Date 2015-06-29.20:50:13.948>
    user = 'https://github.com/vadmium'

    bugs.python.org fields:

    activity = <Date 2015-06-29.20:50:13.948>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2015-06-29.20:50:13.950>
    closer = 'serhiy.storchaka'
    components = ['Extension Modules', 'XML']
    creation = <Date 2013-10-06.06:42:30.384>
    creator = 'martin.panter'
    dependencies = []
    files = ['37476', '37490', '38755', '38756', '38757', '39391', '39820']
    hgrepos = []
    issue_num = 19176
    keywords = ['patch']
    message_count = 22.0
    messages = ['199031', '201812', '214165', '232791', '232860', '232862', '238783', '239671', '239673', '239674', '239757', '239772', '243330', '243332', '243466', '243470', '245047', '245205', '245857', '245945', '245963', '245969']
    nosy_count = 7.0
    nosy_names = ['ezio.melotti', 'Arfrever', 'eli.bendersky', 'python-dev', 'berker.peksag', 'martin.panter', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue19176'
    versions = ['Python 3.4', 'Python 3.5', 'Python 3.6']

    Activity

    1. vadmium commented on Oct 6, 2013

      @vadmium
      MemberAuthor

      I am using the C version of Element Tree via the main ElementTree module. I have subclassed XMLParser, and created my own target object. I am not that interested in XML doctypes, but the following simplified code raises a DeprecationWarning:

      $ python3.3 -Wall
      Python 3.3.2 (default, May 16 2013, 23:40:52) 
      [GCC 4.6.3] on linux
      Type "help", "copyright", "credits" or "license" for more information.
      >>> from xml.etree.ElementTree import XMLParser
      >>> class CustomParser(XMLParser): pass
      ... 
      >>> CustomParser().feed("<!DOCTYPE blaua>")
      __main__:1: DeprecationWarning: This method of XMLParser is deprecated.  Define doctype() method on the TreeBuilder target.

      Looking at the C code, the logic is wrong. Subclasses of XMLParser will normally always have a doctype() method, at least by inheritance. So the code should compare the method with the XMLParser.doctype() base method rather than just checking that it exists. The native Python version seems to get it right.

      http://hg.python.org/cpython/file/50ea4dccb03e/Modules/_elementtree.c#l3091

      It looks like this may not be an issue for Python 3.4 because according to bpo-13248 the deprecated doctype() method is due to be removed.

    2. elibendersky commented on Oct 31, 2013

      elibenderskymannequin
      Mannequin

      Thanks for the report, Martin. I'll take a look once I get some time

    3. vadmium commented on Mar 20, 2014

      @vadmium
      MemberAuthor

      Actually I am still seeing this in 3.4.0. Looks like the doctype() method was not removed after all.

    4. vadmium commented on Dec 17, 2014

      @vadmium
      MemberAuthor

      Since the doctype() method doesn’t appear to be removed from the “default” (3.5?) branch either, here is a patch that avoids the deprecation warning when inheriting method + a test.

      Hopefully this will be useful when you get some time to look at it. I’m not sure if the PyCFunction_ calls are a good idea; they don’t seem to be documented, but I couldn’t think of a better way to check if the method was overridden.

    5. vadmium commented on Dec 18, 2014

      @vadmium
      MemberAuthor

      Here is another patch that removes the method instead, as suggested in the review

    6. Arfrever commented on Dec 18, 2014

      Arfrevermannequin
      Mannequin

      doctype-remove.patch is acceptable for both 3.4 and 3.5 or only 3.5?
      If only 3.5, then please apply inherit-doctype.patch in 3.4.

    7. serhiy-storchaka commented on Mar 21, 2015

      @serhiy-storchaka
      Member

      Looks as there is yet one difference in the behavior of Python and C implementations. In Python implementation either self.target.doctype or self.doctype are called. But in C implementation both are called.

    8. vadmium commented on Mar 31, 2015

      @vadmium
      MemberAuthor

      The difference of calling XMLParser.doctype() between the implementations is another argument for removing it completely. With all these bugs, and no opposition that I know of, I think it should be okay to remove the deprecated doctype() method in 3.5.

      doctype-remove.v2.patch adds a tweaked version of the original test for no DeprecationWarning from the other patch.

    9. vadmium commented on Mar 31, 2015

      @vadmium
      MemberAuthor

      inherit-doctype.v2.patch inverts the logic in the C module. This patch may still be useful if people want to apply it to 3.4, or do not want to remove the deprecated method from 3.5.

    10. vadmium commented on Mar 31, 2015

      @vadmium
      MemberAuthor

      doctype-remove.v3.patch includes an entry on the What’s New page.

    11. berkerpeksag commented on Mar 31, 2015

      @berkerpeksag
      Member

      doctype-remove.v3.patch LGTM.

    12. Arfrever commented on Apr 1, 2015

      Arfrevermannequin
      Mannequin

      Also please apply inherit-doctype.v2.patch in 3.4 branch.

    13. serhiy-storchaka commented on May 16, 2015

      @serhiy-storchaka
      Member

      Updated to the tip. If Eli doesn't have objections, I'll commit it.

    14. elibendersky commented on May 16, 2015

      elibenderskymannequin
      Mannequin

      I don't know how important this is to really warrant removal. Removal means potentially breaking working code when trying to run it with Python 3.5, and my impression was that the core devs are somewhat alergic to this, at least while the transition to Python 3 is ongoing.

      Unless it has already been discussed (sorry if I missed it, haven't had much time for CPython recently, unfortunately), may be worth a quick check with python-dev

    15. serhiy-storchaka commented on May 18, 2015

      @serhiy-storchaka
      Member

      Then what to do with the discrepancy between Python and C implementations (msg238783)?

    16. elibendersky commented on May 18, 2015

      elibenderskymannequin
      Mannequin

      I'm not sure. This is why I'm proposing asking on python-dev

    17. serhiy-storchaka commented on Jun 9, 2015

      @serhiy-storchaka
      Member
    18. vadmium commented on Jun 12, 2015

      @vadmium
      MemberAuthor

      Ideally I guess the Python native behaviour is better: only call target.doctype() if available. It might allow you to easily implement doctype() in both the old and new versions of the API, without worrying about the API being called twice, and without experiencing any DeprecationWarning. But I do not have a real-world use case to demonstrate this, and I don’t think this decision should hold up committing inherit-doctype.v2.patch. They are separate bugs.

      BTW: Another difference between the implementations is that the C version accepts my <!DOCTYPE blaua> test case, but the Python version ignores it. It only works with a more elaborate case like <!DOCTYPE blaua SYSTEM "dtd">. I’m no expert, but I think my original XML should be allowed.

    19. serhiy-storchaka commented on Jun 26, 2015

      @serhiy-storchaka
      Member

      Here is a patch that also fixes other issues with doctype.

      1. Direct call of doctype() issues a warning.
      2. Parser's doctype() is not called if target's doctype() is called.
    20. vadmium commented on Jun 29, 2015

      @vadmium
      MemberAuthor

      inherit-doctype.v3.patch looks good to me

    21. python-dev commented on Jun 29, 2015

      python-devmannequin
      Mannequin

      New changeset 75571407dcd3 by Serhiy Storchaka in branch '3.4':
      Issue bpo-19176: Fixed doctype() related bugs in C implementation of ElementTree.
      https://hg.python.org/cpython/rev/75571407dcd3

      New changeset 6ae8842e9b60 by Serhiy Storchaka in branch '3.5':
      Issue bpo-19176: Fixed doctype() related bugs in C implementation of ElementTree.
      https://hg.python.org/cpython/rev/6ae8842e9b60

      New changeset d792dc240456 by Serhiy Storchaka in branch 'default':
      Issue bpo-19176: Fixed doctype() related bugs in C implementation of ElementTree.
      https://hg.python.org/cpython/rev/d792dc240456

    22. serhiy-storchaka commented on Jun 29, 2015

      @serhiy-storchaka
      Member

      In 2.7 XMLParser is a function, not a class, and therefore can't be subclassed.

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

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions