6 from Cython.Utils import open_new_file
7 from DebugFlags import debug_exception_on_error
11 class PyrexError(Exception):
14 class PyrexWarning(Exception):
18 def context(position):
20 assert not (isinstance(source, unicode) or isinstance(source, str)), (
21 "Please replace filename strings with Scanning.FileSourceDescriptor instances %r" % source)
23 F = list(source.get_lines())
24 except UnicodeDecodeError:
25 # file has an encoding problem
26 s = u"[unprintable code]\n"
28 s = u''.join(F[max(0, position[1]-6):position[1]])
29 s = u'...\n%s%s^\n' % (s, u' '*(position[2]-1))
30 s = u'%s\n%s%s\n' % (u'-'*60, s, u'-'*60)
33 def format_position(position):
35 return u"%s:%d:%d: " % (position[0].get_description(),
36 position[1], position[2])
39 def format_error(message, position):
41 pos_str = format_position(position)
42 cont = context(position)
43 message = u'\nError compiling Cython file:\n%s\n%s%s' % (cont, pos_str, message or u'')
46 class CompileError(PyrexError):
48 def __init__(self, position = None, message = u""):
49 self.position = position
50 self.message_only = message
52 # Deprecated and withdrawn in 2.6:
53 # self.message = message
54 Exception.__init__(self, format_error(message, position))
56 class CompileWarning(PyrexWarning):
58 def __init__(self, position = None, message = ""):
59 self.position = position
60 # Deprecated and withdrawn in 2.6:
61 # self.message = message
62 Exception.__init__(self, format_position(position) + message)
65 class InternalError(Exception):
66 # If this is ever raised, there is a bug in the compiler.
68 def __init__(self, message):
69 self.message_only = message
70 Exception.__init__(self, u"Internal compiler error: %s"
74 class CompilerCrash(CompileError):
75 # raised when an unexpected exception occurs in a transform
76 def __init__(self, pos, context, message, cause, stacktrace=None):
78 message = u'\n' + message
81 self.message_only = message
83 message = u"Compiler crash in %s%s" % (context, message)
87 u'\n\nCompiler crash traceback from this point on:\n' +
88 u''.join(traceback.format_tb(stacktrace)))
92 message += u'%s: %s' % (cause.__class__.__name__, cause)
93 CompileError.__init__(self, pos, message)
100 def open_listing_file(path, echo_to_stderr = 1):
101 # Begin a new error listing. If path is None, no file
102 # is opened, the error counter is just reset.
103 global listing_file, num_errors, echo_file
105 listing_file = open_new_file(path)
109 echo_file = sys.stderr
114 def close_listing_file():
120 def report_error(err):
122 error_stack[-1].append(err)
125 # See Main.py for why dual reporting occurs. Quick fix for now.
126 if err.reported: return
128 try: line = u"%s\n" % err
129 except UnicodeEncodeError:
130 # Python <= 2.5 does this for non-ASCII Unicode exceptions
131 line = format_error(getattr(err, 'message_only', "[unprintable exception message]"),
132 getattr(err, 'position', None)) + u'\n'
134 try: listing_file.write(line)
135 except UnicodeEncodeError:
136 listing_file.write(line.encode('ASCII', 'replace'))
138 try: echo_file.write(line)
139 except UnicodeEncodeError:
140 echo_file.write(line.encode('ASCII', 'replace'))
141 num_errors = num_errors + 1
142 if Options.fatal_errors:
143 raise InternalError, "abort"
145 def error(position, message):
146 #print "Errors.error:", repr(position), repr(message) ###
148 raise InternalError(message)
149 err = CompileError(position, message)
150 if debug_exception_on_error: raise Exception(err) # debug
154 LEVEL=1 # warn about all errors level 1 or higher
156 def message(position, message, level=1):
159 warn = CompileWarning(position, message)
160 line = "note: %s\n" % warn
162 listing_file.write(line)
164 echo_file.write(line)
167 def warning(position, message, level=0):
170 warn = CompileWarning(position, message)
171 line = "warning: %s\n" % warn
173 listing_file.write(line)
175 echo_file.write(line)
179 def warn_once(position, message, level=0):
180 if level < LEVEL or message in _warn_once_seen:
182 warn = CompileWarning(position, message)
183 line = "warning: %s\n" % warn
185 listing_file.write(line)
187 echo_file.write(line)
188 _warn_once_seen[message] = True
192 # These functions can be used to momentarily suppress errors.
197 error_stack.append([])
199 def release_errors(ignore=False):
200 held_errors = error_stack.pop()
202 for err in held_errors:
206 return error_stack[-1]
209 # this module needs a redesign to support parallel cythonisation, but
210 # for now, the following works at least in sequential compiler runs
213 _warn_once_seen.clear()