# HG changeset patch # User Paul Boddie # Date 1219963486 -7200 # Node ID 92d6d68675cbbf5cda3c2a294bb97b88ebd042f2 # Parent ffaf027ed4713c22ce33707d0f76e23f35420975 Introduced stricter checking of unfinished documents. Introduced more generic testing and reporting of fatal errors in parsing. diff -r ffaf027ed471 -r 92d6d68675cb libxml2dom/macrolib/macrolib.py --- a/libxml2dom/macrolib/macrolib.py Thu Aug 28 23:41:04 2008 +0200 +++ b/libxml2dom/macrolib/macrolib.py Fri Aug 29 00:44:46 2008 +0200 @@ -601,28 +601,42 @@ Parser_configure(context, validate, remote) Parser_parse(context) doc = Parser_document(context) + error = Parser_error() - if validate and not Parser_valid(context): + try: + if validate and not Parser_valid(context): + + # NOTE: May not be the correct exception. - # NOTE: May not be the correct exception. + raise LSException( + LSException.PARSE_ERR, + DOMError( + DOMError.SEVERITY_FATAL_ERROR, + get_parse_error_message() or "Document did not validate" + )) + + elif unfinished and (error is None or Parser_errorCode(error) == XML_ERR_TAG_NOT_FINISHED): + + # NOTE: There may be other unfinished conditions. - raise LSException( - LSException.PARSE_ERR, - DOMError( - DOMError.SEVERITY_FATAL_ERROR, - get_parse_error_message() or "Document did not validate" - )) + return doc - elif unfinished or Parser_well_formed(context): - return doc + elif error is not None and Parser_errorLevel(error) == XML_ERR_FATAL: + raise LSException( + LSException.PARSE_ERR, + DOMError( + DOMError.SEVERITY_FATAL_ERROR, + get_parse_error_message() or "Document caused fatal error" + )) - else: - raise LSException( - LSException.PARSE_ERR, - DOMError( - DOMError.SEVERITY_FATAL_ERROR, - get_parse_error_message() or "Document not well-formed" - )) + else: + + # NOTE: Could provide non-fatal errors or warnings. + + return doc + + finally: + Parser_resetError() def toString(node, encoding=None, prettyprint=0): return libxml2mod.serializeNode(node, encoding, prettyprint) @@ -643,6 +657,13 @@ XML_PARSE_NOWARNING = 64 XML_PARSE_NONET = 2048 +XML_ERR_NONE = 0 +XML_ERR_WARNING = 1 +XML_ERR_ERROR = 2 +XML_ERR_FATAL = 3 + +XML_ERR_TAG_NOT_FINISHED = 77 + def html_net_flag(remote): if remote: return 0 @@ -662,7 +683,7 @@ return 0 def get_parse_error_message(): - error = libxml2mod.xmlGetLastError() + error = Parser_error() if error is not None: filename = libxml2mod.xmlErrorGetFile(error) if filename is None: @@ -675,6 +696,18 @@ else: return None +def Parser_error(): + return libxml2mod.xmlGetLastError() + +def Parser_resetError(): + return libxml2mod.xmlResetLastError() + +def Parser_errorLevel(error): + return libxml2mod.xmlErrorGetLevel(error) + +def Parser_errorCode(error): + return libxml2mod.xmlErrorGetCode(error) + def Parser_push(): return libxml2mod.xmlCreatePushParser(None, "", 0, None)