
    X-PhF                       U d 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mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z% ddl&m'Z' dd	l(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. dd
l/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z;  G d d          Z<i d e<d          d e<d          d e<d          d e<d          d e<d          d e<d          d e<d          d e<d           d! e<d"          d# e<d$          d% e<d&          d' e<d(          d) e<d*          d+ e<d,          d- e<d.          d/ e<d0          d1 e<d2          Z=d3e>d4<    e<d5d67           e<d8d67           e<d9d67           e<d:d67           e<d;d<7          d=Z?d3e>d><   d?Z@d@ZA G dA dB          ZBdjdMZCdkdUZDdldYZE G dZ d[e'          ZFdmd_ZGdndbZHdodeZIdpdiZJdS )qzGenerate source code formatted as HTML, with bottlenecks annotated and highlighted.

Various heuristics are used to detect common issues that cause slower than
expected performance.
    )annotationsN)escape)Final)BuildResult)AssignmentStmtCallExprClassDef	DecoratorDictionaryComprehension
ExpressionForStmtFuncDefGeneratorExpr	IndexExpr
LambdaExpr
MemberExprMypyFileNamedTupleExprNameExprNewTypeExprNodeOpExprRefExpr	TupleExprTypedDictExprTypeInfoTypeVarExprVarWithStmt)TraverserVisitor)AnyTypeInstance
ProperTypeType	TypeOfAnyget_proper_type)FancyFormatter)FuncIR)ModuleIR)CallCLoadLiteral
LoadStaticValue)Mapperc                      e Zd ZdZddd	Zd
S )
Annotationz(HTML annotation for compiled source code   messagestrpriorityintreturnNonec                "    || _         || _        d S N)r2   r4   )selfr2   r4   s      N/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/mypyc/annotate.py__init__zAnnotation.__init__6   s     
 !    Nr1   )r2   r3   r4   r5   r6   r7   __name__
__module____qualname____doc__r<    r=   r;   r0   r0   3   s3        22! ! ! ! ! ! !r=   r0   PyNumber_AddzGeneric "+" operation.PyNumber_SubtractzGeneric "-" operation.PyNumber_MultiplyzGeneric "*" operation.PyNumber_TrueDividezGeneric "/" operation.PyNumber_FloorDividezGeneric "//" operation.PyNumber_PositivezGeneric unary "+" operation.PyNumber_NegativezGeneric unary "-" operation.PyNumber_AndzGeneric "&" operation.PyNumber_OrzGeneric "|" operation.PyNumber_XorzGeneric "^" operation.PyNumber_LshiftzGeneric "<<" operation.PyNumber_RshiftzGeneric ">>" operation.PyNumber_InvertzGeneric "~" operation.PyObject_CallzGeneric call operation.PyObject_RichComparezGeneric comparison operation.PyObject_GetItemzGeneric indexing operation.PyObject_SetItemzGeneric indexed assignment.r   op_hintsz4"functools.partial" is inefficient in compiled code.   r4   zQ"itertools.chain" is inefficient in compiled code (hint: replace with for loops).z4"itertools.groupby" is inefficient in compiled code.zb"itertools.islice" is inefficient in compiled code (hint: replace with for loop over index range).zB"copy.deepcopy" tends to be slow. Make a shallow copy if possible.   )zfunctools.partialzitertools.chainzitertools.groupbyzitertools.islicezcopy.deepcopystdlib_hintsz.collapsible {
    cursor: pointer;
}

.content {
    display: block;
    margin-top: 10px;
    margin-bottom: 10px;
}

.hint {
    display: inline;
    border: 1px solid #ccc;
    padding: 5px;
}
ab  document.querySelectorAll('.collapsible').forEach(function(collapsible) {
    collapsible.addEventListener('click', function() {
        const content = this.nextElementSibling;
        if (content.style.display === 'none') {
            content.style.display = 'block';
        } else {
            content.style.display = 'none';
        }
    });
});
c                      e Zd ZdZd
dZd	S )AnnotatedSourcez.Annotations for a single compiled source file.pathr3   r   dict[int, list[Annotation]]r6   r7   c                "    || _         || _        d S r9   )r]   r   )r:   r]   r   s      r;   r<   zAnnotatedSource.__init__   s    	&r=   N)r]   r3   r   r^   r6   r7   r?   rD   r=   r;   r\   r\      s.        88' ' ' ' ' 'r=   r\   	html_fnamr3   resultr   modulesdict[str, ModuleIR]mapperr.   r6   r7   c           
     R   g }|                                 D ]U\  }}|j        |         j        }|j        |         j        }|J  |j        t          |pd|||j        |                     Vt          |          }	t          | d          5 }
