@@ -673,9 +673,14 @@ def process_block_body_with_replay(
673673 block .pdl__id = block_id
674674 if isinstance (block , LeafBlock ) and not isinstance (block , CallBlock ):
675675 assert isinstance (block_id , str )
676- try :
676+ if block_id not in state .replay :
677+ result , background , scope , trace = process_block_body (
678+ state , scope , block , loc
679+ )
680+ state .replay [block_id ] = result
681+ else :
677682 result = state .replay [block_id ]
678- background : LazyMessages = SingletonContext (
683+ background = SingletonContext (
679684 PdlDict ({"role" : state .role , "content" : result })
680685 )
681686 if state .yield_result :
@@ -690,11 +695,6 @@ def process_block_body_with_replay(
690695 assert block .pdl__id is not None
691696 raw_result = state .replay [block .pdl__id + ".modelResponse" ]
692697 scope = scope | {block .modelResponse : raw_result }
693- except KeyError :
694- result , background , scope , trace = process_block_body (
695- state , scope , block , loc
696- )
697- state .replay [block_id ] = result
698698 else :
699699 result , background , scope , trace = process_block_body (state , scope , block , loc )
700700 return result , background , scope , trace
@@ -2099,6 +2099,14 @@ def process_call_code(
20992099 }
21002100 )
21012101 )
2102+ except PDLRuntimeExpressionError as exc :
2103+ raise PDLRuntimeError (
2104+ f"Python Code error: { exc .message } " ,
2105+ loc = loc ,
2106+ trace = block .model_copy (
2107+ update = {"code" : code_s , "pdl__defsite" : block .pdl__id }
2108+ ),
2109+ ) from exc
21022110 except Exception as exc :
21032111 raise PDLRuntimeError (
21042112 f"Python Code error: { traceback .format_exc ()} " ,
@@ -2198,9 +2206,14 @@ def process_call_code(
21982206def call_python (code : str , scope : ScopeType , state : InterpreterState ) -> PdlLazy [Any ]:
21992207 my_namespace = types .SimpleNamespace (PDL_SESSION = __PDL_SESSION , ** scope )
22002208 sys .path .append (str (state .cwd ))
2201- exec (code , my_namespace .__dict__ ) # nosec B102
2202- # [B102:exec_used] Use of exec detected.
2203- # This is the code that the user asked to execute. It can be executed in a docker container with the option `--sandbox`
2209+ try :
2210+ c = compile (code , "<code-block>" , "exec" )
2211+ exec (c , my_namespace .__dict__ ) # nosec B102
2212+ # [B102:exec_used] Use of exec detected.
2213+ # This is the code that the user asked to execute. It can be executed in a docker container with the option `--sandbox`
2214+ except Exception as exc :
2215+ message = traceback .format_exc ()
2216+ raise PDLRuntimeExpressionError (message ) from exc
22042217 result = my_namespace .result
22052218 sys .path .pop ()
22062219 return PdlConst (result )
0 commit comments