o
    â%ƒh;  ã                   @   s¦   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rJd d
lmZ G dd„ dƒZdS )é    N)ÚTYPE_CHECKINGÚAnyÚOptionalÚcast)Úgreenlet)ÚChannelOwnerÚ
Connection)ÚError)ÚMainGreenlet)Úcreate_remote_object)Ú
Playwright)ÚPipeTransport)ÚAbstractChildWatcherc                   @   sD   e Zd Zddd„Zdefdd„Zdefdd„Zd	eddfd
d„ZdS )ÚPlaywrightContextManagerÚreturnNc                 C   s   |  |  d| _ d | _d| _d S )NF)Ú	_own_loopÚ_watcherÚ_exit_was_called©Úself© r   úX/var/www/html/myenv/lib/python3.10/site-packages/playwright/sync_api/_context_manager.pyÚ__init__!   s
   
z!PlaywrightContextManager.__init__c                    sº   zt  ¡ ˆ_W n ty   t  ¡ ˆ_dˆ_Y nw ˆj ¡ r"tdƒ‚d
‡fdd„}t|ƒ}t	|t
tˆjƒˆjƒˆ_t ¡ ‰ dtdd f‡ ‡fd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                      s   ˆ j  ˆ j ¡ ¡ d S ©N)Ú_loopÚrun_until_completeÚ_connectionÚrun_as_syncr   r   r   r   Úgreenlet_main7   s   z9PlaywrightContextManager.__enter__.<locals>.greenlet_mainÚchannel_ownerc                    s    t t| ƒ}t|ƒˆ_ˆ  ¡  d S r   )r   r   ÚSyncPlaywrightÚ_playwrightÚswitch)r   Úplaywright_impl©Úg_selfr   r   r   Úcallback_wrapperE   s   

z<PlaywrightContextManager.__enter__.<locals>.callback_wrapperr   ©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   r$   r   Ú	__enter__(   s2   

þ
ÿüz"PlaywrightContextManager.__enter__c                 C   s   |   ¡ S r   )r3   r   r   r   r   ÚstartS   s   zPlaywrightContextManager.startÚargsc                 G   s|   | j rd S d| _ | j ¡  | jr| j ¡  | jr<t | j¡}dd„ |D ƒD ]}| 	¡  q%| j 
| j ¡ ¡ | j ¡  d S d S )NTc                 S   s    g | ]}|  ¡ s| ¡ s|‘qS r   )ÚdoneÚ	cancelled)Ú.0Útr   r   r   Ú
<listcomp>_   s     z5PlaywrightContextManager.__exit__.<locals>.<listcomp>)r   r   Ú	stop_syncr   Úcloser   r(   Ú	all_tasksr   Úcancelr   Úshutdown_asyncgens)r   r5   Útasksr9   r   r   r   r/   V   s   


ûz!PlaywrightContextManager.__exit__r'   )	Ú__name__Ú
__module__Ú__qualname__r   r    r3   r4   r   r/   r   r   r   r   r       s
    
+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   r   Ú<module>   s   