electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 77287e0fc7d9b4783c820543b3bf1910c47189d3
parent 35f1f2905b79550fe062fe9db8ec1fa160eda121
Author: BenoƮt Verret <verret.benoit@gmail.com>
Date:   Tue, 15 Sep 2020 13:23:22 -0400

Fix multiline script error (#6581)

Syntax errors emerged when running multiline scripts in the console
when using the run() command.
Diffstat:
Melectrum/gui/qt/console.py | 100+++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 49 insertions(+), 51 deletions(-)

diff --git a/electrum/gui/qt/console.py b/electrum/gui/qt/console.py @@ -80,10 +80,7 @@ class Console(QtWidgets.QPlainTextEdit): with open(filename) as f: script = f.read() - # eval is generally considered bad practice. use it wisely! - result = eval(script, self.namespace, self.namespace) - - + self.exec_command(script) def updateNamespace(self, namespace): self.namespace.update(namespace) @@ -205,61 +202,62 @@ class Console(QtWidgets.QPlainTextEdit): for i in range(len(self.prompt) + position): self.moveCursor(QtGui.QTextCursor.Right) - def runCommand(self): + def run_command(self): command = self.getCommand() self.addToHistory(command) command = self.getConstruct(command) if command: - tmp_stdout = sys.stdout - - class stdoutProxy(): - def __init__(self, write_func): - self.write_func = write_func - self.skip = False - - def flush(self): - pass - - def write(self, text): - if not self.skip: - stripped_text = text.rstrip('\n') - self.write_func(stripped_text) - QtCore.QCoreApplication.processEvents() - self.skip = not self.skip - - if type(self.namespace.get(command)) == type(lambda:None): - self.appendPlainText("'{}' is a function. Type '{}()' to use it in the Python console." - .format(command, command)) - self.newPrompt('') - return - - sys.stdout = stdoutProxy(self.appendPlainText) - try: - try: - # eval is generally considered bad practice. use it wisely! - result = eval(command, self.namespace, self.namespace) - if result is not None: - if self.is_json: - util.print_msg(util.json_encode(result)) - else: - self.appendPlainText(repr(result)) - except SyntaxError: - # exec is generally considered bad practice. use it wisely! - exec(command, self.namespace, self.namespace) - except SystemExit: - self.close() - except BaseException: - traceback_lines = traceback.format_exc().split('\n') - # Remove traceback mentioning this file, and a linebreak - for i in (3,2,1,-1): - traceback_lines.pop(i) - self.appendPlainText('\n'.join(traceback_lines)) - sys.stdout = tmp_stdout + self.exec_command(command) self.newPrompt('') self.set_json(False) + def exec_command(self, command): + tmp_stdout = sys.stdout + + class stdoutProxy(): + def __init__(self, write_func): + self.write_func = write_func + self.skip = False + + def flush(self): + pass + + def write(self, text): + if not self.skip: + stripped_text = text.rstrip('\n') + self.write_func(stripped_text) + QtCore.QCoreApplication.processEvents() + self.skip = not self.skip + + if type(self.namespace.get(command)) == type(lambda:None): + self.appendPlainText("'{}' is a function. Type '{}()' to use it in the Python console." + .format(command, command)) + return + + sys.stdout = stdoutProxy(self.appendPlainText) + try: + try: + # eval is generally considered bad practice. use it wisely! + result = eval(command, self.namespace, self.namespace) + if result is not None: + if self.is_json: + util.print_msg(util.json_encode(result)) + else: + self.appendPlainText(repr(result)) + except SyntaxError: + # exec is generally considered bad practice. use it wisely! + exec(command, self.namespace, self.namespace) + except SystemExit: + self.close() + except BaseException: + traceback_lines = traceback.format_exc().split('\n') + # Remove traceback mentioning this file, and a linebreak + for i in (3,2,1,-1): + traceback_lines.pop(i) + self.appendPlainText('\n'.join(traceback_lines)) + sys.stdout = tmp_stdout def keyPressEvent(self, event): if event.key() == QtCore.Qt.Key_Tab: @@ -269,7 +267,7 @@ class Console(QtWidgets.QPlainTextEdit): self.hide_completions() if event.key() in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return): - self.runCommand() + self.run_command() return if event.key() == QtCore.Qt.Key_Home: self.setCursorPosition(0)