a
    ҏi^                     @   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rd d
lmZ G dd dZdS )    N)TYPE_CHECKINGAnyOptionalcast)greenlet)ChannelOwner
Connection)compute_driver_executable)Error)create_remote_object)
Playwright)PipeTransport)AbstractChildWatcherc                   @   sF   e Zd ZddddZedddZedddZedd	d
dZdS )PlaywrightContextManagerNreturnc                 C   s   |  |  d| _ d | _d| _d S )NF)	_own_loop_watcher_exit_was_calledself r   T/home/Claro/venv/lib/python3.9/site-packages/playwright/sync_api/_context_manager.py__init__!   s
    z!PlaywrightContextManager.__init__c                    s   zt  _W n" ty0   t  _d_Y n0 j rDtdd dfdd}t|}t	|t
tjt j_t  td d 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_connectionZrun_as_syncr   r   r   r   greenlet_main7   s    z9PlaywrightContextManager.__enter__.<locals>.greenlet_main)channel_ownerr   c                    s    t t| }t|_   d S r   )r   r   SyncPlaywright_playwrightswitch)r   Zplaywright_implZg_selfr   r   r   callback_wrapperE   s    

z<PlaywrightContextManager.__enter__.<locals>.callback_wrapperr   )asyncioZget_running_loopr   RuntimeErrorZnew_event_loopr   Z
is_runningr
   r   r   r   r   r	   r   Z
getcurrentr   Zcall_on_object_with_known_namer"   r!   __exit__stop)r   r   Zdispatcher_fiberr$   Z
playwrightr   r#   r   	__enter__(   s0    

z"PlaywrightContextManager.__enter__c                 C   s   |   S r   )r)   r   r   r   r   startS   s    zPlaywrightContextManager.start)argsr   c                 G   sx   | j r
d S d| _ | j  | jr*| j  | jrtt| j}dd |D D ]}|	  qJ| j
| j  | j  d S )NTc                 S   s    g | ]}|  s| s|qS r   )doneZ	cancelled).0tr   r   r   
<listcomp>_       z5PlaywrightContextManager.__exit__.<locals>.<listcomp>)r   r   Z	stop_syncr   closer   r%   Z	all_tasksr   cancelr   Zshutdown_asyncgens)r   r+   Ztasksr.   r   r   r   r'   V   s    


z!PlaywrightContextManager.__exit__)	__name__
__module____qualname__r   r    r)   r*   r   r'   r   r   r   r   r       s   +r   )r%   typingr   r   r   r   r   Zplaywright._impl._connectionr   r   Zplaywright._impl._driverr	   Zplaywright._impl._errorsr
   Z playwright._impl._object_factoryr   Zplaywright._impl._playwrightr   Zplaywright._impl._transportr   Zplaywright.sync_api._generatedr    Zasyncio.unix_eventsr   r   r   r   r   r   <module>   s   