ImportError, and a missing driver.
Passed test_expat_nsattrs_empty
Passed test_expat_nsattrs_wattr
Passed test_filter_basic
+Passed test_make_parser
Passed test_nsattrs_empty
Passed test_nsattrs_wattr
Passed test_xmlgen_basic
Passed test_xmlgen_ignorable
Passed test_xmlgen_ns
Passed test_xmlgen_pi
-23 tests, 0 failures
+24 tests, 0 failures
# regression test for SAX 2.0
# $Id$
+from xml.sax import make_parser, ContentHandler
+try:
+ make_parser()
+except xml.sax.SAXReaderNotAvailable:
+ # don't try to test this module if we cannot create a parser
+ raise ImportError("no XML parsers available")
from xml.sax.saxutils import XMLGenerator, escape, XMLFilterBase
from xml.sax.expatreader import create_parser
from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl
-from xml.sax.handler import ContentHandler
from cStringIO import StringIO
from test_support import verbose, TestFailed, findfile
def test_escape_extra():
return escape("Hei på deg", {"å" : "å"}) == "Hei på deg"
+def test_make_parser():
+ try:
+ # Creating a parser should succeed - it should fall back
+ # to the expatreader
+ p = make_parser(['xml.parsers.no_such_parser'])
+ except:
+ return 0
+ else:
+ return p
+
+
# ===== XMLGenerator
start = '<?xml version="1.0" encoding="iso-8859-1"?>\n'
from xmlreader import InputSource
from handler import ContentHandler, ErrorHandler
from _exceptions import SAXException, SAXNotRecognizedException, \
- SAXParseException, SAXNotSupportedException
+ SAXParseException, SAXNotSupportedException, \
+ SAXReaderNotAvailable
def parse(source, handler, errorHandler=ErrorHandler()):
try:
return _create_parser(parser_name)
except ImportError,e:
+ import sys
+ if sys.modules.has_key(parser_name):
+ # The parser module was found, but importing it
+ # failed unexpectedly, pass this exception through
+ raise
+ except SAXReaderNotAvailable:
+ # The parser module detected that it won't work properly,
+ # so try the next one
pass
- raise SAXException("No parsers found", None)
+ raise SAXReaderNotAvailable("No parsers found", None)
# --- Internal utility methods used by make_parser
perform is requested (specifically setting a state or value). SAX
applications and extensions may use this class for similar
purposes."""
+
+# ===== SAXNOTSUPPORTEDEXCEPTION =====
+
+class SAXReaderNotAvailable(SAXNotSupportedException):
+ """Exception class for a missing driver.
+
+ An XMLReader module (driver) should raise this exception when it
+ is first imported, e.g. when a support module cannot be imported.
+ It also may be raised during parsing, e.g. if executing an external
+ program is not permitted."""
version = "0.20"
from xml.sax._exceptions import *
-from xml.parsers import expat
+try:
+ from xml.parsers import expat
+except ImportError:
+ raise SAXReaderNotAvailable("expat not supported",None)
from xml.sax import xmlreader, saxutils, handler
AttributesImpl = xmlreader.AttributesImpl