From e3eb4f54abdd8c6d4f4de443e3517775e9e565e1 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Tue, 29 Oct 2019 14:11:17 +1000 Subject: [PATCH] [processing] Fix string escaping when converting models to python code with expression parameters Fixes #32451 --- .../models/qgsprocessingmodelchildparametersource.cpp | 2 +- tests/src/analysis/testqgsprocessing.cpp | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/core/processing/models/qgsprocessingmodelchildparametersource.cpp b/src/core/processing/models/qgsprocessingmodelchildparametersource.cpp index af5903e19f9..abf50449b4c 100644 --- a/src/core/processing/models/qgsprocessingmodelchildparametersource.cpp +++ b/src/core/processing/models/qgsprocessingmodelchildparametersource.cpp @@ -169,7 +169,7 @@ QString QgsProcessingModelChildParameterSource::asPythonCode( const QgsProcessin } case Expression: - return QStringLiteral( "QgsExpression('%1').evaluate()" ).arg( mExpression ); + return QStringLiteral( "QgsExpression(%1).evaluate()" ).arg( QgsProcessingUtils::stringToPythonLiteral( mExpression ) ); case ExpressionText: return mExpressionText; diff --git a/tests/src/analysis/testqgsprocessing.cpp b/tests/src/analysis/testqgsprocessing.cpp index 66ba8a1059a..e9251655703 100644 --- a/tests/src/analysis/testqgsprocessing.cpp +++ b/tests/src/analysis/testqgsprocessing.cpp @@ -6894,6 +6894,9 @@ void TestQgsProcessing::modelerAlgorithm() expSource.setExpression( "1+3" ); QCOMPARE( expSource.expression(), QStringLiteral( "1+3" ) ); QCOMPARE( expSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr, friendlyNames ), QStringLiteral( "QgsExpression('1+3').evaluate()" ) ); + expSource.setExpression( "'a' || 'b\\'c'" ); + QCOMPARE( expSource.expression(), QStringLiteral( "'a' || 'b\\'c'" ) ); + QCOMPARE( expSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr, friendlyNames ), QStringLiteral( "QgsExpression('\\'a\\' || \\'b\\\\\\'c\\'').evaluate()" ) ); expSource = QgsProcessingModelChildParameterSource::fromStaticValue( 5 ); // check that calling setExpression flips source to Expression QCOMPARE( expSource.source(), QgsProcessingModelChildParameterSource::StaticValue );