
    i;                         d dl Z d dlmZmZmZmZ d dlmZ d dlmZm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d d	lmZ d dlmZ erd d
lmZ  G d d          ZdS )    N)TYPE_CHECKINGAnyOptionalcast)greenlet)ChannelOwner
Connection)Error)MainGreenlet)create_remote_object)
Playwright)PipeTransport)AbstractChildWatcherc                   >    e Zd ZddZdefdZdefdZdeddfdZdS )	PlaywrightContextManagerreturnNc                 8    |  |  d| _         d | _        d| _        d S )NF)	_own_loop_watcher_exit_was_calledselfs    d/Users/shanyulin/my-agent/.venv/lib/python3.11/site-packages/playwright/sync_api/_context_manager.py__init__z!PlaywrightContextManager.__init__!   s*    (-8< %    c                 F    	 t          j                     _        n/# t          $ r" t          j                     _        d _        Y nw xY w j                                        rt          d          d fd}t          |          }t          |t          t           j                   j                   _        t          j                    dt          dd f fd} j                            d|           |                                  j        } j        |_        |S )	NTzjIt looks like you are using Playwright Sync API inside the asyncio loop.
Please use the Async API instead.r   c                  j     j                              j                                                   d S N)_looprun_until_complete_connectionrun_as_syncr   s   r   greenlet_mainz9PlaywrightContextManager.__enter__.<locals>.greenlet_main7   s.    J))$*:*F*F*H*HIIIIIr   channel_ownerc                     t          t          |           }t          |          _                                         d S r   )r   r   SyncPlaywright_playwrightswitch)r$   playwright_implg_selfr   s     r   callback_wrapperz<PlaywrightContextManager.__enter__.<locals>.callback_wrapperE   s3    ":}==O-o>>DMMOOOOOr   r   r   N)asyncioget_running_loopr   RuntimeErrornew_event_loopr   
is_runningr
   r   r	   r   r   r!   r   
getcurrentr   call_on_object_with_known_namer(   r'   __exit__stop)r   r#   dispatcher_fiberr+   
playwrightr*   s   `    @r   	__enter__z"PlaywrightContextManager.__enter__(   sd   	" 133DJJ 	" 	" 	" /11DJ!DNNN	" :  "" 	%  	J 	J 	J 	J 	J 	J (66% $*%%J	
 
 $&&	L 	T 	 	 	 	 	 	 	 	77FVWWW!!!%
-
s    )A	A	c                 *    |                                  S r   )r8   r   s    r   startzPlaywrightContextManager.startS   s    ~~r   argsc                    | j         rd S d| _         | j                                         | j        r| j                                         | j        rt          j        | j                  }d |D             D ]}|	                                 | j        
                    | j                                                   | j                                         d S d S )NTc                 b    g | ],}|                                 |                                *|-S  )done	cancelled).0ts     r   
<listcomp>z5PlaywrightContextManager.__exit__.<locals>.<listcomp>_   s2    JJJAqvvxxJ1;;==JaJJJr   )r   r!   	stop_syncr   closer   r-   	all_tasksr   cancelr    shutdown_asyncgens)r   r;   tasksrB   s       r   r4   z!PlaywrightContextManager.__exit__V   s      	F $""$$$= 	"M!!!> 	%dj11EJJJJJ  



J))$**G*G*I*IJJJJ	 	r   r,   )	__name__
__module____qualname__r   r&   r8   r:   r   r4   r>   r   r   r   r       s        & & & &)> ) ) ) )V ~        c d      r   r   )r-   typingr   r   r   r   r   playwright._impl._connectionr   r	   playwright._impl._errorsr
   playwright._impl._greenletsr    playwright._impl._object_factoryr   playwright._impl._playwrightr   playwright._impl._transportr   playwright.sync_api._generatedr&   asyncio.unix_eventsr   r   r>   r   r   <module>rV      s     5 5 5 5 5 5 5 5 5 5 5 5       A A A A A A A A * * * * * * 4 4 4 4 4 4 A A A A A A 3 3 3 3 3 3 5 5 5 5 5 5 G G G G G G 9888888B B B B B B B B B Br   