From e1ced285a0e6dd84dfc4456a4348d5fae546f8a7 Mon Sep 17 00:00:00 2001
From: Guillaume Chanel <Guillaume.Chanel@unige.ch>
Date: Fri, 21 Oct 2022 11:12:47 +0200
Subject: [PATCH] Add line number of the exception

---
 test/test.py | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/test/test.py b/test/test.py
index 8a6996e..154fda7 100644
--- a/test/test.py
+++ b/test/test.py
@@ -24,6 +24,19 @@ def print_usage():
     print("                         if omitted, the script attempt to find it automatically")
 
 
+def find_last_trace(trace, f):
+    """Follow a trace to find the last onebut still belonging to file f
+    trace: a starting trace
+    f: a file as a full absolute path
+    """
+    if trace.tb_next is None:
+        return trace
+    elif trace.tb_next.tb_frame.f_code.co_filename != f:
+        return trace
+    else:
+        return find_last_trace(trace.tb_next, f)
+
+
 # TODO: move in test class (failed so far)
 test_failed = False
 def test(func):
@@ -34,7 +47,9 @@ def test(func):
             func(self)
         except Exception as e:
             print(Fore.RED + "FAILED" + Fore.RESET)
-            logging.error(f"{type(e).__name__}:{str(e)}")
+            _, _, tb = sys.exc_info()
+            tb = find_last_trace(tb, tb.tb_frame.f_code.co_filename)
+            logging.error(f"{type(e).__name__}:{str(e)} [TeacherCode:{tb.tb_lineno}]")
             test_failed = True
             return
         print(Fore.GREEN + "SUCCESS" + Fore.RESET)
-- 
GitLab