B
    nb\                 @   sl   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ G dd	 d	eZdS )
zS
Widget that handles communications between a console in debugging
mode and Spyder
    N)Qt)RichJupyterWidget)PICKLE_PROTOCOL)CONF)to_text_stringc                   sL   e Zd ZdZdd ZdddZddd	Zd
d Zdd Z fddZ	  Z
S )DebuggingWidgetz
    Widget with the necessary attributes and methods to handle
    communications between a console in debugging mode and
    Spyder
    c             C   s   | j | dS )z7Send raw characters to the IPython kernel through stdinN)kernel_clientinput)selfline r   Flib/python3.7/site-packages/spyder/widgets/ipythonconsole/debugging.pywrite_to_stdin    s    zDebuggingWidget.write_to_stdinFc             C   sJ   | j s
|rFtddi }tj|tdg}t|}d}| j|	| dS )z/Set Spyder breakpoints into a debugging sessionrunbreakpoints)Zprotocolz1!get_ipython().kernel._set_spyder_breakpoints({})N)
_readingr   getpickledumpsr   r   r   r	   format)r
   ZforceZbreakpoints_dictZserialiazed_breakpointsr   cmdr   r   r   set_spyder_breakpoints$   s    

z&DebuggingWidget.set_spyder_breakpoints c             C   s   d ||}| j| dS )z%Run an IPython magic while debugging.z6!get_ipython().kernel.shell.run_line_magic('{}', '{}')N)r   r   r	   )r
   magicargscoder   r   r   dbg_exec_magic2   s    zDebuggingWidget.dbg_exec_magicc             C   sz   d|kr:d|d kr:|d d }|d d }| j || d|krX| jt|d  d|krv| jt|d  dS )z
        Refresh Variable Explorer and Editor from a Pdb session,
        after running any pdb command.

        See publish_pdb_state and notify_spyder in spyder_kernels
        stepfnamelinenoZnamespace_viewZvar_propertiesN)Zsig_pdb_stepemitZsig_namespace_viewastZliteral_evalZsig_var_properties)r
   Z	pdb_stater   r   r   r   r   refresh_from_pdb8   s    

z DebuggingWidget.refresh_from_pdbc                sV    j rtd jj   fdd} jr2d _ j|d d ||d d d d	S )
z#Save history and add a %plot magic.z.Request for raw input during hidden execution.c                s   t  jjdkr  jjd | ksN| dd }d| ttjkrN jj|  | drz|  d } d|  } j	
| n j	
|  d S )Nr    Zdo_z%plot z+__spy_code__ = get_ipython().run_cell('%s'))len_controlhistorysplitdirpdbZPdbappend
startswithr   r	   )r   r   r   )r
   r   r   callbackV   s    
z7DebuggingWidget._handle_input_request.<locals>.callbackFZcontentpromptpassword)r-   r/   N)Z_hiddenRuntimeErrorr   Ziopub_channelflushr   Z	_readline)r
   msgr-   r   )r
   r   _handle_input_requestM   s    z%DebuggingWidget._handle_input_requestc                s   |  }| jr| j| j_|tjkr4| jjdd dS |tjkrP| jjdd dS |tj	tj
fkrl| j  nd| j_tt| |S tt| |S dS )z'Handle Key_Up/Key_Down while debugging.T)ZbackwardFN)keyr   Z_prompt_posr&   Zcurrent_prompt_posr   ZKey_UpZbrowse_historyZKey_DownZ
Key_ReturnZ	Key_EnterZreset_search_posZhist_wholelinesuperr   _event_filter_console_keypress)r
   Zeventr4   )	__class__r   r   r6   l   s     


z.DebuggingWidget._event_filter_console_keypress)F)r   )__name__
__module____qualname____doc__r   r   r   r"   r3   r6   __classcell__r   r   )r7   r   r      s   

r   )r;   r!   r*   r   Zqtpy.QtCorer   Zqtconsole.rich_jupyter_widgetr   Zspyder.config.baser   Zspyder.config.mainr   Zspyder.py3compatr   r   r   r   r   r   <module>
   s   