diff -r f8e24a0a1124 Lib/test/test_xml_etree.py --- a/Lib/test/test_xml_etree.py Sat Jan 07 00:07:45 2017 +0100 +++ b/Lib/test/test_xml_etree.py Sun Jan 08 12:56:12 2017 +0200 @@ -6,6 +6,7 @@ # monkey-patched when running the "test_xml_etree_c" test suite. import copy +import functools import html import io import operator @@ -90,6 +91,16 @@ ENTITY_XML = """\ """ +def checkwarnings(*filters, quiet=False): + def decorator(test): + def newtest(*args, **kwargs): + with support.check_warnings(*filters, quiet=quiet): + test(*args, **kwargs) + functools.update_wrapper(newtest, test) + return newtest + return decorator + + class ModuleTest(unittest.TestCase): def test_sanity(self): # Import sanity. @@ -690,6 +701,10 @@ class ElementTreeTest(unittest.TestCase) ]) + # Element.getchildren() and ElementTree.getiterator() are deprecated. + @checkwarnings(("This method will be removed in future versions. " + "Use .+ instead.", + (DeprecationWarning, PendingDeprecationWarning))) def test_getchildren(self): # Test Element.getchildren() @@ -1558,7 +1573,7 @@ class BugsTest(unittest.TestCase): class EchoTarget: def close(self): return ET.Element("element") # simulate root - parser = ET.XMLParser(EchoTarget()) + parser = ET.XMLParser(target=EchoTarget()) parser.feed("some text") self.assertEqual(parser.close().tag, 'element') @@ -2113,8 +2128,12 @@ class ElementFindTest(unittest.TestCase) self.assertEqual(summarize_list(ET.ElementTree(e).findall('tag')), ['tag'] * 2) # this produces a warning - self.assertEqual(summarize_list(ET.ElementTree(e).findall('//tag')), - ['tag'] * 3) + msg = ("This search is broken in 1.3 and earlier, and will be fixed " + "in a future version. If you rely on the current behaviour, " + "change it to '.+'") + with self.assertWarnsRegex(FutureWarning, msg): + it = ET.ElementTree(e).findall('//tag') + self.assertEqual(summarize_list(it), ['tag'] * 3) class ElementIterTest(unittest.TestCase): @@ -2199,6 +2218,9 @@ class ElementIterTest(unittest.TestCase) self.assertEqual(self._ilist(doc), all_tags) self.assertEqual(self._ilist(doc, '*'), all_tags) + # Element.getiterator() is deprecated. + @checkwarnings(("This method will be removed in future versions. " + "Use .+ instead.", PendingDeprecationWarning)) def test_getiterator(self): doc = ET.XML(''' @@ -2381,13 +2403,13 @@ class XMLParserTest(unittest.TestCase): def test_constructor_args(self): # Positional args. The first (html) is not supported, but should be # nevertheless correctly accepted. - parser = ET.XMLParser(None, ET.TreeBuilder(), 'utf-8') + with self.assertWarnsRegex(DeprecationWarning, r'\bhtml\b'): + parser = ET.XMLParser(None, ET.TreeBuilder(), 'utf-8') parser.feed(self.sample1) self._check_sample_element(parser.close()) # Now as keyword args. parser2 = ET.XMLParser(encoding='utf-8', - html=[{}], target=ET.TreeBuilder()) parser2.feed(self.sample1) self._check_sample_element(parser2.close()) @@ -2904,46 +2926,6 @@ class NoAcceleratorTest(unittest.TestCas # -------------------------------------------------------------------- -class CleanContext(object): - """Provide default namespace mapping and path cache.""" - checkwarnings = None - - def __init__(self, quiet=False): - if sys.flags.optimize >= 2: - # under -OO, doctests cannot be run and therefore not all warnings - # will be emitted - quiet = True - deprecations = ( - # Search behaviour is broken if search path starts with "/". - ("This search is broken in 1.3 and earlier, and will be fixed " - "in a future version. If you rely on the current behaviour, " - "change it to '.+'", FutureWarning), - # Element.getchildren() and Element.getiterator() are deprecated. - ("This method will be removed in future versions. " - "Use .+ instead.", DeprecationWarning), - ("This method will be removed in future versions. " - "Use .+ instead.", PendingDeprecationWarning)) - self.checkwarnings = support.check_warnings(*deprecations, quiet=quiet) - - def __enter__(self): - from xml.etree import ElementPath - self._nsmap = ET.register_namespace._namespace_map - # Copy the default namespace mapping - self._nsmap_copy = self._nsmap.copy() - # Copy the path cache (should be empty) - self._path_cache = ElementPath._cache - ElementPath._cache = self._path_cache.copy() - self.checkwarnings.__enter__() - - def __exit__(self, *args): - from xml.etree import ElementPath - # Restore mapping and path cache - self._nsmap.clear() - self._nsmap.update(self._nsmap_copy) - ElementPath._cache = self._path_cache - self.checkwarnings.__exit__(*args) - - def test_main(module=None): # When invoked without a module, runs the Python ET tests by loading pyET. # Otherwise, uses the given module as the ET. @@ -2983,11 +2965,22 @@ def test_main(module=None): NoAcceleratorTest, ]) + # Provide default namespace mapping and path cache. + from xml.etree import ElementPath + nsmap = ET.register_namespace._namespace_map + # Copy the default namespace mapping + nsmap_copy = nsmap.copy() + # Copy the path cache (should be empty) + path_cache = ElementPath._cache + ElementPath._cache = path_cache.copy() try: - # XXX the C module should give the same warnings as the Python module - with CleanContext(quiet=(pyET is not ET)): - support.run_unittest(*test_classes) + support.run_unittest(*test_classes) finally: + from xml.etree import ElementPath + # Restore mapping and path cache + nsmap.clear() + nsmap.update(nsmap_copy) + ElementPath._cache = path_cache # don't interfere with subsequent tests ET = pyET = None diff -r f8e24a0a1124 Lib/test/test_xml_etree_c.py --- a/Lib/test/test_xml_etree_c.py Sat Jan 07 00:07:45 2017 +0100 +++ b/Lib/test/test_xml_etree_c.py Sun Jan 08 12:56:12 2017 +0200 @@ -1,14 +1,16 @@ # xml.etree test for cElementTree import struct from test import support -from test.support import import_fresh_module +from test.support import import_fresh_module, check_warnings import types import unittest cET = import_fresh_module('xml.etree.ElementTree', fresh=['_elementtree']) -cET_alias = import_fresh_module('xml.etree.cElementTree', - fresh=['_elementtree', 'xml.etree']) + +with check_warnings(quiet=True): + cET_alias = import_fresh_module('xml.etree.cElementTree', + fresh=['_elementtree', 'xml.etree']) @unittest.skipUnless(cET, 'requires _elementtree') diff -r f8e24a0a1124 Lib/xml/etree/ElementTree.py --- a/Lib/xml/etree/ElementTree.py Sat Jan 07 00:07:45 2017 +0100 +++ b/Lib/xml/etree/ElementTree.py Sun Jan 08 12:56:12 2017 +0200 @@ -1430,6 +1430,7 @@ class TreeBuilder: self._tail = 1 return self._last +_sentinel = ['sentinel'] # also see ElementTree and TreeBuilder class XMLParser: @@ -1443,7 +1444,11 @@ class XMLParser: """ - def __init__(self, html=0, target=None, encoding=None): + def __init__(self, html=_sentinel, target=None, encoding=None): + if html is not _sentinel: + warnings.warn( + "The html argument of XMLParser() is deprecated", + DeprecationWarning, stacklevel=2) try: from xml.parsers import expat except ImportError: diff -r f8e24a0a1124 Lib/xml/etree/cElementTree.py --- a/Lib/xml/etree/cElementTree.py Sat Jan 07 00:07:45 2017 +0100 +++ b/Lib/xml/etree/cElementTree.py Sun Jan 08 12:56:12 2017 +0200 @@ -1,3 +1,7 @@ # Deprecated alias for xml.etree.ElementTree +from warnings import warn as _warn +_warn("The xml.etree.cElementTree module is deprecated", + DeprecationWarning, stacklevel=2) + from xml.etree.ElementTree import * diff -r f8e24a0a1124 Modules/_elementtree.c --- a/Modules/_elementtree.c Sat Jan 07 00:07:45 2017 +0100 +++ b/Modules/_elementtree.c Sun Jan 08 12:56:12 2017 +0200 @@ -1363,7 +1363,12 @@ static PyObject * Py_ssize_t i; PyObject* list; - /* FIXME: report as deprecated? */ + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "This method will be removed in future versions. " + "Use 'list(elem)' or iteration over elem instead.", + 1) < 0) { + return NULL; + } if (!self->extra) return PyList_New(0); @@ -1413,6 +1418,28 @@ static PyObject * /*[clinic input] +_elementtree.Element.getiterator + + tag: object = None + +[clinic start generated code]*/ + +static PyObject * +_elementtree_Element_getiterator_impl(ElementObject *self, PyObject *tag) +/*[clinic end generated code: output=cb69ff4a3742dfa1 input=500da1a03f7b9e28]*/ +{ + /* Change for a DeprecationWarning in 1.4 */ + if (PyErr_WarnEx(PyExc_PendingDeprecationWarning, + "This method will be removed in future versions. " + "Use 'tree.iter()' or 'list(tree.iter())' instead.", + 1) < 0) { + return NULL; + } + return _elementtree_Element_iter_impl(self, tag); +} + + +/*[clinic input] _elementtree.Element.itertext [clinic start generated code]*/ @@ -3232,6 +3259,14 @@ static int PyObject *target, const char *encoding) /*[clinic end generated code: output=d6a16c63dda54441 input=155bc5695baafffd]*/ { + if (html != NULL) { + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "The html argument of XMLParser() is deprecated", + 1) < 0) { + return -1; + } + } + self->entity = PyDict_New(); if (!self->entity) return -1; @@ -3704,7 +3739,7 @@ static PyMethodDef element_methods[] = { _ELEMENTTREE_ELEMENT_ITERTEXT_METHODDEF _ELEMENTTREE_ELEMENT_ITERFIND_METHODDEF - {"getiterator", (PyCFunction)_elementtree_Element_iter, METH_FASTCALL, _elementtree_Element_iter__doc__}, + _ELEMENTTREE_ELEMENT_GETITERATOR_METHODDEF _ELEMENTTREE_ELEMENT_GETCHILDREN_METHODDEF _ELEMENTTREE_ELEMENT_ITEMS_METHODDEF diff -r f8e24a0a1124 Modules/clinic/_elementtree.c.h --- a/Modules/clinic/_elementtree.c.h Sat Jan 07 00:07:45 2017 +0100 +++ b/Modules/clinic/_elementtree.c.h Sun Jan 08 12:56:12 2017 +0200 @@ -333,6 +333,35 @@ exit: return return_value; } +PyDoc_STRVAR(_elementtree_Element_getiterator__doc__, +"getiterator($self, /, tag=None)\n" +"--\n" +"\n"); + +#define _ELEMENTTREE_ELEMENT_GETITERATOR_METHODDEF \ + {"getiterator", (PyCFunction)_elementtree_Element_getiterator, METH_FASTCALL, _elementtree_Element_getiterator__doc__}, + +static PyObject * +_elementtree_Element_getiterator_impl(ElementObject *self, PyObject *tag); + +static PyObject * +_elementtree_Element_getiterator(ElementObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + static const char * const _keywords[] = {"tag", NULL}; + static _PyArg_Parser _parser = {"|O:getiterator", _keywords, 0}; + PyObject *tag = Py_None; + + if (!_PyArg_ParseStack(args, nargs, kwnames, &_parser, + &tag)) { + goto exit; + } + return_value = _elementtree_Element_getiterator_impl(self, tag); + +exit: + return return_value; +} + PyDoc_STRVAR(_elementtree_Element_itertext__doc__, "itertext($self, /)\n" "--\n" @@ -702,4 +731,4 @@ static PyObject * exit: return return_value; } -/*[clinic end generated code: output=b4a571a98ced3163 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=f1537b43b2615213 input=a9049054013a1b77]*/