
    ^h2                     |    S r SSKrSSKrSSKrSSKrSSKJr  SSK	J
r
Jr  SSKJr   " S S\5      r " S S	\5      rg)
zaDisplayhook for IPython.

This defines a callable class that IPython uses for `sys.displayhook`.
    N)Configurable)InstanceFloat)warnc                      ^  \ rS rSrSr\" SSS9r\" SSS9r\" S5      r	SU 4S jjr
\S	 5       rS
 rS r\S 5       rS rS rS rSrSSS jjrS rS rS rS rSS jrS rS rSrU =r$ )DisplayHook   zThe 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?c                 p  > [         [        U ]
  " S	SU0UD6  SnUS::  a
  SU l        SnO#X$:  a  SU l        Sn[	        SU-  SS9  OSU l        X l        Xl        Su  U l        U l        U l	        U R                  U R                  U R                  S.nU R                  R                  R                  U5        g )
Nshell   r   z6caching was disabled (min value for cache size is %s).)
stacklevel    r   r   ______ )superr   __init__do_full_cacher   
cache_sizer   r   r   r   user_nsupdate)selfr   r   kwargscache_size_min
to_user_ns	__class__s         R/home/james-whalen/.local/lib/python3.13/site-packages/IPython/core/displayhook.pyr   DisplayHook.__init__$   s    k4)@@@?!"DJ(!"DJI +,. "#D$ 
"*twtx &&dggDHH=


!!*-    c                 .    U R                   R                  $ N)r   execution_countr   s    r"   prompt_countDisplayHook.prompt_count=   s    zz)))r$   c                     S[         R                  ;   aA   U R                  R                  S   nXR                  La  gU R                  R                  S	 gg! [
         a     gf = f)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_underscore DisplayHook.check_for_underscoreF   sc    
 +&&&!ZZ//4
VV+JJ&&s+ '  s   'A A 
A$#A$c                      U R                   R                  R                  S   nU R	                  U5      $ ! [         a     gf = f)z2Should we silence the display hook because of ';'?F)r   history_managerinput_hist_parsed
IndexErrorsemicolon_at_end_of_expression)r   cells     r"   quietDisplayHook.quietT   sH    	::--??CD
 22488	  		s   #6 
