Browse Source

Fix multiline script error (#6581)

Syntax errors emerged when running multiline scripts in the console
when using the run() command.
master
Benoît Verret 5 years ago committed by GitHub
parent
commit
77287e0fc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      electrum/gui/qt/console.py

18
electrum/gui/qt/console.py

@ -80,10 +80,7 @@ class Console(QtWidgets.QPlainTextEdit):
with open(filename) as f: with open(filename) as f:
script = f.read() script = f.read()
# eval is generally considered bad practice. use it wisely! self.exec_command(script)
result = eval(script, self.namespace, self.namespace)
def updateNamespace(self, namespace): def updateNamespace(self, namespace):
self.namespace.update(namespace) self.namespace.update(namespace)
@ -205,13 +202,18 @@ class Console(QtWidgets.QPlainTextEdit):
for i in range(len(self.prompt) + position): for i in range(len(self.prompt) + position):
self.moveCursor(QtGui.QTextCursor.Right) self.moveCursor(QtGui.QTextCursor.Right)
def runCommand(self): def run_command(self):
command = self.getCommand() command = self.getCommand()
self.addToHistory(command) self.addToHistory(command)
command = self.getConstruct(command) command = self.getConstruct(command)
if command: if command:
self.exec_command(command)
self.newPrompt('')
self.set_json(False)
def exec_command(self, command):
tmp_stdout = sys.stdout tmp_stdout = sys.stdout
class stdoutProxy(): class stdoutProxy():
@ -232,7 +234,6 @@ class Console(QtWidgets.QPlainTextEdit):
if type(self.namespace.get(command)) == type(lambda:None): if type(self.namespace.get(command)) == type(lambda:None):
self.appendPlainText("'{}' is a function. Type '{}()' to use it in the Python console." self.appendPlainText("'{}' is a function. Type '{}()' to use it in the Python console."
.format(command, command)) .format(command, command))
self.newPrompt('')
return return
sys.stdout = stdoutProxy(self.appendPlainText) sys.stdout = stdoutProxy(self.appendPlainText)
@ -257,9 +258,6 @@ class Console(QtWidgets.QPlainTextEdit):
traceback_lines.pop(i) traceback_lines.pop(i)
self.appendPlainText('\n'.join(traceback_lines)) self.appendPlainText('\n'.join(traceback_lines))
sys.stdout = tmp_stdout sys.stdout = tmp_stdout
self.newPrompt('')
self.set_json(False)
def keyPressEvent(self, event): def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Tab: if event.key() == QtCore.Qt.Key_Tab:
@ -269,7 +267,7 @@ class Console(QtWidgets.QPlainTextEdit):
self.hide_completions() self.hide_completions()
if event.key() in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return): if event.key() in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return):
self.runCommand() self.run_command()
return return
if event.key() == QtCore.Qt.Key_Home: if event.key() == QtCore.Qt.Key_Home:
self.setCursorPosition(0) self.setCursorPosition(0)

Loading…
Cancel
Save