Skip to content

Commit

Permalink
Show warnings evaluating in repl. Fixes microsoft#774
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Apr 22, 2022
1 parent 3d74999 commit 3a72e9c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
from _pydev_bundle._pydev_saved_modules import socket as socket_module
from _pydevd_bundle.pydevd_constants import (DebugInfoHolder, IS_WINDOWS, IS_JYTHON,
IS_PY36_OR_GREATER, STATE_RUN, ASYNC_EVAL_TIMEOUT_SEC,
get_global_debugger, GetGlobalDebugger, set_global_debugger, silence_warnings_decorator) # Keep for backward compatibility @UnusedImport
get_global_debugger, GetGlobalDebugger, set_global_debugger, # Keep for backward compatibility @UnusedImport
silence_warnings_decorator, filter_all_warnings)
from _pydev_bundle.pydev_override import overrides
import weakref
from _pydev_bundle._pydev_completer import extract_token_and_qualifier
Expand Down Expand Up @@ -1121,7 +1122,6 @@ def _evaluate_response(py_db, request, result, error_message=''):
_global_frame = None


@silence_warnings_decorator
def internal_evaluate_expression_json(py_db, request, thread_id):
'''
:param EvaluateRequest request:
Expand All @@ -1137,13 +1137,15 @@ def internal_evaluate_expression_json(py_db, request, thread_id):
if hasattr(fmt, 'to_dict'):
fmt = fmt.to_dict()

if context == 'repl' and not py_db.is_output_redirected:
ctx = pydevd_io.redirect_stream_to_pydb_io_messages_context()
ctx = NULL
if context == 'repl':
if not py_db.is_output_redirected:
ctx = pydevd_io.redirect_stream_to_pydb_io_messages_context()
else:
ctx = NULL
# If we're not in a repl (watch, hover, ...) don't show warnings.
ctx = filter_all_warnings()

with ctx:

try_exec = False
if frame_id is None:
if _global_frame is None:
Expand Down
11 changes: 9 additions & 2 deletions src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import struct
import warnings
import functools
from contextlib import contextmanager

STATE_RUN = 1
STATE_SUSPEND = 2
Expand Down Expand Up @@ -443,12 +444,18 @@ def as_str(s):
return s


@contextmanager
def filter_all_warnings():
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
yield


def silence_warnings_decorator(func):

@functools.wraps(func)
def new_func(*args, **kwargs):
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
with filter_all_warnings():
return func(*args, **kwargs)

return new_func
Expand Down
25 changes: 25 additions & 0 deletions src/debugpy/_vendored/pydevd/tests_python/test_debugger_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,31 @@ def test_getattr_warning(case_setup):
writer.finished_ok = True


def test_warning_on_repl(case_setup):

def additional_output_checks(writer, stdout, stderr):
assert "WarningCalledOnRepl" in stderr

with case_setup.test_file(
'_debugger_case_evaluate.py',
additional_output_checks=additional_output_checks
) as writer:
json_facade = JsonFacade(writer)

json_facade.write_set_breakpoints(writer.get_line_index_with_content('Break here'))
json_facade.write_make_initial_run()

json_hit = json_facade.wait_for_thread_stopped()

# We want warnings from the in evaluate in the repl (but not hover/watch).
json_facade.evaluate(
'import warnings; warnings.warn("WarningCalledOnRepl")', json_hit.frame_id, context='repl')

json_facade.write_continue()

writer.finished_ok = True


def test_evaluate_numpy(case_setup, pyfile):
try:
import numpy
Expand Down

0 comments on commit 3a72e9c

Please sign in to comment.