From 70dd9687a9989801b6f1414b7dce43b20d12f837 Mon Sep 17 00:00:00 2001 From: Mike Naberezny Date: Wed, 7 Dec 2016 09:03:02 -0800 Subject: [PATCH] Catch exception from address overflow in "add_label" command --- py65/monitor.py | 13 +++++++++---- py65/tests/test_monitor.py | 20 ++++++++++++++++++-- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/py65/monitor.py b/py65/monitor.py index 9a8b255..5d2679c 100644 --- a/py65/monitor.py +++ b/py65/monitor.py @@ -759,10 +759,15 @@ class Monitor(cmd.Cmd): self._output("Syntax error: %s" % args) return self.help_add_label() - address = self._address_parser.number(split[0]) - label = split[1] - - self._address_parser.labels[label] = address + try: + address = self._address_parser.number(split[0]) + except KeyError as exc: + self._output(exc.args[0]) # "Label not found: foo" + except OverflowError: + self._output("Overflow error: %s" % args) + else: + label = split[1] + self._address_parser.labels[label] = address def help_show_labels(self): self._output("show_labels") diff --git a/py65/tests/test_monitor.py b/py65/tests/test_monitor.py index 6e18889..928c9d1 100644 --- a/py65/tests/test_monitor.py +++ b/py65/tests/test_monitor.py @@ -92,9 +92,25 @@ class MonitorTests(unittest.TestCase): def test_do_add_label_syntax_error(self): stdout = StringIO() mon = Monitor(stdout=stdout) - mon.do_add_label('should be label space value') + mon.do_add_label('should be address space label') out = stdout.getvalue() - err = "Syntax error: should be label space value\n" + err = "Syntax error: should be address space label\n" + self.assertTrue(out.startswith(err)) + + def test_do_add_label_overflow_error(self): + stdout = StringIO() + mon = Monitor(stdout=stdout) + mon.do_add_label('$10000 toobig') + out = stdout.getvalue() + err = "Overflow error: $10000 toobig\n" + self.assertTrue(out.startswith(err)) + + def test_do_add_label_label_error(self): + stdout = StringIO() + mon = Monitor(stdout=stdout) + mon.do_add_label('nonexistent foo') + out = stdout.getvalue() + err = "Label not found: nonexistent\n" self.assertTrue(out.startswith(err)) def test_do_add_label_adds_label(self):