
    fn                        d Z ddlmZ  ede       ddlZddl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mZmZmZmZmZmZmZmZ ddlmZmZmZmZmZmZmZmZmZm Z   ejB                  djE                  g d	            Z# ejB                  d
      Z$ ejB                  d      Z%d Z&e	jN                  Z(e	jN                  dz   Z) G d d      Z* G d d      Z+d Z,d Z-d Z. ejB                  dej^                        Z0 ejB                  dej^                        Z1d Z2d Z3d Z4 G d de5      Z6 G d de6      Z7y)a6  DEPRECATED: Input handling and transformation machinery.

This module was deprecated in IPython 7.0, in favour of inputtransformer2.

The first class in this module, :class:`InputSplitter`, is designed to tell when
input from a line-oriented frontend is complete and should be executed, and when
the user should be prompted for another line of code instead. The name 'input
splitter' is largely for historical reasons.

A companion, :class:`IPythonInputSplitter`, provides the same functionality but
with full support for the extended IPython syntax (magics, system calls, etc).
The code to actually do these transformations is in :mod:`IPython.core.inputtransformer`.
:class:`IPythonInputSplitter` feeds the raw code to the transformers in order
and stores the results.

For more details, see the class docstrings below.
    )warnzeIPython.core.inputsplitter is deprecated since IPython 7 in favor of `IPython.core.inputtransformer2`N)List)
leading_indentclassic_prompt
ipy_prompt	cellmagicassemble_logical_lineshelp_endescaped_commandsassign_from_magicassign_from_systemassemble_python_lines)
	ESC_SHELL
ESC_SH_CAPESC_HELP	ESC_HELP2	ESC_MAGIC
ESC_MAGIC2	ESC_QUOTE
ESC_QUOTE2	ESC_PARENESC_SEQUENCES|)z^\s+raise(\s.*)?$z^\s+raise\([^\)]*\).*$z^\s+return(\s.*)?$z^\s+return\([^\)]*\).*$z^\s+pass\s*$z^\s+break\s*$z^\s+continue\s*$z^([ \t\r\f\v]+)z^\s*\#c                 R    t         j                  |       }|r|j                         S y)a  Return the number of initial spaces in a string.

    Note that tabs are counted as a single space.  For now, we do *not* support
    mixing of tabs and spaces in the user's input.

    Parameters
    ----------
    s : string

    Returns
    -------
    n : int
    r   )ini_spaces_rematchend)s
ini_spacess     U/var/www/cvtools/html/venv/lib/python3.12/site-packages/IPython/core/inputsplitter.pynum_ini_spacesr!   O   s'     $$Q'J~~       c                       e Zd ZexZZd Zy)IncompleteStringc                 <    || _         || _        || _        || _        y Nr   startr   line)selfr   r)   r   r*   s        r    __init__zIncompleteString.__init__l   s    
	r"   N)__name__
__module____qualname__INCOMPLETE_STRINGtype
exact_typer,    r"   r    r%   r%   j   s    ))D:r"   r%   c                       e Zd ZexZZd Zy)InMultilineStatementc                 <    d| _         |x| _        | _        || _        y N r(   )r+   posr*   s      r    r,   zInMultilineStatement.__init__t   s     ##
TX	r"   N)r-   r.   r/   IN_MULTILINE_STATEMENTr1   r2   r,   r3   r"   r    r5   r5   r   s    ..D:r"   r5   c              #   j  K   t        j                  |       j                  }t        j                  t        j
                  dddd      }	 t        j                  |      D ]  }|  y	# t        j                  $ r}| j                  d      }t        |      t        |d         f}d|j                  d   v rE|j                  x\  }}}||dz
     |d	 dj                  ||d	       z   } t        | |||d          n)d
|j                  d   v rt        ||d          n Y d	}~y	Y d	}~y	d	}~ww xY ww)a  Iterate over tokens from a possibly-incomplete string of code.

    This adds two special token types: INCOMPLETE_STRING and
    IN_MULTILINE_STATEMENT. These can only occur as the last token yielded, and
    represent the two main ways for code to be incomplete.
    r8   )r#   r   T)keependszmulti-line stringr   r#   Nzmulti-line statement)ioStringIOreadlinetokenize	TokenInfoNEWLINEgenerate_tokens
