o
    iqA                     @   s  d Z ddlmZ ddlmZ ddlmZ ddlZddl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 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 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e"e#ef e"e#e$e f ef Z%ee&Z'de#de(fddZ)de#dede$e fddZ*ded e#de(fd!d"Z+d#eee#ef gef d$ee#ef de(fd%d&Z,d$ee#ef de#defd'd(Z-G d)d* d*Z.e. j/Z0G d+d, d,e1Z2d-eddfd.d/Z3eG d0d1 d1Z4dS )2a  Debugger expression language

This module implements the debugger expression language that is used in the UI
to define probe conditions and metric expressions. The JSON AST is compiled into
Python bytecode.

Full grammar:

    predicate               =>  <direct_predicate> | <arg_predicate> | <value_source>
    direct_predicate        =>  {"<direct_predicate_type>": <predicate>}
    direct_predicate_type   =>  not | isEmpty | isDefined
    value_source            =>  <literal> | <operation>
    literal                 =>  <number> | true | false | "string"
    number                  =>  0 | ([1-9][0-9]*\.[0-9]+)
    identifier              =>  <str.isidentifier>
    arg_predicate           =>  {"<arg_predicate_type>": [<argument_list>]}
    arg_predicate_type      =>  eq | ne | gt | ge | lt | le | any | all | and | or
                                | startsWith | endsWith | contains | matches
    argument_list           =>  <predicate>(,<predicate>)+
    operation               =>  <direct_operation> | <arg_operation>
    direct_opearation       =>  {"<direct_op_type>": <value_source>}
    direct_op_type          =>  len | count | ref
    arg_operation           =>  {"<arg_op_type>": [<argument_list>]}
    arg_op_type             =>  filter | substring | getmember | index
    )	dataclass)Decimal)chainN)FunctionType)Any)Callable)
Collection)Mapping)Optional)Union)cast)BinaryOp)Bytecode)Compare)Instr)Labelsafe_getitem)PYTHON_VERSION_INFO)
get_logger)_isinstancenamereturnc                 C   s   t | to|  S N)
isinstancestrisidentifier)r    r   R/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/debugging/_expressions.py_is_identifier:   s   r   oplabelc                 C   sz   | dkrdnd}t dkrtddtdtd| |td	gS t d
kr3tddtd| |td	gS td| d|gS )NandFALSETRUE      COPY   TO_BOOLPOP_JUMP_IF_POP_TOPr&      JUMP_IF__OR_POP)PYr   )r    r!   valuer   r   r   short_circuit_instrs>   s   & r3   r2   
type_qnamec                 C   s   zt | t| W S  tyM   z)tt| dD ]}t|d}t|d}| d| |kr4 W Y dS qW Y dS  tyL   td|t|  Y Y dS w w )N__mro__
__module____qualname__.Tz2Failed to check instanceof %s for value of type %sF)	r   __builtins__KeyErrorobject__getattribute__typeAttributeErrorlogdebug)r2   r4   cmodulequalnamer   r   r   
instanceofH   s"   
rD   	predicate_localsc                 C   s$   z| | W dS  t y   Y dS w )NFT)BaseException)rE   rF   r   r   r   	isdefinedZ   s   
rH   c                 C   s*   z| | W S  t y   td| dw )NzNo such local variable: '')r:   	NameError)rF   r   r   r   r   	get_localb   s
   
rK   c                   @   s  e Zd Zd-ddZedededefddZeded	edefd
dZededefddZ	de
e deedf dedefddZdedeeegef fddZdedee
e  fddZdedee
e  fddZdedee
e  fddZdede
e de
e fdd Zdedee
e  fd!d"Zdedee
e  fd#d$Zdedee
e  fd%d&Zdedee
e  fd'd(Zdedee
e  fd)d*Zdedeeeef gef fd+d,ZdS ).
DDCompilerr   Nc                 C   s
   d| _ d S )Nr   )_lambda_level)selfr   r   r   __init__j      
zDDCompiler.__init__oac                 C   s   t ||S r   )r;   r<   )clsrQ   rR   r   r   r   __getmember__m   s   zDDCompiler.__getmember__ic                 C   s
   t ||S r   r   )rS   rQ   rU   r   r   r   	__index__q   s   
zDDCompiler.__index__xc                 C   s   |S r   r   )rS   rW   r   r   r   __ref__u   s   zDDCompiler.__ref__instrsargs.r   c                 C   sZ   t g |td}t||_||_||_tjdkr#|dtdd t	|
 i |dd S )NRETURN_VALUEr&      r   RESUMEr   )r   r   lenargcountargnamesr   sysversion_infoinsertr   to_code)rN   rY   rZ   r   abstract_coder   r   r   _make_functiony   s   

