[processing] Fix processing.runAndLoadResults

Fixes #21551
This commit is contained in:
Nyall Dawson 2019-03-14 08:35:32 +10:00
parent ab530e4b04
commit db1645cf12
2 changed files with 24 additions and 1 deletions

View File

@ -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]

View File

@ -32,7 +32,8 @@ import gc
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
@ -91,6 +92,26 @@ class TestProcessingGeneral(unittest.TestCase):
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()