TokenError
splitlineslenargsr   joinr%   r5   )	r   r@   tokenelinesr   lcr)   s	            r    partial_tokensrO   y   s$     {{1~&&Hx//VVRHE--h7 	EK	 d+%j#eBi.(!&&)+ 99$DAq5ac
12qr!33A"1eS%)<<#qvvay0&sE"I66 7 =s1   AD3
A) (D3)D0<B%D+!
D3+D00D3c                    t        t        |             }|d   j                  t        j                  k(  r|j                          |sy|d   j                  t        j                  t        j                  t        j                  hv rO|j                          |d   j                  t        j                  t        j                  t        j                  hv rO|d   j                  t        k(  rydgfd}t        |      }|D ]  }|j                  t        j                  t        j                  hv r ||j                  d          E|j                  t        j                  k(  sc	  |t        |      j                  d           j                         }|d   j                  t"        k(  rE|d   j$                  t        j&                  t        j(                  t        j*                  hv r|dz   S |S |d   j$                  t        j,                  k(  r|dz   S |rd}t/        |      D ](  \  }}|j                  t        j                  k(  s$|dz   }* ||d }|D 	cg c],  }	|	j                  t        j0                  k(  s!|	j2                  . }
}	|
r |
d   dv rt5              D ]  }||k  s	|c S  |S # t         $ r Y  7w xY wc c}	w )	z:Find the number of spaces for the next line of indentationr=   r   c                 :    | d   k7  rj                  |        y y Nr=   )append)nprev_indentss    r    _add_indentz%find_next_indent.<locals>._add_indent   s#    R  " !r"   r#      N>   passbreakraisereturncontinue)listrO   r1   rA   	ENDMARKERpopDEDENTrC   COMMENTr0   iterINDENTr   NLnextr)   StopIterationr:   r2   LPARLSQBLBRACECOLON	enumerateNAMEstringreversed)codetokensrV   tokitertoklast_indentlast_line_startsilast_line_tokenstnamesindentrU   s               @r    find_next_indentr{      sb   .&'Fbz(,,,

":??x0@0@(BRBRSS

 ":??x0@0@(BRBRSS bz++ 3L# 6lG 8899
#hh(++%DM//23 ""$K bz00":  X]]HMM8??$SS?"bz.Q ' 	)FAsxx8+++#$q5 	) ""2"34#3Oaqvv7NOOU1X!QQ"<0 "K'!M" ? ! 0 Ps   &J0"K<K0	J>=J>c                 \    | sy| j                         d   }|dk(  xs |j                         S )zDetermine if the input source ends in a blank.

    A blank is either a newline or a line consisting of whitespace.

    Parameters
    ----------
    src : string
        A single or multiline string.
    Fr=   r8   )rF   isspace)srclls     r    
last_blankr      s/     u
..
2
B"H%%r"   z\n\s*\n\s*$z.+\n\s*\n\s+$c                     | sydj                  dg| j                         dd z         }t        t        j	                  |            xs t        t
        j	                  |            S )zDetermine if the input source ends in two blanks.

    A blank is either a newline or a line consisting of whitespace.

    Parameters
    ----------
    src : string
        A single or multiline string.
    F