zDDCompiler._make_functionastc              	   C   sx   |  j d7  _ | | }d u rtd| z| |ddW | j dks%J |  j d8  _ S | j dks4J |  j d8  _ w )Nr)   Invalid predicate: %r)_dd_it_dd_key	_dd_valuerF   z<lambda>r   )rM   _compile_predicate
ValueErrorrg   rN   rh   rE   r   r   r   _make_lambda   s   zDDCompiler._make_lambdac              
   C   s   t |tsd S tt| \}}|dvrd S | |}|d u r&td| |dkr@| tt	d| 
|ddgt	ddg}|S td	krK|t	d
 |t	d |S )N>   notisEmpty	isDefinedInvalid argument: %rrs   
LOAD_CONSTrF   z<isDefined-predicate>	LOAD_FASTrF   r%   r*   	UNARY_NOT)r   dictnextiteritemsrm   rn   _call_functionrH   r   rg   r1   append)rN   rh   _typeargr2   r   r   r   _compile_direct_predicate   s&   


z$DDCompiler._compile_direct_predicatec              
      s  t |tsd S tt| \}}|dv rG|\}}| || |}}|d u r.td| |d u r8td| t }|t|| | |g S |dv r}|\}}| || |}}|d u rdtd| |d u rntd| || t	dt
t| g S |dkr|\}}| || |}}|d u rtd| |d u rtd| || t	ddg S |dv r|\}}t|  | || |}}	|d u rtd| d	td
tttttttf gtf dtttf dtf fdd}
| |
|t	d|	gt	ddgS |dv r4|\}}| || |}}|d u rtd| |d u r(td| | t
t| ||S |dkrg|\}}| || |}}|d u rStd| |d u r^td| | dd ||S d S )N>   orr"   rt   >   eqgegtleltne
COMPARE_OPcontainsCONTAINS_OPr   >   allanyitcondrF   r   c                    sD   t | tr fddtt|  D S  fdd| D S )Nc                 3   s"    | ]\}}||| V  qd S r   r   .0kvrF   r   r   r   	<genexpr>        zGDDCompiler._compile_arg_predicate.<locals>.coll_iter.<locals>.<genexpr>c                 3   s    | ]
}|d d  V  qd S r   r   r   er   r   r   r      s    )r   ry   r   r|   r   r   rF   fr   r   	coll_iter   s   
"z4DDCompiler._compile_arg_predicate.<locals>.coll_iterru   rw   >   endsWith
startsWithmatchesc                 S   s   t | |d uS r   )rematch)psr   r   r   <lambda>   s    z3DDCompiler._compile_arg_predicate.<locals>.<lambda>)r   ry   rz   r{   r|   rm   rn   r   r3   r   getattrr   upperr9   rp   r   r   r   r   boolr}   lower)rN   rh   r   rZ   rR   bcacbshort_circuitfbr   stringpatternr   r   r   _compile_arg_predicate   sz   

 





z!DDCompiler._compile_arg_predicatec              	   C   s   t |tsd S tt| \}}|dv r*| |}|d u r$td| | t|S |dkrgt |t	s5d S |dv rU| j
dkrHd| d}t|tdd	|d
d   gS | ttddgtd| |gS d S )N>   r_   countrt   ref>   @it@key@valuer   zInvalid use of z outside of lambdarw   _dd_r)   rF   ru   )r   ry   rz   r{   r|   _compile_value_sourcern   r}   r_   r   rM   r   rK   rX   )rN   rh   r   r   r2   msgr   r   r   _compile_direct_operation   s(   



z$DDCompiler._compile_direct_operationfuncc                 G   s   t dkrtd|tdgtt|  tdt|g S t dkr6tdtd|gtt|  tdt|g S t dkrWtdtd|gtt|  tdt|tdt|g S td|gtt|  tdt|g S )	Nr%   ru   	PUSH_NULLCALLr-   r\   PRECALLCALL_FUNCTION)r1   r   listr   r_   )rN   r   rZ   r   r   r   r}     s   ..
(zDDCompiler._call_functionc              
   C   sH  t |tsd S tt| \}}|dvrd S |dkrk|\}}}| || || |}}}	|d u r;td| |d u rEtd| |	d u rOtd| tdkrZtdt	j
}
ntd}
|| |	 tdd|
g S |d	kr|\}}| || |}}|d u rtd| d
tdtttttttf gtf dtttf dtfdd}| ||td|gtddgS |dkr|\}}t|std| | |}|sd S | | j|td|gS |dkr|\}}| |}|sd S | |}|sd S | | j||S |dkr"|\}}| |}|sd S | |}|sd S | t||S d S )N>   indexfilter	getmember	substringrD   r   rt   )r&      	BINARY_OPBINARY_SUBSCRBUILD_SLICE   r   r   r   rF   r   c                    sL   t | trt|  fddtt|  D S t|  fdd| D S )Nc                    s$   i | ]\}}||| r||qS r   r   r   r   r   r   
<dictcomp>J  s   $ zJDDCompiler._compile_arg_operation.<locals>.coll_filter.<locals>.<dictcomp>c                 3   s"    | ]}|d d  r|V  qd S r   r   r   r   r   r   r   K  r   zIDDCompiler._compile_arg_operation.<locals>.coll_filter.<locals>.<genexpr>)r   ry   r=   r   r|   r   r   r   r   coll_filterF  s   
&z6DDCompiler._compile_arg_operation.<locals>.coll_filterru   rw   r   zInvalid identifier: %rr   rD   )r   ry   rz   r{   r|   rm   rn   r1   r   r   SUBSCRrp   r   r   r   r   r}   r   rT   rV   rD   )rN   rh   r   rZ   r   rR   r   cvr   r   subscr_instructionr   r   attrrU   citctr   r   r   _compile_arg_operation$  sx   

