2
0
mirror of https://github.com/m-labs/artiq.git synced 2024-12-19 00:16:29 +08:00

Factor out the code to pretty-print diagnostics.

This commit is contained in:
whitequark 2015-08-31 22:52:39 -06:00
parent b03efbc94d
commit 2df8b946f9
4 changed files with 27 additions and 13 deletions

View File

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

View File

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

View File

@ -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__), "..")),
"<artiq>")
print(message, file=sys.stderr)
print(error.render_string(colored=True), file=sys.stderr)
return
finally:
dmgr.close_devices()

View File

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