Skip to content

Commit

Permalink
[processing] Fix string escaping when converting models to python code
Browse files Browse the repository at this point in the history
with expression parameters

Fixes #32451
  • Loading branch information
nyalldawson committed Oct 29, 2019
1 parent 328f365 commit e3eb4f5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -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 );
Expand Down

0 comments on commit e3eb4f5

Please sign in to comment.