# HG changeset patch # Parent 2ffaac4c8e531de022390e120d5ab35108ed7697 Issue 19176: Inheriting doctype() method is not deprecated usage diff -r 2ffaac4c8e53 Lib/test/test_xml_etree.py --- a/Lib/test/test_xml_etree.py Tue Dec 16 19:43:46 2014 +0200 +++ b/Lib/test/test_xml_etree.py Tue Mar 31 06:06:25 2015 +0000 @@ -12,6 +12,7 @@ import sys import types import unittest +import warnings import weakref from itertools import product @@ -2132,6 +2133,16 @@ ('html', '-//W3C//DTD XHTML 1.0 Transitional//EN', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd')) + def test_inherited_doctype(self): + '''Ensure that ordinary usage is not deprecated (Issue 19176)''' + with warnings.catch_warnings(): + warnings.simplefilter('error', DeprecationWarning) + class MyParserWithoutDoctype(ET.XMLParser): + pass + parser = MyParserWithoutDoctype() + parser.feed(self.sample2) + parser.close() + def test_parse_string(self): parser = ET.XMLParser(target=ET.TreeBuilder()) parser.feed(self.sample3) diff -r 2ffaac4c8e53 Modules/_elementtree.c --- a/Modules/_elementtree.c Tue Dec 16 19:43:46 2014 +0200 +++ b/Modules/_elementtree.c Tue Mar 31 06:06:25 2015 +0000 @@ -2741,7 +2741,7 @@ } XMLParserObject; -#define XMLParser_CheckExact(op) (Py_TYPE(op) == &XMLParser_Type) +static PyObject* xmlparser_doctype(XMLParserObject* self, PyObject* args); /* helpers */ @@ -3142,12 +3142,15 @@ } /* Now see if the parser itself has a doctype method. If yes and it's - * a subclass, call it but warn about deprecation. If it's not a subclass - * (i.e. vanilla XMLParser), do nothing. + * a custom method, call it but warn about deprecation. If it's only + * the vanilla XMLParser method, do nothing. */ parser_doctype = PyObject_GetAttrString(self_pyobj, "doctype"); if (parser_doctype) { - if (!XMLParser_CheckExact(self_pyobj)) { + if (!(PyCFunction_Check(parser_doctype) && + PyCFunction_GET_SELF(parser_doctype) == self_pyobj && + PyCFunction_GET_FUNCTION(parser_doctype) == + (PyCFunction) xmlparser_doctype)) { if (PyErr_WarnEx(PyExc_DeprecationWarning, "This method of XMLParser is deprecated. Define" " doctype() method on the TreeBuilder target.",