
&9_[c           @  s6  d  Z  d d l m Z d d l 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 m Z d d l m Z d d l m Z d	 Z d d
 l m Z d   Z e d  Z e d  Z d e f d     YZ d d  Z d   Z d e f d     YZ e d  Z d S(   s  
Pdb debugger class.

Modified from the standard pdb.Pdb class to avoid including readline, so that
the command line completion of other programs which include this isn't
damaged.

In the future, this class will be expanded with improvements over the standard
pdb.

The code in this file is mainly lifted out of cmd.py in Python 2.2, with minor
changes. Licensing should therefore be under the standard Python terms.  For
details on the PSF (Python Software Foundation) standard license, see:

https://docs.python.org/2/license.html
i(   t   print_functionN(   t   get_ipython(   t
   PyColorizet
   ulinecache(   t	   coloransit	   py3compat(   t   exception_colors(   t   skip_doctests   ipdb> (   t   Pdbc         C  s0   |  d k r d |  d d S|  d k r, d Sd S(   s<   generate the leading arrow in front of traceback or debuggeri   t   -s   > i   t   >t    (    (   t   pad(    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt
   make_arrow4   s
    c         C  sh   t  j d t d d |  t j k r2 t d  n2 | d k	 rQ | |  | |  n t j |  | |  d S(   s   Exception hook which handles `BdbQuit` exceptions.

    All other exceptions are processed using the `excepthook`
    parameter.
    s4   `BdbQuit_excepthook` is deprecated since version 5.1t
   stackleveli   s   Exiting Debugger.N(	   t   warningst   warnt   DeprecationWarningt   bdbt   BdbQuitt   printt   Nonet   BdbQuit_excepthookt   excepthook_ori(   t   ett   evt   tbt
   excepthook(    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyR   =   s    	c         C  s$   t  j d t d d t d  d  S(   Ns<   `BdbQuit_IPython_excepthook` is deprecated since version 5.1R   i   s   Exiting Debugger.(   R   R   R   R   (   t   selfR   R   R   t	   tb_offset(    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   BdbQuit_IPython_excepthookN   s    t   Tracerc           B  s)   e  Z d  Z e d d   Z d   Z RS(   sH  
    DEPRECATED

    Class for local debugging, similar to pdb.set_trace.

    Instances of this class, when called, behave like pdb.set_trace, but
    providing IPython's enhanced capabilities.

    This is implemented as a class which must be initialized in your own code
    and not as a standalone function because we need to detect at runtime
    whether IPython is already active or not.  That detection is done in the
    constructor, ensuring that this code plays nicely with a running IPython,
    while functioning acceptably (though with limitations) if outside of it.
    c         C  s   t  j d t d d t   } | d	 k rO t j t d t j	 t _	 d } n | j
 } | j t j f t  | d	 k r | } n  yE y d d l m } Wn! t k
 r d d l m } n Xd | _ Wn d d	 l } | j   n Xt |  |  _ d	 S(
   sO  
        DEPRECATED

        Create a local debugger instance.

        Parameters
        ----------

        colors : str, optional
            The name of the color scheme to use, it must be one of IPython's
            valid color schemes.  If not given, the function will default to
            the current IPython scheme when running inside IPython, and to
            'NoColor' otherwise.

        Examples
        --------
        ::

            from IPython.core.debugger import Tracer; debug_here = Tracer()

        Later in your code::

            debug_here()  # -> will open up the debugger at that point.

        Once the debugger activates, you can use all of its regular commands to
        step through code, set breakpoints, etc.  See the pdb documentation
        from the Python standard library for usage details.
        s^   `Tracer` is deprecated since version 5.1, directly use `IPython.core.debugger.Pdb.set_trace()`R   i   R   t   NoColori(   t   aRepriP   N(   R   R   R   R   R   t	   functoolst   partialR   t   sysR   t   colorst   set_custom_excR   R   R   t   reprlibR!   t   ImportErrort   reprt	   maxstringt	   tracebackt	   print_excR   t   debugger(   R   R%   t   ipt
   def_colorsR!   R+   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   __init__e   s*    					c         C  s   |  j  j t j   j  d S(   s   Starts an interactive debugger at the point where called.

        This is similar to the pdb.set_trace() function from the std lib, but
        using IPython's enhanced debugger.N(   R-   t	   set_traceR$   t	   _getframet   f_back(   R   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   __call__   s    N(   t   __name__t
   __module__t   __doc__R   R   R0   R4   (    (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyR   U   s   CR   c           s/     f d   } | j  r+ | j  | | _  n  | S(   s   Make new_fn have old_fn's doc string. This is particularly useful
    for the ``do_...`` commands that hook into the help system.
    Adapted from from a comp.lang.python posting
    by Duncan Booth.c            s     |  |   S(   N(    (   t   argst   kw(   t   new_fn(    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   wrapper   s    (   R7   (   R:   t   old_fnt   additional_textR;   (    (   R:   s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   decorate_fn_with_doc   s    	c         C  sC   y t  |   } Wn t k
 r$ g  SX| j   } | j   | Sd S(   s   Return the contents of a named file as a list of lines.

    This function never raises an IOError exception: if the file can't be
    read, it simply returns an empty list.N(   t   opent   IOErrort	   readlinest   close(   t   fnamet   outfilet   out(    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   _file_lines   s    
R   c           B  sd  e  Z d  Z d d d d d d  Z d   Z d   Z d   Z e e e	 j
  Z Z
 d   Z e e e	 j  Z Z d   Z d   Z e e e	 j  Z Z d	   Z d d
  Z d d d  Z d d d  Z e d  Z d   Z d   Z e Z d   Z d   Z e Z d   Z d   Z  d   Z! d   Z" d   Z# d   Z$ e% j& d k rbd   Z' e' Z( n  RS(   s   Modified Pdb class, does not load readline.

    for a standalone version that uses prompt_toolkit, see
    `IPython.terminal.debugger.TerminalPdb` and
    `IPython.terminal.debugger.set_trace()`
    i   c   
      C  s#  y1 t  |  |  _ |  j d k r0 t d   n  Wn# t t f k
 rV t d   n Xt j |  | | |  t   |  _ |  j d  k r t	 j
 d } d d l m } | j   |  _ | t	 j
 d <n  | d  k	 r t j d t  n |  j j } i  |  _ t   |  _ t j } |  j }	 | j |	 d j _ | j |	 d j _ | j |	 d j _ | j |	 d j _ | j |	 d j _ | j |	 d j _ | j |	 d	 j _ | j |	 d	 j _ | j |	 d	 j _ | j |	 d
 j _ | j |	 d
 j _ | j |	 d
 j _ |  j |  t  j!   |  _" t |  _ d  S(   Ni    s"   Context must be a positive integert   __main__i(   t   TerminalInteractiveShells;   The `color_scheme` argument is deprecated since version 5.1R    t   Linuxt   LightBGt   Neutral(#   t   intt   contextt
   ValueErrort	   TypeErrort   OldPdbR0   R   t   shellR   R$   t   modulest!   IPython.terminal.interactiveshellRH   t   instanceR   R   R   R%   t   aliasesR   t   color_scheme_tableR   t
   TermColorsR    t   promptt   breakpoint_enabledt   breakpoint_disabledt   Greent   LightRedt   Redt   Bluet
   set_colorsR   t   Parsert   parser(
   R   t   color_schemet   completekeyt   stdint   stdoutRM   t	   save_mainRH   t   Ct   cst(    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyR0      sJ    
			c         C  s   |  j  j |  d S(   s;   Shorthand access to the color table scheme selector method.N(   RV   t   set_active_scheme(   R   t   scheme(    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyR_     s    c         C  sL   y t  j |  | |  Wn. t k
 rG t j j d |  j j    n Xd  S(   Ns   
(   RP   t   interactiont   KeyboardInterruptR$   Re   t   writeRQ   t   get_exception_only(   R   t   frameR+   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyRk     s    c         C  s   t  j |  |  d  S(   N(   RP   t   do_up(   R   t   arg(    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt	   new_do_up$  s    c         C  s   t  j |  |  d  S(   N(   RP   t   do_down(   R   Rq   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   new_do_down(  s    c         C  s   t  j |  |  d  S(   N(   RP   t   do_frame(   R   Rq   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   new_do_frame-  s    c         C  s4   t  |  d  r$ |  j |  j j _ n  t j |  |  S(   Nt   old_all_completions(   t   hasattrRw   RQ   t	   Completert   all_completionsRP   t   do_quit(   R   Rq   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   new_do_quit0  s    c         C  s   |  j  d  |  j |  S(   s\   Restart command. In the context of ipython this is exactly the same
        thing as 'quit'.s6   Restart doesn't make sense here. Using 'quit' instead.(   t   msgR{   (   R   Rq   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   new_do_restart9  s    c         C  s   | d  k r |  j } n  y+ t |  } | d k rB t d   n  Wn# t t f k
 rh t d   n Xy+ x$ |  j D] } |  j | d | qv WWn t k
 r n Xd  S(   Ni    s"   Context must be a positive integerRM   (   R   RM   RL   RN   RO   t   stackt   print_stack_entryRl   (   R   RM   t   frame_lineno(    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   print_stack_trace?  s    s   
-> c         C  s   | d  k r |  j } n  y+ t |  } | d k rB t d   n  Wn# t t f k
 rh t d   n Xt |  j | d |   | \ } } | j j } |  j	 j
 j | | d  d  S(   Ni    s"   Context must be a positive integerR   (   R   RM   RL   RN   RO   R   t   format_stack_entryt   f_codet   co_filenameRQ   t   hookst   synchronize_with_editor(   R   R   t   prompt_prefixRM   Ro   t   linenot   filename(    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyR   N  s    s   : c      	   C  s%  | d  k r |  j } n  y) t |  } | d k r@ t d  n  Wn! t t f k
 rd t d  n Xy d d  l } Wn t k
 r d d  l } n Xg  } |  j	 j
 } | j } d | j | f } d | j | j | f }	 d | j | f }
 d | j | j | f } | \ } } d } d	 | j k rP| j d	 } | | j |  d
 7} n  | j |  |  j | j j  } | t j |  } | j j r| j j } n d } d } | d k rd | j k r| j | j d  } n d } |	 | | f } n  | |  j k r| j d  n | j d  | j d | | | f  | d | d } t j |  } t | t |  |  } t | d  } | | | | !} x| t  |  D]n \ } } | d | | k } | |  j k s| r| p|
 } | j |  j! | | | d | | d |  qWd j" |  S(   Ni    s"   Context must be a positive integeriu   %s%%s%su   %s%%s%s%%s%su   %%s%s%%s %s%%su   %%s%s%%s %s%%s%sR   t
   __return__s   
s   <lambda>t   ?t   __args__s   ()s   > s     u	   %s(%s)%s
i   i   t   arrow(#   R   RM   RL   R   RO   RN   R'   R(   R)   RV   t   active_colorst   Normalt
   filenameEmt   vNamet   valEmR   t   linenoEmt   linet   f_localst   appendt   canonicR   R   R   t   cast_unicodet   co_namet   curframeR   t   getlinest   mint   lent   maxt	   enumeratet   _Pdb__format_linet   join(   R   R   t   lprefixRM   R'   t   rett   Colorst   ColorsNormalt   tpl_linkt   tpl_callt   tpl_linet   tpl_line_emRo   R   t   return_valuet   rvR   t   linkt   funct   callR8   t   startt   linest   iR   t
   show_arrowt   linetpl(    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyR   `  sn    	
c         C  sA  d } d } |  j  j } |  j j | d |  \ }	 }
 |
 sE |	 } n  d  } | |  j |  k r |  j | |  } | d } n  | r |  j  j } t | j	  } | j
 } | j s | j } q n  d } | r| t t |   t |  } d t |  t |  f } n  d | t |  t |  f } | | | | | f S(   NR   t   strii   s   %s%ss   %*s(   RV   t   active_scheme_nameRa   t   format2R   t   get_file_breakst
   get_breaksR   R   t   numberRY   t   enabledRZ   R   R   (   R   R   R   R   R   R   t   bp_markt   bp_mark_colorRj   t   new_linet   errt   bpt   bpsR   t   numbers_widthR   t   num(    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   __format_line  s,     			  c         C  s>  y&|  j  j } | j } d | j | f } d | j | j | f } g  } | d k rq t |  d  rq |  j } n  x t | | d  D] }	 t	 j
 | |	  }
 |
 s Pn  |	 |  j j k r |  j | | |	 |
 d t }
 n |  j | | |	 |
 d t }
 | j |
  |	 |  _ q Wt d j |   Wn t k
 r9n Xd S(	   sI   The printing (as opposed to the parsing part of a 'list'
        command.s   %%s%s%%s %s%%ss   %%s%s%%s %s%%s%ss   <string>t   _exec_filenamei   R   R   N(   RV   R   R   R   R   R   Rx   R   t   rangeR   t   getlineR   t   f_linenoR   t   Truet   FalseR   R   R   Rl   (   R   R   t   firstt   lastR   R   R   R   t   srcR   R   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   print_list_lines  s(    	!c         C  s\  d |  _  d } | r y t | i  i   } t |  t d  k r | \ } } t |  } t |  } | | k  r | | } q n t d t |  d  } Wq t d t |   d SXn8 |  j d k r t d |  j	 j
 d  } n |  j d } | d k r| d } n  |  j |  j	 j j | |  | } |  j	 j j } |  j j j | | d  d S(	   s9   Print lines of code from the current stack frame
        t   listi   i   s   *** Error in argument:Ni
   i    (    (   t   lastcmdR   t   evalt   typeRL   R   R   R)   R   R   R   R   R   R   RQ   R   R   (   R   Rq   R   t   xR   R   R   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   do_list  s0    	c         C  st   t  j |  \ } } t  j |  r@ | j | j k r@ | d f St  j |  rY | d f St  j | |  | d f S(   Ni   (   t   inspectt
   findsourcet   isframet	   f_globalsR   t   ismodulet   getblock(   R   t   objR   R   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   getsourcelines  s    !

c         C  sy   d |  _  y |  j |  j  \ } } Wn! t k
 rH } |  j |  d SX| t |  } |  j |  j j j | |  d S(   sf   Print lines of code from the current stack frame.

        Shows more lines than 'list' does.
        t   longlistN(	   R   R   R   t   OSErrort   errorR   R   R   R   (   R   Rq   R   R   R   R   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   do_longlist  s    	c         C  sD   d |  j  j f d |  j  j f g } |  j j d  | d | d S(   sZ   Print the call signature for any callable object.

        The debugger interface to %pdeft   Localst   Globalst   pdeft
   namespacesN(   R   R   R   RQ   t   find_line_magic(   R   Rq   R   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   do_pdef#  s    c         C  sD   d |  j  j f d |  j  j f g } |  j j d  | d | d S(   sL   Print the docstring for an object.

        The debugger interface to %pdoc.R   R   t   pdocR   N(   R   R   R   RQ   R   (   R   Rq   R   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   do_pdoc+  s    c         C  sD   d |  j  j f d |  j  j f g } |  j j d  | d | d S(   su   Print (or run through pager) the file where an object is defined.

        The debugger interface to %pfile.
        R   R   t   pfileR   N(   R   R   R   RQ   R   (   R   Rq   R   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   do_pfile3  s    c         C  sD   d |  j  j f d |  j  j f g } |  j j d  | d | d S(   sd   Provide detailed information about an object.

        The debugger interface to %pinfo, i.e., obj?.R   R   t   pinfoR   N(   R   R   R   RQ   R   (   R   Rq   R   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   do_pinfo<  s    c         C  sD   d |  j  j f d |  j  j f g } |  j j d  | d | d S(   sl   Provide extra detailed information about an object.

        The debugger interface to %pinfo2, i.e., obj??.R   R   t   pinfo2R   N(   R   R   R   RQ   R   (   R   Rq   R   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt	   do_pinfo2D  s    c         C  sD   d |  j  j f d |  j  j f g } |  j j d  | d | d S(   s;   Print (or run through pager) the source code for an object.R   R   t   psourceR   N(   R   R   R   RQ   R   (   R   Rq   R   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt
   do_psourceL  s    i   c         C  s0   | r" t  |  } |  j |  n
 |  j   d S(   sH  w(here)
            Print a stack trace, with the most recent frame at the bottom.
            An arrow indicates the "current frame", which determines the
            context of most commands. 'bt' is an alias for this command.

            Take a number as argument as an (optional) number of context line to
            printN(   RL   R   (   R   Rq   RM   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   do_whereS  s    N(   i   ()   R5   R6   R7   R   R0   R_   Rk   Rr   R>   RP   Rp   t   do_uRt   Rs   t   do_dRv   R|   R{   t   do_qR~   R   R   R   R   R   R   R   t   do_lR   R   t   do_llR   R   R   R   R   R   R$   t   version_infoR   t   do_w(    (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyR      sB   C							J		"											c         C  s#   t    j |  p t j   j  d S(   sm   
    Start debugging from `frame`.

    If frame is not specified, debugging starts from caller's frame.
    N(   R   R1   R$   R2   R3   (   Ro   (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyR1   d  s    (    R7   t
   __future__R    R   R"   R   R$   R   t   IPythonR   t   IPython.utilsR   R   R   R   t   IPython.core.excolorsR   t   IPython.testing.skipdoctestR   RX   t   pdbR   RP   R   R   R   R   t   objectR   R>   RF   R1   (    (    (    s4   lib/python2.7/site-packages/IPython/core/debugger.pyt   <module>   s,   		]	 