forked from M-Labs/artiq
1
0
Fork 0

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 from pythonparser import diagnostic
@ -14,7 +14,19 @@ from artiq.coredevice import exceptions
class CompileError(Exception): 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 @syscall
@ -48,7 +60,7 @@ class Core:
return stitcher.object_map, stripped_library, \ return stitcher.object_map, stripped_library, \
lambda addresses: target.symbolize(library, addresses) lambda addresses: target.symbolize(library, addresses)
except diagnostic.Error as error: except diagnostic.Error as error:
raise CompileError() from error raise CompileError(error.diagnostic) from error
def run(self, function, args, kwargs): def run(self, function, args, kwargs):
object_map, kernel_library, symbolizer = self.compile(function, args, kwargs) object_map, kernel_library, symbolizer = self.compile(function, args, kwargs)

View File

@ -1,10 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import logging import sys, logging, argparse
import argparse
from artiq.protocols.file_db import FlatFileDB from artiq.protocols.file_db import FlatFileDB
from artiq.master.worker_db import DeviceManager from artiq.master.worker_db import DeviceManager
from artiq.coredevice.core import CompileError
from artiq.tools import * from artiq.tools import *
@ -53,6 +53,9 @@ def main():
object_map, kernel_library, symbolizer = \ object_map, kernel_library, symbolizer = \
core.compile(exp.run, [exp_inst], {}, core.compile(exp.run, [exp_inst], {},
with_attr_writeback=False) with_attr_writeback=False)
except CompileError as error:
print(error.render_string(colored=True), file=sys.stderr)
return
finally: finally:
dmgr.close_devices() dmgr.close_devices()

View File

@ -124,10 +124,8 @@ def run(with_file=False):
exp_inst.run() exp_inst.run()
exp_inst.analyze() exp_inst.analyze()
except CompileError as error: except CompileError as error:
message = "\n".join(error.__cause__.diagnostic.render(colored=True)) print(error.render_string(colored=True), file=sys.stderr)
message = message.replace(os.path.normpath(os.path.join(os.path.dirname(__file__), "..")), return
"<artiq>")
print(message, file=sys.stderr)
finally: finally:
dmgr.close_devices() dmgr.close_devices()

View File

@ -1,9 +1,7 @@
import os import os, sys, unittest, logging
import sys
import unittest
import logging
from artiq.language import * from artiq.language import *
from artiq.coredevice.core import CompileError
from artiq.protocols.file_db import FlatFileDB from artiq.protocols.file_db import FlatFileDB
from artiq.master.worker_db import DeviceManager, ResultDB from artiq.master.worker_db import DeviceManager, ResultDB
from artiq.frontend.artiq_run import DummyScheduler from artiq.frontend.artiq_run import DummyScheduler
@ -58,5 +56,8 @@ class ExperimentCase(unittest.TestCase):
exp.run() exp.run()
exp.analyze() exp.analyze()
return exp return exp
except CompileError as error:
# Reduce amount of text on terminal.
raise error from None
finally: finally:
self.dmgr.close_devices() self.dmgr.close_devices()