# HG changeset patch # User cmlenz # Date 1174415051 0 # Node ID 5d08a744636e868f1d65a7e5b355a2591380f5f0 # Parent fbdbe27885e27e5fcd48a7d8be4f16361fab6ad9 More work to include absolute file paths in exceptions. diff --git a/genshi/input.py b/genshi/input.py --- a/genshi/input.py +++ b/genshi/input.py @@ -19,6 +19,7 @@ from sets import ImmutableSet as frozenset import HTMLParser as html import htmlentitydefs +import os from StringIO import StringIO from genshi.core import Attrs, QName, Stream, stripentities @@ -47,10 +48,12 @@ """Exception raised when fatal syntax errors are found in the input being parsed.""" - def __init__(self, message, filename='', lineno=-1, offset=-1): + def __init__(self, message, filename=None, lineno=-1, offset=-1): + self.msg = message + if filename: + message += ', in ' + os.path.basename(filename) Exception.__init__(self, message) - self.msg = message - self.filename = filename + self.filename = filename or '' self.lineno = lineno self.offset = offset @@ -142,8 +145,6 @@ break except expat.ExpatError, e: msg = str(e) - if self.filename: - msg += ', in ' + self.filename raise ParseError(msg, self.filename, e.lineno, e.offset) return Stream(_generate()).filter(_coalesce) @@ -293,8 +294,6 @@ break except html.HTMLParseError, e: msg = '%s: line %d, column %d' % (e.msg, e.lineno, e.offset) - if self.filename: - msg += ', in %s' % self.filename raise ParseError(msg, self.filename, e.lineno, e.offset) return Stream(_generate()).filter(_coalesce) diff --git a/genshi/template/directives.py b/genshi/template/directives.py --- a/genshi/template/directives.py +++ b/genshi/template/directives.py @@ -76,7 +76,7 @@ at runtime. `stream` is an event stream that replaces the original stream associated with the directive. """ - return cls(value, namespaces, template.filename, *pos[1:]), stream + return cls(value, namespaces, template.filepath, *pos[1:]), stream attach = classmethod(attach) def __call__(self, stream, ctxt, directives): @@ -197,7 +197,7 @@ __slots__ = [] def attach(cls, template, stream, value, namespaces, pos): - expr = cls._parse_expr(value, template.filename, *pos[1:]) + expr = cls._parse_expr(value, template.filepath, *pos[1:]) return None, [stream[0], (EXPR, expr, pos), stream[-1]] attach = classmethod(attach) @@ -442,7 +442,7 @@ __slots__ = [] def attach(cls, template, stream, value, namespaces, pos): - expr = cls._parse_expr(value, template.filename, *pos[1:]) + expr = cls._parse_expr(value, template.filepath, *pos[1:]) return None, [(EXPR, expr, pos)] attach = classmethod(attach) diff --git a/genshi/template/interpolation.py b/genshi/template/interpolation.py --- a/genshi/template/interpolation.py +++ b/genshi/template/interpolation.py @@ -55,7 +55,7 @@ textbuf = [] textpos = None - for is_expr, chunk in chain(lex(text, pos), [(True, '')]): + for is_expr, chunk in chain(lex(text, pos, filepath), [(True, '')]): if is_expr: if textbuf: yield TEXT, u''.join(textbuf), textpos @@ -66,7 +66,7 @@ expr = Expression(chunk.strip(), pos[0], pos[1]) yield EXPR, expr, tuple(pos) except SyntaxError, err: - raise TemplateSyntaxError(err, pos[0], pos[1], + raise TemplateSyntaxError(err, filepath, pos[1], pos[2] + (err.offset or 0)) else: textbuf.append(chunk) @@ -80,7 +80,7 @@ else: pos[2] += len(chunk) -def lex(text, textpos): +def lex(text, textpos, filepath): offset = pos = 0 end = len(text) escaped = False @@ -103,7 +103,8 @@ while level: match = tokenprog.match(text, pos) if match is None: - raise TemplateSyntaxError('invalid syntax', *textpos) + raise TemplateSyntaxError('invalid syntax', filepath, + *textpos[1:]) pos = match.end() tstart, tend = match.regs[3] token = text[tstart:tend] diff --git a/genshi/template/markup.py b/genshi/template/markup.py --- a/genshi/template/markup.py +++ b/genshi/template/markup.py @@ -146,7 +146,8 @@ include_href = new_attrs.get('href') if not include_href: raise TemplateSyntaxError('Include misses required ' - 'attribute "href"', *pos) + 'attribute "href"', + self.filepath, *pos[1:]) streams.append([]) elif tag.localname == 'fallback': in_fallback += 1