Skip to content
Permalink
Browse files

Fix context going out of scope

(cherry picked from commit 4435a3a)
  • Loading branch information
nyalldawson committed Nov 19, 2018
1 parent 84dc8fa commit 751b2c4565059c718643a2764f320a7d298e4ac6
Showing with 29 additions and 23 deletions.
  1. +29 −23 python/plugins/processing/gui/AlgorithmDialog.py
@@ -73,6 +73,9 @@ def __init__(self, alg, in_place=False, parent=None):
self.in_place = in_place
self.active_layer = None

self.context = None
self.feedback = None

self.setAlgorithm(alg)
self.setMainWidget(self.getParametersPanel(alg, self))

@@ -160,14 +163,14 @@ def getParameterValues(self):
return self.algorithm().preprocessParameters(parameters)

def runAlgorithm(self):
feedback = self.createFeedback()
context = dataobjects.createContext(feedback)
self.feedback = self.createFeedback()
self.context = dataobjects.createContext(self.feedback)

checkCRS = ProcessingConfig.getSetting(ProcessingConfig.WARN_UNMATCHING_CRS)
try:
parameters = self.getParameterValues()

if checkCRS and not self.algorithm().validateInputCrs(parameters, context):
if checkCRS and not self.algorithm().validateInputCrs(parameters, self.context):
reply = QMessageBox.question(self, self.tr("Unmatching CRS's"),
self.tr('Parameters do not all use the same CRS. This can '
'cause unexpected results.\nDo you want to '
@@ -176,7 +179,7 @@ def runAlgorithm(self):
QMessageBox.No)
if reply == QMessageBox.No:
return
ok, msg = self.algorithm().checkParameterValues(parameters, context)
ok, msg = self.algorithm().checkParameterValues(parameters, self.context)
if not ok:
QMessageBox.warning(
self, self.tr('Unable to execute algorithm'), msg)
@@ -198,12 +201,12 @@ def runAlgorithm(self):
self.setInfo(
QCoreApplication.translate('AlgorithmDialog', '<b>Algorithm \'{0}\' starting&hellip;</b>').format(self.algorithm().displayName()), escapeHtml=False)

feedback.pushInfo(self.tr('Input parameters:'))
self.feedback.pushInfo(self.tr('Input parameters:'))
display_params = []
for k, v in parameters.items():
display_params.append("'" + k + "' : " + self.algorithm().parameterDefinition(k).valueAsPythonString(v, context))
feedback.pushCommandInfo('{ ' + ', '.join(display_params) + ' }')
feedback.pushInfo('')
display_params.append("'" + k + "' : " + self.algorithm().parameterDefinition(k).valueAsPythonString(v, self.context))
self.feedback.pushCommandInfo('{ ' + ', '.join(display_params) + ' }')
self.feedback.pushInfo('')
start_time = time.time()

if self.iterateParam:
@@ -215,30 +218,30 @@ def runAlgorithm(self):
pass

self.cancelButton().setEnabled(self.algorithm().flags() & QgsProcessingAlgorithm.FlagCanCancel)
if executeIterating(self.algorithm(), parameters, self.iterateParam, context, feedback):
feedback.pushInfo(
if executeIterating(self.algorithm(), parameters, self.iterateParam, self.context, self.feedback):
self.feedback.pushInfo(
self.tr('Execution completed in {0:0.2f} seconds').format(time.time() - start_time))
self.cancelButton().setEnabled(False)
self.finish(True, parameters, context, feedback)
self.finish(True, parameters, self.context, self.feedback)
else:
self.cancelButton().setEnabled(False)
self.resetGui()
else:
command = self.algorithm().asPythonCommand(parameters, context)
command = self.algorithm().asPythonCommand(parameters, self.context)
if command:
ProcessingLog.addToLog(command)
QgsGui.instance().processingRecentAlgorithmLog().push(self.algorithm().id())
self.cancelButton().setEnabled(self.algorithm().flags() & QgsProcessingAlgorithm.FlagCanCancel)

def on_complete(ok, results):
if ok:
feedback.pushInfo(self.tr('Execution completed in {0:0.2f} seconds').format(time.time() - start_time))
feedback.pushInfo(self.tr('Results:'))
feedback.pushCommandInfo(pformat(results))
self.feedback.pushInfo(self.tr('Execution completed in {0:0.2f} seconds').format(time.time() - start_time))
self.feedback.pushInfo(self.tr('Results:'))
self.feedback.pushCommandInfo(pformat(results))
else:
feedback.reportError(
self.feedback.reportError(
self.tr('Execution failed after {0:0.2f} seconds').format(time.time() - start_time))
feedback.pushInfo('')
self.feedback.pushInfo('')

if self.feedback_dialog is not None:
self.feedback_dialog.close()
@@ -248,28 +251,31 @@ def on_complete(ok, results):
self.cancelButton().setEnabled(False)

if not self.in_place:
self.finish(ok, results, context, feedback)
self.finish(ok, results, self.context, self.feedback)
elif ok:
self.close()

self.feedback = None
self.context = None

if not self.in_place and not (self.algorithm().flags() & QgsProcessingAlgorithm.FlagNoThreading):
# Make sure the Log tab is visible before executing the algorithm
self.showLog()

task = QgsProcessingAlgRunnerTask(self.algorithm(), parameters, context, feedback)
task = QgsProcessingAlgRunnerTask(self.algorithm(), parameters, self.context, self.feedback)
task.executed.connect(on_complete)
self.setCurrentTask(task)
else:
self.proxy_progress = QgsProxyProgressTask(QCoreApplication.translate("AlgorithmDialog", "Executing “{}”").format(self.algorithm().displayName()))
QgsApplication.taskManager().addTask(self.proxy_progress)
feedback.progressChanged.connect(self.proxy_progress.setProxyProgress)
self.feedback.progressChanged.connect(self.proxy_progress.setProxyProgress)
self.feedback_dialog = self.createProgressDialog()
self.feedback_dialog.show()
if self.in_place:
ok, results = execute_in_place(self.algorithm(), parameters, context, feedback)
ok, results = execute_in_place(self.algorithm(), parameters, self.context, self.feedback)
else:
ok, results = execute(self.algorithm(), parameters, context, feedback)
feedback.progressChanged.disconnect()
ok, results = execute(self.algorithm(), parameters, self.context, self.feedback)
self.feedback.progressChanged.disconnect()
self.proxy_progress.finalize(ok)
on_complete(ok, results)

0 comments on commit 751b2c4

Please sign in to comment.
You can’t perform that action at this time.