Commit f7e84e12 authored by Sjoerd Mullender's avatar Sjoerd Mullender

Two changes:

- Use new Error class (subclass of RuntimeError so is backward
  compatible) which is raised when RuntimeError used to be raised.
- Report original attribute name in error messages instead of name
  mangled with namespace URL.
parent 21e4dd04
...@@ -8,6 +8,9 @@ import string ...@@ -8,6 +8,9 @@ import string
version = '0.3' version = '0.3'
class Error(RuntimeError):
pass
# Regular expressions used for parsing # Regular expressions used for parsing
_S = '[ \t\r\n]+' # white space _S = '[ \t\r\n]+' # white space
...@@ -309,7 +312,7 @@ class XMLParser: ...@@ -309,7 +312,7 @@ class XMLParser:
'encoding', 'encoding',
'standalone') 'standalone')
if version[1:-1] != '1.0': if version[1:-1] != '1.0':
raise RuntimeError, 'only XML version 1.0 supported' raise Error('only XML version 1.0 supported')
if encoding: encoding = encoding[1:-1] if encoding: encoding = encoding[1:-1]
if standalone: standalone = standalone[1:-1] if standalone: standalone = standalone[1:-1]
self.handle_xml(encoding, standalone) self.handle_xml(encoding, standalone)
...@@ -390,7 +393,7 @@ class XMLParser: ...@@ -390,7 +393,7 @@ class XMLParser:
i = i+1 i = i+1
continue continue
else: else:
raise RuntimeError, 'neither < nor & ??' raise Error('neither < nor & ??')
# We get here only if incomplete matches but # We get here only if incomplete matches but
# nothing else # nothing else
break break
...@@ -419,7 +422,7 @@ class XMLParser: ...@@ -419,7 +422,7 @@ class XMLParser:
def parse_comment(self, i): def parse_comment(self, i):
rawdata = self.rawdata rawdata = self.rawdata
if rawdata[i:i+4] <> '<!--': if rawdata[i:i+4] <> '<!--':
raise RuntimeError, 'unexpected call to handle_comment' raise Error('unexpected call to handle_comment')
res = commentclose.search(rawdata, i+4) res = commentclose.search(rawdata, i+4)
if res is None: if res is None:
return -1 return -1
...@@ -485,7 +488,7 @@ class XMLParser: ...@@ -485,7 +488,7 @@ class XMLParser:
def parse_cdata(self, i): def parse_cdata(self, i):
rawdata = self.rawdata rawdata = self.rawdata
if rawdata[i:i+9] <> '<![CDATA[': if rawdata[i:i+9] <> '<![CDATA[':
raise RuntimeError, 'unexpected call to parse_cdata' raise Error('unexpected call to parse_cdata')
res = cdataclose.search(rawdata, i+9) res = cdataclose.search(rawdata, i+9)
if res is None: if res is None:
return -1 return -1
...@@ -509,7 +512,7 @@ class XMLParser: ...@@ -509,7 +512,7 @@ class XMLParser:
self.syntax_error('illegal character in processing instruction') self.syntax_error('illegal character in processing instruction')
res = tagfind.match(rawdata, i+2) res = tagfind.match(rawdata, i+2)
if res is None: if res is None:
raise RuntimeError, 'unexpected call to parse_proc' raise Error('unexpected call to parse_proc')
k = res.end(0) k = res.end(0)
name = res.group(0) name = res.group(0)
if self.__map_case: if self.__map_case:
...@@ -622,9 +625,13 @@ class XMLParser: ...@@ -622,9 +625,13 @@ class XMLParser:
nstag = prefix + ':' + nstag # undo split nstag = prefix + ':' + nstag # undo split
self.stack[-1] = tagname, nsdict, nstag self.stack[-1] = tagname, nsdict, nstag
# translate namespace of attributes # translate namespace of attributes
attrnamemap = {} # map from new name to old name (used for error reporting)
for key in attrdict.keys():
attrnamemap[key] = key
if self.__use_namespaces: if self.__use_namespaces:
nattrdict = {} nattrdict = {}
for key, val in attrdict.items(): for key, val in attrdict.items():
okey = key
res = qname.match(key) res = qname.match(key)
if res is not None: if res is not None:
aprefix, key = res.group('prefix', 'local') aprefix, key = res.group('prefix', 'local')
...@@ -645,12 +652,13 @@ class XMLParser: ...@@ -645,12 +652,13 @@ class XMLParser:
elif ns is not None: elif ns is not None:
key = ns + ' ' + key key = ns + ' ' + key
nattrdict[key] = val nattrdict[key] = val
attrnamemap[key] = okey
attrdict = nattrdict attrdict = nattrdict
attributes = self.attributes.get(nstag) attributes = self.attributes.get(nstag)
if attributes is not None: if attributes is not None:
for key in attrdict.keys(): for key in attrdict.keys():
if not attributes.has_key(key): if not attributes.has_key(key):
self.syntax_error("unknown attribute `%s' in tag `%s'" % (key, tagname)) self.syntax_error("unknown attribute `%s' in tag `%s'" % (attrnamemap[key], tagname))
for key, val in attributes.items(): for key, val in attributes.items():
if val is not None and not attrdict.has_key(key): if val is not None and not attrdict.has_key(key):
attrdict[key] = val attrdict[key] = val
...@@ -783,7 +791,7 @@ class XMLParser: ...@@ -783,7 +791,7 @@ class XMLParser:
# Example -- handle relatively harmless syntax errors, could be overridden # Example -- handle relatively harmless syntax errors, could be overridden
def syntax_error(self, message): def syntax_error(self, message):
raise RuntimeError, 'Syntax error at line %d: %s' % (self.lineno, message) raise Error('Syntax error at line %d: %s' % (self.lineno, message))
# To be overridden -- handlers for unknown objects # To be overridden -- handlers for unknown objects
def unknown_starttag(self, tag, attrs): pass def unknown_starttag(self, tag, attrs): pass
...@@ -906,7 +914,7 @@ def test(args = None): ...@@ -906,7 +914,7 @@ def test(args = None):
for c in data: for c in data:
x.feed(c) x.feed(c)
x.close() x.close()
except RuntimeError, msg: except Error, msg:
t1 = time() t1 = time()
print msg print msg
if do_time: if do_time:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment