forked from M-Labs/artiq
1
0
Fork 0

language...ARTIQException -> coredevice...CoreException

gets rid of a cross import
is only used there
This commit is contained in:
Robert Jördens 2016-01-25 15:52:52 -07:00
parent 905063c1b1
commit 2beaf23e6c
4 changed files with 56 additions and 60 deletions

View File

@ -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):

View File

@ -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__),
"..")), "<artiq>")
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 "<unknown>"))
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 "<unknown>"))
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

View File

@ -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()

View File

@ -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__),
"..")), "<artiq>")
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 "<unknown>"))
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 "<unknown>"))
lines.append(" {}^".format(" " * (column - indentation)))
lines.append("{}({}): {}".format(self.name, self.id,
self.message.format(*self.params)))
return "\n".join(lines)