浏览代码

pythonlib: more error for script.task.parse_interface

Trying to avoid any possible unhandled excpetion when parsing modules. This helps modules which are parsed on startup to construct menus (toolboxes).

Motivation is an the following reported (http://lists.osgeo.org/pipermail/grass-dev/2015-May/074999.html) error:
  ParseError: not well-formed (invalid token): line 17, column 19
which occured in parse_interface during _expandRuntimeModules in toolboxes.py


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@65200 15284696-431f-4ddb-bdfa-cd5b030d7da7
Vaclav Petras 10 年之前
父节点
当前提交
1ea1e8e296
共有 1 个文件被更改,包括 12 次插入1 次删除
  1. 12 1
      lib/python/script/task.py

+ 12 - 1
lib/python/script/task.py

@@ -24,6 +24,13 @@ try:
     import xml.etree.ElementTree as etree
 except ImportError:
     import elementtree.ElementTree as etree # Python <= 2.4
+from xml.parsers import expat  # TODO: works for any Python?
+# Get the XML parsing exceptions to catch. The behavior chnaged with Python 2.7
+# and ElementTree 1.3.
+if hasattr(etree, 'ParseError'):
+    ETREE_EXCEPTIONS = (etree.ParseError, expat.ExpatError)
+else:
+    ETREE_EXCEPTIONS = (expat.ExpatError)
 
 from utils import decode, split
 from core import *
@@ -506,7 +513,11 @@ def parse_interface(name, parser=processTask, blackList=None):
     :param parser:
     :param blackList:
     """
-    tree = etree.fromstring(get_interface_description(name))
+    try:
+        tree = etree.fromstring(get_interface_description(name))
+    except ETREE_EXCEPTIONS as error:
+        raise ScriptError(_("Cannot parse interface description of"
+            "<{name}> module: {error}").format(name=name, error=error))
     return parser(tree, blackList=blackList).get_task()