From c5eab53a8750070ee36b15bd46d9b5955ecd66c1 Mon Sep 17 00:00:00 2001 From: Akhil kp Date: Sun, 19 Nov 2017 18:20:58 +0530 Subject: [PATCH 1/6] Added --file for :debug-pyeval --- qutebrowser/misc/utilcmds.py | 28 ++++++++++++++++++----- tests/end2end/data/misc/pyeval_file.py | 2 ++ tests/end2end/features/qutescheme.feature | 9 ++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 tests/end2end/data/misc/pyeval_file.py diff --git a/qutebrowser/misc/utilcmds.py b/qutebrowser/misc/utilcmds.py index 386282a25..27365011d 100644 --- a/qutebrowser/misc/utilcmds.py +++ b/qutebrowser/misc/utilcmds.py @@ -229,18 +229,34 @@ def debug_trace(expr=""): @cmdutils.register(maxsplit=0, debug=True, no_cmd_split=True) -def debug_pyeval(s, quiet=False): +def debug_pyeval(s, file=False, quiet=False): """Evaluate a python string and display the results as a web page. Args: s: The string to evaluate. + file: Interpret s as a path to file. quiet: Don't show the output in a new tab. """ - try: - r = eval(s) - out = repr(r) - except Exception: - out = traceback.format_exc() + if file: + quiet = True + path = os.path.expanduser(s) + message.info(path) + try: + with open(path, 'r', encoding='utf-8') as f: + s = f.read() + except OSError as e: + raise cmdexc.CommandError(str(e)) + try: + exec(s) + out = "No Error" + except Exception: + out = traceback.format_exc() + else: + try: + r = eval(s) + out = repr(r) + except Exception: + out = traceback.format_exc() qutescheme.pyeval_output = out if quiet: diff --git a/tests/end2end/data/misc/pyeval_file.py b/tests/end2end/data/misc/pyeval_file.py new file mode 100644 index 000000000..092ae2a54 --- /dev/null +++ b/tests/end2end/data/misc/pyeval_file.py @@ -0,0 +1,2 @@ +from qutebrowser.utils import message +message.info("Hello world") diff --git a/tests/end2end/features/qutescheme.feature b/tests/end2end/features/qutescheme.feature index e4ae20215..3b8e10822 100644 --- a/tests/end2end/features/qutescheme.feature +++ b/tests/end2end/features/qutescheme.feature @@ -171,6 +171,15 @@ Feature: Special qute:// pages And I wait until qute://pyeval/ is loaded Then the page should contain the plaintext "ZeroDivisionError" + Scenario: Running :pyveal with --file using a file that exists aspython code + When I run :debug-pyeval --file (testdata)/misc/pyeval_file.py + Then the message "Hello World" should be shown + And "pyeval output: No error" should be logged + + Scenario: Running :pyeval --file using a non existing file + When I run :debug-pyeval --file nonexestentfile + Then the error "[Errno 2] No such file or directory: 'nonexistentfile'" should be shown + Scenario: Running :pyeval with --quiet When I run :debug-pyeval --quiet 1+1 Then "pyeval output: 2" should be logged From ba6d90aa7a07e3d1539f3003c1058186df0d2a5f Mon Sep 17 00:00:00 2001 From: akhilkpdasan Date: Sun, 19 Nov 2017 19:08:52 +0530 Subject: [PATCH 2/6] fixed docmentation for pyeval --- qutebrowser/misc/utilcmds.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/qutebrowser/misc/utilcmds.py b/qutebrowser/misc/utilcmds.py index 27365011d..6f6854e2a 100644 --- a/qutebrowser/misc/utilcmds.py +++ b/qutebrowser/misc/utilcmds.py @@ -234,13 +234,12 @@ def debug_pyeval(s, file=False, quiet=False): Args: s: The string to evaluate. - file: Interpret s as a path to file. + file: Interpret s as a path to file also implies --quiete. quiet: Don't show the output in a new tab. """ if file: quiet = True path = os.path.expanduser(s) - message.info(path) try: with open(path, 'r', encoding='utf-8') as f: s = f.read() From 4644642c38dc03f93d56a161db612e5cb06f26ea Mon Sep 17 00:00:00 2001 From: akhilkpdasan Date: Sun, 19 Nov 2017 19:10:36 +0530 Subject: [PATCH 3/6] fixed test for pyeval --file --- tests/end2end/features/qutescheme.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/end2end/features/qutescheme.feature b/tests/end2end/features/qutescheme.feature index 3b8e10822..b7a2e498b 100644 --- a/tests/end2end/features/qutescheme.feature +++ b/tests/end2end/features/qutescheme.feature @@ -171,7 +171,7 @@ Feature: Special qute:// pages And I wait until qute://pyeval/ is loaded Then the page should contain the plaintext "ZeroDivisionError" - Scenario: Running :pyveal with --file using a file that exists aspython code + Scenario: Running :pyveal with --file using a file that exists as python code When I run :debug-pyeval --file (testdata)/misc/pyeval_file.py Then the message "Hello World" should be shown And "pyeval output: No error" should be logged From 9a58fe229ca5b07ea1ff8d1332843f3ec6508115 Mon Sep 17 00:00:00 2001 From: akhilkpdasan Date: Sun, 19 Nov 2017 19:32:24 +0530 Subject: [PATCH 4/6] fixed spelling error --- qutebrowser/misc/utilcmds.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qutebrowser/misc/utilcmds.py b/qutebrowser/misc/utilcmds.py index 6f6854e2a..6f01686bf 100644 --- a/qutebrowser/misc/utilcmds.py +++ b/qutebrowser/misc/utilcmds.py @@ -234,7 +234,7 @@ def debug_pyeval(s, file=False, quiet=False): Args: s: The string to evaluate. - file: Interpret s as a path to file also implies --quiete. + file: Interpret s as a path to file, also implies --quiet. quiet: Don't show the output in a new tab. """ if file: From 21e731ebeb4c55c5ff96dd0a0df9d540eaef7d3d Mon Sep 17 00:00:00 2001 From: Akhil kp Date: Sun, 19 Nov 2017 23:49:11 +0530 Subject: [PATCH 5/6] fixed build errors(typing errors) --- tests/end2end/data/misc/pyeval_file.py | 6 +++++- tests/end2end/features/qutescheme.feature | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/end2end/data/misc/pyeval_file.py b/tests/end2end/data/misc/pyeval_file.py index 092ae2a54..27764eb20 100644 --- a/tests/end2end/data/misc/pyeval_file.py +++ b/tests/end2end/data/misc/pyeval_file.py @@ -1,2 +1,6 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +"""Simple test file for :debug-pyeval.""" + from qutebrowser.utils import message -message.info("Hello world") +message.info("Hello World") diff --git a/tests/end2end/features/qutescheme.feature b/tests/end2end/features/qutescheme.feature index b7a2e498b..8dd1a6edd 100644 --- a/tests/end2end/features/qutescheme.feature +++ b/tests/end2end/features/qutescheme.feature @@ -177,7 +177,7 @@ Feature: Special qute:// pages And "pyeval output: No error" should be logged Scenario: Running :pyeval --file using a non existing file - When I run :debug-pyeval --file nonexestentfile + When I run :debug-pyeval --file nonexistentfile Then the error "[Errno 2] No such file or directory: 'nonexistentfile'" should be shown Scenario: Running :pyeval with --quiet From 740d629b36566074c45663de4ac0b6e06d9f1c01 Mon Sep 17 00:00:00 2001 From: akhilkpdasan Date: Mon, 20 Nov 2017 00:56:09 +0530 Subject: [PATCH 6/6] Update utilcmds.py --- qutebrowser/misc/utilcmds.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qutebrowser/misc/utilcmds.py b/qutebrowser/misc/utilcmds.py index 6f01686bf..4054f85bb 100644 --- a/qutebrowser/misc/utilcmds.py +++ b/qutebrowser/misc/utilcmds.py @@ -247,7 +247,7 @@ def debug_pyeval(s, file=False, quiet=False): raise cmdexc.CommandError(str(e)) try: exec(s) - out = "No Error" + out = "No error" except Exception: out = traceback.format_exc() else: