+
     2i  c                   s  a  R t*0 t R tRt^ RIt^ RIHt ^ RIHt ^ RIt^ RIH	t	H
t
 ^ RIHt ^ RIHtHtHtHtHtHt ]'       d   ^ RIHt ^ RIt^ RIt^ RIt^ RIt^ RIt^ RItR	 R
 ltRR R llt ! R R]4      tR R ltRt ] ^ k Rt!] ^k R R R llt"R!R R llt#R"R R llt$R#R R llt%R$R R llt&]'R8X  d#   ]! ]PP                  PS                  4       4       R# R# )%z=Diagnostic functions, mainly for use when doing tech support.ZMITN)BytesIO)
HTMLParser)BeautifulSoup__version__)builder_registry)AnyIOListOptionalTupleTYPE_CHECKING)_IncomingMarkupc                s     V ^8  d   QhRRRR/# )   datar   returnN format   "1/usr/lib/python3.14/site-packages/bs4/diagnose.py__annotate__r       s     5 5$ 5 5    c           	     s   \        R\        ,          4       \        R\        P                  ,          4       . ROpV FP  p\        P
                   F  pW#P                  9   g   K   K+  	  VP                  V4       \        RV,          4       KR  	  RV9   dR   TP                  R4        ^ RI	H
p \        RR	P                  \        \        VP                  4      4      ,          4       RV9   d"    ^ RIp\        RVP                  ,          4       \#        V R4      '       d   V P%                  4       p V Fd  p\        RV,          4       Rp \'        WR7      pRpV'       d,   \        RV,          4       \        XP/                  4       4       \        R4       Kf  	  R#   \         d    \        R
4        Li ; i  \         d    \        R4        Li ; i  \(         d+    \        RT,          4       \*        P,                  ! 4         Li ; i)zDiagnostic suite for isolating common problems.

