From 2df8b946f9f3b8fecc5ec6b5affe31804fd0e06d Mon Sep 17 00:00:00 2001 From: whitequark Date: Mon, 31 Aug 2015 22:52:39 -0600 Subject: [PATCH] Factor out the code to pretty-print diagnostics. --- artiq/coredevice/core.py | 18 +++++++++++++++--- artiq/frontend/artiq_compile.py | 7 +++++-- artiq/frontend/artiq_run.py | 6 ++---- artiq/test/hardware_testbench.py | 9 +++++---- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/artiq/coredevice/core.py b/artiq/coredevice/core.py index afced23bf..ff8cc41cd 100644 --- a/artiq/coredevice/core.py +++ b/artiq/coredevice/core.py @@ -1,4 +1,4 @@ -import sys +import os from pythonparser import diagnostic @@ -14,7 +14,19 @@ from artiq.coredevice import exceptions class CompileError(Exception): - pass + def __init__(self, diagnostic): + self.diagnostic = diagnostic + + def render_string(self, colored=False): + def shorten_path(path): + return path.replace(os.path.normpath(os.path.join(__file__, "..", "..")), "") + lines = [shorten_path(path) for path in self.diagnostic.render(colored=colored)] + return "\n".join(lines) + + def __str__(self): + # Prepend a newline so that the message shows up on after + # exception class name printed by Python. + return "\n" + self.render_string(colored=True) @syscall @@ -48,7 +60,7 @@ class Core: return stitcher.object_map, stripped_library, \ lambda addresses: target.symbolize(library, addresses) except diagnostic.Error as error: - raise CompileError() from error + raise CompileError(error.diagnostic) from error def run(self, function, args, kwargs): object_map, kernel_library, symbolizer = self.compile(function, args, kwargs) diff --git a/artiq/frontend/artiq_compile.py b/artiq/frontend/artiq_compile.py index 9262edcb3..04ee22cca 100755 --- a/artiq/frontend/artiq_compile.py +++ b/artiq/frontend/artiq_compile.py @@ -1,10 +1,10 @@ #!/usr/bin/env python3 -import logging -import argparse +import sys, logging, argparse from artiq.protocols.file_db import FlatFileDB from artiq.master.worker_db import DeviceManager +from artiq.coredevice.core import CompileError from artiq.tools import * @@ -53,6 +53,9 @@ def main(): object_map, kernel_library, symbolizer = \ core.compile(exp.run, [exp_inst], {}, with_attr_writeback=False) + except CompileError as error: + print(error.render_string(colored=True), file=sys.stderr) + return finally: dmgr.close_devices() diff --git a/artiq/frontend/artiq_run.py b/artiq/frontend/artiq_run.py index 0943baf7c..e7fd06dc4 100755 --- a/artiq/frontend/artiq_run.py +++ b/artiq/frontend/artiq_run.py @@ -124,10 +124,8 @@ def run(with_file=False): exp_inst.run() exp_inst.analyze() except CompileError as error: - message = "\n".join(error.__cause__.diagnostic.render(colored=True)) - message = message.replace(os.path.normpath(os.path.join(os.path.dirname(__file__), "..")), - "") - print(message, file=sys.stderr) + print(error.render_string(colored=True), file=sys.stderr) + return finally: dmgr.close_devices() diff --git a/artiq/test/hardware_testbench.py b/artiq/test/hardware_testbench.py index c135bb696..ab34e46d6 100644 --- a/artiq/test/hardware_testbench.py +++ b/artiq/test/hardware_testbench.py @@ -1,9 +1,7 @@ -import os -import sys -import unittest -import logging +import os, sys, unittest, logging from artiq.language import * +from artiq.coredevice.core import CompileError from artiq.protocols.file_db import FlatFileDB from artiq.master.worker_db import DeviceManager, ResultDB from artiq.frontend.artiq_run import DummyScheduler @@ -58,5 +56,8 @@ class ExperimentCase(unittest.TestCase): exp.run() exp.analyze() return exp + except CompileError as error: + # Reduce amount of text on terminal. + raise error from None finally: self.dmgr.close_devices()