-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
DeprecationWarning for doctype() method when subclassing _elementtree.XMLParser #63375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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. |
Thanks for the report, Martin. I'll take a look once I get some time |
Actually I am still seeing this in 3.4.0. Looks like the doctype() method was not removed after all. |
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. |
Here is another patch that removes the method instead, as suggested in the review |
doctype-remove.patch is acceptable for both 3.4 and 3.5 or only 3.5? |
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. |
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. |
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. |
doctype-remove.v3.patch includes an entry on the What’s New page. |
doctype-remove.v3.patch LGTM. |
Also please apply inherit-doctype.v2.patch in 3.4 branch. |
Updated to the tip. If Eli doesn't have objections, I'll commit it. |
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 |
Then what to do with the discrepancy between Python and C implementations (msg238783)? |
I'm not sure. This is why I'm proposing asking on python-dev |
Started topic on Python-Dev: http://comments.gmane.org/gmane.comp.python.devel/153655 . |
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. |
Here is a patch that also fixes other issues with doctype.
|
inherit-doctype.v3.patch looks good to me |
New changeset 75571407dcd3 by Serhiy Storchaka in branch '3.4': New changeset 6ae8842e9b60 by Serhiy Storchaka in branch '3.5': New changeset d792dc240456 by Serhiy Storchaka in branch 'default': |
In 2.7 XMLParser is a function, not a class, and therefore can't be subclassed. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: