Skip to content

Commit 9b0241c

Browse files
committedJul 2, 2018
[processing] Fix managled paths in history which contain '\' chars
(cherry-picked from ead18fc)
1 parent 03c21c2 commit 9b0241c

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed
 

‎src/core/processing/qgsprocessingparameters.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,7 +1543,7 @@ QString QgsProcessingParameterMapLayer::valueAsPythonString( const QVariant &val
15431543
p.insert( name(), val );
15441544
QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
15451545
return layer ? QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer->source() ) )
1546-
: QString();
1546+
: QgsProcessingUtils::stringToPythonLiteral( val.toString() );
15471547
}
15481548

15491549
QgsProcessingParameterMapLayer *QgsProcessingParameterMapLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
@@ -2418,7 +2418,8 @@ 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::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer->source() ) ) : QString();
2421+
return layer ? QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer->source() ) )
2422+
: QgsProcessingUtils::stringToPythonLiteral( val.toString() );
24222423
}
24232424

24242425
QgsProcessingParameterRasterLayer *QgsProcessingParameterRasterLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
@@ -2630,7 +2631,6 @@ QString QgsProcessingParameterString::valueAsPythonString( const QVariant &value
26302631
return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
26312632

26322633
QString s = value.toString();
2633-
s.replace( '\n', QStringLiteral( "\\n" ) );
26342634
return QgsProcessingUtils::stringToPythonLiteral( s );
26352635
}
26362636

@@ -2715,7 +2715,6 @@ QString QgsProcessingParameterExpression::valueAsPythonString( const QVariant &v
27152715
return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
27162716

27172717
QString s = value.toString();
2718-
s.replace( '\n', QStringLiteral( "\\n" ) );
27192718
return QgsProcessingUtils::stringToPythonLiteral( s );
27202719
}
27212720

@@ -2819,7 +2818,7 @@ QString QgsProcessingParameterVectorLayer::valueAsPythonString( const QVariant &
28192818
p.insert( name(), val );
28202819
QgsVectorLayer *layer = QgsProcessingParameters::parameterAsVectorLayer( this, p, context );
28212820
return layer ? QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer->source() ) )
2822-
: QString();
2821+
: QgsProcessingUtils::stringToPythonLiteral( val.toString() );
28232822
}
28242823

