22import builtins
33from lazyllm import config
44from lazyllm .common import LazyLLMRegisterMetaClass , package , kwargs , arguments , bind , root
5- from lazyllm .common import ReadOnlyWrapper , LOG , globals , locals
5+ from lazyllm .common import ReadOnlyWrapper , LOG , globals , locals , _get_callsite
6+ from lazyllm .common import _register_trim_module , HandledException , _change_exception_type
67from lazyllm .common .bind import _MetaBind
78from functools import partial
89from contextlib import contextmanager
2122from itertools import repeat
2223
2324
24- class FlowException (Exception ):
25+ class FlowException (HandledException ):
2526 pass
2627
2728
28- def _is_lazyllm_internal_frame (frame , expected : Optional [dict ] = None ):
29- expected = expected or {'lazyllm.flow.flow' : ['_run' , 'invoke' ], 'lazyllm.common.bind' : ['__call__' ]}
30- mod = frame .f_globals .get ('__name__' , '' )
31- if not mod .startswith ('lazyllm.' ): return False
32- if mod in expected and frame .f_code .co_name in expected [mod ]: return True
33- return False
34-
35- _is_lazyllm_call_frame = bind (_is_lazyllm_internal_frame , expected = {'lazyllm.flow.flow' : '__call__' })
36-
37- def _trim_traceback (tb ):
38- kept , keep_call = [], True
39- while tb :
40- if not _is_lazyllm_internal_frame (tb .tb_frame ):
41- if _is_lazyllm_call_frame (tb .tb_frame ):
42- if keep_call :
43- kept .append (tb )
44- keep_call = False
45- else :
46- kept .append (tb )
47- keep_call = True
48- tb = tb .tb_next
49-
50- new_tb = None
51- for tb in reversed (kept ):
52- new_tb = types .TracebackType (tb_next = new_tb , tb_frame = tb .tb_frame , tb_lasti = tb .tb_lasti , tb_lineno = tb .tb_lineno )
53- return new_tb
54-
55- _original_excepthook = sys .excepthook
56-
57- def _lazyllm_excepthook (exc_type , exc_value , tb ):
58- _original_excepthook (exc_type , exc_value , _trim_traceback (tb ))
59-
60- sys .excepthook = _lazyllm_excepthook
61-
6229class _FuncWrap (object ):
6330 def __init__ (self , f ):
6431 self ._f = f ._f if isinstance (f , _FuncWrap ) else f
@@ -76,19 +43,6 @@ def __getattr__(self, __key):
7643 return getattr (self ._f , __key )
7744 return super (__class__ , self ).__getattr__ (__key )
7845
79-
80- def _get_callsite (depth = 1 ):
81- try :
82- frame = inspect .currentframe ()
83- for _ in range (depth ): frame = frame .f_back
84- if frame is None : return None
85- else :
86- while frame .f_code .co_name == '__setattr__' and frame .f_globals .get ('__name__' , '' ) == 'lazyllm.common.bind' :
87- frame = frame .f_back
88- return f'"file: { os .path .abspath (frame .f_code .co_filename )} ", line { frame .f_lineno } '
89- except Exception :
90- return None
91-
9246_oldins = isinstance
9347def new_ins (obj , cls ):
9448 if _oldins (obj , _FuncWrap ) and os .getenv ('LAZYLLM_ON_CLOUDPICKLE' , None ) != 'ON' :
@@ -105,6 +59,9 @@ def _is_function(f):
10559_flow_stack = threading .local ()
10660_flow_stack .value = []
10761
62+ _register_trim_module ({'lazyllm.flow.flow' : ['__call__' ]}, continuous = True )
63+ _register_trim_module ({'lazyllm.flow.flow' : ['_run' , 'invoke' ], 'lazyllm.common.bind' : ['__call__' ]})
64+
10865class FlowBase (metaclass = _MetaBind ):
10966 def __init__ (self , * items , item_names = None , auto_capture = False ) -> None :
11067 self ._father = None
@@ -315,8 +272,7 @@ def invoke(self, it, __input, *, bind_args_source=None, **kw):
315272 return it (* __input , ** kw ) if isinstance (__input , package ) else it (** __input , ** kw )
316273 else :
317274 return it (__input , ** kw )
318- except FlowException as e :
319- raise e
275+ except HandledException as e : raise e
320276 except Exception as e :
321277 try :
322278 pos = self ._item_pos [self ._items .index (it )]
@@ -329,7 +285,7 @@ def invoke(self, it, __input, *, bind_args_source=None, **kw):
329285 LOG .error (err_msg )
330286 LOG .debug (f'Error type: { type (e ).__name__ } , Error message: { str (e )} \n '
331287 f'Traceback: { "" .join (traceback .format_exception (* sys .exc_info ()))} ' )
332- raise FlowException ( err_msg ) from e
288+ raise _change_exception_type ( e , FlowException ) from None
333289
334290 def bind (self , * args , ** kw ):
335291 return bind (self , * args , ** kw )
0 commit comments