
    Mh3                         d 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
mZ ddlmZ ddlmZ  G d d	e          Z G d
 d          ZdS )zaDisplayhook for IPython.

This defines a callable class that IPython uses for `sys.displayhook`.
    N)Configurable)InstanceFloat)warn   )HistoryOutputc                       e Zd ZdZ edd          Z edd          Z ed          Zd fd		Z	e
d
             Zd Zd Zed             Zd Ze
d             Zd Zd ZdZdddZd Zd Zd Zd ZddZd Zd Z xZS )DisplayHookzThe custom IPython displayhook to replace sys.displayhook.

    This class does many things, but the basic idea is that it is a callable
    that gets called anytime user code returns a value.
    z1IPython.core.interactiveshell.InteractiveShellABCT)
allow_nonez-IPython.core.interactiveshell.ExecutionResultg?N  c                     t          t          |           j        d
d|i| d| _        d}|dk    r
d| _        d}n+||k     rd| _        d}t          d|z  d           nd| _        || _        || _        d\  | _        | _	        | _
        | j        | j	        | j
        d	}| j        j                            |           d S )NshellF   r   z6caching was disabled (min value for cache size is %s).)
stacklevelr    r   r   ______ )superr
   __init__
_is_activedo_full_cacher   
cache_sizer   r   r   r   user_nsupdate)selfr   r   kwargscache_size_min
to_user_ns	__class__s         X/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/IPython/core/displayhook.pyr   zDisplayHook.__init__&   s    )k4  )@@@@@@??!"DJJ.((!"DJI +,. . . . . "#D$ 
"*twtx &dgDH==

!!*-----    c                     | j         j        S N)r   execution_countr   s    r$   prompt_countzDisplayHook.prompt_count@   s    z))r%   c                     dt           j        v r>	 | j        j        d         }|| j        urdS | j        j        d= dS # t
          $ r Y dS w xY wdS )z3Check if the user has set the '_' variable by hand.r   N)builtin_mod__dict__r   r   r   KeyError)r   
user_values     r$   check_for_underscorez DisplayHook.check_for_underscoreI   su    
 +&&&!Z/4
TV++FJ&s+++    '&s   < < 
A
	A
c                     	 | j         j        j        d         }n# t          $ r Y dS w xY w|                     |          S )z2Should we silence the display hook because of ';'?F)r   history_managerinput_hist_parsed
IndexErrorsemicolon_at_end_of_expression)r   cells     r$   quietzDisplayHook.quietW   sS    	:-?CDD 	 	 	55	 224888s    
((c                 Z   t          j        |           }t          t          j        |j                            }t          |          D ]`}|d         t          j        t          j        t          j	        t          j
        fv r9|d         t          j        k    r|d         dk    r dS  dS dS )z=Parse Python expression and detects whether last token is ';'r   r   ;TFN)_ioStringIOlisttokenizegenerate_tokensreadlinereversed	ENDMARKERNLNEWLINECOMMENTOP)
expressionsiotokenstokens       r$   r6   z*DisplayHook.semicolon_at_end_of_expressionc   s     l:&&h.s|<<==f%% 	 	EQxH.X=MxO_```aHK''eAh#oottuu	 	r%   c                     d| _         dS )z.Start the displayhook, initializing resources.TNr   r)   s    r$   start_displayhookzDisplayHook.start_displayhookr   s    r%   c                     | j         S r'   rL   r)   s    r$   	is_activezDisplayHook.is_activev   s
    r%   c                     t           j                            | j        j                   d                    | j        j                  }| j        r!t           j                            |           dS dS )zyWrite the output prompt.

        The default implementation simply writes the prompt to
        ``sys.stdout``.
        z	Out[{}]: N)sysstdoutwriter   separate_outformatr(   r   )r   	outprompts     r$   write_output_promptzDisplayHook.write_output_promptz   sh     	