:param data: Some markup that needs to be explained.
:return: None; diagnostics are printed to standard output.
z'Diagnostic running on Beautiful Soup %szPython version %shtml5liblxmlz;I noticed that %s is not installed. Installing it may help.zlxml-xmletreezFound lxml version %sZ.z.lxml is not installed or couldn't be imported.NzFound html5lib version %sz2html5lib is not installed or couldn't be imported.readz#Trying to parse your markup with %sF)featuresT%s could not parse the markup.z#Here's what %s did with the markup:)html.parserr   r   zP--------------------------------------------------------------------------------)printr   sysZversionr   Zbuildersr   Zremoveappendr   r   joinZmapstrZLXML_VERSIONZImportErrorr   Zhasattrr   r   	Exception	traceback	print_excZprettify)	r   Zbasic_parsersnameZbuilderr   r   parsersuccessZsoup	   &        r   diagnoser*       s    
4{BD	,.7M'00G''' 1   &NQUU  Z(	D"*SXXc#u?Q?Q6R-SSU ]"	H.1E1EEG tVyy{4v=?	" 7DG 86AC4==?$x    	DBC	D  	HFG	H  	"3f<>!	"s6   *?F 1 F; GF87F8;GG2HHc                s8    V ^8  d   QhRRR\         R\        RR/# )r   r   r   htmlkwargsr   N)boolr   r   r   r   r   r   X   s/     D D& Dd DS DT Dr   c                s\   ^ RI Hp VP                  RR4      p\        V \        4      '       d   V P                  R4      p \        V \        4      '       g   \        V 4      pVP                  ! X3RVRV/VB  F0  w  rg\        V: RVP                  :R RVP                  : 24       K2  	  R# )	a  Print out the lxml events that occur during parsing.

This lets you see how lxml parses a document when no Beautiful
Soup code is running. You can use this to determine whether
an lxml-specific problem is in Beautiful Soup's lxml tree builders
or in lxml itself.

:param data: Some markup.
:param html: If True, markup will be parsed with lxml's HTML parser.
   if False, lxml's XML parser will be used.
r   recoverTZutf8r+   z, z>4N)r   r   ZpopZ
isinstancer"   Zencoder   r    Z	iterparser   ZtagZtext)r   r+   r,   r   r.   ZreaderZeventZelements   &&,     r   
lxml_tracer/   X   s     jjD)G${{6"dB//&WtWWWPVWw{{GLLAC Xr   c                   s   a  ] tR t^ot o RtV 3R lR ltRV 3R lR lltRV 3R lR lltV 3R lR	 ltV 3R
 lR lt	V 3R lR lt
V 3R lR ltV 3R lR ltV 3R lR ltV 3R lR ltRtV tR# )AnnouncingParserzSubclass of HTMLParser that announces parse events, without doing
anything else.

You can use this to get a picture of how html.parser sees a given
document. The easiest way to do this is to call `htmlparser_trace`.
c                $   < V ^8  d   QhRS[ RR/# )r   sr   Nr"   r   __classdict__   "r   r   AnnouncingParser.__annotate__w   s      C D r   c                s    \        V4       R # )N)r   )selfr2      &&r   _pZAnnouncingParser._pw   s	    ar   c                sf   < V ^8  d   QhRS[ RS[S[S[ S[S[ ,          3,          ,          RS[RR/# )r   r&   attrshandle_empty_elementr   N)r"   r   r	   r   r-   r4   r6   r   r   r7   z   sF     ) )) E#x},-.) #	)
 
)r   c                s4    V P                  V R V R24       R# ) z STARTNr:   )r8   r&   r;   r<   s   &&&&r   handle_starttagZ AnnouncingParser.handle_starttagz   s     	4&%'(r   c                s*   < V ^8  d   QhRS[ RS[RR/# )r   r&   check_already_closedr   N)r"   r-   r4   r6   r   r   r7      s"     ! !# !T !T !r   c                6    V P                  R V,          4       R# )z%s ENDNr>   )r8   r&   r@   s   &&&r   handle_endtagZAnnouncingParser.handle_endtag   s    4 r   c                r1   r   r   r   Nr3   r4   r6   r   r   r7           " " " "r   c                rA   )z%s DATANr>   r8   r   r9   r   handle_dataZAnnouncingParser.handle_data       	D !r   c                r1   r   r&   r   Nr3   r4   r6   r   r   r7           % %3 %4 %r   c                rA   )z
%s CHARREFNr>   r8   r&   r9   r   handle_charrefZAnnouncingParser.handle_charref       t#$r   c                r1   rH   r3   r4   r6   r   r   r7      s     ' 'S 'T 'r   c                rA   )z%s ENTITYREFNr>   rJ   r9   r   handle_entityrefZ!AnnouncingParser.handle_entityref   s    %&r   c                r1   rC   r3   r4   r6   r   r   r7      rI   r   c                rA   )z
%s COMMENTNr>   rE   r9   r   handle_commentZAnnouncingParser.handle_comment   rL   r   c                r1   rC   r3   r4   r6   r   r   r7      rD   r   c                rA   )z%s DECLNr>   rE   r9   r   handle_declZAnnouncingParser.handle_decl   rG   r   c                r1   rC   r3   r4   r6   r   r   r7      s     * * * *r   c                rA   )z%s UNKNOWN-DECLNr>   rE   r9   r   unknown_declZAnnouncingParser.unknown_decl   s    !D()r   c                r1   rC   r3   r4   r6   r   r   r7      s        c  d  r   c                rA   )z%s PINr>   rE   r9   r   	handle_piZAnnouncingParser.handle_pi   s    $r   r   NT)__name__Z
__module__Z__qualname__Z__firstlineno____doc__r:   r?   rB   rF   rK   rM   rN   rO   rP   rQ   Z__static_attributes__Z__classdictcell__)r5      @r   r0   r0   o   sn      ) )! !" "% %' '% %" "* *   r   r0   c                (    V ^8  d   QhR\         RR/# rC   r3   r   r   r   r   r      s     	 	3 	4 	r   c                s<    \        4       pVP                  V 4       R# )zPrint out the HTMLParser events that occur during parsing.

This lets you see how HTMLParser parses a document when no
Beautiful Soup code is running.

:param data: Some markup.
N)r0   Zfeed)r   r'      & r   htmlparser_tracerX      s     F
KKr   ZaeiouZbcdfghjklmnpqrstvwxyzc                0    V ^8  d   QhR\         R\        /# r   lengthr   intr"   r   r   r   r   r      s      # c r   c                s    Rp\        V 4       F;  pV^,          ^ 8X  d   \        pM\        pV\        P                  ! V4      ,          pK=  	  V# )z4Generate a random word-like string.

:meta private:
Z )range_consonants_vowelsrandomchoice)r[   r2   iZts   &   r   rwordrd      sF    
 	A6]q5A:AA	V]]1  Hr   c                rY   rZ   r\   r   r   r   r   r      s     H Hc H# Hr   c                sD    RP                  R \        V 4       4       4      # )z8Generate a random sentence-like string.

