# HG changeset patch # User catherine@cordelia # Date 1243891917 14400 # Node ID 692ce3ee80d29202086c815a741c53b2e9e055c7 # Parent f42f92dc746419ff155e954d4d7bf781a3b54ea5 error location reporting working diff -r f42f92dc7464 -r 692ce3ee80d2 sqlpython/sqlpyPlus.py --- a/sqlpython/sqlpyPlus.py Mon Jun 01 16:23:44 2009 -0400 +++ b/sqlpython/sqlpyPlus.py Mon Jun 01 17:31:57 2009 -0400 @@ -261,6 +261,27 @@ else: return lst[max(center-width,0):center+width+1] + +def offset_to_line(instring, offset): + r""" + >>> offset_to_line('abcdefghijkl', 5) + (0, 'abcdefghijkl', 5) + >>> offset_to_line('ab\ncd\nefg', 6) + (2, 'efg', 0) + >>> offset_to_line('ab\ncd\nefg', 5) + (1, 'cd\n', 2) + >>> offset_to_line('abcdefghi\njkl', 5) + (0, 'abcdefghi\n', 5) + >>> offset_to_line('abcdefghi\njkl', 700) + + """ + lineNum = 0 + for line in instring.splitlines(True): + if offset < len(line): + return lineNum, line, offset + lineNum += 1 + offset -= len(line) + class sqlpyPlus(sqlpython.sqlpython): defaultExtension = 'sql' abbrev = True @@ -317,9 +338,10 @@ def perror(self, err, statement=None): try: - linenum = statement.parsed.raw[:err.message.offset].count('\n') - print statement.parsed.raw.splitlines()[linenum] - print '%s^' % (' ' * (err.message.offset + len(self.prompt))) + linenum, line, offset = offset_to_line(statement.parsed.raw, err.message.offset) + print line.strip() + print '%s*' % (' ' * offset) + print 'ERROR at line %d:' % (linenum + 1) except AttributeError: pass print str(err)