Skip to content

Commit

Permalink
[processing] Fix processing.runAndLoadResults
Browse files Browse the repository at this point in the history
Fixes #21551
  • Loading branch information
nyalldawson committed Mar 13, 2019
1 parent 3845d79 commit e9fef0b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions python/plugins/processing/gui/Postprocessing.py
Expand Up @@ -96,6 +96,8 @@ def handleAlgorithmResults(alg, context, feedback=None, showResults=True, parame
scope = QgsExpressionContextScope()
expcontext.appendScope(scope)
for out in alg.outputDefinitions():
if out.name() not in parameters:
continue
outValue = parameters[out.name()]
if hasattr(outValue, "sink"):
outValue = outValue.sink.valueAsString(expcontext)[0]
Expand Down
23 changes: 22 additions & 1 deletion python/plugins/processing/tests/ProcessingGeneralTest.py
Expand Up @@ -32,7 +32,8 @@
from qgis.core import (QgsApplication,
QgsProcessing,
QgsProcessingContext,
QgsVectorLayer)
QgsVectorLayer,
QgsProject)
from qgis.PyQt import sip
from qgis.analysis import (QgsNativeAlgorithms)
from qgis.testing import start_app, unittest
Expand Down Expand Up @@ -91,6 +92,26 @@ def testRun(self):
gc.collect()
self.assertTrue(sip.isdeleted(layer))

def testRunAndLoadResults(self):
QgsProject.instance().removeAllMapLayers()
context = QgsProcessingContext()

# try running an alg using processing.runAndLoadResults - ownership of result layer should be transferred to
# project, and layer should be present in project
res = processing.runAndLoadResults('qgis:buffer',
{'DISTANCE': 1, 'INPUT': points(), 'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT},
context=context)
self.assertIn('OUTPUT', res)
# output should be the layer path
self.assertIsInstance(res['OUTPUT'], str)

self.assertEqual(context.layersToLoadOnCompletion()[res['OUTPUT']].project, QgsProject.instance())
layer = QgsProject.instance().mapLayer(res['OUTPUT'])
self.assertIsInstance(layer, QgsVectorLayer)

# Python should NOT have ownership
self.assertFalse(sip.ispyowned(layer))


if __name__ == '__main__':
nose2.main()

0 comments on commit e9fef0b

Please sign in to comment.