AAc                 x   [         R                  " U 5      n[        [        R                  " UR
                  5      5      n[        U5       Hl  nUS   [        R                  [        R                  [        R                  [        R                  4;   a  MJ  US   [        R                  :X  a  US   S:X  a    g  g   g)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"   r7   *DisplayHook.semicolon_at_end_of_expression`   s     ll:&h..s||<=f%EQxH..X=M=MxO_O_``aHKK'eAh#o &r$   c                     g)z.Start the displayhook, initializing resources.Nr   r(   s    r"   start_displayhookDisplayHook.start_displayhooko   s    r$   c                    [         R                  R                  U R                  R                  5        SR                  U R                  R                  5      nU R                  (       a   [         R                  R                  U5        gg)zaWrite 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_promptDisplayHook.write_output_prompts   sZ     	

001&&tzz'A'AB	JJY' r$   c                 L    U R                   R                  R                  U5      $ )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_formatterrV   r   results     r"   compute_format_dataDisplayHook.compute_format_data   s    < zz++226::r$   Fc                 @   SU;  a  gUS   nSU;   a  U R                   (       d  SU-   n [        U5        g! [         a]    [        UR                  [        R
                  R                  S5      R                  [        R
                  R                  5      5         gf = f)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encoderR   rS   encodingdecode)r   format_dictmd_dictresult_reprs       r"   write_format_dataDisplayHook.write_format_data   s     {* ",/; **"[0	j+! 	j +$$SZZ%8%89KLSSTWT^T^TgTghi	js   6 A$BBc                    U R                   (       Ga  XR                  R                  S   LGa  [        U R                  R                  S   5      U R                   :  a!  U R                  (       a  U R                  5         Sn[        SS5       Vs/ s H  nSU-  PM
     sn HS  nX@R                  R                  ;  a  M  [        X5      U R                  R                  R                  U5      Ld  MQ  SnMU     U R                  U l
        U R                  U l	        Xl        S[        R                  ;  aC  U(       a<  U R                  R                  U R                  U R                  U R                  S.SS9  0 nU R                  (       aS  S	U R                  -  nXU'   U R                  R                  USS9  XR                  R                  S   U R                  '   g
g
g
g
s  snf )z7Update user_ns with various things like _, __, _1, etc._ohTr      r   Fr   )interactivez_%sN)r   r   r   lenr   
cull_cacherangegetattrgetr   r   r   r,   r-   pushr)   )r   r]   update_undersiundersto_main
new_results          r"   update_user_nsDisplayHook.update_user_ns   s    ???vZZ-?-?-FF4::%%e,-@TEWEW! !M*/!*5*Q3q5*5!3!334(

0B0B0F0Fv0NN$)M	 6 wwDHffDGF;///m

TVV&*gg&*hh!0=B   D
 G!!"T%6%66
&,
#

U;?E

""5)$*;*;<	 "5  G? 6s   G!c                 @    U R                   b  XR                   l        g g r&   )exec_resultr]   r\   s     r"   fill_exec_resultDisplayHook.fill_exec_result   s    '&,# (r$   c                    SU;  a  gU R                   R                  R                  (       a)  U R                   R                  R                  US   S5        US   U R                   R                  R
                  U R                  '   g)zLog the output.ra   Noutput)r   logger
log_output	log_writer4   output_hist_reprsr)   )r   rj   s     r"   r   DisplayHook.log_output   sg    {*::''JJ''L(A8L4?4M 	

""44T5F5FGr$   c                     [         R                  R                  U R                  R                  5        [         R                  R                  5         g)z%Finish up all displayhook activities.N)rR   rS   rT   r   separate_out2flushr(   s    r"   finish_displayhookDisplayHook.finish_displayhook   s.    

112

r$   c                 t   U R                  5         Ub  U R                  5       (       d  U R                  5         U R                  5         U R	                  U5      u  p#U R                  U5        U R                  U5        U(       a"  U R                  X#5        U R                  U5        U R                  5         ggg)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   r9   rO   rX   r^   r~   r   rm   r   r   r   r]   rj   rk   s       r"   __call__DisplayHook.__call__  s     	!!#djjll""$$$&#'#;#;F#C K'!!&)&&{<,##% '3r$   c                    U R                   R                  R                  S0 5      n[        U5      n[	        [        X R                  -  5      S5      n[        SR                  X#S95        [        [        U5      5       HG  u  pEXC:  a    gU R                   R                  R                  SU-  S5        UR                  US5        MI     g)z-Output cache is full, cull the oldest entriesrp      zVOutput cache limit (currently {sz} entries) hit.
Flushing oldest {cull_count} entries.)sz
cull_countz_%iN)r   r   rw   rs   maxintcull_fractionr   rV   	enumeratesortedpop)r   ohr   r   rz   ns         r"   rt   DisplayHook.cull_cache  s    ZZ##E2.WR"4"445q9
 55;VrV5Y	[ fRj)DAJJ""519d3FF1dO	 *r$   c                    U R                   (       d  [        S5      e[        SU R                  S-   5       HA  nS[	        U5      -   n U R
                  R                  U	  U R
                  R                  U	 MC     U R
                  R                  R                  SS 5      nUb  UR                  5         Su  U l        U l        U l        S[        R                  ;  aG  U R
                  R                  R!                  U R                  U R                  U R                  S.5        SS Kn[$        R&                  S:w  a  UR)                  5         g g ! [         a     Nf = f! [         a     GM6  f = f)	NzJYou shouldn't have reached the cache flush if full caching is not enabled!r   r   rp   r   r   r   cli)r   
ValueErrorru   r)   reprr   user_ns_hiddenr.   r   rw   clearr   r   r   r,   r-   r   gcrR   platformcollect)r   r   keyr   r   s        r"   r   DisplayHook.flush#  s7   !! ? @ @ q**Q./Ad1g+CJJ--c2JJ&&s+ 0 ZZ##E40>HHJ %/!k***JJ%%466twwTXX&NO <<5 JJL !)    s$   E E
EE
E-,E-)r   r   r   r   r   r   )Ni  r&   )returnN)__name__
__module____qualname____firstlineno____doc__r   r   r   r   r   r   propertyr)   r0   r9   staticmethodr7   rO   rX   r^   rd   rm   r~   r   r   r   r   rt   r   __static_attributes____classcell__)r!   s   @r"   r   r      s     H $&EJ&*,K#JM.2 * *
9  
(;B 'jR"FH-N
&$ r$   r   c                   (    \ rS rSrSS jrSS jrSrg)CapturingDisplayHookiE  Nc                 &    Xl         Uc  / nX l        g r&   )r   outputs)r   r   r   s      r"   r   CapturingDisplayHook.__init__F  s    
?Gr$   c                     Uc  g U R                   R                  R                  U5      u  p#U R                  R	                  X#S.5        g )N)datametadata)r   r[   rV   r   appendr   s       r"   r   CapturingDisplayHook.__call__L  s<    >#zz;;BB6JkHIr$   )r   r   r&   )r   r   r   r   r   r   r   r   r$   r"   r   r   E  s    Jr$   r   )r   builtinsr,   rR   ior=   r@   traitlets.config.configurabler   	traitletsr   r   warningsr   r   objectr   r   r$   r"   <module>r      s>     
   6 % k, k\	J6 Jr$   