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"
72 def open_listing_file(path, echo_to_stderr = 1):
73 # Begin a new error listing. If path is None, no file
74 # is opened, the error counter is just reset.
75 global listing_file, num_errors, echo_file
77 listing_file = open_new_file(path)
81 echo_file = sys.stderr
86 def close_listing_file():
92 def report_error(err):
94 error_stack[-1].append(err)
97 # See Main.py for why dual reporting occurs. Quick fix for now.
98 if err.reported: return
102 listing_file.write(line)
104 echo_file.write(line)
105 num_errors = num_errors + 1
107 def error(position, message):
108 #print "Errors.error:", repr(position), repr(message) ###
109 err = CompileError(position, message)
110 #if position is not None: raise Exception(err) # debug
114 LEVEL=1 # warn about all errors level 1 or higher
116 def warning(position, message, level=0):
119 warn = CompileWarning(position, message)
120 line = "warning: %s\n" % warn
122 listing_file.write(line)
124 echo_file.write(line)
127 # These functions can be used to momentarily suppress errors.
132 error_stack.append([])
134 def release_errors(ignore=False):
135 held_errors = error_stack.pop()
137 for err in held_errors:
141 return error_stack[-1]