import cgi
import copy
+import functools
import io
import pickle
import StringIO
"""
+def checkwarnings(*filters):
+ def decorator(test):
+ def newtest(*args, **kwargs):
+ with support.check_warnings(*filters):
+ test(*args, **kwargs)
+ functools.update_wrapper(newtest, test)
+ return newtest
+ return decorator
+
+
class ModuleTest(unittest.TestCase):
# TODO: this should be removed once we get rid of the global module vars
])
+ # 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()
# Test custom builder.
class EchoTarget:
+ def start(self, tag, attrib):
+ pass
+ def end(self, tag):
+ pass
+ def data(self, text):
+ pass
def close(self):
return ET.Element("element") # simulate root
- parser = ET.XMLParser(EchoTarget())
+ parser = ET.XMLParser(target=EchoTarget())
parser.feed("<element>some text</element>")
self.assertEqual(parser.close().tag, 'element')
e = ET.XML(SAMPLE_XML)
self.assertEqual(ET.ElementTree(e).find('tag').tag, 'tag')
self.assertEqual(ET.ElementTree(e).find('./tag').tag, 'tag')
- self.assertEqual(ET.ElementTree(e).find('/tag').tag, 'tag')
+ # this produces a warning
+ 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 support.check_warnings((msg, FutureWarning)):
+ self.assertEqual(ET.ElementTree(e).find('/tag').tag, 'tag')
e[2] = ET.XML(SAMPLE_SECTION)
self.assertEqual(ET.ElementTree(e).find('section/tag').tag, 'tag')
self.assertIsNone(ET.ElementTree(e).find('tog'))
self.assertEqual(ET.ElementTree(e).findtext('tog/foo', 'default'),
'default')
self.assertEqual(ET.ElementTree(e).findtext('./tag'), 'text')
- self.assertEqual(ET.ElementTree(e).findtext('/tag'), 'text')
+ with support.check_warnings((msg, FutureWarning)):
+ self.assertEqual(ET.ElementTree(e).findtext('/tag'), 'text')
self.assertEqual(ET.ElementTree(e).findtext('section/tag'), 'subtext')
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'] * 2)
+ with support.check_warnings((msg, FutureWarning)):
+ it = ET.ElementTree(e).findall('/tag')
+ self.assertEqual(summarize_list(it), ['tag'] * 2)
class ElementIterTest(unittest.TestCase):
self.assertEqual(self._ilist(doc, '*'), all_tags)
def test_getiterator(self):
+ # Element.getiterator() is deprecated.
+ if sys.py3kwarning or ET is pyET:
+ with support.check_warnings(("This method will be removed in future versions. "
+ "Use .+ instead.", PendingDeprecationWarning)):
+ self._test_getiterator()
+ else:
+ self._test_getiterator()
+
+ def _test_getiterator(self):
doc = ET.XML('''
<document>
<house>
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 support.check_py3k_warnings((r'.*\bhtml\b', DeprecationWarning)):
+ 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())
# --------------------------------------------------------------------
-class CleanContext(object):
- """Provide default namespace mapping and path cache."""
- checkwarnings = None
-
- def __init__(self, quiet=False):
- deprecations = (
- ("This method of XMLParser is deprecated. Define doctype\(\) "
- "method on the TreeBuilder target.", DeprecationWarning),
- # 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 = pyET._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.
NoAcceleratorTest,
])
+ # Provide default namespace mapping and path cache.
+ from xml.etree import ElementPath
+ nsmap = pyET._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 = None
int i;
PyObject* list;
- /* FIXME: report as deprecated? */
+ if (PyErr_WarnPy3k("This method will be removed in future versions. "
+ "Use 'list(elem)' or iteration over elem instead.",
+ 1) < 0) {
+ return NULL;
+ }
if (!PyArg_ParseTuple(args, ":getchildren"))
return NULL;
}
static PyObject*
-element_iter(ElementObject* self, PyObject* args)
+element_iter_impl(ElementObject* self, PyObject* tag)
{
+ PyObject* args;
PyObject* result;
-
- PyObject* tag = Py_None;
- if (!PyArg_ParseTuple(args, "|O:iter", &tag))
- return NULL;
if (!elementtree_iter_obj) {
PyErr_SetString(
return result;
}
+static PyObject*
+element_iter(ElementObject* self, PyObject* args)
+{
+ PyObject* tag = Py_None;
+ if (!PyArg_ParseTuple(args, "|O:iter", &tag))
+ return NULL;
+
+ return element_iter_impl(self, tag);
+}
+
+static PyObject*
+element_getiterator(ElementObject* self, PyObject* args)
+{
+ PyObject* tag = Py_None;
+ if (!PyArg_ParseTuple(args, "|O:getiterator", &tag))
+ return NULL;
+
+ /* Change for a DeprecationWarning in 1.4 */
+ if (Py_Py3kWarningFlag &&
+ 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 element_iter_impl(self, tag);
+}
+
static PyObject*
element_itertext(ElementObject* self, PyObject* args)
{"itertext", (PyCFunction) element_itertext, METH_VARARGS},
{"iterfind", (PyCFunction) element_iterfind, METH_VARARGS},
- {"getiterator", (PyCFunction) element_iter, METH_VARARGS},
+ {"getiterator", (PyCFunction) element_getiterator, METH_VARARGS},
{"getchildren", (PyCFunction) element_getchildren, METH_VARARGS},
{"items", (PyCFunction) element_items, METH_VARARGS},