@@ -50,7 +50,7 @@ async def send_return_as_dict(self, method: str, params: Dict = None) -> Any:
50
50
)
51
51
52
52
def send_no_reply (self , method : str , params : Dict = None ) -> None :
53
- self ._connection .wrap_api_call (
53
+ self ._connection .wrap_api_call_sync (
54
54
lambda : self ._connection ._send_message_to_server (
55
55
self ._guid , method , {} if params is None else params
56
56
)
@@ -355,26 +355,35 @@ def _replace_guids_with_channels(self, payload: Any) -> Any:
355
355
return result
356
356
return payload
357
357
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 :
359
361
if self ._api_zone .get ():
360
- return cb ()
362
+ return await cb ()
361
363
task = asyncio .current_task (self ._loop )
362
364
st : List [inspect .FrameInfo ] = getattr (task , "__pw_stack__" , inspect .stack ())
363
365
metadata = _extract_metadata_from_stack (st , is_internal )
364
366
if metadata :
365
367
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 )
373
372
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 )
378
387
379
388
380
389
def from_channel (channel : Channel ) -> Any :
@@ -388,6 +397,12 @@ def from_nullable_channel(channel: Optional[Channel]) -> Optional[Any]:
388
397
def _extract_metadata_from_stack (
389
398
st : List [inspect .FrameInfo ], is_internal : bool
390
399
) -> Optional [Dict ]:
400
+ if is_internal :
401
+ return {
402
+ "apiName" : "" ,
403
+ "stack" : [],
404
+ "internal" : True ,
405
+ }
391
406
playwright_module_path = str (Path (playwright .__file__ ).parents [0 ])
392
407
last_internal_api_name = ""
393
408
api_name = ""
@@ -419,6 +434,5 @@ def _extract_metadata_from_stack(
419
434
return {
420
435
"apiName" : api_name ,
421
436
"stack" : stack ,
422
- "isInternal" : is_internal ,
423
437
}
424
438
return None
0 commit comments