Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tools: make Python import error more readable #14154

Merged
merged 1 commit into from
Feb 14, 2020
Merged

Conversation

julianoes
Copy link
Contributor

@julianoes julianoes commented Feb 13, 2020

The problem with printing the exception was that starting with Python 3.6 the ImportError is yet another (sub) exception called ModuleNotFoundError which can't be printed as a string and then triggers
another exception:

Traceback (most recent call last):
  File "/home/julianoes/src/Firmware/Tools/serial/generate_config.py", line 11, in <module>
    import jinja2
ModuleNotFoundError: No module named 'jinja2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/julianoes/src/Firmware/Tools/serial/generate_config.py", line 13, in <module>
    print("Failed to import jinja2: " + e)
TypeError: must be str, not ModuleNotFoundError

As per @bkueng's suggestion the easiest is to cast the exception to str and that way prevent the second exception.

@julianoes julianoes requested a review from TSC21 February 13, 2020 14:41
TSC21
TSC21 previously approved these changes Feb 13, 2020
Copy link
Member

@bkueng bkueng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explicitly cast to string? Otherwise it might hide problems.

Tools/mavlink_shell.py Outdated Show resolved Hide resolved
The problem with printing the exception was that starting with
Python 3.6 the ImportError is yet another (sub) exception called
ModuleNotFoundError which can't be printed as a string and then triggers
another exception:

```
Traceback (most recent call last):
  File "/home/julianoes/src/Firmware/Tools/serial/generate_config.py", line 11, in <module>
    import jinja2
ModuleNotFoundError: No module named 'jinja2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/julianoes/src/Firmware/Tools/serial/generate_config.py", line 13, in <module>
    print("Failed to import jinja2: " + e)
TypeError: must be str, not ModuleNotFoundError
```

As per @bkueng's suggestion the easiest is to cast the exception to str
and that way prevent the second exception.
@bkueng bkueng merged commit 9a96ca1 into master Feb 14, 2020
@bkueng bkueng deleted the pr-fix-python-imports branch February 14, 2020 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants