← index #4667PR #419
Off-topic · high · value 2.014
QUERY · ISSUE

Print traceback without an exception

openby mzakharocscopened 2019-04-02updated 2019-05-14

Hello, in CPython, there is a way to print a traceback outside of an exception, through traceback.print_stack() function. What would it take to add similar functionality to micropython?

CANDIDATE · PULL REQUEST

- show traceback when test failed

openby mahoopened 2021-04-24updated 2021-07-26

i was surprised that exception in tested function is just silently redirected to nowhere.

I think it's good to show the traceback.

6 comments
stinos · 2021-04-25

Is this the same as what CPython does?

maho · 2021-04-25

Let's check:

test_ee.py

import unittest

class MyTestCase(unittest.TestCase):

    def _fn2(self):
        raise Exception("this shouldn't happend, but it did")

    def _fn(self):
        return self._fn2()


    def test_exception(self):
        self._fn()

if __name__ == '__main__':
    unittest.main()
       

micropython:

% micropython test_ee.py                                                                                                                                                                  :( 1 21-04-25 - 17:39:17
test_exception (MyTestCase) ... FAIL
Traceback (most recent call last):
  File "/home/maho/workspace/servo/dispenser/.micropython-lib/unittest/unittest.py", line 201, in run_class
  File "test_ee.py", line 13, in test_exception
  File "test_ee.py", line 9, in _fn
  File "test_ee.py", line 6, in _fn2
Exception: this shouldn't happend, but it did
Ran 1 tests

FAILED (failures=1, errors=0)

CPython 3.8:

% python3 /tmp/test_ee.py                                                                                                                                                                 :( 1 21-04-25 - 17:39:23
E
======================================================================
ERROR: test_exception (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test_ee.py", line 13, in test_exception
    self._fn()
  File "/tmp/test_ee.py", line 9, in _fn
    return self._fn2()
  File "/tmp/test_ee.py", line 6, in _fn2
    raise Exception("this shouldn't happend, but it did")
Exception: this shouldn't happend, but it did

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

So this change makes it closer to behaviour of CPython.

stinos · 2021-04-26

Makes sense. Adding a newline would be nice though because raising multiple exceptions results in text which gets hard to read.

maho · 2021-06-02

should I do any more changes or it's ok?

maho · 2021-07-26

@stinos? Anyone?

stinos · 2021-07-26

I think this is ok, but it's not up to me to merge it :)

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied