Skip to content

Commit 3f16335

Browse files
authored
Merge pull request #7296 from wonder-sk/processing-python-escape-quotes
Fix escaping of quotes of map layer source in processing
2 parents 7ab6597 + bc737ce commit 3f16335

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/core/processing/qgsprocessingparameters.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -1645,7 +1645,7 @@ QString QgsProcessingParameterExtent::valueAsPythonString( const QVariant &value
16451645
p.insert( name(), value );
16461646
QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
16471647
if ( layer )
1648-
return QgsProcessingUtils::normalizeLayerSource( layer->source() ).prepend( '\'' ).append( '\'' );
1648+
return QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer->source() ) );
16491649

16501650
return QgsProcessingParameterDefinition::valueAsPythonString( value, context );
16511651
}
@@ -2418,7 +2418,7 @@ QString QgsProcessingParameterRasterLayer::valueAsPythonString( const QVariant &
24182418
QVariantMap p;
24192419
p.insert( name(), val );
24202420
QgsRasterLayer *layer = QgsProcessingParameters::parameterAsRasterLayer( this, p, context );
2421-
return layer ? QgsProcessingUtils::normalizeLayerSource( layer->source() ).prepend( '\'' ).append( '\'' ) : QString();
2421+
return layer ? QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer->source() ) ) : QString();
24222422
}
24232423

24242424
QgsProcessingParameterRasterLayer *QgsProcessingParameterRasterLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
@@ -2631,7 +2631,7 @@ QString QgsProcessingParameterString::valueAsPythonString( const QVariant &value
26312631

26322632
QString s = value.toString();
26332633
s.replace( '\n', QStringLiteral( "\\n" ) );
2634-
return s.prepend( '\'' ).append( '\'' );
2634+
return QgsProcessingUtils::stringToPythonLiteral( s );
26352635
}
26362636

26372637
QString QgsProcessingParameterString::asScriptCode() const
@@ -2716,7 +2716,7 @@ QString QgsProcessingParameterExpression::valueAsPythonString( const QVariant &v
27162716

27172717
QString s = value.toString();
27182718
s.replace( '\n', QStringLiteral( "\\n" ) );
2719-
return s.prepend( '\'' ).append( '\'' );
2719+
return QgsProcessingUtils::stringToPythonLiteral( s );
27202720
}
27212721

27222722
QStringList QgsProcessingParameterExpression::dependsOnOtherParameters() const
@@ -3155,7 +3155,7 @@ QString QgsProcessingParameterFeatureSource::valueAsPythonString( const QVariant
31553155
// prefer to use layer source instead of id if possible (since it's persistent)
31563156
if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerString, context ) ) )
31573157
layerString = layer->source();
3158-
return layerString.prepend( '\'' ).append( '\'' );
3158+
return QgsProcessingUtils::stringToPythonLiteral( layerString );
31593159
}
31603160
}
31613161
else
@@ -3172,10 +3172,10 @@ QString QgsProcessingParameterFeatureSource::valueAsPythonString( const QVariant
31723172
}
31733173
else if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( value ) ) )
31743174
{
3175-
return layer->source().prepend( '\'' ).append( '\'' );
3175+
return QgsProcessingUtils::stringToPythonLiteral( layer->source() );
31763176
}
31773177

3178-
return value.toString().prepend( '\'' ).append( '\'' );
3178+
return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
31793179
}
31803180

31813181
QString QgsProcessingParameterFeatureSource::asScriptCode() const
@@ -3334,7 +3334,7 @@ QString QgsProcessingParameterFeatureSink::valueAsPythonString( const QVariant &
33343334
}
33353335
}
33363336

3337-
return value.toString().prepend( '\'' ).append( '\'' );
3337+
return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
33383338
}
33393339

33403340
QString QgsProcessingParameterFeatureSink::asScriptCode() const
@@ -3559,7 +3559,7 @@ QString QgsProcessingParameterRasterDestination::valueAsPythonString( const QVar
35593559
}
35603560
}
35613561

3562-
return value.toString().prepend( '\'' ).append( '\'' );
3562+
return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
35633563
}
35643564

35653565
QgsProcessingOutputDefinition *QgsProcessingParameterRasterDestination::toOutputDefinition() const
@@ -3675,7 +3675,7 @@ QString QgsProcessingParameterFileDestination::valueAsPythonString( const QVaria
36753675
}
36763676
}
36773677

