diff -r 75990a013d4d Lib/test/test_xml_etree.py --- a/Lib/test/test_xml_etree.py Tue Feb 21 19:54:26 2012 +0100 +++ b/Lib/test/test_xml_etree.py Wed Feb 22 10:49:27 2012 +1300 @@ -1903,6 +1903,23 @@ def test_correct_import_pyET(self): self.assertEqual(pyET.Element.__module__, 'xml.etree.ElementTree') +class TestElementSourceline(unittest.TestCase): + """Ensure that element are given sourceline attribute. + """ + def test_sourceline_from_string(self): + "Simple example from string source" + tree = pyET.XML(SAMPLE_XML) + section_element = tree.find("section") + self.assertEqual(section_element.sourceline, 4) + + def test_sourceline_from_file(self): + "Find line numbers of all elements" + tree = pyET.ElementTree() + tree.parse(SIMPLE_XMLFILE) + element_lines = [(e.tag, e.sourceline) for e in tree.iter()] + self.assertEqual(element_lines, + [('root', 2), ('element', 3), ('element', 4), ('empty-element', 5)]) + def test_main(module=pyET): from test import test_xml_etree @@ -1911,6 +1928,7 @@ test_xml_etree.ET = module support.run_unittest(TestAcceleratorNotImported) + support.run_unittest(TestElementSourceline) # XXX the C module should give the same warnings as the Python module with CleanContext(quiet=(module is not pyET)): diff -r 75990a013d4d Lib/xml/etree/ElementTree.py --- a/Lib/xml/etree/ElementTree.py Tue Feb 21 19:54:26 2012 +0100 +++ b/Lib/xml/etree/ElementTree.py Wed Feb 22 10:49:27 2012 +1300 @@ -1571,7 +1571,10 @@ if attrib_in: for i in range(0, len(attrib_in), 2): attrib[fixname(attrib_in[i])] = attrib_in[i+1] - return self.target.start(tag, attrib) + element = self.target.start(tag, attrib) + if element is not None: + element.sourceline = self._parser.CurrentLineNumber + return element def _data(self, text): return self.target.data(text)