From 2beaf23e6c0e6af38ee723fe9fc11f6e2b504dbb Mon Sep 17 00:00:00 2001 From: Robert Jordens Date: Mon, 25 Jan 2016 15:52:52 -0700 Subject: [PATCH] language...ARTIQException -> coredevice...CoreException gets rid of a cross import is only used there --- artiq/coredevice/comm_generic.py | 17 ++++++----- artiq/coredevice/exceptions.py | 47 ++++++++++++++++++++++++++++++- artiq/frontend/artiq_run.py | 4 +-- artiq/language/core.py | 48 -------------------------------- 4 files changed, 56 insertions(+), 60 deletions(-) diff --git a/artiq/coredevice/comm_generic.py b/artiq/coredevice/comm_generic.py index b97d13a4d..872b86cc2 100644 --- a/artiq/coredevice/comm_generic.py +++ b/artiq/coredevice/comm_generic.py @@ -4,7 +4,6 @@ import traceback from enum import Enum from fractions import Fraction -from artiq.language import core as core_language from artiq.coredevice import exceptions from artiq import __version__ as software_version @@ -458,8 +457,8 @@ class CommGeneric: self._write_header(_H2DMsgType.RPC_EXCEPTION) - if hasattr(exn, 'artiq_exception'): - exn = exn.artiq_exception + if hasattr(exn, 'core_exception'): + exn = exn.core_exception self._write_string(exn.name) self._write_string(exn.message) for index in range(3): @@ -505,16 +504,16 @@ class CommGeneric: traceback = list(reversed(symbolizer(backtrace))) + \ [(filename, line, column, function, None)] - exception = core_language.ARTIQException(name, message, params, traceback) + core_exception = exceptions.CoreException(name, message, params, traceback) - print(exception.id, exception.name) - if exception.id == 0: - python_exn_type = getattr(exceptions, exception.name.split('.')[-1]) + print(core_exception.id, core_exception.name) + if core_exception.id == 0: + python_exn_type = getattr(exceptions, core_exception.name.split('.')[-1]) else: - python_exn_type = object_map.retrieve(exception.id) + python_exn_type = object_map.retrieve(core_exception.id) python_exn = python_exn_type(message.format(*params)) - python_exn.artiq_exception = exception + python_exn.core_exception = core_exception raise python_exn def serve(self, object_map, symbolizer): diff --git a/artiq/coredevice/exceptions.py b/artiq/coredevice/exceptions.py index b0dbf740f..ea74f7a61 100644 --- a/artiq/coredevice/exceptions.py +++ b/artiq/coredevice/exceptions.py @@ -1,5 +1,5 @@ import builtins -from artiq.language.core import ARTIQException +from artiq.coredevice.runtime import source_loader ZeroDivisionError = builtins.ZeroDivisionError @@ -7,6 +7,51 @@ ValueError = builtins.ValueError IndexError = builtins.IndexError +class CoreException: + """Information about an exception raised or passed through the core device.""" + + def __init__(self, name, message, params, traceback): + if ':' in name: + exn_id, self.name = name.split(':', 2) + self.id = int(exn_id) + else: + self.id, self.name = 0, name + self.message, self.params = message, params + self.traceback = list(traceback) + + def __str__(self): + lines = [] + lines.append("Core Device Traceback (most recent call last):") + for (filename, line, column, function, address) in self.traceback: + stub_globals = {"__name__": filename, "__loader__": source_loader} + source_line = linecache.getline(filename, line, stub_globals) + indentation = re.search(r"^\s*", source_line).end() + + if address is None: + formatted_address = "" + else: + formatted_address = " (RA=0x{:x})".format(address) + + filename = filename.replace(os.path.normpath(os.path.join(os.path.dirname(__file__), + "..")), "") + if column == -1: + lines.append(" File \"{file}\", line {line}, in {function}{address}". + format(file=filename, line=line, function=function, + address=formatted_address)) + lines.append(" {}".format(source_line.strip() if source_line else "")) + else: + lines.append(" File \"{file}\", line {line}, column {column}," + " in {function}{address}". + format(file=filename, line=line, column=column + 1, + function=function, address=formatted_address)) + lines.append(" {}".format(source_line.strip() if source_line else "")) + lines.append(" {}^".format(" " * (column - indentation))) + + lines.append("{}({}): {}".format(self.name, self.id, + self.message.format(*self.params))) + return "\n".join(lines) + + class InternalError(Exception): """Raised when the runtime encounters an internal error condition.""" artiq_builtin = True diff --git a/artiq/frontend/artiq_run.py b/artiq/frontend/artiq_run.py index 480114a46..f1697632f 100755 --- a/artiq/frontend/artiq_run.py +++ b/artiq/frontend/artiq_run.py @@ -132,8 +132,8 @@ def run(with_file=False): except CompileError as error: return except Exception as exn: - if hasattr(exn, 'artiq_exception'): - print(exn.artiq_exception, file=sys.stderr) + if hasattr(exn, 'core_exception'): + print(exn.core_exception, file=sys.stderr) raise exn finally: device_mgr.close_devices() diff --git a/artiq/language/core.py b/artiq/language/core.py index 10eb3dcef..648f0dddd 100644 --- a/artiq/language/core.py +++ b/artiq/language/core.py @@ -10,7 +10,6 @@ from functools import wraps __all__ = ["host_int", "int", "host_round", "round", "kernel", "portable", "syscall", "host_only", "set_time_manager", "set_watchdog_factory", - "ARTIQException", "TerminationRequested"] # global namespace for kernels @@ -372,50 +371,3 @@ def watchdog(timeout): class TerminationRequested(Exception): """Raised by ``pause`` when the user has requested termination.""" pass - - -class ARTIQException: - """Information about an exception raised or passed through the core device.""" - - def __init__(self, name, message, params, traceback): - if ':' in name: - exn_id, self.name = name.split(':', 2) - self.id = host_int(exn_id) - else: - self.id, self.name = 0, name - self.message, self.params = message, params - self.traceback = list(traceback) - - def __str__(self): - # lazy import this - from artiq.coredevice.runtime import source_loader - lines = [] - lines.append("Core Device Traceback (most recent call last):") - for (filename, line, column, function, address) in self.traceback: - stub_globals = {"__name__": filename, "__loader__": source_loader} - source_line = linecache.getline(filename, line, stub_globals) - indentation = re.search(r"^\s*", source_line).end() - - if address is None: - formatted_address = "" - else: - formatted_address = " (RA=0x{:x})".format(address) - - filename = filename.replace(os.path.normpath(os.path.join(os.path.dirname(__file__), - "..")), "") - if column == -1: - lines.append(" File \"{file}\", line {line}, in {function}{address}". - format(file=filename, line=line, function=function, - address=formatted_address)) - lines.append(" {}".format(source_line.strip() if source_line else "")) - else: - lines.append(" File \"{file}\", line {line}, column {column}," - " in {function}{address}". - format(file=filename, line=line, column=column + 1, - function=function, address=formatted_address)) - lines.append(" {}".format(source_line.strip() if source_line else "")) - lines.append(" {}^".format(" " * (column - indentation))) - - lines.append("{}({}): {}".format(self.name, self.id, - self.message.format(*self.params))) - return "\n".join(lines)