From b8fafdbe7d30917b52bc7326cdd8abec229bba2a Mon Sep 17 00:00:00 2001 From: Andrey Lisin Date: Wed, 20 Feb 2019 17:13:37 +0300 Subject: [PATCH] PY-34144 Suppress warnings while resolving variables --- .../pydev/_pydevd_bundle/pydevd_resolver.py | 15 +++++++++-- .../debug/test_warnings_suppressing.py | 16 ++++++++++++ .../env/python/PythonDebuggerTest.java | 26 ++++++++++++++++++- 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 python/testData/debug/test_warnings_suppressing.py diff --git a/python/helpers/pydev/_pydevd_bundle/pydevd_resolver.py b/python/helpers/pydev/_pydevd_bundle/pydevd_resolver.py index f48b17457c02..4ad3806122ce 100644 --- a/python/helpers/pydev/_pydevd_bundle/pydevd_resolver.py +++ b/python/helpers/pydev/_pydevd_bundle/pydevd_resolver.py @@ -3,6 +3,8 @@ try: except: import io as StringIO import traceback +import warnings +from contextlib import contextmanager from os.path import basename from _pydevd_bundle import pydevd_constants @@ -55,6 +57,13 @@ except: MethodWrapperType = None +@contextmanager +def suppress_warnings(): + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + yield + + #======================================================================================================================= # See: pydevd_extension_api module for resolver interface #======================================================================================================================= @@ -69,7 +78,8 @@ class DefaultResolver: ''' def resolve(self, var, attribute): - return getattr(var, attribute) + with suppress_warnings(): + return getattr(var, attribute) def get_dictionary(self, var, names=None): if MethodWrapperType: @@ -164,7 +174,8 @@ class DefaultResolver: continue try: - attr = getattr(var, n) + with suppress_warnings(): + attr = getattr(var, n) #filter builtins? if filterBuiltIn: diff --git a/python/testData/debug/test_warnings_suppressing.py b/python/testData/debug/test_warnings_suppressing.py new file mode 100644 index 000000000000..86e3d4a04b21 --- /dev/null +++ b/python/testData/debug/test_warnings_suppressing.py @@ -0,0 +1,16 @@ +import warnings + + +class ClassWithDeprecatedProperty: + @property + def x(self): + warnings.warn("This property is deprecated!", DeprecationWarning) + return 42 + + +obj = ClassWithDeprecatedProperty() + +warnings.warn("This warning should appear in the output.") + +print(obj.x) +print(obj) diff --git a/python/testSrc/com/jetbrains/env/python/PythonDebuggerTest.java b/python/testSrc/com/jetbrains/env/python/PythonDebuggerTest.java index 9fbaf748412d..918a46940119 100644 --- a/python/testSrc/com/jetbrains/env/python/PythonDebuggerTest.java +++ b/python/testSrc/com/jetbrains/env/python/PythonDebuggerTest.java @@ -1875,5 +1875,29 @@ public class PythonDebuggerTest extends PyEnvTestCase { } }); } -} + @Test + public void testWarningsSuppressing() { + runPythonTest(new PyDebuggerTask("/debug", "test_warnings_suppressing.py") { + @Override + public void before() { + toggleBreakpoint(getFilePath(getScriptName()), 14); + } + + @Override + public void testing() throws Exception { + waitForPause(); + List frameVariables = loadFrame(); + PyDebugValue obj = findDebugValueByName(frameVariables, "obj"); + loadVariable(obj); + String out = output(); + assertTrue(out.contains("This warning should appear in the output.")); + assertFalse(out.contains("This property is deprecated!")); + toggleBreakpoint(getFilePath(getScriptName()), 15); + resume(); + waitForPause(); + waitForOutput("This property is deprecated!"); + } + }); + } +}