[pypy-commit] pypy default: Add xml.parsers.expat.errors module.
hodgestar
noreply at buildbot.pypy.org
Sat Jul 2 17:18:46 CEST 2011
Author: Simon Cross <hodgestar at gmail.com>
Branch:
Changeset: r45269:2d43f3bebc8d
Date: 2011-07-02 17:20 +0200
http://bitbucket.org/pypy/pypy/changeset/2d43f3bebc8d/
Log: Add xml.parsers.expat.errors module.
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
@@ -2,6 +2,22 @@
from pypy.interpreter.mixedmodule import MixedModule
+class ErrorsModule(MixedModule):
+ "Definition of pyexpat.errors module."
+
+ appleveldefs = {
+ }
+
+ interpleveldefs = {
+ }
+
+ def setup_after_space_initialization(self):
+ from pypy.module.pyexpat import interp_pyexpat
+ 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)))
+
class Module(MixedModule):
"Python wrapper for Expat parser."
@@ -21,6 +37,10 @@
'version_info': 'interp_pyexpat.get_expat_version_info(space)',
}
+ submodules = {
+ 'errors': ErrorsModule,
+ }
+
for name in ['XML_PARAM_ENTITY_PARSING_NEVER',
'XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE',
'XML_PARAM_ENTITY_PARSING_ALWAYS']:
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
@@ -31,6 +31,48 @@
XML_Content_Ptr = lltype.Ptr(lltype.ForwardReference())
XML_Parser = rffi.COpaquePtr(typedef='XML_Parser')
+xml_error_list = [
+ "XML_ERROR_NO_MEMORY",
+ "XML_ERROR_SYNTAX",
+ "XML_ERROR_NO_ELEMENTS",
+ "XML_ERROR_INVALID_TOKEN",
+ "XML_ERROR_UNCLOSED_TOKEN",
+ "XML_ERROR_PARTIAL_CHAR",
+ "XML_ERROR_TAG_MISMATCH",
+ "XML_ERROR_DUPLICATE_ATTRIBUTE",
+ "XML_ERROR_JUNK_AFTER_DOC_ELEMENT",
+ "XML_ERROR_PARAM_ENTITY_REF",
+ "XML_ERROR_UNDEFINED_ENTITY",
+ "XML_ERROR_RECURSIVE_ENTITY_REF",
+ "XML_ERROR_ASYNC_ENTITY",
+ "XML_ERROR_BAD_CHAR_REF",
+ "XML_ERROR_BINARY_ENTITY_REF",
+ "XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF",
+ "XML_ERROR_MISPLACED_XML_PI",
+ "XML_ERROR_UNKNOWN_ENCODING",
+ "XML_ERROR_INCORRECT_ENCODING",
+ "XML_ERROR_UNCLOSED_CDATA_SECTION",
+ "XML_ERROR_EXTERNAL_ENTITY_HANDLING",
+ "XML_ERROR_NOT_STANDALONE",
+ "XML_ERROR_UNEXPECTED_STATE",
+ "XML_ERROR_ENTITY_DECLARED_IN_PE",
+ "XML_ERROR_FEATURE_REQUIRES_XML_DTD",
+ "XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING",
+ # Added in Expat 1.95.7.
+ "XML_ERROR_UNBOUND_PREFIX",
+ # Added in Expat 1.95.8.
+ "XML_ERROR_UNDECLARING_PREFIX",
+ "XML_ERROR_INCOMPLETE_PE",
+ "XML_ERROR_XML_DECL",
+ "XML_ERROR_TEXT_DECL",
+ "XML_ERROR_PUBLICID",
+ "XML_ERROR_SUSPENDED",
+ "XML_ERROR_NOT_SUSPENDED",
+ "XML_ERROR_ABORTED",
+ "XML_ERROR_FINISHED",
+ "XML_ERROR_SUSPEND_PE",
+ ]
+
class CConfigure:
_compilation_info_ = eci
XML_Content = rffi_platform.Struct('XML_Content', [
@@ -56,6 +98,9 @@
XML_FALSE = rffi_platform.ConstantInteger('XML_FALSE')
XML_TRUE = rffi_platform.ConstantInteger('XML_TRUE')
+ for name in xml_error_list:
+ locals()[name] = rffi_platform.ConstantInteger(name)
+
for k, v in rffi_platform.configure(CConfigure).items():
globals()[k] = v
@@ -298,7 +343,8 @@
XML_GetErrorCode = expat_external(
'XML_GetErrorCode', [XML_Parser], rffi.INT)
XML_ErrorString = expat_external(
- 'XML_ErrorString', [rffi.INT], rffi.CCHARP)
+ 'XML_ErrorString', [rffi.INT],
+ rffi.CCHARP)
XML_GetCurrentLineNumber = expat_external(
'XML_GetCurrentLineNumber', [XML_Parser], rffi.INT)
XML_GetErrorLineNumber = XML_GetCurrentLineNumber
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
@@ -38,7 +38,7 @@
parser = pyexpat.ParserCreate()
raises(pyexpat.ExpatError, "parser.Parse(xml, True)")
- def test_encoding(self):
+ def test_encoding_argument(self):
import pyexpat
for encoding_arg in (None, 'utf-8', 'iso-8859-1'):
for namespace_arg in (None, '{'):
@@ -68,7 +68,7 @@
assert p.buffer_size == 150
raises(TypeError, setattr, p, 'buffer_size', sys.maxint + 1)
- def test_encoding(self):
+ def test_encoding_xml(self):
# use one of the few encodings built-in in expat
xml = "<?xml version='1.0' encoding='iso-8859-1'?><s>caf\xe9</s>"
import pyexpat
@@ -120,3 +120,14 @@
return True
p.ExternalEntityRefHandler = handler
p.Parse(xml)
+
+ def test_errors(self):
+ import types
+ import pyexpat
+ assert isinstance(pyexpat.errors, types.ModuleType)
+ # check a few random errors
+ assert pyexpat.errors.XML_ERROR_SYNTAX == 'syntax error'
+ assert (pyexpat.errors.XML_ERROR_INCORRECT_ENCODING ==
+ 'encoding specified in XML declaration is incorrect')
+ assert (pyexpat.errors.XML_ERROR_XML_DECL ==
+ 'XML declaration not well-formed')
More information about the pypy-commit
mailing list