|
	                    |	           d d d            n# 1 swxY w Y   t          t          j        t          j        d          }|                    t          j                            |           ddd          }t#          d| d           d S )	Nz<source>wFnoneT)	underlineboldz
Wrote z -- open in browser to view
)itemsgraphr]   treeappendgenerate_annotationstypesgenerate_html_reportopenwriter'   sysstdoutstderrstyleosabspathprint)r`   ra   rb   rd   r   modmod_irr]   rl   htmlf	formatter	formatteds                r;   generate_annotated_htmlr      sd    K}} 
 
V|C %|C % !3T66<QWXX	
 	
 	
 	
  ,,D	i		 	               sz3:u==I	 : :FdY]^^I	
=Y
=
=
=>>>>>s   B..B25B2r]   rl   r   irr)   type_mapdict[Expression, Type]c                4   i }|j         D ]%}|                    t          ||                     &t          ||          }|j        D ]}|                    |           |                    |j                   |j        D ]	}	|	|v r||	= 
t          | |          S r9   )		functionsupdatefunction_annotationsASTAnnotateVisitordefsacceptannsignored_linesr\   )
r]   rl   r   r   rd   r   func_irvisitordefnlines
             r;   rn   rn      s     D< 9 9($778888 622G	  GKK%  4<<T
4&&&r=   r   r(   r^   c                   i }| j         D ]@}|j        D ]4}t          |t                    r|j        }d}|dk    r0t          |j        d                   }|dv rd}n|rd| d}nd}n|dk    r2t          |j        d                   }|d	k    rd}n^|rd
| d}nTd}nP|dk    r(t          |j        d                   }|rd| d}n&d}n"|t          v rt          |         }n
|dv rt          |j        d         t                    rt          |j        d         t                    r| j
        dk    r|j        d         }	t          |j        d         j                  }|j                            |          }