"

 





z!DDCompiler._compile_arg_operationc                 C      |  |p	| |S r   )r   r   rN   rh   r   r   r   _compile_operationp     zDDCompiler._compile_operationc                 C   s,   t |tttttfs|d u sd S td|gS )Nru   )r   r   intfloatr   r   r   r   r   r   r   _compile_literalt  s   zDDCompiler._compile_literalc                 C   r   r   )r   r   r   r   r   r   r   {  r   z DDCompiler._compile_value_sourcec                 C   s   |  |p| |p| |S r   )r   r   r   r   r   r   r   rm     s   zDDCompiler._compile_predicatec                 C   s,   |  | }d u rtd| | |ddS )Nri   rv   z<expr>)rm   rn   rg   ro   r   r   r   compile  s   zDDCompiler.compile)r   N)__name__r6   r7   rO   classmethodr   r   rT   rV   rX   r   r   tupler   rg   	DDASTTyper   rp   r
   r   r   r   r}   r   r   r   r   rm   r	   r   r   r   r   r   rL   i   s(    
&KL(rL   c                       s.   e Zd ZdZdededdf fddZ  ZS )DDExpressionEvaluationErrorz>Thrown when an error occurs while evaluating a dsl expression.dslr   r   Nc                    s,   t  d|t|f  || _t|| _d S )Nz&Failed to evaluate expression "%s": %s)superrO   r   r   error)rN   r   r   	__class__r   r   rO     s   z$DDExpressionEvaluationError.__init__)r   r6   r7   __doc__r   	ExceptionrO   __classcell__r   r   r   r   r     s    "r   _c                 C   s   dS )zForces probes with invalid expression/conditions to never trigger.

    Any signs of invalid conditions in logs is an indication of a problem with
    the expression compiler.
    Nr   )r   r   r   r   _invalid_expression  s   r   c                
   @   s   e Zd ZU eZeed< eeee	f ge	f ed< deee	f de	fddZ
deee	f de	fddZeded	edeeee	f ge	f fd
dZedeee	f dd fddZdS )DDExpressionr   callablescoper   c              
   C   s4   z|  |W S  ty } zt| j||d }~ww r   )r   r   r   r   )rN   r   r   r   r   r   eval  s   zDDExpression.evalc                 C   s
   |  |S r   )r   )rN   r   r   r   r   __call__  rP   zDDExpression.__call__excc                 C   s   t jd|dd tS )NzCannot compile expression: %sT)exc_info)r?   r   r   )rS   r   r   r   r   r   on_compiler_error  s   zDDExpression.on_compiler_errorexprc              
   C   s\   |d }|d }z|  |}W n ty' } z| ||}W Y d }~nd }~ww | ||dS )Njsonr   )r   r   )__compiler__r   r   )rS   r   rh   r   compiledr   r   r   r   r     s   zDDExpression.compileN)r   r6   r7   
dd_compiler   r   __annotations__r   r	   r   r   r   r   r   r   r   r   r   r   r   r     s   
 * r   )5r   dataclassesr   decimalr   	itertoolsr   r   rb   typesr   typingr   r   r   r	   r
   r   r   bytecoder   r   r   r   r   ddtrace.debugging._safetyr   ddtrace.internal.compatr   r1   ddtrace.internal.loggerr   ddtrace.internal.safetyr   ry   r   r   r   r   r?   r   r   r3   rD   rH   rK   rL   r   r   r   r   r   r   r   r   r   r   <module>   sL    "
0  %		