o
    _cG%                     @   s   d Z ddlZddlZ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 ddlmZmZ ddlmZ erBddlmZ dd	lmZ eeZd
ZedejZG dd deZG dd deZde ddfddZ!dS )zExecution Context Module.    N)AnyDictOptionalTYPE_CHECKING)helper)
CDPSession)ElementHandleErrorNetworkError)
debugError)ElementHandle)FrameZ__pyppeteer_evaluation_script__z(^[\040\t]*//[@#] sourceURL=\s*(\S*?)\s*$c                   @   s   e Zd ZdZ	ddedededdddf
d	d
Zede	d fddZ
dddedededefddZdddedededdfddZdedefddZdddZdS )ExecutionContextzExecution Context class.NclientcontextPayloadobjectHandleFactoryframer   returnc                 C   sB   || _ || _|d| _|dddi}t|d| _|| _d S )NidauxDataZ	isDefaultF)_client_frameget
_contextIdboolZ
_isDefault_objectHandleFactory)selfr   r   r   r   r    r   F/usr/local/lib/python3.10/dist-packages/pyppeteer/execution_context.py__init__    s   
zExecutionContext.__init__c                 C      | j S )z4Return frame associated with this execution context.)r   r   r   r   r   r   *      zExecutionContext.frameF)
force_exprpageFunctionargsr"   c             
      s   | j |g|R d|iI dH }z	| I dH }W n) tyB } zd|jd v r/W Y d}~dS d|jd v r=W Y d}~dS  d}~ww | I dH  |S )zmExecute ``pageFunction`` on this context.

        Details see :meth:`pyppeteer.page.Page.evaluate`.
        r"   Nz"Object reference chain is too longr   z$Object couldn't be returned by value)evaluateHandle	jsonValuer	   r$   dispose)r   r#   r"   r$   handleresulter   r   r   evaluate/   s(   zExecutionContext.evaluateJSHandlec          
   
      sl  dt  }|s|sct|scz"t|r|}n| d| } jd| jddddI dH }W n tyG } z
t	| W Y d}~nd}~ww |
d}|rYtd	t||
d
}	 |	S z! jd| d| d j fdd|D ddddI dH }W n ty } z
t	| W Y d}~nd}~ww |
d}|rtd	t||
d
}	 |	S )zsExecute ``pageFunction`` on this context.

        Details see :meth:`pyppeteer.page.Page.evaluateHandle`.
        z//# sourceURL=
zRuntime.evaluateFT)Z
expressionZ	contextIdreturnByValueawaitPromiseuserGestureNexceptionDetailszEvaluation failed: {}r)   Runtime.callFunctionOnc                    s   g | ]}  |qS r   )_convertArgument).0argr    r   r   
<listcomp>f   s    z3ExecutionContext.evaluateHandle.<locals>.<listcomp>)functionDeclarationZexecutionContextId	argumentsr.   r/   r0   )EVALUATION_SCRIPT_URLr   Z	is_jsfuncSOURCE_URL_REGEXmatchr   sendr   	Exception_rewriteErrorr   r   formatZgetExceptionMessager   )
r   r#   r"   r$   suffixZexpressionWithSourceUrl_objr*   r1   remoteObjectr   r    r   r%   B   s^   







zExecutionContext.evaluateHandler5   c                 C   s   |t jkr	ddiS |t j krddiS t|tr|nd }|rR|j| kr'td|jr.td|jdr<d|jdiS |jdsJd|jdiS d|jdiS d|iS )NZunserializableValueInfinityz	-InfinityzAJSHandles can be evaluated only in the context they were created!zJSHandle is disposed!objectIdvalue)	mathinf
isinstancer,   _contextr   	_disposed_remoteObjectr   )r   r5   objectHandler   r   r   r3   u   s    

