Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSLExpressionEngine : Fix handling of runtime errors from OSL #5318

Open
johnhaddon opened this issue May 22, 2023 · 0 comments
Open

OSLExpressionEngine : Fix handling of runtime errors from OSL #5318

johnhaddon opened this issue May 22, 2023 · 0 comments

Comments

@johnhaddon
Copy link
Member

Description

When OSL encounters a runtime error, it reports this via the ErrorHandler given to the ShadingSystem. Because we are not providing an error handler, the default is to print an error to the terminal but otherwise continue. Instead, we need to catch the error and turn it into an exception that halts Gaffer's current computation.

Steps to reproduce

This unit test shows the problem.

	def testExpressionError( self ) :

		s = Gaffer.ScriptNode()

		s["n"] = Gaffer.Node()
		s["n"]["user"]["o"] = Gaffer.IntPlug( flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic )

		s["e"] = Gaffer.Expression()
		s["e"].setExpression( 'string i[2] = { "one", "two" }; parent.n.user.o = strlen( i[2] );', "OSL" )

		with self.assertRaisesRegex( Gaffer.ProcessException, r".*Index \[2\] out of range.*" ) :
			s["n"]["user"]["o"].getValue()

Implementation notes

OSL's ErrorHandler class isn't given any access to per-shade state, and we use a single ShadingSystem (and therefore ErrorHandler) for all OSL expressions. We'll need to create an error handler that delegates to a per-thread stack of handlers that we can push and pop to direct the errors appropriately. Something like this :

shadingSystem = ShadingSystem( ThreadLocalErrorHandler() )

CapturingErrorHandler myHandler;
{
    ThreadLocalErrorHandler::HandlerScope myScope( myHandler );
    shadingSystem->execute();
}

if( myHandler.errors().size() )
{
    throw ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant