
    c                         d Z dZ	 ddlZddlZddlZddlmZ ddlZddl	m
Z
 	 ddlmZ d Zd Zd Z G d de      Zd Zd Zy# e$ r dZY Cw xY w# e$ r	 d	 Zd
 ZY /w xY w)zCustomized version of pdb's default debugger.

- sets up a history file
- uses ipython if available to colorize lines of code
- overrides list command to search for current block instead
  of using 5 lines of context




zrestructuredtext en    N)Pdb)StringIO)
PyColorizec                 X   t        j                         }t               }|j                  | |       g }t	        |j                         j                               D ]>  \  }}||z   }||k(  r|j                  |dd|        (|j                  |dd|        @ dj                  |      S )zUcolorize and annotate source with linenos
        (as in pdb's list command)
        z>4z	->	z		
)	r   Parserr   format	enumerategetvalue
splitlinesappendjoin)	sourcestart_lineno	curlinenoparseroutput	annotatedindexlinelinenos	            9/usr/lib/python3/dist-packages/logilab/common/debugger.pycolorizer   <   s     ""$ff%	$V__%6%A%A%CD 	;KE4\)F"  F2;fTF!;<  F2;d4&!9:	; yy##    c                     t        j                         }t               }|j                  | |       |j	                         S )zcolorize given source)r   r   r   r	   r   )r   r   r   s      r   colorize_sourcer   L   s2    ""$ff%  r   c                     | S )zfallback colorize function )r   r   r   s      r   r   r   3   s    r   c                     | S Nr   )r   s    r   r   r   7   s    r   c                 X    t        j                  |       \  }}dj                  |      |fS )a  Return the text of the source code for an object.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a single string.  An
    IOError is raised if the source code cannot be retrieved. )inspectgetsourcelinesr   )objlineslnums      r   	getsourcer(   T   s+     ((-KE4775>4r   c                   V    e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
 Zd ZeZd ZeZy)Debuggerzcustom debugger

    - sets up a history file
    - uses ipython if available to colorize lines of code
    - overrides list command to search for current block instead
      of using 5 lines of context
    Nc                     t        j                  |        | j                          |r%|j                  |j                  }|j                  || _        t
        j                  j                  d      | _        y )Nz
~/.pdbhist)	r   __init__resettb_next_tcbkospath
expanduser	_histfile)selftcbks     r   r,   zDebugger.__init__h   sV    T

,,*|| ,,*
++L9r   c                 p    t         !	 t        j                  | j                         yy# t        $ r Y yw xY w)z/if readline is available, read pdb history fileN)readlineread_history_filer3   OSErrorr4   s    r   setup_history_filezDebugger.setup_history_fileq   s:     **4>>:	  
  s   ) 	55c                 d    | j                  | j                  j                  | j                         y)zstarts the interactive modeN)interactionr/   tb_framer:   s    r   startzDebugger.start{   s     ,,djj9r   c                 R    | j                          t        j                  | ||       y)zsetup hook: set up history fileN)r;   r   setup)r4   framer5   s      r   rA   zDebugger.setup   s    !		$t$r   c                 x    t         t        j                  | j                         t        j                  |        y)z,quit hook: save commands in the history fileN)r7   write_history_filer3   r   set_quitr:   s    r   rE   zDebugger.set_quit   s&    ''7Tr   c                    t        | j                  j                        }|j                  | j                  j                         d|v r| j                  ||      S |D cg c]  }|j                  |      s| c}S c c}w )z7provide variable names completion for the ``p`` command.)dictcurframe	f_globalsupdatef_localsattr_matches
startswith)r4   textr   	begin_idxend_idx	namespacevarnames          r   
complete_pzDebugger.complete_p   si    001	//0$;$$T955'0MGG4F4Ft4LMMMs   A=6A=c                 x   ddl }|j                  d|      }|sy|j                  dd      \  }}t        ||      }t	        |      }t        |d      r/|j                  d       || j                  |j                        z   }g }	t        |      }
|D ]'  }|d|
 |k(  s|dk7  s|	j                  | d|        ) |	S )	a  implementation coming from rlcompleter.Completer.attr_matches
        Compute matches when text contains a dot.

        Assuming the text is of the form NAME.NAME....[NAME], and is
        evaluatable in self.namespace, it will be evaluated and its attributes
        (as revealed by dir()) are used as possible completions.  (For class
        instances, class members are also considered.)

        WARNING: this can still invoke arbitrary C code, if an object
        with a __getattr__ hook is evaluated.

        r   Nz(\w+(\.\w+)*)\.(\w*)      	__class____builtins__rG   )
rematchgroupevaldirhasattrr   get_class_membersrX   len)r4   rO   rR   rZ   mexprattrobjectwordsmatchesnwords               r   rM   zDebugger.attr_matches   s     	HH,d3WWQ]
ddI&F6;'LL%D2263C3CDDEI 	1DBQx4DN$:$q/0	1 r   c                 ~    t        |      }t        |d      r%|j                  D ]  }|| j                  |      z   } |S )z8implementation coming from rlcompleter.get_class_members	__bases__)r^   r_   rk   r`   )r4   klassretbases       r   r`   zDebugger.get_class_members   sB    %j5+& 9D224889
r   c                 F   d| _         |sS	 t        | j                        \  }}t        t	        dj                  |      || j                  j                               yt        j                  | |       y# t        $ r Y yt        $ r t        j                  | |       Y yw xY w)znoverrides default list command to display the surrounding block
        instead of 5 lines of context
        listr"   N)lastcmdr(   rI   printr   r   f_linenoKeyboardInterruptr9   r   do_list)r4   argr   r   s       r   ru   zDebugger.do_list   s     ''0'?$hrwwvdmm>T>TUV KKc" %  'D#&'s   AA4 4	B ?B B c                     | j                   j                  j                  }| j                   j                  }d| d| }t	        j
                  |       y)z:opens source file corresponding to the current stack levelzemacsclient --no-wait + N)rI   f_codeco_filenamers   r0   system)r4   rv   filenamer   cmds        r   do_openzDebugger.do_open   sC    ==''33'''xq
;
		#r   r    )__name__
__module____qualname____doc__r,   r;   r?   rA   rE   rT   rM   r`   ru   do_lr~   do_or   r   r   r*   r*   _   sH    ::%
N>#  D Dr   r*   c                  V    t        t        j                        } | j                          y)zuse our custom debuggerN)r*   syslast_tracebackr?   )dbgs    r   pmr      s    
3%%
&CIIKr   c                  n    t               j                  t        j                         j                         y r    )r*   	set_tracer   	_getframef_backr   r   r   r   r      s    J//0r   )r   __docformat__r7   ImportErrorr0   r   pdbr   r#   logilab.common.compatr   IPythonr   r   r   r(   r*   r   r   r   r   r   <module>r      s   $
 & 
 
   *"!"$ ! ts tn1s   H	  s"   A A AAAA