[processing] Fix string escaping when converting models to python code

with expression parameters

Fixes #32451
This commit is contained in:
Nyall Dawson 2019-10-29 14:11:17 +10:00
parent 328f365b16
commit e3eb4f54ab
2 changed files with 4 additions and 1 deletions

View File

@ -169,7 +169,7 @@ QString QgsProcessingModelChildParameterSource::asPythonCode( const QgsProcessin
} }
case Expression: case Expression:
return QStringLiteral( "QgsExpression('%1').evaluate()" ).arg( mExpression ); return QStringLiteral( "QgsExpression(%1).evaluate()" ).arg( QgsProcessingUtils::stringToPythonLiteral( mExpression ) );
case ExpressionText: case ExpressionText:
return mExpressionText; return mExpressionText;

View File

@ -6894,6 +6894,9 @@ void TestQgsProcessing::modelerAlgorithm()
expSource.setExpression( "1+3" ); expSource.setExpression( "1+3" );
QCOMPARE( expSource.expression(), QStringLiteral( "1+3" ) ); QCOMPARE( expSource.expression(), QStringLiteral( "1+3" ) );
QCOMPARE( expSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr, friendlyNames ), QStringLiteral( "QgsExpression('1+3').evaluate()" ) ); 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 ); expSource = QgsProcessingModelChildParameterSource::fromStaticValue( 5 );
// check that calling setExpression flips source to Expression // check that calling setExpression flips source to Expression
QCOMPARE( expSource.source(), QgsProcessingModelChildParameterSource::StaticValue ); QCOMPARE( expSource.source(), QgsProcessingModelChildParameterSource::StaticValue );