Ë
    ðmxiF(  ã                  óô   — d Z ddlmZ ddlZddlZddlZddlZddl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ZdZ	 da	 d
„ Z G d„ d«      Zdd„Zej.                  dd„«       Zy# e$ r dZY Œ4w xY w)z§Profiling support for unit and performance tests.

These are special purpose profiling methods which operate
in a more fine-grained way than nose's profiling plugin.

é    )ÚannotationsNé   )Úconfig)Ú
gc_collecté   )Úfreethreading)Úhas_compiled_extc                óR   — | a t        j                  rt        j                  «        y y ©N)Ú_current_testÚ_profile_statsÚforce_writeÚreset_count)Úid_s    úS/home/htdocs/ttos/venv/lib/python3.12/site-packages/sqlalchemy/testing/profiling.pyÚ_start_current_testr   7   s!   € à€Mä×!Ò!Ü×"Ñ"Õ$ð "ó    c                  óR   — e Zd ZdZdd„Zed„ «       Zd„ Zd„ Zd„ Z	d„ Z
d	„ Zd
„ Zd„ Zy)ÚProfileStatsFilezøStore per-platform/fn profiling results in a file.

    There was no json module available when this was written, but now
    the file format which is very deterministically line oriented is kind of
    handy in any case for diffs and merges.

    Nc                óB  — t         j                  d uxr t         j                  j                  | _        | j                  xs. t         j                  d uxr t         j                  j                  | _        t        j                  j                  |«      | _	        t        j                  j                  | j                  «      d   | _        t        j                  d„ «      | _        || _        || _        | j#                  «        | j
                  r| j%                  «        y y )Néÿÿÿÿc                 ó4   — t        j                  t        «      S r   )ÚcollectionsÚdefaultdictÚdict© r   r   ú<lambda>z+ProfileStatsFile.__init__.<locals>.<lambda>R   s   € ”K×+Ñ+¬DÓ1€ r   )r   ÚoptionsÚforce_write_profilesr   Úwrite_profilesÚwriteÚosÚpathÚabspathÚfnameÚsplitÚshort_fnamer   r   ÚdataÚdumpÚsortÚ_readÚ_write)ÚselfÚfilenamer*   r)   s       r   Ú__init__zProfileStatsFile.__init__H   sÐ   € ä�N‰N $Ð&ÒN¬6¯>©>×+NÑ+Nð 	Ôð ×%Ñ%ò 
Ü�N‰N $Ð&ÒH¬6¯>©>×+HÑ+Hð 	Œ
ô —W‘W—_‘_ XÓ.ˆŒ
ÜŸ7™7Ÿ=™=¨¯©Ó4°RÑ8ˆÔÜ×+Ñ+Ù1ó
ˆŒ	ð ˆŒ	ØˆŒ	Ø�
‰
ŒØ�:Š:ð �K‰K�Mð r   c                óX  — t         j                  j                  dz   t         j                  j                  z   }t         j                  j                  j
                  r|dz  }t         j                  j                  dk(  rFt         j                  j                  j                  t         j                  j                  «      r|dz  }dj                  t        j                  dd D �cg c]  }t        |«      ‘Œ c}«      }t        r|dz  }t        j                  «       t        j                  «       j!                  «       t        j"                  «       j!                  «       ||g}|j%                  d	«       t'        «       }|j%                  |xr d
