+
Skip to content

Differences in handling exceptions? #24

Open
@hydrargyrum

Description

@hydrargyrum

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载