0111&&tz'ABB	 	(JY'''''	( 	(r%   c                 @    | j         j                            |          S )a  Compute format data of the object to be displayed.

        The format data is a generalization of the :func:`repr` of an object.
        In the default implementation the format data is a :class:`dict` of
        key value pair where the keys are valid MIME types and the values
        are JSON'able data structure containing the raw data for that MIME
        type. It is up to frontends to determine pick a MIME to to use and
        display that data in an appropriate manner.

        This method only computes the format data for the object and should
        NOT actually print or write that to a stream.

        Parameters
        ----------
        result : object
            The Python object passed to the display hook, whose format will be
            computed.

        Returns
        -------
        (format_dict, md_dict) : dict
            format_dict is a :class:`dict` whose keys are valid MIME types and values are
            JSON'able raw data for that MIME type. It is recommended that
            all return values of this should always include the "text/plain"
            MIME type representation of the object.
            md_dict is a :class:`dict` with the same MIME type keys
            of metadata associated with each output.

        )r   display_formatterrU   r   results     r$   compute_format_datazDisplayHook.compute_format_data   s    < z+226:::r%   Freturnc                 ,   d|vrdS |d         }d|v r| j         sd|z   }	 t          |           dS # t          $ rX t          |                    t          j        j        d                              t          j        j                             Y dS w xY w)a  Write the format data dict to the frontend.

        This default version of this method simply writes the plain text
        representation of the object to ``sys.stdout``. Subclasses should
        override this method to send the entire `format_dict` to the
        frontends.

        Parameters
        ----------
        format_dict : dict
            The format dict for the object passed to `sys.displayhook`.
        md_dict : dict (optional)
            The metadata dict to be associated with the display data.
        
text/plainN
backslashreplace)prompt_end_newlineprintUnicodeEncodeErrorencoderQ   rR   encodingdecode)r   format_dictmd_dictresult_reprs       r$   write_format_datazDisplayHook.write_format_data   s     {**F ",/; * 1"[0	j+! 	j 	j 	j +$$SZ%89KLLSSTWT^Tghhiiiiii	js   1 ABBc                    | j         re|| j        j        d         urRt          | j        j        d                   | j         k    r| j        r|                                  d}d t          dd          D             D ]B}|| j        j        vrt          | |          | j        j                            |          urd}C| j	        | _
        | j        | _	        || _        dt          j        vr1|r/| j                            | j        | j	        | j
        dd	           i }| j        rKd
| j        z  }|||<   | j                            |d	           || j        j        d         | j        <   dS dS dS dS )z7Update user_ns with various things like _, __, _1, etc._ohTc                     g | ]}d |z  S )r   r   ).0is     r$   
<listcomp>z.DisplayHook.update_user_ns.<locals>.<listcomp>   s    555Q3q5555r%   r      Fr   r   )interactivez_%sN)r   r   r   lenr   
cull_cacherangegetattrgetr   r   r   r,   r-   pushr*   )r   r[   update_undersundersto_main
new_results         r$   update_user_nszDisplayHook.update_user_ns   s    ? 	FvTZ-?-FFF4:%e,--@@TEW@!!! !M55%!**555 * *!3334((
0B0F0Fv0N0NNN$)MwDHfDGDF;///m/
TV&*g&*h!0 !0=B   D D D
 G! F"T%66
&,
#
U;;;?E
"5)$*;<<<=	F 	FFF4F Fr%   c                 0    | j         || j         _        d S d S r'   )exec_resultr[   rZ   s     r$   fill_exec_resultzDisplayHook.fill_exec_result   s$    '&,D### ('r%   c                 :   | j         j        j        | j                                     t          d|                     d|vrdS | j         j        j        r&| j         j                            |d         d           |d         | j         j        j	        | j        <   dS )zLog the output.execute_result)output_typebundler_   Noutput)
r   r3   outputsr*   appendr   logger
log_output	log_writeoutput_hist_reprs)r   rh   s     r$   r   zDisplayHook.log_output   s    
"*4+<=DD&6{KKK	
 	
 	
 {**F:' 	MJ''L(A8LLL4?4M 	
"4T5FGGGr%   c                     t           j                            | j        j                   t           j                                         d| _        dS )z%Finish up all displayhook activities.FN)rQ   rR   rS   r   separate_out2flushr   r)   s    r$   finish_displayhookzDisplayHook.finish_displayhook  s<    
1222
r%   c                    |                                   ||                                 s|                                  |                                  |                     |          \  }}|                     |           |                     |           |r+|                     ||           |                     |           | 	                                 dS dS dS )zPrinting with history cache management.

        This is invoked every time the interpreter needs to print, and is
        activated by setting the variable sys.displayhook to it.
        N)
r0   r8   rM   rW   r\   r~   r   rk   r   r   r   r[   rh   ri   s       r$   __call__zDisplayHook.__call__  s     	!!###djjll""$$$$$&&&#'#;#;F#C#C K'''!!&))) -&&{G<<<,,,##%%%%% r%   c                    | j         j                            di           }t          |          }t	          t          || j        z            d          }t          d                    ||                     t          t          |                    D ]G\  }}||k    r dS | j         j                            d|z  d           |                    |d           HdS )z-Output cache is full, cull the oldest entriesrm      zVOutput cache limit (currently {sz} entries) hit.
Flushing oldest {cull_count} entries.)sz
cull_countz_%iN)r   r   rx   rt   maxintcull_fractionr   rU   	enumeratesortedpop)r   ohr   r   rp   ns         r$   ru   zDisplayHook.cull_cache  s    Z##E2..WWR$"4455q99
 55;VrjV5Y5Y	[ 	[ 	[ fRjj)) 	 	DAqJJ""519d333FF1dOOOO		 	r%   c                 p   | j         st          d          t          d| j        dz             D ]Q}dt	          |          z   }	 | j        j        |= n# t          $ r Y nw xY w	 | j        j        |= B# t          $ r Y Nw xY w| j        j        	                    dd           }||
                                 d\  | _        | _        | _        dt          j        vr2| j        j                            | j        | j        | j        d           dd l}t$          j        dk    r|                                 d S d S )	NzJYou shouldn't have reached the cache flush if full caching is not enabled!r   r   rm   r   r   r   cli)r   