xs d«       dj                  |«      S c c}w )NÚ_Ú_asyncÚsqliteÚ_fileú.r   r   ÚtÚdbapiunicodeÚcextensionsÚnocextensions)r   ÚdbÚnameÚdriverÚdialectÚis_asyncÚ_is_url_file_dbÚurlÚjoinÚsysÚversion_infoÚstrr   ÚplatformÚmachineÚsystemÚlowerÚpython_implementationÚappendr	   )r-   Ú	dbapi_keyÚvÚ
py_versionÚplatform_tokensÚ	_has_cexts         r   Úplatform_keyzProfileStatsFile.platform_key\   s?  € ä—I‘I—N‘N SÑ(¬6¯9©9×+;Ñ+;Ñ;ˆ	Ü�9‰9×Ñ×%Ò%Ø˜Ñ!ˆIä�9‰9�>‰>˜XÒ%¬&¯)©)×*;Ñ*;×*KÑ*KÜ�I‰I�M‰Mô+
ð ˜Ñ ˆIð —X‘X¬s×/?Ñ/?ÀÀ!Ð/DÖE¨!œs 1�vÒEÓFˆ
ÝØ˜#ÑˆJô ×ÑÓÜ�O‰OÓ×#Ñ#Ó%Ü×*Ñ*Ó,×2Ñ2Ó4ØØð
ˆð 	×Ñ˜~Ô.Ü$Ó&ˆ	Ø×Ñ˜yÒ:¨]ÒM¸oÔNØ�x‰x˜Ó(Ð(ùò Fs   Ã%F'c                ód   — t         }|| j                  v xr | j                  | j                  |   v S r   ©r   r(   rP   )r-   Útest_keys     r   Ú	has_statszProfileStatsFile.has_statsy   s1   € Ü ˆà˜Ÿ	™	Ð!ÒN d×&7Ñ&7¸4¿9¹9ÀXÑ;NÐ&Nð	
r   c                ó@  — t         }| j                  |   }|| j                     }d|vrg x|d<   }n|d   }d|vrdx|d<   }n|d   }t        |«      |kD  }|s0|j	                  |«       | j
                  r| j                  «        d }n
|d   ||   f}|dxx   dz  cc<   |S )NÚcountsÚcurrent_countr   Úlinenor   )r   r(   rP   ÚlenrJ   r!   r,   )	r-   Ú	callcountrS   Úper_fnÚper_platformrV   rW   Ú	has_countÚresults	            r   r^   zProfileStatsFile.result   sÂ   € Ü ˆØ—‘˜8Ñ$ˆØ˜d×/Ñ/Ñ0ˆà˜<Ñ'Ø.0Ð0ˆL˜Ñ"¡Và! (Ñ+ˆFà ,Ñ.Ø<=Ð=ˆL˜Ñ)©Mà(¨Ñ9ˆMä˜“K -Ñ/ˆ	áØ�M‰M˜)Ô$Ø�zŠzØ—‘”Ø‰Fà! (Ñ+¨V°MÑ-BÐBˆFØ�_Ó%¨Ñ*Ó%Øˆr   c                ó¢   — t         }|| j                  vry | j                  |   }| j                  |vry || j                     }d|v r	g |d   d d  y y )NrV   rR   )r-   rS   r[   r\   s       r   r   zProfileStatsFile.reset_countš   sc   € Ü ˆð ˜4Ÿ9™9Ñ$ØØ—‘˜8Ñ$ˆØ×Ñ FÑ*ØØ˜d×/Ñ/Ñ0ˆØ�|Ñ#Ø(*ˆL˜Ñ"¡1Ñ%ð $r   c                óÒ   — t         }| j                  |   }|| j                     }|d   }|d   }|t        |«      k  r	|||dz
  <   n||d<   | j                  r| j                  «        y y )NrV   rW   r   r   )r   r(   rP   rY   r!   r,   )r-   rZ   rS   r[   r\   rV   rW   s          r   ÚreplacezProfileStatsFile.replace§   sr   € Ü ˆØ—‘˜8Ñ$ˆØ˜d×/Ñ/Ñ0ˆØ˜hÑ'ˆØ$ _Ñ5ˆØœ3˜v›;Ò&Ø(1ˆF�= 1Ñ$Ò%à"ˆF�2‰JØ�:Š:Ø�K‰K�Mð r   c                ó    — d| j                   z  S )Na  # %s
# This file is written out on a per-environment basis.
# For each test in aaa_profiling, the corresponding function and 
# environment is located within this file.  If it doesn't exist,
# the test is skipped.
# If a callcount does exist, it is compared to what we received. 
# assertions are raised if the counts do not match.
# 
# To add a new callcount test, apply the function_call_count 
# decorator and re-run the tests using the --write-profiles 
# option - this file will be rewritten including the new count.
# 
)r%   )r-   s    r   Ú_headerzProfileStatsFile._header´   s   € ðð �Z‰Zñ!ð 	r   c                ó¨  — 	 t        | j                  «      }t        |«      D ]Š  \  }}|j	                  «       }|r|j                  d«      rŒ*|j                  «       \  }}}| j                  |   }||   }|j                  d«      D �	cg c]  }	t        |	«      ‘Œ }
}	|
|d<   |dz   |d<   d|d<   ŒŒ |j                  «        y # t        $ r Y y w xY wc c}	w )Nú#ú,rV   r   rX   r   rW   )
Úopenr%   ÚOSErrorÚ	enumerateÚstripÚ
startswithr&   r(   ÚintÚclose)r-   Ú	profile_frX   ÚlinerS   rP   rV   r[   r\   ÚcountÚcs              r   r+   zProfileStatsFile._readÇ   sÛ   € ð	Ü˜TŸZ™ZÓ(ˆIô & iÓ0ò 	.‰LˆF�DØ—:‘:“<ˆDÙ˜4Ÿ?™?¨3Ô/Øà-1¯Z©Z«\Ñ*ˆH�l FØ—Y‘Y˜xÑ(ˆFØ! ,Ñ/ˆLØ)/¯©°cÓ):Ö; ”�U•Ð;ˆAÐ;Ø%&ˆL˜Ñ"Ø%+¨a¡ZˆL˜Ñ"Ø,-ˆL˜Ò)ð	.ð 	�‰Õøô ò 	Ùð	üò <s   ‚C  ÂCÃ 	CÃCc           
     óÌ  — t        d| j                  z  «       t        | j                  d«      }|j                  | j	                  «       «       t        | j                  «      D ]o  }| j                  |   }|j                  d|z  «       t        |«      D ]<  }||   }dj                  d„ |d   D «       «      }|j                  |›d|›d|›d�«       Œ> Œq |j                  «        y )	NzWriting profile file %sÚwz