28252824
QList<int> QgsProcessingParameterLimitedDataTypes::dataTypes() const
@@ -3326,7 +3325,7 @@ QString QgsProcessingParameterFeatureSink::valueAsPythonString( const QVariant &
33263325
QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
33273326
if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
33283327
{
3329-
return QStringLiteral( "'%1'" ).arg( fromVar.sink.staticValue().toString() );
3328+
return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
33303329
}
33313330
else
33323331
{
@@ -3551,7 +3550,7 @@ QString QgsProcessingParameterRasterDestination::valueAsPythonString( const QVar
35513550
QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
35523551
if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
35533552
{
3554-
return QStringLiteral( "'%1'" ).arg( fromVar.sink.staticValue().toString() );
3553+
return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
35553554
}
35563555
else
35573556
{
@@ -3667,7 +3666,7 @@ QString QgsProcessingParameterFileDestination::valueAsPythonString( const QVaria
36673666
QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
36683667
if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
36693668
{
3670-
return QStringLiteral( "'%1'" ).arg( fromVar.sink.staticValue().toString() );
3669+
return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
36713670
}
36723671
else
36733672
{
@@ -3890,7 +3889,7 @@ QString QgsProcessingParameterVectorDestination::valueAsPythonString( const QVar
38903889
QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
38913890
if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
38923891
{
3893-
return QStringLiteral( "'%1'" ).arg( fromVar.sink.staticValue().toString() );
3892+
return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
38943893
}
38953894
else
38963895
{

‎src/core/processing/qgsprocessingutils.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ QString QgsProcessingUtils::normalizeLayerSource( const QString &source )
310310
QString QgsProcessingUtils::stringToPythonLiteral( const QString &string )
311311
{
312312
QString s = string;
313+
s.replace( '\\', QStringLiteral( "\\\\" ) );
314+
s.replace( '\n', QStringLiteral( "\\n" ) );
315+
s.replace( '\r', QStringLiteral( "\\r" ) );
316+
s.replace( '\t', QStringLiteral( "\\t" ) );
313317
s.replace( '"', QStringLiteral( "\\\"" ) );
314318
s.replace( '\'', QStringLiteral( "\\\'" ) );
315319
s = s.prepend( '\'' ).append( '\'' );

‎tests/src/analysis/testqgsprocessing.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,6 +2153,7 @@ void TestQgsProcessing::parameterCrs()
21532153
QCOMPARE( def->valueAsPythonString( QVariant(), context ), QStringLiteral( "None" ) );
21542154
QCOMPARE( def->valueAsPythonString( "EPSG:12003", context ), QStringLiteral( "'EPSG:12003'" ) );
21552155
QCOMPARE( def->valueAsPythonString( "ProjectCrs", context ), QStringLiteral( "'ProjectCrs'" ) );
2156+
QCOMPARE( def->valueAsPythonString( QStringLiteral( "c:\\test\\new data\\test.dat" ), context ), QStringLiteral( "'c:\\\\test\\\\new data\\\\test.dat'" ) );
21562157
QCOMPARE( def->valueAsPythonString( raster1, context ), QString( "'" ) + testDataDir + QStringLiteral( "tenbytenraster.asc'" ) );
21572158
QCOMPARE( def->valueAsPythonString( r1->id(), context ), QString( "'" ) + testDataDir + QStringLiteral( "tenbytenraster.asc'" ) );
21582159
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
@@ -2269,6 +2270,7 @@ void TestQgsProcessing::parameterLayer()
22692270
QCOMPARE( def->valueAsPythonString( r1->id(), context ), QString( "'" ) + testDataDir + QStringLiteral( "tenbytenraster.asc'" ) );
22702271
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( r1 ), context ), QString( "'" ) + testDataDir + QStringLiteral( "tenbytenraster.asc'" ) );
22712272
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
2273+
QCOMPARE( def->valueAsPythonString( QStringLiteral( "c:\\test\\new data\\test.dat" ), context ), QStringLiteral( "'c:\\\\test\\\\new data\\\\test.dat'" ) );
22722274

22732275
QString code = def->asScriptCode();
22742276
QCOMPARE( code, QStringLiteral( "##non_optional=layer" ) );
@@ -2545,6 +2547,7 @@ void TestQgsProcessing::parameterExtent()
25452547
QCOMPARE( def->valueAsPythonString( QgsReferencedRectangle( QgsRectangle( 11, 12, 13, 14 ), QgsCoordinateReferenceSystem( "epsg:4326" ) ), context ), QStringLiteral( "'11, 13, 12, 14 [EPSG:4326]'" ) );
25462548
QCOMPARE( def->valueAsPythonString( "1,2,3,4 [EPSG:4326]", context ), QStringLiteral( "'1,2,3,4 [EPSG:4326]'" ) );
25472549
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );
2550+
QCOMPARE( def->valueAsPythonString( QStringLiteral( "c:\\test\\new data\\test.dat" ), context ), QStringLiteral( "'c:\\\\test\\\\new data\\\\test.dat'" ) );
25482551

25492552
QString code = def->asScriptCode();
25502553
QCOMPARE( code, QStringLiteral( "##non_optional=extent 1,2,3,4" ) );
@@ -2776,6 +2779,7 @@ void TestQgsProcessing::parameterFile()
27762779
QCOMPARE( def->valueAsPythonString( "bricks.bmp", context ), QStringLiteral( "'bricks.bmp'" ) );
27772780
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
27782781
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );
2782+
QCOMPARE( def->valueAsPythonString( QStringLiteral( "c:\\test\\new data\\test.dat" ), context ), QStringLiteral( "'c:\\\\test\\\\new data\\\\test.dat'" ) );
27792783

27802784
QString code = def->asScriptCode();
27812785
QCOMPARE( code, QStringLiteral( "##non_optional=file abc.bmp" ) );
@@ -3518,6 +3522,7 @@ void TestQgsProcessing::parameterRasterLayer()
35183522
QCOMPARE( def->valueAsPythonString( r1->id(), context ), QString( "'" ) + testDataDir + QStringLiteral( "tenbytenraster.asc'" ) );
35193523
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( r1 ), context ), QString( "'" ) + testDataDir + QStringLiteral( "tenbytenraster.asc'" ) );
35203524
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
3525+
QCOMPARE( def->valueAsPythonString( QStringLiteral( "c:\\test\\new data\\test.dat" ), context ), QStringLiteral( "'c:\\\\test\\\\new data\\\\test.dat'" ) );
35213526

35223527
QString code = def->asScriptCode();
35233528
QCOMPARE( code, QStringLiteral( "##non_optional=raster" ) );
@@ -3806,6 +3811,7 @@ void TestQgsProcessing::parameterString()
38063811
QCOMPARE( def->valueAsPythonString( QStringLiteral( "abc\ndef" ), context ), QStringLiteral( "'abc\\ndef'" ) );
38073812
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
38083813
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );
3814+
QCOMPARE( def->valueAsPythonString( QStringLiteral( "c:\\test\\new data\\test.dat" ), context ), QStringLiteral( "'c:\\\\test\\\\new data\\\\test.dat'" ) );
38093815

38103816
QString code = def->asScriptCode();
38113817
QCOMPARE( code, QStringLiteral( "##non_optional=string" ) );
@@ -4251,6 +4257,7 @@ void TestQgsProcessing::parameterVectorLayer()
42514257
QCOMPARE( def->valueAsPythonString( v1->id(), context ), QString( "'" ) + testDataDir + QStringLiteral( "multipoint.shp'" ) );
42524258
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( v1 ), context ), QString( "'" ) + testDataDir + QStringLiteral( "multipoint.shp'" ) );
42534259
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
4260+
QCOMPARE( def->valueAsPythonString( QStringLiteral( "c:\\test\\new data\\test.dat" ), context ), QStringLiteral( "'c:\\\\test\\\\new data\\\\test.dat'" ) );
42544261

