Source code for curry.exceptions


[docs] class CurryError(Exception): '''The root of the Curry exception tree.'''
[docs] class ForkError(CurryError, RuntimeError): '''Sprite was busy when this child process was forked.'''
[docs] class CompileError(CurryError): '''Raised when an error occurs while compiling Curry code.''' def __init__(self, message, report=None, line=None, column=None): import re report = message if report is None else report message = message.replace('\r\n', '\n') match = re.search(r'(?m)^([^\n]*?\.curry):(\d+):(\d+)([^\n]*\bError:[^\n]*)', message) if match: import os line, column = int(match[2]), int(match[3]) first = os.path.basename(match[1]) + ':' + match[2] + ':' + match[3] + match[4] following = message[match.end():].strip().splitlines() if first.endswith('Error:') and following: first += ' ' + following[0].strip() message = first CurryError.__init__(self, message.strip()) self.report, self.line, self.column = report, line, column for index, (header, body) in enumerate(_diagnostic_blocks(report)): if index == 0 and header.endswith('Error:'): body = '\n'.join(body.splitlines()[1:]) note = body if index == 0 else header + ('\n' + body if body else '') if note: self.add_note(note)
def _diagnostic_blocks(report): import re import ntpath headers = re.compile(r'(?m)^([^\n]*?\.curry):(\d+):(\d+)([^\n]*\b(?:Error|Warning):[^\n]*)') matches = list(headers.finditer(report.replace('\r\n', '\n'))) report = report.replace('\r\n', '\n') import os from . import config import tempfile directories = set() for directory in (config.installed_path(), tempfile.gettempdir(), os.getcwd()): for spelling in (str(directory), os.path.realpath(directory)): directories.add(spelling.rstrip('/\\')) if os.name == 'nt': directories.add(spelling.replace('\\', '/').rstrip('/')) directories.add(spelling.replace('/', '\\').rstrip('\\')) prefixes = '|'.join(re.escape(path) for path in sorted(directories, key=len, reverse=True) if path) paths = re.compile(r"(?<![\w:/\\])(?:" + prefixes + r")[/\\][^\s'\"<>:;,()]+") def message_text(text): return paths.sub(lambda match: ntpath.basename(match[0]), text) for index, match in enumerate(matches): if 'Error:' not in match[4]: continue end = matches[index + 1].start() if index + 1 < len(matches) else len(report) lines = report[match.end():end].splitlines() body = [] for line in lines: if line and not line[0].isspace(): break body.append(line) header = ntpath.basename(match[1]) + ':' + match[2] + ':' + match[3] + match[4] yield message_text(header), message_text('\n'.join(body).strip('\n'))
[docs] class NotConstructorError(CurryError, ValueError): '''Raised when a non-constructor value occurs where disallowed.''' def __init__(self, arg): self.arg = arg
[docs] class ModuleLookupError(CurryError, ModuleNotFoundError): '''Raised when a Curry module is not found.'''
[docs] class SymbolLookupError(CurryError, AttributeError): '''Raised when a Curry symbol is not found.'''
[docs] class TypeLookupError(CurryError, AttributeError): '''Raised when a Curry type is not found.'''
[docs] class CurryIndexError(CurryError, IndexError): '''Raised when indexing into a Curry expression fails.''' pass
[docs] class CurryTypeError(CurryError, TypeError): '''Raised when a type error occurs while evaluating a Curry program.''' def __init__(self, lines, where=None, symbol=None, expected=None, actual=None, value=None): if isinstance(lines, str): lines = [lines] CurryError.__init__(self, '\n'.join(lines)) self.lines = list(lines) self.where, self.symbol = where, symbol self.expected, self.actual, self.value = expected, actual, value @property def first_line(self): return self.lines[0]
[docs] class TypeSyntaxError(CurryTypeError): '''A type annotation cannot be parsed or has an invalid type or kind.'''
[docs] class InterfaceError(CurryTypeError): '''The interface of a module cannot be read.'''
[docs] class ModuleExistsError(CurryError, ValueError): '''A module of the given name is already loaded.'''
[docs] class LoadError(CurryError, OSError): '''An object could not be written, read or replaced.'''
[docs] class FlagError(CurryError, ValueError): '''An unknown flag or a value its validator refuses.'''
[docs] class CurryWarning(UserWarning): '''A warning of the front end or loader.'''
[docs] class IntOverflowError(CurryError, OverflowError): '''A bounded conversion is out of range, or a Float is not finite.'''
[docs] class NoValueError(CurryError, LookupError): '''Results.one found no value.'''
[docs] class ManyValuesError(CurryError, ValueError): '''Results.one found a second value.'''
[docs] class StaleError(CurryError, RuntimeError): '''An object belongs to an earlier session or module version.'''
[docs] class ReentrancyError(CurryError, RuntimeError): '''A host callback entered Sprite while its thread held an internal lock.'''
[docs] class PrerequisiteError(CurryError, IOError): '''Raised when a build prerequisite is invalid.'''
###### # Evaluation Errors
[docs] class EvaluationError(CurryError): '''Signals an error raised from a Curry program.''' pass
[docs] class SuspensionError(EvaluationError): '''Evaluation failed due to suspended constraints.''' def __init__(self, message='Evaluation suspended'): EvaluationError.__init__(self, message)
[docs] class DivisionByZeroError(EvaluationError, ZeroDivisionError): '''An integral division has a zero divisor.'''
[docs] class CurryIOError(EvaluationError, OSError): '''An uncaught error of an I/O action.''' def __str__(self): return str(self.args[0]) if self.args else ''
[docs] class CurryFileNotFoundError(CurryIOError, FileNotFoundError): '''An I/O action could not find its file.'''
[docs] class NondetError(EvaluationError): '''A choice occurred inside an I/O action.''' CTOR_INDEX = 3 def __init__(self, message='non-determinism in monadic actions occurred!'): EvaluationError.__init__(self, message)
[docs] class StackLimitError(EvaluationError, RecursionError): '''An evaluation reached its stack limit.'''
[docs] class EvaluationTimeout(EvaluationError): '''An evaluation exceeded its time limit.'''
[docs] class InstantiationError(EvaluationError): '''A free variable of function type could not be instantiated.'''
def evaluation_error(message, kind='evaluation', filename=None): """Translate the runtime's explicit error kind at the native boundary.""" cls = { 'evaluation': EvaluationError, 'io': CurryIOError , 'file_not_found': CurryFileNotFoundError, 'nondet': NondetError , 'overflow': IntOverflowError, 'division': DivisionByZeroError , 'stack': StackLimitError }[kind] error = cls(message) if kind == 'file_not_found': error.filename = filename return error