[pypy-svn] r57703 - in pypy/dist/pypy/lib: . app_test

fijal at codespeak.net fijal at codespeak.net
Sun Aug 31 19:07:35 CEST 2008


Author: fijal
Date: Sun Aug 31 19:07:32 2008
New Revision: 57703

Modified:
   pypy/dist/pypy/lib/app_test/test_pyexpat.py
   pypy/dist/pypy/lib/pyexpat.py
Log:
A test and a fix for error reporting with parser error


Modified: pypy/dist/pypy/lib/app_test/test_pyexpat.py
==============================================================================
--- pypy/dist/pypy/lib/app_test/test_pyexpat.py	(original)
+++ pypy/dist/pypy/lib/app_test/test_pyexpat.py	Sun Aug 31 19:07:32 2008
@@ -646,3 +646,16 @@
 
     def test_segfault(self):
         py.test.raises(TypeError, expat.ParserCreate, 1234123123)
+
+def test_invalid_data():
+    parser = expat.ParserCreate()
+    parser.Parse('invalid.xml', 0)
+    try:
+        parser.Parse("", 1)
+    except expat.ExpatError, e:
+        assert e.code == 2 # XXX is this reliable?
+        assert e.lineno == 1
+        assert e.message.startswith('syntax error')
+    else:
+        py.test.fail("Did not raise")
+

Modified: pypy/dist/pypy/lib/pyexpat.py
==============================================================================
--- pypy/dist/pypy/lib/pyexpat.py	(original)
+++ pypy/dist/pypy/lib/pyexpat.py	Sun Aug 31 19:07:32 2008
@@ -148,6 +148,7 @@
         self.buffer_size = 8192
         self.character_data_handler = None
         self.intern = {}
+        self.__exc_info = None
 
     def _flush_character_buffer(self):
         if not self.buffer:
@@ -189,13 +190,19 @@
         e.lineno = lineno
         err = XML_ErrorString(code)[:200]
         e.s = "%s: line: %d, column: %d" % (err, lineno, colno)
+        e.message = e.s
         self._error = e
 
     def Parse(self, data, is_final=0):
         res = XML_Parse(self.itself, data, len(data), is_final)
         if res == 0:
             self._set_error(XML_GetErrorCode(self.itself))
-            raise self.__exc_info[0], self.__exc_info[1], self.__exc_info[2]
+            if self.__exc_info:
+                exc_info = self.__exc_info
+                self.__exc_info = None
+                raise exc_info[0], exc_info[1], exc_info[2]
+            else:
+                raise self._error
         self._flush_character_buffer()
         return res
 



More information about the Pypy-commit mailing list