
    uki                        d dl mZ d dlZd dlZd dlZd dlZd dlmZmZ d dl	m
Z ej                  Z G d dej                        Z	 	 d
dZ ej                   ded	       y)    )annotationsN)AnyIO)corec                       e Zd ZdZdZ	 	 d	 	 	 	 	 d fdZd Zd Zd Zd Z	ddZ
d	 Zd
 Zd Zd ZeZd ZeZd ZeZd ZexZZd ZexZxZZd ZexZZd Z xZS )CliDebuggerzA text-based debugger.z(jdb) c                t    t         |   |||       |d u | _        || _        d| _        || _        d| _        y )N)stdinstdoutcompletekeyr   zEntering jdb:)super__init__use_rawinputframesframe_index	thread_idintro)selfr   r   r
   r   r   	__class__s         Y/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/debugger/cli_debugger.pyr   zCliDebugger.__init__   sB     
G5[IDDKDDN DJ    c                4    | j                   | j                     S N)r   r   r   s    r   current_framezCliDebugger.current_frame(   s    ;;t''((r   c                    i }| j                   | j                     }|j                  |j                         |j                  |j                         t        |i |      S r   )r   r   updateglobalslocalseval)r   exprenv
curr_frames       r   evaluatezCliDebugger.evaluate+   sN    
CT--.JJJz!!"JJz  !b#r   c                    	 t        t        | j                  |            | j                         y#  | j	                          Y yxY w)zEvaluates an expression.fileNprintreprr$   r   _error_messager   args     r   defaultzCliDebugger.default2   s6    Ds#$4;;7
	   /2 Ac                   g }|j                  d       | j                  d d d   D ]  }|j                  d|j                   d|j                          |j                  |j                  d       L|j
                  |j                     }|j                  d|j                                  t        dj                  |      | j                         y )	Nz
Traceback:z  File "z", line z    <no source>    
r&   )
appendr   filenamelinenooffsetsourcestripr)   joinr   )r   	backtraceframelines       r   print_backtracezCliDebugger.print_backtrace9   s    I\"TrT" 0%..!1%,,HI		*+||ELL)4

~./0 
$))I
T[[1r   c                   | j                   | j                     }g }|j                  d|j                   d|j                   d       t        |j                        D ]w  \  }}|j                  J |j                  dz
  |z
  |cxk  r|j                  |z   k  s=n @||j                  k(  r|j                  d|        d|j                  d|        y t        dj                  |      | j                         y )	Nz> ()   z->  r2   r3   r&   )r   r   r4   r5   r6   	enumerater8   r7   r)   r:   r   )r   	num_linesr#   contextir=   s         r   print_contextzCliDebugger.print_contextE   s    T--.JGNNR
++,Aj.?.?-@BCZ../ (4***


a
)
+q (


i
'(
!!!
..4v
'
..4v
'( 
$))G
4;;/r   c                    t        j                         d d }t        j                  | d   j	                         }t        d|| j                         y )N   r1   z***r&   )sysexc_info	tracebackformat_exception_onlyr9   r)   r   )r   rK   msgs      r   r+   zCliDebugger._error_messageS   sB    ||~bq!H

)
)8
4R
8
>
>
@C	%4;;'r   c                    	 t        t        | j                  |            | j                         y#  | j	                          Y yxY w)zEp expression
    Evaluates and prints the value of an expression
    r&   Nr(   r,   s     r   do_pzCliDebugger.do_pX   s6    Ds#$4;;7
r/   c                    	 t        t        j                  | j                  |            | j                         y#  | j                          Y yxY w)zMpp expression
    Evaluates and pretty-prints the value of an expression
    r&   N)r)   pprintpformatr$   r   r+   r,   s     r   do_ppzCliDebugger.do_ppa   s:    FNN4==-.T[[A
s	   9< Ac                    | j                   t        | j                        dz
  k(  rt        d| j                         n| xj                   dz  c_         | j                          y)z$u(p)
    Move up a stack frame.
    rB   zAt topmost frame.r&   N)r   lenr   r)   r   rG   r   _s     r   do_upzCliDebugger.do_upj   sL     3t{{+a//dkk2
!r   c                    | j                   dk(  rt        d| j                         n| xj                   dz  c_         | j                          y)z(d(own)
    Move down a stack frame.
    r   zAt bottommost frame.r&   rB   N)r   r)   r   rG   rW   s     r   do_downzCliDebugger.do_downu   s>     1"5
!r   c                (    | j                  d       y)z6l(ist)
    List source code for the current file.
       )rD   N)rG   rW   s     r   do_listzCliDebugger.do_list   s     	#r   c                     y)z7c(ont(inue))
    Continue the program's execution.
    T rW   s     r   do_continuezCliDebugger.do_continue   s     r   c                .    t        j                  d       y)zOq(uit)
(exit)
    Quit the debugger. The program is given an exit command.
    r   N)rJ   exitrW   s     r   do_quitzCliDebugger.do_quit   s     HHQKr   c                $    | j                          y)zvw(here)
    Prints a stack trace with the most recent frame on the bottom.
    'bt' is an alias for this command.
    N)r>   rW   s     r   do_wherezCliDebugger.do_where   s    
 	r   c                ~    	 	 | j                          y # t        $ r t        dt        j                         Y nw xY w=)Nz--KeyboardInterrupt--r&   )cmdloopKeyboardInterruptr)   rJ   r   r   s    r   runzCliDebugger.run   s9    
8 8%CJJ78	 s    $;;)NNtab)r   list[DebuggerFrame]r
   IO[str] | Noner   rm   r   str)rI   ) __name__
__module____qualname____doc__promptr   r   r$   r.   r>   rG   r+   rP   rT   rY   do_ur[   do_dr^   do_lra   do_cdo_contrd   do_qdo_EOFdo_exitrf   do_wdo_btrj   __classcell__)r   s   @r   r   r      s    & >B!!,:!!)
20(
 
$ 
$$
 
$
 $
 $#$#' $8r   r   c                :    t        | |fi |j                          y r   )r   rj   )r   r   kwargss      r   run_debuggerr      s    fi*6*..0r   clir1   )r   rl   r   z
int | Noner   r   )
__future__r   cmdrR   rJ   rL   typingr   r   jax._src.debuggerr   debugger_coreDebuggerFrameCmdr   r   register_debuggerr`   r   r   <module>r      s]    # 
  
   3++I8#'' I8V11    |R 8r   