Skip to content

Python debugger breaks on caught exception within a decorator and context manager #1155

@damien135

Description

@damien135

Type: Bug

Behaviour

Expected vs. Actual

Expected behavior: If breakpoints are enabled only for Unchaught Exceptions and User Uncaught Exceptions, the debugger should not break in the code sample below.

Actual behavior: The debugger breaks on line 19.

Steps to reproduce:

  1. Open VS code
  2. Run the code sample below (integrated terminal):
from contextlib import contextmanager
from functools import wraps
def some_decorator(parameter):
    def generic_wrapper(func):
        @wraps(func)
        def wrapper(self, *args, **kwargs):
            with self.some_context_manager(parameter):
                return func(self, *args, **kwargs)
        return wrapper
    return generic_wrapper

class ExampleClass:
    @some_decorator('some value')
    def get_property_value(self):
        return {}

    @contextmanager
    def some_context_manager(self, parameter):
        raise Exception("User uncaught exception!")

example = ExampleClass()
try:
    test = example.get_property_value()
except Exception as ex:
    print("Exception successfully caught!")

Diagnostic data

  • Python version (& distribution if applicable, e.g. Anaconda): 3.10.6
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Global
  • Value of the python.languageServer setting: Default
Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

Exception has occurred: Exception       (note: full exception trace is shown but execution is paused at: some_context_manager)
User uncaught exception!
  File "...\TestException.py", line 19, in some_context_manager (Current frame)
    raise Exception("User uncaught exception!")
  File "...\TestException.py", line 7, in wrapper
    with self.some_context_manager(parameter):
  File "...\TestException.py", line 23, in <module>
    test = example.get_property_value()

(paths edited for privacy)

User Settings


languageServer: "Jedi"

Extension version: 2022.12.1
VS Code version: Code 1.70.1 (6d9b74a70ca9c7733b29f0456fd8195364076dda, 2022-08-10T06:08:33.642Z)
OS version: Windows_NT x64 10.0.19044
Modes:

System Info
Item Value
CPUs Intel(R) Core(TM) i7-10610U CPU @ 1.80GHz (8 x 2304)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_renderer: enabled_on
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
Load (avg) undefined
Memory (System) 15.73GB (7.64GB free)
Process Argv
Screen Reader no
VM 0%

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions