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