|
rr|
j        rk|	j        dk    r`|	j        dk    rU|
j        j        t&          v rt&          |
j        j                 }n*t          |
j        t(                    r
d| ddz   }nd| d}|rRt          |t                    rt+          |          }|                    |j        g                               |           6B|S )z'Generate annotations based on mypyc IR.NCPyObject_GetAttrr1   )__prepare__GeneratorExitStopIterationzGet non-native attribute "".zDynamic attribute lookup.PyObject_SetAttr__mypyc_attrs__zSet non-native attribute "zDynamic attribute set.PyObject_VectorcallMethodr   zCall non-native method "z:" (it may be defined in a non-native class, or decorated).zDynamic method call.)CPyDict_GetItemCPyDict_SetItem__top_level__staticglobalszAccess global "z" through namespace z=dictionary (hint: access is faster if you can make it Final).zAccess "z&" through global namespace dictionary.)blocksops
isinstancer*   function_nameget_str_literalargsrV   r,   r+   namer3   valuenamesgetnode	namespace
identifierfullnamerZ   r   r0   
setdefaultr   rm   )r   rl   r   blockopr   ann	attr_namemethod_nameloadsyms              r;   r   r      s    )+D <= <=) ;	= ;	=B"e$$ :='/3... /
 ; ;I $UUU #" :H9HHH9/// /
 ; ;I $555"" 7H9HHH6888"1"'!*"="=K" 5 A  A  A  A4X%%"4.CCCCC"271:z::^&rwqz;??^ $LO;;!wqz"271:#344"jnnT22^ #^ !%( : : $9 < <"x0L@@&2383D&E!+CHc!:!: ^$Pd$P$P$P&e%f !$
 '^&]&]&] =!#s++ .(ooOOBGR0077<<<w;	=x Kr=   c                       e Zd ZdZd2dZd3 fdZd4 fdZd5 fdZd6 fdZd7dZ	d8 fdZ
d9 fdZd: fdZd;dZd< fd Zd= fd"Zd>d%Zd? fd'Zd@dAd.ZdBd1Z xZS )Cr   z6Generate annotations from mypy AST and inferred types.r   r   rd   r.   r6   r7   c                d    i | _         t                      | _        d| _        || _        || _        d S Nr   )r   setr   
func_depthr   rd   )r:   r   rd   s      r;   r<   zASTAnnotateVisitor.__init__   s.    13	'*uu r=   or   c                   | j         dk    r|                     |d           | xj         dz  c_         t                                          |           | xj         dz  c_         d S )Nr   zoA nested function object is allocated each time statement is executed. A module-level function would be faster.r1   )r   annotatesupervisit_func_defr:   r   	__class__s     r;   r   z!ASTAnnotateVisitor.visit_func_def  sl    ?QMM=  
 	1q!!!1r=   r   c                   |                      |j        gd           t                                          |           d S )NzFor loop)check_iterationexprr   visit_for_stmtr   s     r;   r   z!ASTAnnotateVisitor.visit_for_stmt  s;    afXz222q!!!!!r=   r   c                   |                      |j        d           t                                          |           d S )NComprehension)r   	sequencesr   visit_dictionary_comprehensionr   s     r;   r   z1ASTAnnotateVisitor.visit_dictionary_comprehension  s9    Q[/:::..q11111r=   r   c                   |                      |j        d           t                                          |           d S )NzComprehension or generator)r   r   r   visit_generator_exprr   s     r;   r   z'ASTAnnotateVisitor.visit_generator_expr  s:    Q[*FGGG$$Q'''''r=   expressionslist[Expression]kindr3   c                *   |D ]}|                      |          }t          |t                    r|                     || d           Ft          |t                    r4|j        j        dv r&|                     || d|j        j         d           d S )Nz3 uses generic operations (iterable has type "Any").)ztyping.Iterableztyping.Iteratorztyping.Sequenceztyping.MutableSequencez: uses generic operations (iterable has the abstract type "z").)get_typer   r!   r   r"   typer   )r:   r   r   r   typs        r;   r   z"ASTAnnotateVisitor.check_iteration  s     	 	D--%%C#w'' dt$`$`$`aaaaC** 	sx/@ E 0 0 mmWZW_Whmmm  	 	r=   r	   c                  t                                          |           | j        dk    r|j        D ]!}| j                            |j                   "|j        j        D ]r}t          |t                    r | j                            |j                   7t          |t                    r$| j                            |j        j                   qd S d S r   )r   visit_class_defr   base_type_exprsr   addr   r   bodyr   r   r
   func)r:   r   basesr   s       r;   r   z"ASTAnnotateVisitor.visit_class_def*  s    """?a) 2 2"&&ty1111V[ 8 8a00 8&**1622229-- 8 &**16;777  
