@@ -50,7 +50,7 @@ async def send_return_as_dict(self, method: str, params: Dict = None) -> Any:
5050 )
5151
5252 def send_no_reply (self , method : str , params : Dict = None ) -> None :
53- self ._connection .wrap_api_call (
53+ self ._connection .wrap_api_call_sync (
5454 lambda : self ._connection ._send_message_to_server (
5555 self ._guid , method , {} if params is None else params
5656 )
@@ -355,26 +355,35 @@ def _replace_guids_with_channels(self, payload: Any) -> Any:
355355 return result
356356 return payload
357357
358- def wrap_api_call (self , cb : Callable [[], Any ], is_internal : bool = False ) -> Any :
358+ async def wrap_api_call (
359+ self , cb : Callable [[], Any ], is_internal : bool = False
360+ ) -> Any :
359361 if self ._api_zone .get ():
360- return cb ()
362+ return await cb ()
361363 task = asyncio .current_task (self ._loop )
362364 st : List [inspect .FrameInfo ] = getattr (task , "__pw_stack__" , inspect .stack ())
363365 metadata = _extract_metadata_from_stack (st , is_internal )
364366 if metadata :
365367 self ._api_zone .set (metadata )
366- result = cb ()
367-
368- async def _ () -> None :
369- try :
370- return await result
371- finally :
372- self ._api_zone .set (None )
368+ try :
369+ return await cb ()
370+ finally :
371+ self ._api_zone .set (None )
373372
374- if asyncio .iscoroutine (result ):
375- return _ ()
376- self ._api_zone .set (None )
377- return result
373+ def wrap_api_call_sync (
374+ self , cb : Callable [[], Any ], is_internal : bool = False
375+ ) -> Any :
376+ if self ._api_zone .get ():
377+ return cb ()
378+ task = asyncio .current_task (self ._loop )
379+ st : List [inspect .FrameInfo ] = getattr (task , "__pw_stack__" , inspect .stack ())
380+ metadata = _extract_metadata_from_stack (st , is_internal )
381+ if metadata :
382+ self ._api_zone .set (metadata )
383+ try :
384+ return cb ()
385+ finally :
386+ self ._api_zone .set (None )
378387
379388
380389def from_channel (channel : Channel ) -> Any :
@@ -388,6 +397,12 @@ def from_nullable_channel(channel: Optional[Channel]) -> Optional[Any]:
388397def _extract_metadata_from_stack (
389398 st : List [inspect .FrameInfo ], is_internal : bool
390399) -> Optional [Dict ]:
400+ if is_internal :
401+ return {
402+ "apiName" : "" ,
403+ "stack" : [],
404+ "internal" : True ,
405+ }
391406 playwright_module_path = str (Path (playwright .__file__ ).parents [0 ])
392407 last_internal_api_name = ""
393408 api_name = ""
@@ -419,6 +434,5 @@ def _extract_metadata_from_stack(
419434 return {
420435 "apiName" : api_name ,
421436 "stack" : stack ,
422- "isInternal" : is_internal ,
423437 }
424438 return None
0 commit comments