-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
py/modsys.c: Add sys._exc_traceback. #11244
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
base: master
Are you sure you want to change the base?
Conversation
2b52b5b to
e7a799e
Compare
|
Code size report: |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #11244 +/- ##
==========================================
- Coverage 98.38% 98.33% -0.05%
==========================================
Files 171 171
Lines 22298 22309 +11
==========================================
Hits 21937 21937
- Misses 361 372 +11 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
First thing to ask is how would one do this in CPython? Then we need to consider if it's worth the cost (code size) and whether it should be guarded by a config macro. I would say yes it should be, but then at what level would it be enabled, eg |
|
You can get this info in CPython is like this: import traceback
def foo():
1/0
try:
foo()
except Exception as e:
tb = e.__traceback__
while tb is not None:
print("====")
print(" filename: " + str(tb.tb_frame.f_code.co_filename))
print(" line: " + str(tb.tb_lineno))
print(" name: " + str(tb.tb_frame.f_code.co_name))
tb = tb.tb_nextThis involves at least three different classes:
The Python standard library also comes with a traceback module that introduces more classes. MicroPython implements a minimal traceback module. CPython does not have |
|
I changed it so that this function is only enabled by default if |
4b5c6c3 to
d872e6e
Compare
|
This is an automated heads-up that we've just merged a Pull Request See #13763 A search suggests this PR might apply the STATIC macro to some C code. If it Although this is an automated message, feel free to @-reply to me directly if |
This makes it easier to provide custom formatting of exception stack traces, e.g. to make them fit on a tiny display. This is an experimental function that exposes internal details of MicroPython and it might change in the future. Signed-off-by: David (Pololu) <dev-david@pololu.com>
d872e6e to
2775717
Compare
|
Thanks for keeping this PR alive. I can see how it would be useful. To reduce code size I suggest implementing the functionality as an attribute of the exception type, rather than as a new function. So it would go in import sys
def foo():
1/0
try:
foo()
except Exception as e:
sys.print_exception(e)
print(e.__traceback__)Now, that's not fully compatible with CPython, but it's a little bit closer than having a separate And then if you don't care about CPython compatibility and you want to efficiently get the traceback data, you can use the value of |
This new function makes it easier to provide custom formatting of exception stack traces, for example to make them fit on a tiny 16x8-character display. (Without this, I think you'd have to call
sys.print_exception, somehow get the output as a string, and then scrape information from that string.)I'm thinking of this as an experimental function that exposes internal details of MicroPython and therefore might change in the future. That's why it has the underscore in the name.
Here is an example of its output:
The regular output from
sys.print_exceptionfor the same exception is:Both outputs were generated by the following script:
Is this the right approach for getting info about exception stack traces? I'd be happy to change the name, change the output format, add tests, or add documentation.