:meta private:
r=   c              3   sb   "   T F%  p\        \        P                  ! ^^	4      4      x  K'  	  R# 5i)   N)rd   ra   randint)Z.0rc   rW   r   Z	<genexpr>Zrsentence.<locals>.<genexpr>   s#     GAE&..A.//s   -/)r!   r^   )r[   s   &r   	rsentencerg      s    
 88GvGGGr   c                rY   )r   num_elementsr   r\   r   r   r   r   r      s     6 6s 6c 6r   c           	     s   . ROp. p\        V 4       F  p\        P                  ! ^ ^4      pV^ 8X  d1   \        P                  ! V4      pVP	                  RV,          4       KQ  V^8X  d2   VP	                  \        \        P                  ! ^^4      4      4       K  V^8X  g   K  \        P                  ! V4      pVP	                  RV,          4       K  	  RRP                  V4      ,           R,           # )z<Randomly generate an invalid HTML document.

:meta private:
z<%s>z</%s>z<html>Z
z</html>)ZpZdivZspanrc   bZscriptZtable)r^   ra   rf   rb   r    rg   r!   )rh   Z	tag_namesZelementsrc   rb   Ztag_names   &     r   rdocrj      s    
 BIH< 1%Q;}}Y/HOOFX-.q[OOIfnnQ&:;<q[}}Y/HOOGh./ ! dii))I55r   c                rV   )r   rh   r   N)r]   r   r   r   r   r      s      B  BC  BT  Br   c                sL   \        R\        ,          4       \        V 4      p\        R\        V4      ,          4       RRR.RR3 Fb  pRp \        P                  ! 4       p\        W4       \        P                  ! 4       pRpV'       g   KG  \        R
VXX,
          3,          4       Kd  	  ^ RI	H
p \        P                  ! 4       pVP                  V4       \        P                  ! 4       p\        RWT,
          ,          4       ^ RIpVP                  4       p\        P                  ! 4       pVP                  V4       \        P                  ! 4       p\        RWT,
          ,          4       R#   \         d,    \        R	T,          4       \        P                  ! 4         EL i ; i)z.Very basic head-to-head performance benchmark.z1Comparative parser benchmark on Beautiful Soup %sz3Generated a large invalid HTML document (%d bytes).r   r+   r   r   FTr   z"BS4+%s parsed the markup in %.2fs.r   z$Raw lxml parsed the markup in %.2fs.Nz(Raw html5lib parsed the markup in %.2fs.)r   r   rj   Zlentimer   r#   r$   r%   r   r   ZHTMLr   r   Zparse)	rh   r   Zparser_namer(   Zari   r   r   r'   r)   r   benchmark_parsersrl      s5   	>LND	@3t9LN 0*mL	"		A$,		AG 77;A:NNP M 		A	JJt		A	1QU;=  "F		A
LL		A	5?A'  	"3kAC!	"s   7E--2F#"F#c                s4    V ^8  d   QhR\         R\        RR/# )r   rh   r'   r   Nr\   r   r   r   r   r      s!     + +# + + +r   c                s"   \         P                  ! 4       pVP                  p\        V 4      p\	        \
        WAR7      p\        P                  ! RWUV4       \        P                  ! V4      pVP                  R4       VP                  R^24       R# )z7Use Python's profiler on a randomly generated document.)bs4r   r'   zbs4.BeautifulSoup(data, parser)Z
cumulativez_html5lib|bs4N)tempfileZNamedTemporaryFiler&   rj   Zdictrm   cProfileZrunctxpstatsZStatsZ
sort_statsZprint_stats)rh   r'   Z
filehandleZfilenamer   ZvarsZstatss   &&     r   profilerq      sl    ,,.JHDCd2DOO5t8LLL"E	\"	or*r   Z__main__c                sh    V ^8  d   Qh/ ^ \         9   d
   \        ;R&   ^\         9   d
   \        ;R&   # )r   r`   r_   )__conditional_annotations__r"   r   r   r   r   r      s2     C CL   M DN + *S *O Dr   rR   )i   )re   )i  )順 )rs   r   )+rr   rT   Z__license__ro   Zior    Zhtml.parserr   rm   r   r   Zbs4.builderr   Ztypingr   r   r   r   r	   r
   Zbs4._typingr   rp   ra   rn   rk   r$   r   r*   r/   r0   rX   r`   r_   rd   rg   rj   rl   rq   rS   Zstdinr   r   )rr   rU   r   <module>rt      s    C    " 
 * (  +      
5pD.) z ) X	  * *H6, BF+  zSYY^^ r   