6 from Cython.Utils import open_new_file
9 class PyrexError(Exception):
12 class PyrexWarning(Exception):
16 def context(position):
18 assert not (isinstance(source, unicode) or isinstance(source, str)), (
19 "Please replace filename strings with Scanning.FileSourceDescriptor instances %r" % source)
21 F = list(source.get_lines())
22 except UnicodeDecodeError:
23 # file has an encoding problem
24 s = "[unprintable code]\n"
26 s =''.join(F[max(0, position[1]-6):position[1]])
27 s = '...\n' + s + ' '*(position[2]-1) + '^\n'
28 s = '-'*60 + '\n' + s + '-'*60 + '\n'
31 class CompileError(PyrexError):
33 def __init__(self, position = None, message = ""):
34 self.position = position
35 self.message_only = message
37 # Deprecated and withdrawn in 2.6:
38 # self.message = message
40 pos_str = "%s:%d:%d: " % (position[0].get_description(), position[1], position[2])
41 cont = context(position)
45 Exception.__init__(self, '\nError converting Pyrex file to C:\n' + cont + '\n' + pos_str + message )
47 class CompileWarning(PyrexWarning):
49 def __init__(self, position = None, message = ""):
50 self.position = position
51 # Deprecated and withdrawn in 2.6:
52 # self.message = message
54 pos_str = "%s:%d:%d: " % (position[0].get_description(), position[1], position[2])
57 Exception.__init__(self, pos_str + message)
60 class InternalError(Exception):
61 # If this is ever raised, there is a bug in the compiler.
63 def __init__(self, message):
64 Exception.__init__(self, "Internal compiler error: %s"
68 class CompilerCrash(CompileError):
69 # raised when an unexpected exception occurs in a transform
70 def __init__(self, pos, context, message, cause, stacktrace=None):
72 message = u'\n' + message
76 message = "Compiler crash in " + context + message
80 u'\n\nCompiler crash traceback from this point on:\n' +
81 u''.join(traceback.format_tb(stacktrace)))
85 message += u'%s: %s' % (cause.__class__.__name__, cause)
86 CompileError.__init__(self, pos, message)
93 def open_listing_file(path, echo_to_stderr = 1):
94 # Begin a new error listing. If path is None, no file
95 # is opened, the error counter is just reset.
96 global listing_file, num_errors, echo_file
98 listing_file = open_new_file(path)
102 echo_file = sys.stderr
107 def close_listing_file():
113 def report_error(err):
115 error_stack[-1].append(err)
118 # See Main.py for why dual reporting occurs. Quick fix for now.
119 if err.reported: return
123 listing_file.write(line)
125 echo_file.write(line)
126 num_errors = num_errors + 1
128 def error(position, message):
129 #print "Errors.error:", repr(position), repr(message) ###
130 err = CompileError(position, message)
131 #if position is not None: raise Exception(err) # debug
135 LEVEL=1 # warn about all errors level 1 or higher
137 def warning(position, message, level=0):
140 warn = CompileWarning(position, message)
141 line = "warning: %s\n" % warn
143 listing_file.write(line)
145 echo_file.write(line)
148 # These functions can be used to momentarily suppress errors.
153 error_stack.append([])
155 def release_errors(ignore=False):
156 held_errors = error_stack.pop()
158 for err in held_errors:
162 return error_stack[-1]