z###
rW   N)rI   rF   boollast_two_blanks_rer   last_two_blanks_re2)r~   new_srcs     r    last_two_blanksr      s`     u ii	CNN$4RS$99:G#))'23 5$**7347r"   c                 0    t        j                  dd|       S )a  Remove all comments from input source.

    Note: comments are NOT recognized inside of strings!

    Parameters
    ----------
    src : string
        A single or multiline input string.

    Returns
    -------
    String with all Python comments removed.
    z#.*r8   )resub)r~   s    r    remove_commentsr      s     66%S!!r"   c                  D    t        t        j                  dd      } | d} | S )zcReturn the default standard input encoding.

    If sys.stdin has no encoding, 'ascii' is returned.encodingNascii)getattrsysstdin)r   s    r    get_input_encodingr   
  s&     syy*d3HOr"   c                       e Zd ZU dZdZdZdZdZee	   e
d<   ej                  e
d<   dZdZdd	Zd
 Zd Zd Zde	defdZd Zd Z ee      ZddZd Zy)InputSplittera$  An object that can accumulate lines of Python source before execution.

    This object is designed to be fed python source line-by-line, using
    :meth:`push`. It will return on each push whether the currently pushed
    code could be executed already. In addition, it provides a method called
    :meth:`push_accepts_more` that can be used to query whether more input
    can be pushed into a single interactive block.

    This is a simple example of how an interactive terminal-based client can use
    this tool::

        isp = InputSplitter()
        while isp.push_accepts_more():
            indent = ' '*isp.indent_spaces
            prompt = '>>> ' + indent
            line = indent + raw_input(prompt)
            isp.push(line)
        print 'Input source was:\n', isp.source_reset(),
    NNr8   N_buffer_compileFr\   c                 b    g | _         t        j                         | _        t	               | _        y)z$Create a new InputSplitter instance.N)r   codeopCommandCompilerr   r   r   r+   s    r    r,   zInputSplitter.__init__L  s#    ..0*,r"   c                 Z    g | j                   dd d| _        d| _        d| _        d| _        yz,Reset the input buffer and associated state.Nr8   F)r   sourcerp   _is_complete_is_invalidr   s    r    resetzInputSplitter.resetR  s-    Q	! r"   c                 >    | j                   }| j                          |S )z:Return the input source and perform a full reset.
        )r   r   r+   outs     r    source_resetzInputSplitter.source_resetZ  s     kk


r"   c                 r   | j                          	 | j                  |       | j                  r	 | j                          y| j                         r"d| j	                         f| j                          S 	 | j                          y# t
        $ r Y | j                          yw xY w# | j                          w xY w)ak  Return whether a block of code is ready to execute, or should be continued

        This is a non-stateful API, and will reset the state of this InputSplitter.

        Parameters
        ----------
        source : string
            Python input code, which can be multiline.

        Returns
        -------
        status : str
            One of 'complete', 'incomplete', or 'invalid' if source is not a
            prefix of valid code.
        indent_spaces : int or None
            The number of spaces by which to indent the next line of code. If
            status is not 'incomplete', this is None.
        )invalidN
incomplete)completeN)r   pushr   push_accepts_moreget_indent_spacesSyntaxError)r+   r   s     r    check_completezInputSplitter.check_completea  s    & 	

	IIf & JJL '')#T%;%;%== JJL (JJL  	# # JJL	# JJLs.   B B$ !B$ 	B!B$  B!!B$ $B6rL   c                 *   t        |t              sJ | j                  |       | j                  }d\  | _        | _        d| _        |j                  d      ry	 t        j                         5  t        j                  dt               | j                  |d      | _        ddd       | j                  du| _        | j
                  S # 1 sw Y   (xY w# t        t        t        t         t"        t        f$ r d| _        d| _        Y | j
                  S w xY w)	a  Push one or more lines of input.

        This stores the given lines and returns a status code indicating
        whether the code forms a complete Python block or not.

        Any exceptions generated in compilation are swallowed, but if an
        exception was produced, the method returns True.

        Parameters
        ----------
        lines : string
            One or more lines of Python input.

        Returns
        -------
        is_complete : boolean
            True if the current input source (the result of the current input
            plus prior inputs) forms a complete Python execution block.  Note that
            this value is also stored as a private attribute (``_is_complete``), so it
            can be queried at any time.
        r   Fz\
errorexec)symbolNT)
isinstancestr_storer   rp   r   r   endswithwarningscatch_warningssimplefilterSyntaxWarningr   r   OverflowError
ValueError	TypeErrorMemoryError)r+   rL   r   s      r    r   zInputSplitter.push  s    , %%%%E
 (2$	4$  ??6"	6((* A%%g}= MM&M@	A !%		 5D   #A A ]J	], 	$ $D#D    	$s*   C -3C C CC 1DDc                    | j                   sy| j                  j                         d   }|r|j                         ry| j	                         dk(  rt        | j                  j                               dk  ry	 t        j                  dj                  | j                              }t        |j                        dk(  rt        |j                  d   d      syyy# t        $ r Y yw xY w)a  Return whether a block of interactive input can accept more input.

        This method is meant to be used by line-oriented frontends, who need to
        guess whether a block is complete or not based solely on prior and
        current input lines.  The InputSplitter considers it has a complete
        interactive block and will not accept more input when either:

        * A SyntaxError is raised

        * The code is complete and consists of a single line or a single
          non-compound statement

        * The code is complete and has a blank line at the end

        If the current input produces a syntax error, this method immediately
        returns False but does *not* raise the syntax error exception, as
        typically clients will want to send invalid syntax to an execution
        backend which might convert the invalid syntax into valid Python via
        one of the dynamic IPython mechanisms.
        Tr=   Fr   r#   r8   body)r   r   rF   r}   r   rG   astparserI   r   r   hasattr	Exception)r+   	last_linecode_asts      r    r   zInputSplitter.push_accepts_more  s    0    KK**,R0	i//1
 !!#q(4;;))+,1	!99RWWT\\%:;
 x}}%*(/a0@&(I    s   9.C 	C'&C'c                     | j                   \  }}|| j                  k(  r|S t        | j                  d d       }| j                  |f| _         |S rR   )_indent_spaces_cacher   r{   )r+   	sourceforrT   s      r    r   zInputSplitter.get_indent_spaces  sO    00	1#H T[["-.%)[[!$4!r"   c                     || j                   }|j                  d      r|j                  |       n|j                  |dz          t        | || j	                  |             y)zStore one or more lines of input.

        If input lines are not newline-terminated, a newline is automatically
        appended.Nr   )r   r   rS   setattr_set_source)r+   rL   bufferstores       r    r   zInputSplitter._store  sQ     >\\F>>$MM% MM%*%eT--f56r"   c                 $    dj                  |      S r7   )rI   )r+   r   s     r    r   zInputSplitter._set_source  s    xxr"   )r\   N)Nr   )r-   r.   r/   __doc__r   r   r   rp   r   r   __annotations__r   r   r   r   r,   r   r   r   r   r   r   r   propertyindent_spacesr   r   r3   r"   r    r   r     s    2 & H F D
 #Y$$$LK-!"H6! 6! 6!p5n ./M7 r"   r   c                        e Zd ZdZdZdZdZdZ	 	 d fd	Ze	d        Z
e	d        Z fdZd	 Zd
 Zd Z fdZd Zdedef fdZd Z xZS )IPythonInputSplitterzBAn input splitter that recognizes all of IPython's special syntax.r8   FNc                 |   t         t        |           g | _        d| _        ||| _        n-t               t               t               t        |      g| _        t               | _
        ||| _        n+t               t               t               t               g| _        t!               | _        ||| _        y g | _        y )NT)end_on_blank_line)superr   r,   _buffer_raw	_validatephysical_line_transformsr   r   r   r   r	   logical_line_transformsr
   r   r   r   r   python_line_transforms)r+   line_input_checkerr   r   r   	__class__s        r    r,   zIPythonInputSplitter.__init__)  s    "D24#/,DD) .<-=-;-=-7\-6I[-\	-.D) '=&>#".+BD( -5J,<,>,=,?,>,@	,-D( &;%<"!-*@D' +-D'r"   c                     | j                   | j                  gz   | j                  z   | j                  gz   | j                  z   S )z!Quick access to all transformers.)r   r	   r   r   r   r   s    r    
transformszIPythonInputSplitter.transformsK  sS     ,,(()*,0,H,HI''(),0,G,GH 	Hr"   c                     | j                   dd }| j                  s|| j                  g| j                  z   z  }|| j                  gz   | j
                  z   S )zTTransformers, excluding logical line transformers if we're in a
        Python line.N)r   within_python_liner	   r   r   r   )r+   rx   s     r    transforms_in_usez&IPythonInputSplitter.transforms_in_useR  sZ     ))!,&&$--.1M1MMMAD..//$2M2MMMr"   c                     t         t        |           g | j                  dd d| _        d| _        d| _        | j                  D ]  }	 |j                           y# t        $ r Y "w xY wr   )	r   r   r   r   
source_rawtransformer_accumulatingr   r   r   )r+   rx   r   s     r    r   zIPythonInputSplitter.reset[  so    "D/1 (-%"' 	A		   s   A	A'&A'c                     d }g }| j                   D ]  } |||      } t        |      }|r!| j                  dj                  |             y y )Nc              3      K   |D ]/  }|j                         D ]  }| j                  |      }||  1 | j                         }|| yyw)a)  yield transformed lines

            always strings, never None

            transform: the current transform
            outs: an iterable of previously transformed inputs.
                 Each may be multiline, which will be passed
                 one line at a time to transform.
            N)rF   r   r   )	transformoutsr   r*   tmps        r    _flushz7IPythonInputSplitter.flush_transformers.<locals>._flushl  sb       "NN, "D#...C!		"" //#C	 s
   ,A Ar   )r   r^   r   rI   )r+   r   r   rx   s       r    flush_transformersz'IPythonInputSplitter.flush_transformersk  sU    	, '' 	!AC.C	! 3iKK		#' r"   c                 >    | j                   }| j                          |S )z8Return raw input only and perform a full reset.
        )r   r   r   s     r    	raw_resetzIPythonInputSplitter.raw_reset  s     oo


r"   c                     	 | j                          | j                  | j                          S # | j                          w xY wr'   )r   r   r   r   s    r    r   z!IPythonInputSplitter.source_reset  s-    	##%;;JJLDJJLs	   . A c                 B    | j                   ryt        t        |          S NT)r   r   r   r   )r+   r   s    r    r   z&IPythonInputSplitter.push_accepts_more  s     ((-tFHHr"   c                     | j                          	 | j                  |       | j                          | j                  | j                          S # | j                          w xY w)z/Process and translate a cell of input.
        )r   r   r   r   )r+   cells     r    transform_cellz#IPythonInputSplitter.transform_cell  sC     	

	IIdO##%;;JJLDJJLs   ,A A!rL   r\   c                 6   t        |t              sJ |j                         }|sdg}| j                  || j                  d       g }|D ]'  }| j                  |      }||j                  |       ) |r$dj                  |      }t        t        | +  |      S y)aJ  Push one or more lines of IPython input.

        This stores the given lines and returns a status code indicating
        whether the code forms a complete Python block or not, after processing
        all input lines for special IPython syntax.

        Any exceptions generated in compilation are swallowed, but if an
        exception was produced, the method returns True.

        Parameters
        ----------
        lines : string
            One or more lines of Python input.

        Returns
        -------
        is_complete : boolean
            True if the current input source (the result of the current input
            plus prior inputs) forms a complete Python execution block.  Note that
            this value is also stored as a private attribute (_is_complete), so it
            can be queried at any time.
        r8   r   r   F)r   r   rF   r   r   _transform_linerS   rI   r   r   r   )r+   rL   
lines_listtransformed_lines_listr*   transformedtransformed_linesr   s          r    r   zIPythonInputSplitter.push  s    . %%%% %%'
J
 	E4++\:!# 	;D..t4K&&--k:	;
 " $		*@ A-t9:KLL r"   c                      fd} j                   D ]  }|j                  |      }| ||      c S   j                  sS j                  j                  |      }| |d      S  j                  D ]  }|j                  |      }| ||      c S   j
                  j                  |      }|d _         |d      S d _         j                  D ]  }|j                  |      }| ||      c S  d _        |S )zPush a line of input code through the various transformers.

        Returns any output from the transformers, or None if a transformer
        is accumulating lines.

        Sets self.transformer_accumulating as a side effect.
        c                     d_         y r   )r   )dbgr+   s    r    _accumulatingz;IPythonInputSplitter._transform_line.<locals>._accumulating  s    ,0D)r"   zacc logical lineTzacc python lineF)r   r   r   r	   r   r   r   r   )r+   r*   r   transformers   `   r    r   z$IPythonInputSplitter._transform_line  s#   	
  88 	2K##D)D|$[11	2
 &&..33D9D|$%788#;; 6"''-<(556
 ))..t4<&*D# !233&+D#66 	2K##D)D|$[11	2 ).%r"   )TNNN)r-   r.   r/   r   r   r   r   r   r,   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r   s   @r    r   r     s    L J  % 
 KIMIM -D H H N N (>I	/ / /b*r"   r   )8r   r   r   DeprecationWarningr   r   r>   r   r   rA   typingr   IPython.core.inputtransformerr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   compilerI   	dedent_rer   comment_line_rer!   N_TOKENSr0   r:   r%   r5   rO   r{   r   	MULTILINEr   r   r   r   r   objectr   r   r3   r"   r    <module>r     sD  $  l
   	 	 
   
- 
- 
-Y Y Y BJJsxx !  	 

-. "**Y', %% !**Q.   2:z&  RZZ=  bjj!12<<@ 7."$	z F z zn= nr"   