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 = u"[unprintable code]\n"
26 s = u''.join(F[max(0, position[1]-6):position[1]])
27 s = u'...\n%s%s^\n' % (s, u' '*(position[2]-1))
28 s = u'%s\n%s%s\n' % (u'-'*60, s, u'-'*60)
31 class CompileError(PyrexError):
33 def __init__(self, position = None, message = u""):
34 self.position = position
35 self.message_only = message
37 # Deprecated and withdrawn in 2.6:
38 # self.message = message
40 pos_str = u"%s:%d:%d: " % (position[0].get_description(), position[1], position[2])
41 cont = context(position)
45 Exception.__init__(self, u'\nError converting Pyrex file to C:\n%s\n%s%s' % (
46 cont, pos_str, message))
48 class CompileWarning(PyrexWarning):
50 def __init__(self, position = None, message = ""):
51 self.position = position
52 # Deprecated and withdrawn in 2.6:
53 # self.message = message
55 pos_str = u"%s:%d:%d: " % (position[0].get_description(), position[1], position[2])
58 Exception.__init__(self, pos_str + message)
61 class InternalError(Exception):
62 # If this is ever raised, there is a bug in the compiler.
64 def __init__(self, message):
65 Exception.__init__(self, u"Internal compiler error: %s"
69 class CompilerCrash(CompileError):
70 # raised when an unexpected exception occurs in a transform
71 def __init__(self, pos, context, message, cause, stacktrace=None):
73 message = u'\n' + message
77 message = u"Compiler crash in %s%s" % (context, message)
81 u'\n\nCompiler crash traceback from this point on:\n' +
82 u''.join(traceback.format_tb(stacktrace)))
86 message += u'%s: %s' % (cause.__class__.__name__, cause)
87 CompileError.__init__(self, pos, message)
94 def open_listing_file(path, echo_to_stderr = 1):
95 # Begin a new error listing. If path is None, no file
96 # is opened, the error counter is just reset.
97 global listing_file, num_errors, echo_file
99 listing_file = open_new_file(path)
103 echo_file = sys.stderr
108 def close_listing_file():
114 def report_error(err):
116 error_stack[-1].append(err)
119 # See Main.py for why dual reporting occurs. Quick fix for now.
120 if err.reported: return
124 try: listing_file.write(line)
125 except UnicodeEncodeError:
126 listing_file.write(line.encode('ASCII', 'replace'))
128 try: echo_file.write(line)
129 except UnicodeEncodeError:
130 echo_file.write(line.encode('ASCII', 'replace'))
131 num_errors = num_errors + 1
133 def error(position, message):
134 #print "Errors.error:", repr(position), repr(message) ###
135 err = CompileError(position, message)
136 #if position is not None: raise Exception(err) # debug
140 LEVEL=1 # warn about all errors level 1 or higher
142 def warning(position, message, level=0):
145 warn = CompileWarning(position, message)
146 line = "warning: %s\n" % warn
148 listing_file.write(line)
150 echo_file.write(line)
154 def warn_once(position, message, level=0):
155 if level < LEVEL or message in _warn_once_seen:
157 warn = CompileWarning(position, message)
158 line = "warning: %s\n" % warn
160 listing_file.write(line)
162 echo_file.write(line)
163 _warn_once_seen[message] = True
167 # These functions can be used to momentarily suppress errors.
172 error_stack.append([])
174 def release_errors(ignore=False):
175 held_errors = error_stack.pop()
177 for err in held_errors:
181 return error_stack[-1]