Skip to content

Commit

Permalink
Merge pull request #5832 from johnhaddon/tweakPlugCrashFix
Browse files Browse the repository at this point in the history
TweakPlug : Fix crash in IgnoreOrReplace mode
  • Loading branch information
ericmehl authored Apr 30, 2024
2 parents b4b15d4 + 0cd4d60 commit 7f39dc3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
15 changes: 15 additions & 0 deletions python/GafferTest/TweakPlugTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,5 +481,20 @@ def testStringSubstitutions( self ) :
self.assertIs( type( data["v"] ), type( source ) )
self.assertEqual( data["v"].value, result )

def testStringSubstitutionsAndMissingMode( self ) :

plug = Gaffer.TweakPlug( "v", "{source}suffix" )
data = IECore.CompoundData()

with self.assertRaisesRegex( Exception, "This parameter does not exist" ) :
plug.applyTweak( data, Gaffer.TweakPlug.MissingMode.Error )
self.assertEqual( data, IECore.CompoundData() )

self.assertFalse( plug.applyTweak( data, Gaffer.TweakPlug.MissingMode.Ignore ) )
self.assertEqual( data, IECore.CompoundData() )

self.assertTrue( plug.applyTweak( data, Gaffer.TweakPlug.MissingMode.IgnoreOrReplace ) )
self.assertEqual( data["v"], IECore.StringData( "suffix" ) )

if __name__ == "__main__":
unittest.main()
11 changes: 8 additions & 3 deletions src/Gaffer/TweakPlug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,18 @@ void TweakPlug::applyReplaceTweak( const IECore::Data *sourceData, IECore::Data
{
if( auto stringData = IECore::runTimeCast<IECore::StringData>( tweakData ) )
{
boost::replace_all( stringData->writable(), "{source}", static_cast<const IECore::StringData *>( sourceData )->readable() );
/// \todo The `sourceData` null check is only necessary because of the deprecated `MissingMode::IgnoreOrReplace`.
/// Remove that mode and remove the checks.
boost::replace_all(
stringData->writable(), "{source}",
sourceData ? static_cast<const IECore::StringData *>( sourceData )->readable() : ""
);
}
else if( auto internedStringData = IECore::runTimeCast<IECore::InternedStringData>( tweakData ) )
{
internedStringData->writable() = boost::replace_all_copy(
internedStringData->readable().string(),
"{source}", static_cast<const IECore::InternedStringData *>( sourceData )->readable().string()
internedStringData->readable().string(), "{source}",
sourceData ? static_cast<const IECore::InternedStringData *>( sourceData )->readable().string() : ""
);
}
}
Expand Down

0 comments on commit 7f39dc3

Please sign in to comment.