8 8r=   r   c                  |j         D ]}t          |t                    rt          |j        t                    rf|j        j        }t          |t                    rEt          d |j        D                       r'| 	                    |d|j
         ddz   dz   d           t                                          |           d S )Nc              3  n   K   | ]0}t          |t                    o|j        o|j        j        d k    V  1dS )zcontextlib.contextmanagerN)r   r   r   r   ).0ds     r;   	<genexpr>z5ASTAnnotateVisitor.visit_with_stmt.<locals>.<genexpr>?  s`          #1g.. KFKFO/JJ     r=   "z&" uses @contextmanager, which is slow z*in compiled code. Use a native class with z+"__enter__" and "__exit__" methods instead.rW   rX   )r   r   r   calleer   r   r
   any
decoratorsr   r   r   visit_with_stmt)r:   r   r   r   r   s       r;   r   z"ASTAnnotateVisitor.visit_with_stmt:  s    F 	 	D$)) jg.N.N {'dI..    "&	       Q	QQQJKKL &' &    	"""""r=   r   c               |   d}| j         dk    r|j        }t          |j        t          t          t
          f          r|j        j        }|j        s(t          |t          t          t          t          f          rd}|r| j                            |j                   t                                          |           d S )NFr   T)r   rvaluer   r   r   r   analyzedis_alias_defr   r   r   r   r   r   r   r   visit_assignment_stmt)r:   r   special_formr   r   s       r;   r   z(ASTAnnotateVisitor.visit_assignment_stmtN  s    ?a*+(H!(Xy&$ABB -8,~ $;{S" " $  $ /"&&qv...%%a(((((r=   r   c               x    t                               |j                  x}r|                     ||           d S d S r9   )rZ   r   r   r   )r:   r   r   s      r;   visit_name_exprz"ASTAnnotateVisitor.visit_name_expr]  sC    ""1:...3 	"MM!S!!!!!	" 	"r=   r   c                   t                                          |           t                              |j                  x}r|                     ||           d S d S r9   )r   visit_member_exprrZ   r   r   r   )r:   r   r   r   s      r;   r   z$ASTAnnotateVisitor.visit_member_expra  s\    !!!$$$""1:...3 	"MM!S!!!!!	" 	"r=   r   c                  t                                          |           t          |j        t                    rL|j        j        dk    r<t          |j                  dk    r$|j        d         }|                     |           d S t          |j        t                    rt          |j        j	        t                    r|j        j	        }| j        j                            |          }|r|j        r|?|j                            d          s%|                     |d|j         ddz   d           d S |r,|j        r'|                     |d|j         d	d
z   d           d S d S d S t          |j        t                    rlt          |j        j	        t&                    rO|j        j	        }| j                            |j                  r&|                     |d|j         dd           d S d S d S d S )Nzbuiltins.isinstancerY   r1   z	builtins.z*Creating an instance of non-native class "z" zis slow.zClass "z " is only partially native, and z!constructing an instance is slow.zCalling a decorated function ("z'") is inefficient, even if it's native.)r   visit_call_exprr   r   r   r   lenr   check_isinstance_argr   r   rd   
type_to_irr   is_ext_class
startswithr   r   is_augmentedr
   is_native_ref_expr)r:   r   arginfoclass_ir	decoratorr   s         r;   r   z"ASTAnnotateVisitor.visit_call_exprf  s3   """qx))	!%:::AFq  &)C%%c*****'** 	z!(-/R/R 	8=D{-11$77H !6  )A)A+)N)N QDIQQQT^^`a      h3 IdiIII9:	        '** 	z!(-/S/S 	I{--ah77 ninnnn    	 	 	 	 r=   r   r   c                <   t          |t                    rPt          |j        t                    r2|j        j        r(|                     |d|j        j         d           d S d S d S t          |t                    r|j        D ]}| 	                    |           d S d S )Nz/Expensive isinstance() check against protocol "r   )
