Skip to content

Commit

Permalink
Show warnings evaluating in repl. Fixes #774
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Apr 23, 2022
1 parent 3d74999 commit eaadb6c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 36 deletions.
70 changes: 36 additions & 34 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 Expand Up @@ -1204,36 +1206,36 @@ def __create_frame():
_evaluate_response(py_db, request, result='', error_message='Thread id: %s is not current thread id.' % (thread_id,))
return

safe_repr_custom_attrs = {}
if context == 'clipboard':
safe_repr_custom_attrs = dict(
maxstring_outer=2 ** 64,
maxstring_inner=2 ** 64,
maxother_outer=2 ** 64,
maxother_inner=2 ** 64,
)
safe_repr_custom_attrs = {}
if context == 'clipboard':
safe_repr_custom_attrs = dict(
maxstring_outer=2 ** 64,
maxstring_inner=2 ** 64,
maxother_outer=2 ** 64,
maxother_inner=2 ** 64,
)

if context == 'repl' and eval_result is None:
# We don't want "None" to appear when typing in the repl.
body = pydevd_schema.EvaluateResponseBody(
result=None,
variablesReference=0,
)
if context == 'repl' and eval_result is None:
# We don't want "None" to appear when typing in the repl.
body = pydevd_schema.EvaluateResponseBody(
result=None,
variablesReference=0,
)

else:
variable = frame_tracker.obtain_as_variable(expression, eval_result, frame=frame)
var_data = variable.get_var_data(fmt=fmt, **safe_repr_custom_attrs)

body = pydevd_schema.EvaluateResponseBody(
result=var_data['value'],
variablesReference=var_data.get('variablesReference', 0),
type=var_data.get('type'),
presentationHint=var_data.get('presentationHint'),
namedVariables=var_data.get('namedVariables'),
indexedVariables=var_data.get('indexedVariables'),
)
variables_response = pydevd_base_schema.build_response(request, kwargs={'body':body})
py_db.writer.add_command(NetCommand(CMD_RETURN, 0, variables_response, is_json=True))
else:
variable = frame_tracker.obtain_as_variable(expression, eval_result, frame=frame)
var_data = variable.get_var_data(fmt=fmt, **safe_repr_custom_attrs)

body = pydevd_schema.EvaluateResponseBody(
result=var_data['value'],
variablesReference=var_data.get('variablesReference', 0),
type=var_data.get('type'),
presentationHint=var_data.get('presentationHint'),
namedVariables=var_data.get('namedVariables'),
indexedVariables=var_data.get('indexedVariables'),
)
variables_response = pydevd_base_schema.build_response(request, kwargs={'body':body})
py_db.writer.add_command(NetCommand(CMD_RETURN, 0, variables_response, is_json=True))


def _evaluate_response_return_exception(py_db, request, exc_type, exc, initial_tb):
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 eaadb6c

Please sign in to comment.