[pypy-commit] pypy py3k: pyexpat: add mappings for error messages.

amauryfa noreply at buildbot.pypy.org
Sat Oct 20 23:30:15 CEST 2012


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r58281:45e1d87468c3
Date: 2012-10-20 22:35 +0200
http://bitbucket.org/pypy/pypy/changeset/45e1d87468c3/

Log:	pyexpat: add mappings for error messages.

diff --git a/pypy/module/pyexpat/__init__.py b/pypy/module/pyexpat/__init__.py
--- a/pypy/module/pyexpat/__init__.py
+++ b/pypy/module/pyexpat/__init__.py
@@ -9,10 +9,24 @@
 
     def setup_after_space_initialization(self):
         from pypy.module.pyexpat import interp_pyexpat
+        space = self.space
+        # Three mappings for errors: the module contains errors
+        # message by symbol (errors.XML_ERROR_SYNTAX == 'syntax error'),
+        # codes is a dict mapping messages to numeric codes
+        # (errors.codes['syntax error'] == 2), and messages is a dict
+        # mapping numeric codes to messages (messages[2] == 'syntax error').
+        w_codes = space.newdict()
+        w_messages = space.newdict()
         for name in interp_pyexpat.xml_error_list:
-            self.space.setattr(self, self.space.wrap(name),
-                    interp_pyexpat.ErrorString(self.space,
-                    getattr(interp_pyexpat, name)))
+            w_name = space.wrap(name)
+            num = getattr(interp_pyexpat, name)
+            w_num = space.wrap(num)
+            w_message = interp_pyexpat.ErrorString(space, num)
+            space.setattr(self, w_name, w_message)
+            space.setitem(w_codes, w_message, w_num)
+            space.setitem(w_messages, w_num, w_message)
+        space.setattr(self, space.wrap("codes"), w_codes)
+        space.setattr(self, space.wrap("messages"), w_messages)
 
 class ModelModule(MixedModule):
     "Definition of pyexpat.model module."
diff --git a/pypy/module/pyexpat/interp_pyexpat.py b/pypy/module/pyexpat/interp_pyexpat.py
--- a/pypy/module/pyexpat/interp_pyexpat.py
+++ b/pypy/module/pyexpat/interp_pyexpat.py
@@ -788,7 +788,7 @@
         type_name = space.type(w_encoding).getname(space)
         raise OperationError(
             space.w_TypeError,
-            space.wrap('ParserCreate() argument 1 must be string or None,'
+            space.wrap('ParserCreate() argument 1 must be str or None,'
                        ' not %s' % (type_name,)))
 
     if space.is_none(w_namespace_separator):
@@ -808,7 +808,7 @@
         type_name = space.type(w_namespace_separator).getname(space)
         raise OperationError(
             space.w_TypeError,
-            space.wrap('ParserCreate() argument 2 must be string or None,'
+            space.wrap('ParserCreate() argument 2 must be str or None,'
                        ' not %s' % (type_name,)))
 
     # Explicitly passing None means no interning is desired.
diff --git a/pypy/module/pyexpat/test/test_parser.py b/pypy/module/pyexpat/test/test_parser.py
--- a/pypy/module/pyexpat/test/test_parser.py
+++ b/pypy/module/pyexpat/test/test_parser.py
@@ -148,3 +148,17 @@
     def test_model(self):
         import pyexpat
         assert isinstance(pyexpat.model.XML_CTYPE_EMPTY, int)
+
+    def test_codes(self):
+        from pyexpat import errors
+        # verify mapping of errors.codes and errors.messages
+        message = errors.messages[errors.codes[errors.XML_ERROR_SYNTAX]]
+        assert errors.XML_ERROR_SYNTAX == message
+
+    def test_expaterror(self):
+        import pyexpat
+        from pyexpat import errors
+        xml = '<'
+        parser = pyexpat.ParserCreate()
+        e = raises(pyexpat.ExpatError, parser.Parse, xml, True)
+        assert e.value.code == errors.codes[errors.XML_ERROR_UNCLOSED_TOKEN]


More information about the pypy-commit mailing list