r   r   r   r   is_protocolr   r   r   rj   r   )r:   r   items      r;   r   z'ASTAnnotateVisitor.check_isinstance_arg  s    c7## 	0#(H-- #(2F \38=\\\        Y'' 	0	 0 0))$////	0 	00 0r=   r   c               v    |                      |d           t                                          |           d S )NzhA new object is allocated for lambda each time it is evaluated. A module-level function would be faster.)r   r   visit_lambda_exprr   s     r;   r   z$ASTAnnotateVisitor.visit_lambda_expr  sA    9	
 	
 	

 	!!!$$$$$r=   r1   r   r   str | Annotationr4   r5   c                    t          |t                    rt          ||          }| j                            |j        g                               |           d S )NrX   )r   r3   r0   r   r   r   rm   )r:   r   r   r4   s       r;   r   zASTAnnotateVisitor.annotate  sT    c3 	5S8444C	QVR((//44444r=   er#   c                    | j                             |          }|rt          |          S t          t          j                  S r9   )r   r   r&   r!   r%   unannotated)r:   r  ts      r;   r   zASTAnnotateVisitor.get_type  s>    Ma   	&"1%%%y,---r=   )r   r   rd   r.   r6   r7   )r   r   r6   r7   )r   r   r6   r7   )r   r   r6   r7   )r   r   r6   r7   )r   r   r   r3   r6   r7   )r   r	   r6   r7   )r   r   r6   r7   )r   r   r6   r7   )r   r   r6   r7   )r   r   r6   r7   )r   r   r6   r7   )r   r   r6   r7   )r   r   r6   r7   r>   )r   r   r   r   r4   r5   r6   r7   )r  r   r6   r#   )r@   rA   rB   rC   r<   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r   s   @r;   r   r      s       @@   	 	 	 	 	 	" " " " " "2 2 2 2 2 2( ( ( ( ( (    8 8 8 8 8 8 # # # # # #() ) ) ) ) )" " " "" " " " " "
           D0 0 0 0% % % % % %5 5 5 5 5
. . . . . . . .r=   r   vr-   
str | Nonec                r    t          | t                    r!t          | j        t                    r| j        S d S r9   )r   r+   r   r3   )r  s    r;   r   r     s3    ![!! j#&>&> w4r=   r   list[Annotation]c                R    t          d | D                       fd| D             S )Nc              3  $   K   | ]}|j         V  d S r9   rX   r   as     r;   r   zget_max_prio.<locals>.<genexpr>  s$      ,,!1:,,,,,,r=   c                *    g | ]}|j         k    |S rD   rX   )r   r  max_prios     r;   
<listcomp>z get_max_prio.<locals>.<listcomp>  s%    666!qzX55A555r=   )max)r   r  s    @r;   get_max_prior    s9    ,,t,,,,,H6666t6666r=   sourceslist[AnnotatedSource]c                   g }|                     d           |                     dt           d           |                     d           |                     d           | D ]*}|                     d|j         d           |                     d           |j        }t	          |j                  5 }|                                }d d d            n# 1 swxY w Y   t          |          D ]\  }}t          |          }|d	z   }d
|z  }	||v rIt          ||                   }
d |
D             }d	                    |          }t          |	||          }n|	dz   |z   }|                     |           |                     d           ,|                     d           |                     t                     |                     d           |                     d           d	                    |          S )Nz<html>
<head>
z<style>
z	
</style>z</head>
z<body>
z<h2><tt>z</tt></h2>
z<pre>r1   z%5dc                    g | ]	}|j         
S rD   )r2   r  s     r;   r  z(generate_html_report.<locals>.<listcomp>  s    444!AI444r=    )	hint_html  z</pre>z<script>z	</script>z</body></html>
 )rm   CSSr]   r   rq   	readlines	enumerater   r  joincolorize_lineJS)r  r|   srcsrc_annsr}   linesir   r   linenumr   ann_strshints                r;   rp   rp     s)   DKK"###KK+C+++,,,KKKK
  5sx555666G?#(^^ 	"qKKMME	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"e$$ 	 	DAqq		Aq5DdlGx#HTN3344t444xx))!'1===dNQ&KKNNNNHKK
KKOOOKKKK"###774==s   3CC	C	r&  r   r  c                `    dt          |           z  dz   }d|  d| d}d| d| d}d| | d	S )
Nr  r  z8<div class="collapsible" style="background-color: #fcc">z</div>z<div class="content">z<div class="hint">z</div></div>z<span>z</span>)r   )r&  r   r  hint_prefix	line_spanhint_divs         r;   r   r     s^    G$t+K_7__VW___I]{]]i]]]H0I0x0000r=   )
r`   r3   ra   r   rb   rc   rd   r.   r6   r7   )r]   r3   rl   r   r   r)   r   r   rd   r.   r6   r\   )r   r(   rl   r   r6   r^   )r  r-   r6   r  )r   r
  r6   r
  )r  r  r6   r3   )r&  r3   r   r3   r  r3   r6   r3   )KrC   
__future__r   os.pathrw   rs   r|   r   typingr   
mypy.buildr   
mypy.nodesr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   mypy.traverserr    
mypy.typesr!   r"   r#   r$   r%   r&   	mypy.utilr'   mypyc.ir.func_irr(   mypyc.ir.module_irr)   mypyc.ir.opsr*   r+   r,   r-   mypyc.irbuild.mapperr.   r0   rV   __annotations__rZ   r  r!  r\   r   rn   r   r   r   r  rp   r   rD   r=   r;   <module>r:     s     # " " " " "  



             " " " " " "                                                     6 , + + + + + V V V V V V V V V V V V V V V V $ $ $ $ $ $ # # # # # # ' ' ' ' ' ' > > > > > > > > > > > > ' ' ' ' ' '! ! ! ! ! ! ! !JJ788$<== $<== ::&>??	
 JJ'@AA $BCC $BCC JJ788 ::677 JJ788 zz";<< zz";<< zz":;; ZZ 9:: JJ'FGG  

#@AA!" 

#@AA#    * $>   "z[   $>   #
l    ZLWX       ($' ' ' ' ' ' ' '? ? ? ?(' ' ' ' A A A AHj. j. j. j. j.) j. j. j.Z   7 7 7 7
   D1 1 1 1 1 1r=   