# TEST: %s

rf   c              3  ó2   K  — | ]  }t        |«      –— Œ y ­wr   )rD   )Ú.0rp   s     r   ú	<genexpr>z*ProfileStatsFile._write.<locals>.<genexpr>ã   s   è ø€ ÒL¨EœS ŸZÑLùs   ‚rV   ú ú
)	Úprintr%   rg   r!   rc   Úsortedr(   rA   rm   )r-   rn   rS   r[   rP   r\   rq   s          r   r,   zProfileStatsFile._writeÚ   sÇ   € ÜÐ'¨$¯*©*Ñ4Ô5Ü˜Ÿ™ SÓ)ˆ	Ø�‰˜Ÿ™›Ô'Ü˜tŸy™yÓ)ò 	LˆHØ—Y‘Y˜xÑ(ˆFØ�O‰OÐ.°Ñ9Ô:Ü & v£ò L�Ø% lÑ3�Ø—H‘HÑL°\À(Ñ5KÔLÓL�Ø—‘²º,ÊÐ JÕKñLð	Lð 	�‰Õr   )Ú
cumulativeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r/   ÚpropertyrP   rT   r^   r   ra   rc   r+   r,   r   r   r   r   r   ?   sC   „ ñóð( ñ)ó ð)ò8
òò6+òòò&ó&r   r   c                ó0   ‡ ‡‡— ddl m} |ˆˆ ˆfd„«       }|S )a©  Assert a target for a test case's function call count.

    The main purpose of this assertion is to detect changes in
    callcounts for various functions - the actual number is not as important.
    Callcounts are stored in a file keyed to Python version and OS platform
    information.  This file is generated automatically for new tests,
    and versioned so that unexpected changes in callcounts will be detected.

    r   )Ú	decoratorc                ó°   •— t        ‰	«      D ]
  } | |i |¤Ž Œ t        ‰«      }t        ‰¬«      5  |D ]
  } | |i |¤Ž}Œ cd d d «       S # 1 sw Y   y xY w)N)Úvariance)ÚrangeÚcount_functions)
