diff mbox series

[2/2] utils/genrandconfig: dump traceback for unhandled exceptions

Message ID e8e8f938d57fc7afb0e6e4a6a971eca7896b7387.1661071280.git.yann.morin.1998@free.fr
State Accepted
Headers show
Series utils/genrandconfig: fix host-glibc version check (branch yem/gerandconfig-fixups) | expand

Commit Message

Yann E. MORIN Aug. 21, 2022, 8:41 a.m. UTC
In case of an unexpected error, we currently only print the exception as
an str(). For example, the recent issue with the glibc version check
only reported:
    TypeError: cannot use a string pattern on a bytes-like object

That does not help in fixing the issue; the exception text is also not
usually very user-friendly either anyway.

We change the reporting to print the traceback, which in the glibc
version check mentioned above, the error is reported as:

    Traceback (most recent call last):
      File "./utils/genrandconfig", line 740, in <module>
        ret = gen_config(args)
      File "./utils/genrandconfig", line 676, in gen_config
        if not is_toolchain_usable(configfile, toolchainconfig):
      File "./utils/genrandconfig", line 186, in is_toolchain_usable
        if StrictVersion('2.14') > StrictVersion(glibc_version):
      File "/usr/lib/python3.8/distutils/version.py", line 40, in __init__
        self.parse(vstring)
      File "/usr/lib/python3.8/distutils/version.py", line 135, in parse
        match = self.version_re.match(vstring)
    TypeError: cannot use a string pattern on a bytes-like object

With this, the error is much easier to pinpoint (it's the last one that
is not in a system module).

Signed-off-by: Yann E. MORIN <yann.morin.1998@free.fr>
---
 utils/genrandconfig | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Peter Korsgaard Sept. 17, 2022, 11:06 a.m. UTC | #1
>>>>> "Yann" == Yann E MORIN <yann.morin.1998@free.fr> writes:

 > In case of an unexpected error, we currently only print the exception as
 > an str(). For example, the recent issue with the glibc version check
 > only reported:
 >     TypeError: cannot use a string pattern on a bytes-like object

 > That does not help in fixing the issue; the exception text is also not
 > usually very user-friendly either anyway.

 > We change the reporting to print the traceback, which in the glibc
 > version check mentioned above, the error is reported as:

 >     Traceback (most recent call last):
 >       File "./utils/genrandconfig", line 740, in <module>
 >         ret = gen_config(args)
 >       File "./utils/genrandconfig", line 676, in gen_config
 >         if not is_toolchain_usable(configfile, toolchainconfig):
 >       File "./utils/genrandconfig", line 186, in is_toolchain_usable
 >         if StrictVersion('2.14') > StrictVersion(glibc_version):
 >       File "/usr/lib/python3.8/distutils/version.py", line 40, in __init__
 >         self.parse(vstring)
 >       File "/usr/lib/python3.8/distutils/version.py", line 135, in parse
 >         match = self.version_re.match(vstring)
 >     TypeError: cannot use a string pattern on a bytes-like object

 > With this, the error is much easier to pinpoint (it's the last one that
 > is not in a system module).

 > Signed-off-by: Yann E. MORIN <yann.morin.1998@free.fr>

Committed to 2022.05.x and 2022.02.x, thanks.
diff mbox series

Patch

diff --git a/utils/genrandconfig b/utils/genrandconfig
index 21d4592049..4c00d67691 100755
--- a/utils/genrandconfig
+++ b/utils/genrandconfig
@@ -25,6 +25,7 @@  import os
 from random import randint
 import subprocess
 import sys
+import traceback
 from distutils.version import StrictVersion
 import platform
 
@@ -737,7 +738,7 @@  if __name__ == '__main__':
 
     try:
         ret = gen_config(args)
-    except Exception as e:
-        print(str(e), file=sys.stderr)
+    except Exception:
+        traceback.print_exc()
         parser.exit(1)
     parser.exit(ret)