3678-
return value.toString().prepend( '\'' ).append( '\'' );
3678+
return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
36793679
}
36803680

36813681
QgsProcessingOutputDefinition *QgsProcessingParameterFileDestination::toOutputDefinition() const
@@ -3898,7 +3898,7 @@ QString QgsProcessingParameterVectorDestination::valueAsPythonString( const QVar
38983898
}
38993899
}
39003900

3901-
return value.toString().prepend( '\'' ).append( '\'' );
3901+
return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
39023902
}
39033903

39043904
QString QgsProcessingParameterVectorDestination::asScriptCode() const

tests/src/analysis/testqgsprocessing.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -3805,7 +3805,7 @@ void TestQgsProcessing::parameterString()
38053805
QCOMPARE( def->valueAsPythonString( QStringLiteral( "abc" ), context ), QStringLiteral( "'abc'" ) );
38063806
QCOMPARE( def->valueAsPythonString( QStringLiteral( "abc\ndef" ), context ), QStringLiteral( "'abc\\ndef'" ) );
38073807
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
3808-
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\'complex\' username=\"complex\"'" ) );
3808+
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );
38093809

38103810
QString code = def->asScriptCode();
38113811
QCOMPARE( code, QStringLiteral( "##non_optional=string" ) );
@@ -4368,7 +4368,7 @@ void TestQgsProcessing::parameterFeatureSource()
43684368
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingFeatureSourceDefinition( QgsProperty::fromExpression( "\"abc\" || \"def\"" ) ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"abc\" || \"def\"')" ) );
43694369
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
43704370
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( v2 ), context ), QStringLiteral( "'%1'" ).arg( vector2 ) );
4371-
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\'complex\' username=\"complex\"'" ) );
4371+
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );
43724372

43734373
QVariantMap map = def->toVariantMap();
43744374
QgsProcessingParameterFeatureSource fromMap( "x" );
@@ -4480,7 +4480,7 @@ void TestQgsProcessing::parameterFeatureSink()
44804480
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromValue( "abc" ) ) ), context ), QStringLiteral( "'abc'" ) );
44814481
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromExpression( "\"abc\" || \"def\"" ) ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"abc\" || \"def\"')" ) );
44824482
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
4483-
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\'complex\' username=\"complex\"'" ) );
4483+
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );
44844484

44854485
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "shp" ) );
44864486
QCOMPARE( def->generateTemporaryDestination(), QStringLiteral( "memory:" ) );
@@ -4608,7 +4608,7 @@ void TestQgsProcessing::parameterVectorOut()
46084608
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromValue( "abc" ) ) ), context ), QStringLiteral( "'abc'" ) );
46094609
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromExpression( "\"abc\" || \"def\"" ) ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"abc\" || \"def\"')" ) );
46104610
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
4611-
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\'complex\' username=\"complex\"'" ) );
4611+
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );
46124612

46134613
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "shp" ) );
46144614
QVERIFY( def->generateTemporaryDestination().endsWith( QStringLiteral( ".shp" ) ) );
@@ -4724,7 +4724,7 @@ void TestQgsProcessing::parameterRasterOut()
47244724
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromValue( "abc" ) ) ), context ), QStringLiteral( "'abc'" ) );
47254725
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromExpression( "\"abc\" || \"def\"" ) ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"abc\" || \"def\"')" ) );
47264726
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
4727-
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\'complex\' username=\"complex\"'" ) );
4727+
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );
47284728

47294729
QVariantMap map = def->toVariantMap();
47304730
QgsProcessingParameterRasterDestination fromMap( "x" );
@@ -4847,7 +4847,7 @@ void TestQgsProcessing::parameterFileOut()
48474847
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromValue( "abc" ) ) ), context ), QStringLiteral( "'abc'" ) );
48484848
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromExpression( "\"abc\" || \"def\"" ) ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"abc\" || \"def\"')" ) );
48494849
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
4850-
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\'complex\' username=\"complex\"'" ) );
4850+
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );
48514851

48524852
QVariantMap map = def->toVariantMap();
48534853
QgsProcessingParameterFileDestination fromMap( "x" );

0 commit comments

Comments
 (0)