ValueErrorrv   r*   reprr   user_ns_hiddenr.   r   rx   clearr   r   r   r,   r-   r   gcrQ   platformcollect)r   r   keyr   r   s        r$   r   zDisplayHook.flush-  sn   ! 	@ ? @ @ @ q*Q.// 		 		Ad1gg+CJ-c22   J&s++    Z##E400>HHJJJ %/!k***J%%46twTX&N&NOOO			 <5  JJLLLLL ! s$   A
AA#A11
A>=A>)Nr   r'   )r]   N)__name__
__module____qualname____doc__r   r   r   r   r   r   propertyr*   r0   r8   staticmethodr6   rM   rO   rW   r\   rb   rk   r~   r   r   r   r   ru   r   __classcell__)r#   s   @r$   r
   r
      s         HH $& & &E(J&*, , ,KE#JJM. . . . . .4 * * X*  
9 
9 
9   \     X
( 
( 
(; ; ;B 'j 'j 'j 'j 'jR"F "F "FH- - -N N N     & & & &$        r%   r
   c                       e Zd ZddZddZdS )CapturingDisplayHookNc                 *    || _         |g }|| _        d S r'   )r   r   )r   r   r   s      r$   r   zCapturingDisplayHook.__init__P  s    
?Gr%   c                     |d S | j         j                            |          \  }}| j                            ||d           d S )N)datametadata)r   rY   rU   r   r   r   s       r$   r   zCapturingDisplayHook.__call__V  sM    >F#z;BB6JJWkwHHIIIIIr%   r'   )r   r   r   r   r   r   r%   r$   r   r   O  sB           J J J J J Jr%   r   )r   builtinsr,   rQ   ior;   r>   traitlets.config.configurabler   	traitletsr   r   warningsr   historyr   r
   r   r   r%   r$   <module>r      s         



      6 6 6 6 6 6 % % % % % % % %       " " " " " "s s s s s, s s sl	J J J J J J J J J Jr%   