z!ExecutionContext._convertArgumentprototypeHandlec                    sR   |j rtd|jdstd| jdd|jd iI dH }| |dS )zSSend query.

        Details see :meth:`pyppeteer.page.Page.queryObjects`.
        zPrototype JSHandle is disposed!rD   z:Prototype JSHandle must not be referencing primitive valuezRuntime.queryObjectsZprototypeObjectIdNobjects)rJ   r   rK   r   r   r<   r   )r   rM   responser   r   r   queryObjects   s   
zExecutionContext.queryObjects)N)rM   r,   r   r,   )__name__
__module____qualname____doc__r   r   r   r   propertyr   r   strr   r+   r%   r3   rP   r   r   r   r   r      s4    



3r   c                   @   s   e Zd ZdZdedededdfddZedefd	d
Z	de
dd fddZdee
d f fddZdefddZded fddZdddZde
fddZdS )r,   zJSHandle class.

    JSHandle represents an in-page JavaScript object. JSHandle can be created
    with the :meth:`~pyppeteer.page.Page.evaluateHandle` method.
    contextr   rB   r   Nc                 C   s   || _ || _|| _d| _d S )NF)rI   r   rK   rJ   )r   rW   r   rB   r   r   r   r      s   
zJSHandle.__init__c                 C   r   )z%Get execution context of this handle.)rI   r    r   r   r   executionContext   r!   zJSHandle.executionContextpropertyNamec                    s@   | j d| |I dH }| I dH }|| }| I dH  |S )z'Get property value of ``propertyName``.z(object, propertyName) => {
                const result = {__proto__: null};
                result[propertyName] = object[propertyName];
                return result;
            }N)rI   r%   getPropertiesr'   )r   rY   rL   
propertiesr)   r   r   r   getProperty   s   
zJSHandle.getPropertyc                    sf   | j d| jddddI dH }t }|d D ]}|ds"q| j|d	||d
< q|S )z"Get all properties of this handle.zRuntime.getPropertiesrD    T)rD   ZownPropertiesNr)   Z
enumerablerE   name)r   r<   rK   r   dictrI   r   )r   rO   r)   propr   r   r   rZ      s   
zJSHandle.getPropertiesc                    sJ   | j d}|r| jdd|dddI dH }t|d S t| j S )z"Get Jsonized value of this object.rD   r2   zfunction() { return this; }T)r7   rD   r.   r/   Nr)   )rK   r   r   r<   r   valueFromRemoteObject)r   rD   rO   r   r   r   r&      s   zJSHandle.jsonValuer   c                 C   s   dS )z/Return either null or the object handle itself.Nr   r    r   r   r   	asElement   s   zJSHandle.asElementc              
      s`   | j rdS d| _ zt| j| jI dH  W dS  ty/ } ztt| W Y d}~dS d}~ww )zStop referencing the handle.NT)rJ   r   ZreleaseObjectr   rK   r=   r
   logger)r   r*   r   r   r   r'      s   zJSHandle.disposec                 C   s@   | j dr| j dp| j d}d| S dt| j S )zGet string representation.rD   subtypetypez	JSHandle@zJSHandle:{})rK   r   r?   r   ra   )r   _typer   r   r   toString   s   


zJSHandle.toString)r   N)rQ   rR   rS   rT   r   r   r   r   rU   rX   rV   r\   rZ   r&   r   rb   r'   rg   r   r   r   r   r,      s    



r,   errorr   c                 C   s$   | j d drd}t| || )Nr   z%Cannot find context with specified idzEExecution context was destroyed, most likely because of a navigation.)r$   endswithre   )rh   msgr   r   r   r>      s   r>   )"rT   loggingrF   retypingr   r   r   r   Z	pyppeteerr   Zpyppeteer.connectionr   Zpyppeteer.errorsr   r	   Zpyppeteer.helperr
   Zpyppeteer.element_handler   Zpyppeteer.frame_managerr   	getLoggerrQ   rc   r9   compile	MULTILINEr:   objectr   r,   r=   r>   r   r   r   r   <module>   s*   
zS