PY-35529 Use repr by default in Variables View

1. If `__repr__` is overridden - use `reprlib` or `repr()`
2. If `__str__` is overridden - use `str()`
3. Use `reprlib` or `repr()` otherwise

IJ-CR-110917

GitOrigin-RevId: dff583d556600a8c77024a4d1bd86034f24f9bf9
This commit is contained in:
Egor.Eliseev
2023-07-18 08:33:05 +00:00
committed by intellij-monorepo-bot
parent 07a7de0be2
commit ebf5d71eef
3 changed files with 59 additions and 3 deletions
@@ -166,10 +166,15 @@ if IS_PY3K:
if result is not None:
return result
# if `__str__` method is overridden then return str(x)
if x.__str__ != object.__str__:
# if `__repr__` is overridden, then use `reprlib`
if x.__class__.__repr__ != object.__repr__:
return super().repr_instance(x, level)
# if `__str__` is overridden, then return str(x)
if x.__class__.__str__ != object.__str__:
return str(x)
# else use `reprlib`
return super().repr_instance(x, level)
@@ -188,7 +193,11 @@ else:
return ('%s' % take_first_n_coll_elements(value, MAX_REPR_ITEM_SIZE)).rstrip(')]}') + '...'
return None
# other types
# if `__repr__` is overridden, then return repr(value)
if hasattr(value.__class__, "__repr__"):
return repr(value)
# else
return str(value)
@@ -0,0 +1,18 @@
class FooStr:
def __str__(self):
return "str"
class FooRepr:
def __repr__(self):
return "repr"
class FooReprlib:
pass
foo_str = FooStr()
foo_repr = FooRepr()
foo_reprlib = FooReprlib()
print()
@@ -15,6 +15,7 @@ import com.jetbrains.env.EnvTestTagsRequired;
import com.jetbrains.env.PyEnvTestCase;
import com.jetbrains.python.console.pydev.PydevCompletionVariant;
import com.jetbrains.python.debugger.PyDebugValue;
import com.jetbrains.python.debugger.PyDebuggerException;
import com.jetbrains.python.debugger.PyExceptionBreakpointProperties;
import com.jetbrains.python.debugger.PyExceptionBreakpointType;
import com.jetbrains.python.debugger.pydev.ProcessDebugger;
@@ -1709,6 +1710,34 @@ public class PythonDebuggerTest extends PyEnvTestCase {
});
}
@Test
public void testStringRepresentationInVariablesView() {
runPythonTest(new PyDebuggerTask("/debug", "test_string_representation_in_variables_view.py") {
@Override
public void before() {
toggleBreakpoint(getFilePath(getScriptName()), 17);
}
@Override
public void testing() throws Exception {
waitForPause();
List<PyDebugValue> frameVariables = loadFrame();
checkVariableValue(frameVariables, "str", "foo_str");
checkVariableValue(frameVariables, "repr", "foo_repr");
String expected = eval("repr(foo_reprlib)").getValue().replaceAll("[\"']", "");
checkVariableValue(frameVariables, expected, "foo_reprlib");
resume();
waitForTerminate();
}
private void checkVariableValue(List<PyDebugValue> frameVariables, String expected, String name) throws PyDebuggerException {
PyDebugValue value = findDebugValueByName(frameVariables, name);
loadVariable(value);
assertEquals(expected, value.getValue());
}
});
}
private static class PyDebuggerTaskTagAware extends PyDebuggerTask {
private PyDebuggerTaskTagAware(@Nullable String relativeTestDataPath, String scriptName) {