From f79b56550a3d6f2d53f9f35479daf7782589107a Mon Sep 17 00:00:00 2001 From: William Stein Date: Sat, 21 Oct 2006 19:47:08 -0700 Subject: [PATCH] Added nice error messages with context information. Finally, you can see the line itself where the error occured, instead of just the line number!! --- Cython/Compiler/Errors.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Cython/Compiler/Errors.py b/Cython/Compiler/Errors.py index 7381b067..070707d1 100644 --- a/Cython/Compiler/Errors.py +++ b/Cython/Compiler/Errors.py @@ -12,6 +12,13 @@ class PyrexError(Exception): class PyrexWarning(Exception): pass +def context(position): + F = open(position[0]).readlines() + s = ''.join(F[position[1]-6:position[1]]) + s += ' '*(position[2]-1) + '^' + s = '-'*60 + '\n...\n' + s + '\n' + '-'*60 + '\n' + return s + class CompileError(PyrexError): def __init__(self, position = None, message = ""): @@ -19,9 +26,11 @@ class CompileError(PyrexError): self.message = message if position: pos_str = "%s:%d:%d: " % position + cont = context(position) else: pos_str = "" - Exception.__init__(self, pos_str + message) + cont = '' + Exception.__init__(self, '\nError converting Pyrex file to C:\n' + cont + '\n' + pos_str + message ) class CompileWarning(PyrexWarning): -- 2.26.2