Format: application/x-python-code
⬇ Download File (19.8 KB)
   1  o
   2  
   3  ��h�q�@s�dZddlmZddlmZede�ddlZddlZddlZddl	Z	ddl
   4  Z
   5  ddlZddlZddlm
   6  Z
   7  mZmZmZmZddlmZddlmZmZmZmZmZmZmZmZmZmZdd	lm Z dd
   8  lm!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*er�ddl+m,Z,e	�-d�.gd
   9  ���Z/e	�-d�Z0e	�-d�Z1dd�Z2ej3Z4ej3dZ5Gdd�d�Z6Gdd�d�Z7dd�Z8d+dd�Z9dd�Z:e	�-de	j;�Z<e	�-d e	j;�Z=d!d"�Z>d#d$�Z?d%d&�Z@Gd'd(�d(eA�ZBGd)d*�d*eB�ZCdS),a6DEPRECATED: Input handling and transformation machinery.
  10  
  11  This module was deprecated in IPython 7.0, in favour of inputtransformer2.
  12  
  13  The first class in this module, :class:`InputSplitter`, is designed to tell when
  14  input from a line-oriented frontend is complete and should be executed, and when
  15  the user should be prompted for another line of code instead. The name 'input
  16  splitter' is largely for historical reasons.
  17  
  18  A companion, :class:`IPythonInputSplitter`, provides the same functionality but
  19  with full support for the extended IPython syntax (magics, system calls, etc).
  20  The code to actually do these transformations is in :mod:`IPython.core.inputtransformer`.
  21  :class:`IPythonInputSplitter` feeds the raw code to the transformers in order
  22  and stores the results.
  23  
  24  For more details, see the class docstrings below.
  25  �)�annotations)�warnzeIPython.core.inputsplitter is deprecated since IPython 7 in favor of `IPython.core.inputtransformer2`N)�List�Tuple�Union�Optional�
  26  TYPE_CHECKING)�CodeType)
  27  �leading_indent�classic_prompt�
  28  ipy_prompt�	cellmagic�assemble_logical_lines�help_end�escaped_commands�assign_from_magic�assign_from_system�assemble_python_lines)�	tokenutil)
  29  �	ESC_SHELL�
  30  ESC_SH_CAP�ESC_HELP�	ESC_HELP2�	ESC_MAGIC�
  31  ESC_MAGIC2�	ESC_QUOTE�
  32  ESC_QUOTE2�	ESC_PAREN�
  33  ESC_SEQUENCES)�Self�|)z^\s+raise(\s.*)?$z^\s+raise\([^\)]*\).*$z^\s+return(\s.*)?$z^\s+return\([^\)]*\).*$z^\s+pass\s*$z
  34  ^\s+break\s*$z^\s+continue\s*$z^([ \t\r\f\v]+)z^\s*\#cCs*tjddtd�t�|�}|r|��SdS)aReturn the number of initial spaces in a string.
  35  
  36      Note that tabs are counted as a single space.  For now, we do *not* support
  37      mixing of tabs and spaces in the user's input.
  38  
  39      Parameters
  40      ----------
  41      s : string
  42  
  43      Returns
  44      -------
  45      n : int
  46      z�`num_ini_spaces` is Pending Deprecation since IPython 8.17.It is considered for removal in in future version. Please open an issue if you believe it should be kept.�)�
  47  stacklevel�categoryr)�warningsr�PendingDeprecationWarning�
  48  ini_spaces_re�match�end)�sZ
  49  ini_spaces�r*�Pc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/core/inputsplitter.py�num_ini_spacesUs�
  50  r,�c@�eZdZeZZdd�ZdS)�IncompleteStringcCs||_||_||_||_dS�N�r)�startr(�line)�selfr)r2r(r3r*r*r+�__init__xs
  51  zIncompleteString.__init__N)�__name__�
  52  __module__�__qualname__�INCOMPLETE_STRING�type�
  53  exact_typer5r*r*r*r+r/v�r/c@r.)�InMultilineStatementcCsd|_||_|_||_dS�N�r1)r4�posr3r*r*r+r5�s
  54  zInMultilineStatement.__init__N)r6r7r8�IN_MULTILINE_STATEMENTr:r;r5r*r*r*r+r=~r<r=c	
  55  cs�t�|�j}t�tjdddd�}zt�|�D]}|VqWd	Stjy�}z[|j	dd�}t
  56  |�t
  57  |d�f}d|jdvrd|j\}}}||d|d	�d�
  58  ||d	��}t||||d�Vnd
  59  |jdvrtt||d�Vn�WYd	}~d	SWYd	}~d	Sd	}~ww)aIterate over tokens from a possibly-incomplete string of code.
  60  
  61      This adds two special token types: INCOMPLETE_STRING and
  62      IN_MULTILINE_STATEMENT. These can only occur as the last token yielded, and
  63      represent the two main ways for code to be incomplete.
  64      r?)r-rT)�keepends�����zmulti-line stringrr-Nzmulti-line statement)�io�StringIO�readline�tokenize�	TokenInfo�NEWLINErZgenerate_tokens_catch_errors�
  65  TokenError�
  66  splitlines�len�argsr(�joinr/r=)	r)rF�token�e�linesr(�l�cr2r*r*r+�partial_tokens�s*��&���rT�return�intc	stt|��}|djtjkr|��|sdS|djtjtjtjtj	hvr8|��|djtjtjtjtj	hvs%|djt
  67  krV|djtjhvrV|�d�|djtjhvsH|djtkr_dSdg��fdd�}t
  68  |�}|D]/}|jtjtjhvr�||jd�qn|jtjkr�z|t|�jd�Wqnty�Ynwqn���}|djt
  69  kr�|djtjtjtjhvr�|dS|S|djtjkr�|dS|�rd}t|�D]\}}|jtjkr�|d}q�||d�}d	d
  70  �|D�}	|	�r|	ddv�rt��D]}
  71  |
  72  |k�r|
  73  Sq�|S)z:Find the number of spaces for the next line of indentationrCr�����cs|�dkr
  74  ��|�dSdS�NrC)�append)�n�Zprev_indentsr*r+�_add_indent�s�z%find_next_indent.<locals>._add_indentr-�NcSsg|]}|jtjkr|j�qSr*)r:rG�NAME�string)�.0�tr*r*r+�
  75  <listcomp>�sz$find_next_indent.<locals>.<listcomp>>�continue�break�passrU�raise)�listrTr:rG�	ENDMARKER�pop�DEDENTrI�COMMENT�
  76  ERRORTOKENrA�NLr9�iter�INDENTr(�nextr2�
  77  StopIterationr;�LPAR�LSQB�LBRACE�COLON�	enumerate�reversed)�code�tokensr\Ztokiter�tokZlast_indentZlast_line_starts�iZlast_line_tokens�names�indentr*r[r+�find_next_indent�st���
  78  
  79  ����
  80  �r~cCs$|sdS|��d}|dkp|��S)z�Determine if the input source ends in a blank.
  81  
  82      A blank is either a newline or a line consisting of whitespace.
  83  
  84      Parameters
  85      ----------
  86      src : string
  87          A single or multiline string.
  88      FrCr?)rK�isspace)�src�llr*r*r+�
  89  last_blank�s
  90  r�z\n\s*\n\s*$z
  91  .+\n\s*\n\s+$cCs@|sdSd�dg|��dd��}tt�|��ptt�|��S)z�Determine if the input source ends in two blanks.
  92  
  93      A blank is either a newline or a line consisting of whitespace.
  94  
  95      Parameters
  96      ----------
  97      src : string
  98          A single or multiline string.
  99      F�
 100  z###
 101  rWN)rNrK�bool�last_two_blanks_rer'�last_two_blanks_re2)r�Znew_srcr*r*r+�last_two_blanks�s
 102  
 103  �r�cCst�dd|�S)aRemove all comments from input source.
 104  
 105      Note: comments are NOT recognized inside of strings!
 106  
 107      Parameters
 108      ----------
 109      src : string
 110          A single or multiline input string.
 111  
 112      Returns
 113      -------
 114      String with all Python comments removed.
 115      z#.*r?)�re�sub)r�r*r*r+�remove_commentssr�cCsttjdd�}|dur
 116  d}|S)zcReturn the default standard input encoding.
 117  
 118      If sys.stdin has no encoding, 'ascii' is returned.�encodingN�ascii)�getattr�sys�stdin)r�r*r*r+�get_input_encoding#sr�c@s�eZdZUdZdZded<dZdZded<dZd	ed
 119  <ded<d
 120  ed<dZ	ded<dZ
 121  ded<d*dd�Zdd�Zdd�Z
 122  dd�Zd+dd �Zd!d"�Zd,d$d%�Zee�Zd-d&d'�Zd(d)�ZdS).�
 123  InputSplittera$An object that can accumulate lines of Python source before execution.
 124  
 125      This object is designed to be fed python source line-by-line, using
 126      :meth:`push`. It will return on each push whether the currently pushed
 127      code could be executed already. In addition, it provides a method called
 128      :meth:`push_accepts_more` that can be used to query whether more input
 129      can be pushed into a single interactive block.
 130  
 131      This is a simple example of how an interactive terminal-based client can use
 132      this tool::
 133  
 134          isp = InputSplitter()
 135          while isp.push_accepts_more():
 136              indent = ' '*isp.indent_spaces
 137              prompt = '>>> ' + indent
 138              line = indent + raw_input(prompt)
 139              isp.push(line)
 140          print('Input source was:\n', isp.source_reset())
 141      �NNz)Union[Tuple[None, None], Tuple[str, int]]�_indent_spaces_cacher?�str�sourceNzOptional[CodeType]rx�	List[str]�_bufferzcodeop.CommandCompiler�_compilezOptional[bool]�_is_completeFr��_is_invalidrU�NonecCsg|_t��|_t�|_dS)z$Create a new InputSplitter instance.N)r��codeopZCommandCompilerr�r�r��r4r*r*r+r5es
 142  zInputSplitter.__init__cCs*g|jdd�<d|_d|_d|_d|_dS�z,Reset the input buffer and associated state.Nr?F)r�r�rxr�r�r�r*r*r+�resetks
 143  
 144  zInputSplitter.resetcC�|j}|��|S)z:Return the input source and perform a full reset.
 145          )r�r��r4�outr*r*r+�source_resets�zInputSplitter.source_resetcCs�|��z7z|�|�WntyYW|��dSw|jr&W|��dS|��r5d|��fW|��SW|��dS|��w)akReturn whether a block of code is ready to execute, or should be continued
 146  
 147          This is a non-stateful API, and will reset the state of this InputSplitter.
 148  
 149          Parameters
 150          ----------
 151          source : string
 152              Python input code, which can be multiline.
 153  
 154          Returns
 155          -------
 156          status : str
 157              One of 'complete', 'incomplete', or 'invalid' if source is not a
 158              prefix of valid code.
 159          indent_spaces : int or None
 160              The number of spaces by which to indent the next line of code. If
 161              status is not 'incomplete', this is None.
 162          )�invalidN�
 163  incomplete)ZcompleteN)r��push�SyntaxErrorr��push_accepts_more�get_indent_spaces)r4r�r*r*r+�check_completezs	��
 164  �zInputSplitter.check_completerQc
 165  Cs�t|t�sJ�|�|�|j}d\|_|_d|_|�d�rdSz$t�	��t�
 166  dt�|j|dd�|_Wd�n1s=wYWnt
 167  tttttfyZd|_d|_Y|jSw|jdu|_|jS)	aPush one or more lines of input.
 168  
 169          This stores the given lines and returns a status code indicating
 170          whether the code forms a complete Python block or not.
 171  
 172          Any exceptions generated in compilation are swallowed, but if an
 173          exception was produced, the method returns True.
 174  
 175          Parameters
 176          ----------
 177          lines : string
 178              One or more lines of Python input.
 179  
 180          Returns
 181          -------
 182          is_complete : boolean
 183              True if the current input source (the result of the current input
 184              plus prior inputs) forms a complete Python execution block.  Note that
 185              this value is also stored as a private attribute (``_is_complete``), so it
 186              can be queried at any time.
 187          r�Fz\
 188  �error�exec)�symbolNT)�
 189  isinstancer��_storer�rxr�r��endswithr$�catch_warnings�simplefilter�
 190  SyntaxWarningr�r��
 191  OverflowError�
 192  ValueError�	TypeError�MemoryError)r4rQr�r*r*r+r��s,
 193  
 194  
 195  ��
 196  
 197  ��zInputSplitter.pushcCs�|jsdS|j��d}|r|��rdS|��dkrLt|j���dkr%dSzt�d�|j	��}Wn
 198  t
 199  y:YdSwt|j�dkrLt|jdd�sLdSdS)a�Return whether a block of interactive input can accept more input.
 200  
 201          This method is meant to be used by line-oriented frontends, who need to
 202          guess whether a block is complete or not based solely on prior and
 203          current input lines.  The InputSplitter considers it has a complete
 204          interactive block and will not accept more input when either:
 205  
 206          * A SyntaxError is raised
 207  
 208          * The code is complete and consists of a single line or a single
 209            non-compound statement
 210  
 211          * The code is complete and has a blank line at the end
 212  
 213          If the current input produces a syntax error, this method immediately
 214          returns False but does *not* raise the syntax error exception, as
 215          typically clients will want to send invalid syntax to an execution
 216          backend which might convert the invalid syntax into valid Python via
 217          one of the dynamic IPython mechanisms.
 218          TrCFrr-r?�body)
 219  r�r�rKrr�rL�ast�parserNr��	Exceptionr��hasattr)r4�	last_lineZcode_astr*r*r+r��s$��zInputSplitter.push_accepts_morerVcCsF|j\}}||jkr|dusJ�|St|jdd��}|j|f|_|SrX)r�r�r~)r4Z	sourceforrZr*r*r+r�
 220  s
 221  
 222  zInputSplitter.get_indent_spacescCsH|dur|j}|�d�r|�|�n|�|d�t|||�|��dS)z�Store one or more lines of input.
 223  
 224          If input lines are not newline-terminated, a newline is automatically
 225          appended.Nr�)r�r�rY�setattr�_set_source)r4rQ�buffer�storer*r*r+r�s
 226  zInputSplitter._storecCs
 227  d�|�Sr>)rN)r4r�r*r*r+r�,s
 228  zInputSplitter._set_source)rUr��rQr�rUr��rUrV)Nr�)r6r7r8�__doc__r��__annotations__r�r�rxr�r�r5r�r�r�r�r�r��propertyZ
 229  indent_spacesr�r�r*r*r*r+r�2s(
 230  
 231  
 232  $8
 233  7
 234  r�cs�eZdZUdZdZdZdZded<		d$�fdd	�	Ze	d
 235  d��Z
 236  e	dd
 237  ��Z�fdd�Zd%dd�Z
 238  dd�Zdd�Z�fdd�Zdd�Zd&�fd d!�Zd"d#�Z�ZS)'�IPythonInputSplitterzBAn input splitter that recognizes all of IPython's special syntax.r?Fr��_buffer_rawTNcs�tt|���g|_d|_|dur||_n
 239  t�t�t�t	|d�g|_t
 240  �|_
 241  |dur.||_nt�t
 242  �t�t�g|_t�|_|durF||_dSg|_dS)NT)Zend_on_blank_line)�superr�r5r��	_validate�physical_line_transformsr
 243  rrr
 244  r�logical_line_transformsrrrrr�python_line_transforms)r4Zline_input_checkerr�r�r���	__class__r*r+r5Cs,��
 245  
 246  zIPythonInputSplitter.__init__cCs"|j|jg|j|jg|jS)z!Quick access to all transformers.)r�rr�rr�r�r*r*r+�
 247  transformses����zIPythonInputSplitter.transformscCs8|jdd�}|js||jg|j7}||jg|jS)zTTransformers, excluding logical line transformers if we're in a
 248          Python line.N)r��within_python_linerr�rr��r4rar*r*r+�transforms_in_uselsz&IPythonInputSplitter.transforms_in_usec	s\tt|���g|jdd�<d|_d|_d|_|jD]}z|��Wqty+YqwdSr�)	r�r�r�r��
 249  source_raw�transformer_accumulatingr�r�r�r�r�r*r+r�us
 250  ��zIPythonInputSplitter.resetr4rcCsHddd�}g}|jD]}|||�}q
 251  t|�}|r"|�d�|��dSdS)N�outsr�cssP�|D]}|��D]}|�|�}|dur|Vq	q|��}|dur&|VdSdS)a)yield transformed lines
 252  
 253              always strings, never None
 254  
 255              transform: the current transform
 256              outs: an iterable of previously transformed inputs.
 257                   Each may be multiline, which will be passed
 258                   one line at a time to transform.
 259              N)rKr�r�)Z	transformr�r�r3�tmpr*r*r+�_flush�s�
 260  
 261  ��
 262  �z7IPythonInputSplitter.flush_transformers.<locals>._flushr�)r�r�)r�rgr�rN)r4r�r�rar*r*r+�flush_transformers�s
 263  
 264  �z'IPythonInputSplitter.flush_transformerscCr�)z8Return raw input only and perform a full reset.
 265          )r�r�r�r*r*r+�	raw_reset�r�zIPythonInputSplitter.raw_resetcCs$z|��|jW|��S|��wr0)r�r�r�r�r*r*r+r��sz!IPythonInputSplitter.source_resetcs|jrdStt|���S�NT)r�r�r�r�r�r�r*r+r��sz&IPythonInputSplitter.push_accepts_morecCs6|��z|�|�|��|jW|��S|��w)z/Process and translate a cell of input.
 266          )r�r�r�r�)r4�cellr*r*r+�transform_cell�s
 267  z#IPythonInputSplitter.transform_cellrQr�rUr�cs|t|t�sJ�|��}|sdg}|�||jd�g}|D]}|�|�}|dur,|�|�q|r<d�|�}tt	|��
 268  |�SdS)aJPush one or more lines of IPython input.
 269  
 270          This stores the given lines and returns a status code indicating
 271          whether the code forms a complete Python block or not, after processing
 272          all input lines for special IPython syntax.
 273  
 274          Any exceptions generated in compilation are swallowed, but if an
 275          exception was produced, the method returns True.
 276  
 277          Parameters
 278          ----------
 279          lines : string
 280              One or more lines of Python input.
 281  
 282          Returns
 283          -------
 284          is_complete : boolean
 285              True if the current input source (the result of the current input
 286              plus prior inputs) forms a complete Python execution block.  Note that
 287              this value is also stored as a private attribute (_is_complete), so it
 288              can be queried at any time.
 289          r?r�Nr�F)r�r�rKr�r��_transform_linerYrNr�r�r�)r4rQZ
 290  lines_listZtransformed_lines_listr3ZtransformedZtransformed_linesr�r*r+r��s
 291  
 292  �
 293  zIPythonInputSplitter.pushcs��fdd�}�jD]}|�|�}|dur||�Sq	�jsA�j�|�}|dur,|d�S�jD]}|�|�}|dur@||�Sq/�j�|�}|durRd�_|d�Sd�_�jD]}|�|�}|duri||�SqXd�_|S)z�Push a line of input code through the various transformers.
 294  
 295          Returns any output from the transformers, or None if a transformer
 296          is accumulating lines.
 297  
 298          Sets self.transformer_accumulating as a side effect.
 299          cs
 300  d�_dSr�)r�)Zdbgr�r*r+�
 301  _accumulating�sz;IPythonInputSplitter._transform_line.<locals>._accumulatingNzacc logical lineTzacc python lineF)r�r�r�rr�rr�r�)r4r3r�Ztransformerr*r�r+r��s6
 302  
 303  �
 304  
 305  �
 306  
 307  �z$IPythonInputSplitter._transform_line)TNNN)r4rr�)r6r7r8r�r�r�r�r�r5r�r�r�r�r�r�r�r�r�r�r��
 308  __classcell__r*r*r�r+r�0s*
 309  �"
 310  
 311  
 312  1r�r�)Dr��
 313  __future__rr$r�DeprecationWarningr�r�rDr�r�rG�typingrrrrr�typesr	ZIPython.core.inputtransformerr
 314  rrr
 315  rrrrrrZ
 316  IPython.utilsrrrrrrrrrrrZtyping_extensionsr�compilerNZ	dedent_rer&Zcomment_line_rer,�N_TOKENSr9rAr/r=rTr~r��	MULTILINEr�r�r�r�r��objectr�r�r*r*r*r+�<module>sN�00
 317  	
 318  
 319  
 320  J