Skip to content

Differences in handling exceptions? #24

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

Open
hydrargyrum opened this issue Mar 18, 2022 · 2 comments
Open

Differences in handling exceptions? #24

hydrargyrum opened this issue Mar 18, 2022 · 2 comments

Comments

@hydrargyrum
Copy link

I observe differences when handling different kind of exceptions
In an interactive interpreter:

>>> import stack_data.formatting
>>> stack_data.formatting.Formatter(show_variables=True).set_hook()

A simple exception:

>>> a=1
>>> a/0
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero

(By the way the variable "a" is not shown)

Another kind of exception:

>>> 1+
Traceback (most recent call last):
 File "/tmp/plop-2681655/lib/python3.9/site-packages/stack_data/formatting.py", line 67, in Formatter.set_hook.<locals>.excepthook
      66 | def excepthook(_etype, evalue, _tb):
-->   67 |     self.print_exception(evalue)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_etype = <class 'SyntaxError'>
_tb = None
evalue = SyntaxError('invalid syntax', ('<stdin>', 1, 3, '1+\n'))
self = <stack_data.formatting.Formatter object at 0x7f1f5b5f1eb0>
 File "/tmp/plop-2681655/lib/python3.9/site-packages/stack_data/formatting.py", line 72, in Formatter.print_exception
      71 | def print_exception(self, e=None, *, file=None):
-->   72 |     self.print_lines(self.format_exception(e), file=file)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
e = SyntaxError('invalid syntax', ('<stdin>', 1, 3, '1+\n'))
file = None
self = <stack_data.formatting.Formatter object at 0x7f1f5b5f1eb0>
 File "/tmp/plop-2681655/lib/python3.9/site-packages/stack_data/formatting.py", line 83, in Formatter.print_lines
      80 | def print_lines(self, lines, *, file=None):
      81 |     if file is None:
      82 |         file = sys.stderr
-->   83 |     for line in lines:
      84 |         print(line, file=file, end="")
file = <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>
file is None = False
line = '      84 |         print(line, file=file, end="")\n'
lines = <generator object Formatter.format_exception at 0x7f1f5b5f9040>
self = <stack_data.formatting.Formatter object at 0x7f1f5b5f1eb0>
sys.stderr = <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>
 File "/tmp/plop-2681655/lib/python3.9/site-packages/stack_data/formatting.py", line 100, in Formatter.format_exception
      96 |         yield from self.format_exception(e.__context__)
      97 |         yield traceback._context_message
      99 | yield 'Traceback (most recent call last):\n'
-->  100 | yield from self.format_stack(e.__traceback__)
     101 | yield from traceback.format_exception_only(type(e), e)
e = SyntaxError('invalid syntax', ('<stdin>', 1, 3, '1+\n'))
e is None = False
e.__suppress_context__ = False
not e.__suppress_context__ = True
self = <stack_data.formatting.Formatter object at 0x7f1f5b5f1eb0>
self.chain = True
traceback._cause_message = '\nThe above exception was the direct cause of the following exception:\n\n'
traceback._context_message = '\nDuring handling of the above exception, another exception occurred:\n\n'
type(e) = <class 'SyntaxError'>
  File "<stdin>", line 1
    1+
      ^
SyntaxError: invalid syntax
@alexmojaki
Copy link
Owner

The normal interactive interpreter doesn't give access to source code, so neither stack_data nor normal tracebacks work well.

The behaviour for a SyntaxError is surprising, I don't know why it's doing that. But it still only seems to happen in the shell which just isn't supported. Running a script like this doesn't show anything similar:

import stack_data.formatting
stack_data.formatting.Formatter(show_variables=True).set_hook()
eval("1+")

@hydrargyrum
Copy link
Author

hydrargyrum commented Mar 18, 2022

By the way, pressing Ctrl-C in the interactive interpreter shows the same kind of full-backtrace as with the SyntaxError.

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

No branches or pull requests

2 participants