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 );