diff --git a/docs/HISTORY.txt b/docs/HISTORY.txt index 1875070..e4c05c0 100644 --- a/docs/HISTORY.txt +++ b/docs/HISTORY.txt @@ -3,6 +3,9 @@ Changelog 3.0.dev0 - (unreleased) ----------------------- +* Change: Added handler for SAXParseException and wrote a test in order to + cover this exception + [petchesi-iulian refs #88573] 2.9 - (2017-09-29) ------------------ diff --git a/sparql-client/tests/invalid-result.srx b/sparql-client/tests/invalid-result.srx new file mode 100644 index 0000000..e93e4b8 --- /dev/null +++ b/sparql-client/tests/invalid-result.srx @@ -0,0 +1,101 @@ + + + + + + + + + + + + + + + + + + + + + + + barbuanc + + + http://www.eea.europa.eu/portal_types/Assessment#Assessment + + + 2011 2.8.1 + + + IND-305-en + + + ENER025 + + + http://www.eea.europa.eu/data-and-maps/indicators/energy-efficiency-and-energy-consumption-6/assessment + + + 2015-09-04T19:00:02Z + + + Energy efficiency and energy consumption in industry + + + 2012-04-11T15:18:19Z + + + Over the period 1990-2009, in EU-27 countries, energy efficiency in industry has improved by 30% at an annual average rate of 1.8% per year, with large differences among countries. Energy efficiency improvement has been realized in all industrial branches except textile. Over the period 2005-2009 energy efficiency improved by 1.5%/year with an important deterioration in 2009 due to the economic crisis. + + + en + + + 2012-04-11T15:28:20Z + + + + + vedludia + + + http://www.eea.europa.eu/portal_types/Assessment#Assessment + + + 2016 1.1.2 + + + IND-35-en + + + CSI035, TERM012 + + + http://www.eea.europa.eu/data-and-maps/indicators/passenger-transport-demand-version-2/assessment-6 + + + 2017-08-23T10:21:54Z + + + Passenger transport demand + + + 2016-09-28T10:02:20Z + + + + Passenger transport demand in the EU-28 increased by 1.8 % between 2013 and 2014, after an overall downward trend since its peak in 2009. Car passenger travel remains the dominant transport mode accounting for well over 70 % of total transport. Air transport demand grew by 4 % in 2014 and has a modal share of 9 % (the same as before the economic recession). Rail passenger travel is stable, accounting for 6.5 % of transport demand in 2014. + Land- based  passenger transport demand also grew in the other EEA member countries. Growth in 2014 compared with the previous year was 5.1 % in Iceland , 3.2 % in Norway,  3.1 % in Turkey and 1.8 % in Switzerland . + + + + en + + + 2016-12-01T10:03:43Z + + + + \ No newline at end of file diff --git a/sparql-client/tests/testparser.py b/sparql-client/tests/testparser.py index 2e5e616..49860cc 100644 --- a/sparql-client/tests/testparser.py +++ b/sparql-client/tests/testparser.py @@ -4,6 +4,8 @@ import unittest import sparql import os.path +from mock import patch +from xml.dom import pulldom def _open_datafile(name): return open(os.path.join(os.path.dirname(__file__), name)) @@ -104,6 +106,19 @@ def test_big_text(self): self.assertEqual("http://example.com/", row0[1].value) self.assertEqual("bnode.id", row0[2].value) + def side_effect_fetchhead(): + fp = _open_datafile("invalid-result.srx") + return pulldom.parse(fp) + + @patch('sparql._ResultsParser._fetchhead', side_effect=side_effect_fetchhead) + def test_invalid_fetchone(self, mocked_element): + """ Simple query with invalid characters """ + resultfp = _open_datafile("invalid-result.srx") + result = sparql._ResultsParser(resultfp) + setattr(result, 'events', result._fetchhead()) + for row in result.fetchone(): + print row + if __name__ == '__main__': unittest.main() diff --git a/sparql.py b/sparql.py index 6240952..2e4b8a3 100755 --- a/sparql.py +++ b/sparql.py @@ -51,6 +51,7 @@ from string import replace from urllib import urlencode from xml.dom import pulldom +from xml.sax import SAXParseException import compiler import copy import decimal @@ -608,31 +609,37 @@ def fetchone(self): """ idx = -1 - for (event, node) in self.events: - if event == pulldom.START_ELEMENT: - if node.tagName == 'result': - self._vals = [None] * len(self.variables) - elif node.tagName == 'binding': - idx = self.variables.index(node.attributes['name'].value) - elif node.tagName == 'uri': - self.events.expandNode(node) - data = ''.join(t.data for t in node.childNodes) - self._vals[idx] = IRI(data) - elif node.tagName == 'literal': - self.events.expandNode(node) - data = ''.join(t.data for t in node.childNodes) - lang = node.getAttribute('xml:lang') or None - datatype = Datatype(node.getAttribute('datatype')) or None - self._vals[idx] = Literal(data, datatype, lang) - elif node.tagName == 'bnode': - self.events.expandNode(node) - data = ''.join(t.data for t in node.childNodes) - self._vals[idx] = BlankNode(data) - - elif event == pulldom.END_ELEMENT: - if node.tagName == 'result': - #print "rtn:", len(self._vals), self._vals - yield tuple(self._vals) + try: + for (event, node) in self.events: + if event == pulldom.START_ELEMENT: + if node.tagName == 'result': + self._vals = [None] * len(self.variables) + elif node.tagName == 'binding': + idx = self.variables.index(node.attributes['name'].value) + elif node.tagName == 'uri': + self.events.expandNode(node) + data = ''.join(t.data for t in node.childNodes) + self._vals[idx] = IRI(data) + elif node.tagName == 'literal': + self.events.expandNode(node) + data = ''.join(t.data for t in node.childNodes) + lang = node.getAttribute('xml:lang') or None + datatype = Datatype(node.getAttribute('datatype')) or None + self._vals[idx] = Literal(data, datatype, lang) + elif node.tagName == 'bnode': + self.events.expandNode(node) + data = ''.join(t.data for t in node.childNodes) + self._vals[idx] = BlankNode(data) + elif event == pulldom.END_ELEMENT: + if node.tagName == 'result': + #print "rtn:", len(self._vals), self._vals + yield tuple(self._vals) + except SAXParseException, e: + import sys + + faultString = 'The data is ' + e.message + print >>sys.stderr, faultString + yield tuple() def fetchall(self): """ Loop through the result to build up a list of all rows.