42554262
QString code = def->asScriptCode();
42564263
QCOMPARE( code, QStringLiteral( "##non_optional=vector somelayer" ) );
@@ -4369,6 +4376,7 @@ void TestQgsProcessing::parameterFeatureSource()
43694376
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
43704377
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( v2 ), context ), QStringLiteral( "'%1'" ).arg( vector2 ) );
43714378
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );
4379+
QCOMPARE( def->valueAsPythonString( QStringLiteral( "c:\\test\\new data\\test.dat" ), context ), QStringLiteral( "'c:\\\\test\\\\new data\\\\test.dat'" ) );
43724380

43734381
QVariantMap map = def->toVariantMap();
43744382
QgsProcessingParameterFeatureSource fromMap( "x" );
@@ -4481,6 +4489,7 @@ void TestQgsProcessing::parameterFeatureSink()
44814489
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromExpression( "\"abc\" || \"def\"" ) ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"abc\" || \"def\"')" ) );
44824490
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
44834491
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );
4492+
QCOMPARE( def->valueAsPythonString( QStringLiteral( "c:\\test\\new data\\test.dat" ), context ), QStringLiteral( "'c:\\\\test\\\\new data\\\\test.dat'" ) );
44844493

44854494
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "shp" ) );
44864495
QCOMPARE( def->generateTemporaryDestination(), QStringLiteral( "memory:" ) );
@@ -4609,6 +4618,7 @@ void TestQgsProcessing::parameterVectorOut()
46094618
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromExpression( "\"abc\" || \"def\"" ) ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"abc\" || \"def\"')" ) );
46104619
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
46114620
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );
4621+
QCOMPARE( def->valueAsPythonString( QStringLiteral( "c:\\test\\new data\\test.dat" ), context ), QStringLiteral( "'c:\\\\test\\\\new data\\\\test.dat'" ) );
46124622

46134623
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "shp" ) );
46144624
QVERIFY( def->generateTemporaryDestination().endsWith( QStringLiteral( ".shp" ) ) );
@@ -4725,6 +4735,7 @@ void TestQgsProcessing::parameterRasterOut()
47254735
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromExpression( "\"abc\" || \"def\"" ) ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"abc\" || \"def\"')" ) );
47264736
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
47274737
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );
4738+
QCOMPARE( def->valueAsPythonString( QStringLiteral( "c:\\test\\new data\\test.dat" ), context ), QStringLiteral( "'c:\\\\test\\\\new data\\\\test.dat'" ) );
47284739

47294740
QVariantMap map = def->toVariantMap();
47304741
QgsProcessingParameterRasterDestination fromMap( "x" );
@@ -4848,6 +4859,7 @@ void TestQgsProcessing::parameterFileOut()
48484859
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromExpression( "\"abc\" || \"def\"" ) ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"abc\" || \"def\"')" ) );
48494860
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
48504861
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );
4862+
QCOMPARE( def->valueAsPythonString( QStringLiteral( "c:\\test\\new data\\test.dat" ), context ), QStringLiteral( "'c:\\\\test\\\\new data\\\\test.dat'" ) );
48514863

48524864
QVariantMap map = def->toVariantMap();
48534865
QgsProcessingParameterFileDestination fromMap( "x" );
@@ -4943,6 +4955,7 @@ void TestQgsProcessing::parameterFolderOut()
49434955
QCOMPARE( def->valueAsPythonString( QStringLiteral( "abc" ), context ), QStringLiteral( "'abc'" ) );
49444956
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
49454957
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );
4958+
QCOMPARE( def->valueAsPythonString( QStringLiteral( "c:\\test\\new data\\" ), context ), QStringLiteral( "'c:\\\\test\\\\new data\\\\'" ) );
49464959

49474960
QVariantMap map = def->toVariantMap();
49484961
QgsProcessingParameterFolderDestination fromMap( "x" );
@@ -6664,6 +6677,7 @@ void TestQgsProcessing::stringToPythonLiteral()
66646677
QCOMPARE( QgsProcessingUtils::stringToPythonLiteral( QString() ), QStringLiteral( "''" ) );
66656678
QCOMPARE( QgsProcessingUtils::stringToPythonLiteral( QStringLiteral( "a 'string'" ) ), QStringLiteral( "'a \\'string\\''" ) );
66666679
QCOMPARE( QgsProcessingUtils::stringToPythonLiteral( QStringLiteral( "a \"string\"" ) ), QStringLiteral( "'a \\\"string\\\"'" ) );
6680+
QCOMPARE( QgsProcessingUtils::stringToPythonLiteral( QStringLiteral( "a \n str\tin\\g" ) ), QStringLiteral( "'a \\n str\\tin\\\\g'" ) );
66676681
}
66686682

66696683
void TestQgsProcessing::defaultExtensionsForProvider()

0 commit comments

Comments
 (0)
Please sign in to comment.