From 37c43abb2c26bcf9f6ab9043cbe77467c3ed2638 Mon Sep 17 00:00:00 2001 From: besfahbod <45701775+besfahbod@users.noreply.github.com> Date: Mon, 28 Jan 2019 11:38:43 -0800 Subject: [PATCH 1/2] pydbg.py: Use repr for actual exp value Use `__repr__()` of expression value, as it's the intended representation for debugging. For primitive types, this differentiates between `"True"` and `True`, and `"123"` and `123`, which can be crucial. And for custom types, there are many using `__str__()` as part of their API for to-string conversion. This will remove the type details needed for debugging. `__repr__()` is usually created to preserve such details. --- pydbg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydbg.py b/pydbg.py index 5cdc3eb..dc08654 100644 --- a/pydbg.py +++ b/pydbg.py @@ -28,7 +28,7 @@ def square(x: int) -> int: ctx = i.code_context[0] if "dbg" in ctx: print( - f"[{i.filename}:{i.lineno}] {ctx[ctx.find('(') + 1 : ctx.rfind(')')]} = {exp}" + f"[{i.filename}:{i.lineno}] {ctx[ctx.find('(') + 1 : ctx.rfind(')')]} = {exp!r}" ) break return exp From a7e8c45c855f7f37f35b60a0cd09153d302eb99b Mon Sep 17 00:00:00 2001 From: besfahbod <45701775+besfahbod@users.noreply.github.com> Date: Mon, 28 Jan 2019 11:49:44 -0800 Subject: [PATCH 2/2] Update test_pydbg.py --- tests/test_pydbg.py | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/tests/test_pydbg.py b/tests/test_pydbg.py index bf91ea4..c72d321 100644 --- a/tests/test_pydbg.py +++ b/tests/test_pydbg.py @@ -7,27 +7,40 @@ cwd = os.getcwd() def test_variables(): + NoneType = None + boolType = True intType = 2 - floatType = 2.1 + floatType = 3.4 strType = "mystring" - boolType = True - NoneType = None + strType1 = "None" + strType2 = "True" + strType3 = "2" + strType4 = "3.4" out = io.StringIO() with redirect_stdout(out): + dbg(NoneType) + dbg(boolType) dbg(intType) dbg(floatType) dbg(strType) - dbg(boolType) - dbg(NoneType) - dbg(add(1, 2)) + dbg(strType1) + dbg(strType2) + dbg(strType3) + dbg(strType4) + dbg(add(5, 6)) - want = f"""[{cwd}/tests/test_pydbg.py:18] intType = 2 -[{cwd}/tests/test_pydbg.py:19] floatType = 2.1 -[{cwd}/tests/test_pydbg.py:20] strType = mystring -[{cwd}/tests/test_pydbg.py:21] boolType = True + want = f"""\ [{cwd}/tests/test_pydbg.py:22] NoneType = None -[{cwd}/tests/test_pydbg.py:23] add(1, 2) = 3 +[{cwd}/tests/test_pydbg.py:23] boolType = True +[{cwd}/tests/test_pydbg.py:24] intType = 2 +[{cwd}/tests/test_pydbg.py:25] floatType = 3.4 +[{cwd}/tests/test_pydbg.py:26] strType = 'mystring' +[{cwd}/tests/test_pydbg.py:27] strType1 = 'None' +[{cwd}/tests/test_pydbg.py:28] strType2 = 'True' +[{cwd}/tests/test_pydbg.py:29] strType3 = '2' +[{cwd}/tests/test_pydbg.py:30] strType4 = '3.4' +[{cwd}/tests/test_pydbg.py:31] add(5, 6) = 11 """ assert out.getvalue() == want