ÚfnÚargsÚkwÚwarmÚ	timerangeÚtimeÚrvÚtimesr„   Úwarmups
          €€€r   Úwrapz!function_call_count.<locals>.wrapù   sh   ø€ ä˜&“Mò 	ˆDÙ�ˆO˜‹Oð	ô ˜%“Lˆ	Ü hÔ/ñ 	Ø!ò %�Ù˜�_ ‘_‘ð%à÷	÷ 	ò 	ús   ±AÁA)Úsqlalchemy.utilr‚   )r„   rŽ   r�   r‚   r�   s   ```  r   Úfunction_call_countr’   è   s!   ú€ õ *àõó ðð €Kr   c              #  óÖ  K  — t         €t        j                  d«      ‚t        j	                  «       s6t        j
                  s&t        j                  dt        j                  z  «       t        «        t        j                  «       }|j                  «        d –— |j                  «        t        j                  |t        j                  ¬«      }|j                   }t        j#                  |«      }|€d }n|\  }}t%        d||fz  «        |j&                  t)        j*                  dt        j,                  «      Ž  |j/                  «        t        j0                  rtt2        j4                  j7                  t        j0                  «      \  }}t8        j+                  d«      d   }	|›d|	›|xs d	›�}
|j;                  |
«       t%        d
|
z  «       t        j<                  rt        j?                  |«       y |rqtA        || z  «      }tC        ||z
  «      |kD  }|rOt        j
                  rt        j?                  |«       y tE        d|›d| dz  ›d|›dt        jF                  ›d�	«      ‚y y ­w)NzcProfile is not installedzŒNo profiling stats available on this platform for this function.  Run tests with --write-profiles to add statistics to %s for this platform.)ÚstreamzPstats calls: %d Expected %sz[, ]r5   r   r1   z.profilezDumped stats to file %szAdjusted function call count z not within éd   z% of expected z, platform z;. Rerun with --write-profiles to regenerate this callcount.)$ÚcProfiler   Ú_skip_test_exceptionr   rT   r!   Ú	skip_testr'   r   ÚProfileÚenableÚdisableÚpstatsÚStatsrB   ÚstdoutÚtotal_callsr^   ry   Ú
sort_statsÚrer&   r*   Úprint_statsr)   r"   r#   Úsplitextr   Ú
dump_statsr   ra   rl   ÚabsÚAssertionErrorrP   )r„   ÚprÚstatsrZ   ÚexpectedÚexpected_countÚline_noÚbaseÚextÚ	test_nameÚdumpfileÚdevianceÚfaileds                r   r†   r†     sþ  è ø€ äÐÜ×)Ñ)Ð*EÓFÐFä×#Ñ#Ô%¬n×.BÒ.BÜ×Ñðô  .×9Ñ9ñ:ô	
ô „Lä	×	Ñ	Ó	€BØ‡I�I„Kã	à‡J�J„Lô �L‰L˜¤C§J¡JÔ/€Eð ×!Ñ!€Iä×$Ñ$ YÓ/€HàÐØ‰à"*Ñˆ�ä	Ð
(¨I°~Ð+FÑ
FÔGØ€E×Ñ”b—h‘h˜w¬×(;Ñ(;Ó<Ñ=Ø	×ÑÔÜ×ÒÜ—G‘G×$Ñ$¤^×%8Ñ%8Ó9‰	ˆˆcÜ!×'Ñ'¨Ó,¨RÑ0ˆ	Ú $¡i°Ò1B¸
Ð1BÐCˆØ×Ñ˜Ô"ÜÐ'¨(Ñ2Ô3ä×!Ò!Ü×Ñ˜yÕ)Ù	Ü�y 8Ñ+Ó,ˆÜ�Y Ñ/Ó0°8Ñ;ˆáÜ×#Ò#Ü×&Ñ& yÕ1å$ò "Ø! C›Ú&Ü&×3Ó3ð	óð ð	 ð	 
ùs   ‚I'I))çš™™™™™©?r   r   )r²   )r   Ú
__future__r   r   Ú
contextlibr"   rE   rœ   r¡   rB   Ú r   Úutilr   r   r	   r–   ÚImportErrorr   r   r   r   r’   Úcontextmanagerr†   r   r   r   ú<module>r¹      sš   ðñõ #ã Û Û 	Û Û Û 	Û 
å Ý Ý  Ý #ðÛð €ðð €ðò%÷fñ fóRð> ×ÑòAó ñAøðK ò Ø‚Hðús   ¾A- Á-A7Á6A7