Format: application/x-python-code
⬇ Download File (30.9 KB)
   1  o
   2  
   3  ߛh��@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
   4  mZddlm
   5  Z
   6  ddlmZddlmZddlmZddlmZdd	lmZdd
   7  lmZddlmZddlmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+er�dd
   8  l,m-Z-m.Z.ddl/m0Z0ej1ej2fZ3edd�Z4Gdd�dej5�Z6Gdd�dej7�Z7Gdd�de7�Z8dVdd�Z9dd�Z:dZ;Gdd�de<�Z=e)d�Z>dd�Z?Gd d!�d!e@�ZAGd"d#�d#e@�ZBGd$d%�d%ejC�ZDeEd&d'�ejFD��ZGd(d)�ZHd*ZId+d,�ZJGd-d.�d.e@�ZKd/d0�ZLd1d2�ZMd3d4�ZNd5d6�ZOd7d8�ZPd9d:�ZQd;d<�ZRd=d>�ZSd?d@�ZTe�ZUe4dAdB��ZVdCdD�ZWdEdF�ZXdGdH�ZYdIdJ�ZZdKdL�Z[dMdN�Z\dOdP�Z]dQdR�Z^ej_dSk�r`dTdUl`maZbdSeKZbdS)Wa+
   9  MIT License
  10  
  11  Copyright (c) 2021 Alex Hall
  12  
  13  Permission is hereby granted, free of charge, to any person obtaining a copy
  14  of this software and associated documentation files (the "Software"), to deal
  15  in the Software without restriction, including without limitation the rights
  16  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17  copies of the Software, and to permit persons to whom the Software is
  18  furnished to do so, subject to the following conditions:
  19  
  20  The above copyright notice and this permission notice shall be included in all
  21  copies or substantial portions of the Software.
  22  
  23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29  SOFTWARE.
  30  �N)�defaultdict)�deepcopy)�	lru_cache)�islice)�zip_longest)�
  31  attrgetter)�Path)�RLock)�detect_encoding)�
  32  TYPE_CHECKING�Any�Callable�Dict�Iterable�Iterator�List�Optional�Sequence�Set�Sized�Tuple�Type�TypeVar�Union�cast)�	ASTTokens�ASTText)�ASTTextBase)�maxsizec@�eZdZdZdS)�EnhancedASTN)�__name__�
  33  __module__�__qualname__�parent�r%r%�Ic:\Users\shubh\Videos\agent\nova\Lib\site-packages\executing/executing.pyr 9�r c@r)�InstructionN)r!r"r#�linenor%r%r%r&r(=r'r(c@r)�EnhancedInstructionN)r!r"r#�_copiedr%r%r%r&r*Cr'r*�cCs|stt|���dS)z�
  34      Like an assert statement, but unaffected by -O
  35      :param condition: value that is expected to be truthy
  36      :type message: Any
  37      N)�AssertionError�str)�	condition�messager%r%r&�assert_Hs�r1ccsD�|j}t�|�D]}tt|�}|jp|}t|�||_|Vq	dS�N)�co_firstlineno�dis�get_instructionsrr*�starts_liner1r))�cor)�instr%r%r&r5Ss�
  38  
  39  �r5cs eZdZgf�fdd�	Z�ZS)�NotOneValueFoundcs||_tt|��|�dSr2)�values�superr9�__init__)�self�msgr:��	__class__r%r&r<bszNotOneValueFound.__init__)r!r"r#r<�
  40  __classcell__r%r%r?r&r9asr9�TcCsrt|t�rt|�dkrtdt|���t|�dStt|d��}t|�dkr*td��t|�dkr5td|��|dS)N�zExpected one value, found %sr�zExpected one value, found 0z!Expected one value, found several)�
  41  isinstancer�lenr9�list�tupler)�it�lstr%r%r&�onlyjs
  42  
  43  rKc@s�eZdZdZdd�Zed dd��Ze		d!dd	��Zed
  44  d��Zedd
  45  ��Z	edd��Z
  46  edd��Zedd��Z
  47  dd�Zdd�Zdd�Zedd��Zedd��Zdd�ZdS)"�Sourcea�
  48      The source code of a single file and associated metadata.
  49  
  50      The main method of interest is the classmethod `executing(frame)`.
  51  
  52      If you want an instance of this class, don't construct it.
  53      Ideally use the classmethod `for_frame(frame)`.
  54      If you don't have a frame, use `for_filename(filename [, module_globals])`.
  55      These methods cache instances by filename, so at most one instance exists per filename.
  56  
  57      Attributes:
  58          - filename
  59          - text
  60          - lines
  61          - tree: AST parsed from text, or None if text is not valid Python
  62              All nodes in the tree have an extra `parent` attribute
  63  
  64      Other methods of interest:
  65          - statements_at_line
  66          - asttokens
  67          - code_qualname
  68      c	Cs�||_d�|�|_dd�|D�|_tt�|_d|_i|_d|_	d|_
  69  ztj|j|d�|_Wnt
  70  tfy9YdSwt�|j�D]"}t�|�D]}tt|�tt|�_qGt|�D]
  71  }|j|�|�qWq@t�}|�|j�|j|_dS)zG
  72          Don't call this constructor, see the class docstring.
  73          r,cSsg|]}|�d��qS)z
  74  )�rstrip)�.0�liner%r%r&�
  75  <listcomp>�sz#Source.__init__.<locals>.<listcomp>N��filename)rR�join�text�linesrrG�_nodes_by_line�tree�
  76  _qualnames�
  77  _asttokens�_asttext�ast�parse�SyntaxError�
  78  ValueError�walk�iter_child_nodesrr r$�node_linenos�append�QualnameVisitor�visit�	qualnames)r=rRrU�node�childr)�visitorr%r%r&r<�s,
  79  ��zSource.__init__TcCs|�|jj|jp	i|�S)zb
  80          Returns the `Source` object corresponding to the file the frame is executing in.
  81          )�for_filename�f_code�co_filename�	f_globals)�cls�frame�	use_cacher%r%r&�	for_frame�szSource.for_frameNcsht�t�r	t�����fdd�}tj���}t���|�}|dur,|s,|tj�<|�}|��t|��S)Ncst�tt����Sr2)�	linecache�getlinesrr.r%�rR�module_globalsr%r&�	get_lines��z&Source.for_filename.<locals>.get_lines)	rErr.rq�cache�get�
  82  checkcache�_for_filename_and_linesrH)rmrRrtroru�entryrUr%rsr&ri�s
  83  
  84  
  85  zSource.for_filenamecCsH|�di�}z|||fWStyYnw|||�}|||f<|S)NZ__source_cache_with_lines)�_class_local�KeyError)rmrRrUZsource_cache�resultr%r%r&rz�s�zSource._for_filename_and_linescCst�|jj|j�dSr2)rq�	lazycacherjrkrl)rmrnr%r%r&r�szSource.lazycachecCs(t|tj�r|}|j}|j}|j}n|}|j}|j}|j}|t	|�|f}|�
  86  di�}|�|�}	|	s�d}
  87  }}|�|�}
  88  |
  89  j
  90  }|r�z4|
  91  �|�}|ret|�rWt||||
  92  �\}}
  93  nt|||||
  94  �}|j}
  95  |j}|
  96  rtt|
  97  �h}t||k�|}Wnty�tr�Ynw|
  98  |
  99  ||f||<}	t|g|	�R�S)z�
 100          Returns an `Executing` object representing the operation
 101          currently executing in the given frame or traceback object.
 102          Z__executing_cacheN)rE�types�
 103  TracebackType�tb_frame�	tb_lineno�tb_lasti�f_lineno�f_lastirj�idr|rxrprW�statements_at_line�is_ipython_cell_code�find_node_ipython�
 104  NodeFinderr~�	decorator�statement_containing_noder1�	Exception�TESTING�	Executing)rmZframe_or_tb�tbrnr)�lasti�code�keyZexecuting_cache�argsrf�stmtsr��sourcerW�node_finderZ	new_stmtsr%r%r&�	executing�sJ
 105  
 106  
 107  
 108  ���zSource.executingcCs|j�||�}t|||�|S)z�
 109          Returns an attribute directly associated with this class
 110          (as opposed to subclasses), setting default if necessary
 111          )�__dict__rx�setattr)rm�name�defaultr~r%r%r&r|!szSource._class_localcCsdd�|j|D�S)az
 112          Returns the statement nodes overlapping the given line.
 113  
 114          Returns at most one statement unless semicolons are present.
 115  
 116          If the `text` attribute is not valid python, meaning
 117          `tree` is None, returns an empty set.
 118  
 119          Otherwise, `Source.for_frame(frame).statements_at_line(frame.f_lineno)`
 120          should return at least one statement.
 121          cSsh|]}t|��qSr%)r�)rNrfr%r%r&�	<setcomp><s��z,Source.statements_at_line.<locals>.<setcomp>)rV)r=r)r%r%r&r�-s�zSource.statements_at_linecCs2ddlm}|jdur||j|j|jd�|_|jS)z�
 122          Returns an ASTText object for getting the source of specific AST nodes.
 123  
 124          See http://asttokens.readthedocs.io/en/latest/api-index.html
 125          r)rN�rWrR)�	asttokensrrZrTrWrR)r=rr%r%r&�asttextBs
 126  zSource.asttextcCsLddl}|jdur#t|d�r|��j|_|jS|j|j|j|jd�|_|jS)z�
 127          Returns an ASTTokens object for getting the source of specific AST nodes.
 128  
 129          See http://asttokens.readthedocs.io/en/latest/api-index.html
 130          rNrr�)r�rY�hasattrr�rrTrWrR�r=r�r%r%r&r�Ps
 131  
 132  �zSource.asttokenscCs"ddl}t|d�r
 133  |��S|��S)Nrr)r�r�r�r�r%r%r&�
 134  _asttext_base`s
 135  zSource._asttext_basecCs"t|t�rt�|�}|�|�S|Sr2)rE�bytesrLr
 136  �decode)r��encodingr%r%r&�
 137  decode_sourceis
 138  
 139  
 140  zSource.decode_sourcecCstt�|�j�dS)Nr)r
 141  �io�BytesIO�readline)r�r%r%r&r
 142  rszSource.detect_encodingcCs(t|j|jk�|j�|j|jf|j�S)a
 143          Imitates the __qualname__ attribute of functions for code objects.
 144          Given:
 145  
 146              - A function `func`
 147              - A frame `frame` for an execution of `func`, meaning:
 148                  `frame.f_code is func.__code__`
 149  
 150          `Source.for_frame(frame).code_qualname(frame.f_code)`
 151          will be equal to `func.__qualname__`*. Works for Python 2 as well,
 152          where of course no `__qualname__` attribute exists.
 153  
 154          Falls back to `code.co_name` if there is no appropriate qualname.
 155  
 156          Based on https://github.com/wbolster/qualname
 157  
 158          (* unless `func` is a lambda
 159          nested inside another lambda on the same line, in which case
 160          the outer lambda's qualname will be returned for the codes
 161          of both lambdas)
 162          )r1rkrRrXrx�co_namer3�r=r�r%r%r&�
 163  code_qualnamewszSource.code_qualname)T�NT)r!r"r#�__doc__r<�classmethodrprirzrr�r|rwr�r�r�r��staticmethodr�r
 164  r�r%r%r%r&rLzs6�
 165  
 166  
 167  9
 168  
 169  	
 170  
 171  rLc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
 172  S)r�a�
 173      Information about the operation a frame is currently executing.
 174  
 175      Generally you will just want `node`, which is the AST node being executed,
 176      or None if it's unknown.
 177  
 178      If a decorator is currently being called, then:
 179          - `node` is a function or class definition
 180          - `decorator` is the expression in `node.decorator_list` being called
 181          - `statements == {node}`
 182      cCs"||_||_||_||_||_dSr2)rnr�rfZ
 183  statementsr�)r=rnr�rfr�r�r%r%r&r<�s
 184  
 185  zExecuting.__init__cCs|j�|jj�Sr2)r�r�rnrj�r=r%r%r&r���zExecuting.code_qualnamecC�|j���|j�Sr2)r�r�Zget_textrfr�r%r%r&rT�rvzExecuting.textcCr�r2)r�r�Zget_text_rangerfr�r%r%r&�
 186  text_range�rvzExecuting.text_rangeN)r!r"r#r�r<r�rTr�r%r%r%r&r��sr�csDeZdZ�fdd�Zddd�Zddd�ZeZdd	�Zd
 187  d�Z�Z	S)
 188  rccstt|���g|_i|_dSr2)r;rcr<�stackrer�r?r%r&r<�s
 189  zQualnameVisitor.__init__NcCsT|p|j}|j�|�t|dd�r|jdj}n|j}|j�||fd�|j��dS)N�decorator_listr%r�.)	r�r�rb�getattrr�r)re�
 190  setdefaultrS)r=rfr�r)r%r%r&�add_qualname�s
 191  zQualnameVisitor.add_qualnamecCs�t|tjtjtjf�sJ|��|�||�|j�d�g}t|tj�r(|jg}n|j}|D]}|�	|�q-|j�
 192  �|j�
 193  �t�|�D]*\}}|dkrMqDt|tj�rY|�	|�qDt|t
 194  �rn|D]
 195  }t|tj�rm|�	|�q`qDdS)Nz<locals>�body)rEr[�FunctionDef�AsyncFunctionDef�Lambdar�r�rbr�rd�pop�iter_fields�ASTrG)r=rfr��childrenrg�fieldZ
 196  grandchildr%r%r&�visit_FunctionDef�s,
 197  
 198  
 199  
 200  
 201  ��z!QualnameVisitor.visit_FunctionDefcCs t|tj�sJ�|�|d�dS)Nz<lambda>)rEr[r�r��r=rfr%r%r&�visit_Lambda�szQualnameVisitor.visit_LambdacCs2t|tj�sJ�|�|�|�|�|j��dSr2)rEr[�ClassDefr��
 202  generic_visitr�r�r�r%r%r&�visit_ClassDef�s
 203  
 204  zQualnameVisitor.visit_ClassDefr2)
 205  r!r"r#r<r�r��visit_AsyncFunctionDefr�r�rAr%r%r?r&rc�s
 206  
 207  
 208  rcccs�|]	}tt|�jVqdSr2)r��
 209  __future__�
 210  compiler_flag)rN�fnamer%r%r&�	<genexpr>�s�
 211  �r�cCst||jdt|j@dd�S)N�execT)�flags�dont_inherit)�compilerk�future_flags�co_flags)r�Z
 212  matching_coder%r%r&�compile_similar_to�s�r�Z8io8urthglkjdghvljusketgIYRFYUVGHFRTBGVHKGF78678957647698cCstdd�t|�D��S)Ncss2�|]}|jdkot|jt�o|j�d�VqdS)�
 213  LOAD_CONSTz@pyN)�opnamerE�argvalr.�
 214  startswith)rN�bcr%r%r&r�s
 215  � �
 216  �z)is_rewritten_by_pytest.<locals>.<genexpr>)�anyr5�r�r%r%r&�is_rewritten_by_pytests�r�c@sPeZdZdZdd�Zdd�Zdd�Zdd	�Zd
 217  d�Zdd
 218  �Z	dd�Z
 219  dd�ZdS)�SentinelNodeFinderNc
 220  s�t|�||_||_|j|_}t|�|_|jr tt|��|_	nt�|_	d|_
 221  |�|�|_��j
 222  }dd��td��td��|�d�rGtj�n�|�d�rStj�tj�n�|�d�r�tj�ttjtjttdd�tjtjtjtjtjtjtjtj tj!tj"d�
 223  |��fd	d��nm|�d
 224  �r�tj#�ttj$tj%tj&tj'd�|��fdd��nP|d
 225  vr�tj(�tj��fdd��n?|dvr�tj)�tj��fdd��n.|dvr�tj*�dd��n"|�d�r�tj+�tj�n|�d�r�tj+�tj(��fdd��nt,|��t-�K���fdd�|D�}�tj+k�rt.|�|_/	Wd�dSt0|�1|��}	|	�s*�tjk�r*|�2|�n
 226  t.|	�|_/Wd�dSWd�dS1�sCwYdS)NcSsdSr�r%��er%r%r&�<lambda>!sz-SentinelNodeFinder.__init__.<locals>.<lambda>ZCALL_)�
 227  BINARY_SUBSCRzSLICE+ZBINARY_�MatMultr%)
 228  �BINARY_POWER�BINARY_MULTIPLY�BINARY_MATRIX_MULTIPLY�BINARY_FLOOR_DIVIDE�BINARY_TRUE_DIVIDE�
 229  BINARY_MODULO�
 230  BINARY_ADD�BINARY_SUBTRACT�
 231  BINARY_LSHIFT�
 232  BINARY_RSHIFT�
 233  BINARY_AND�
 234  BINARY_XOR�	BINARY_ORc�t|j��Sr2�rE�opr���op_typer%r&r�;�ZUNARY_)�UNARY_POSITIVE�UNARY_NEGATIVE�	UNARY_NOT�UNARY_INVERTcr�r2r�r�r�r%r&r�Dr�)�	LOAD_ATTR�LOAD_METHOD�
 235  LOOKUP_METHODc�t|j�j�Sr2��attr_names_match�attrr�r���instructionr%r&r�H�)�	LOAD_NAME�LOAD_GLOBAL�	LOAD_FAST�
 236  LOAD_DEREF�LOAD_CLASSDEREFcs|j�jkSr2)r�r�r�rr%r&r�Lr�)�
 237  COMPARE_OP�IS_OP�CONTAINS_OPcSst|j�dkS)NrC)rF�opsr�r%r%r&r�Or)ZSTORE_SLICE�STORE_SUBSCR�
 238  STORE_ATTRcrr2rr�rr%r&r�VrcsVh|]'}t�|�D]}t|��r(tt|dd���r(�|�r(t|�|kr	tt|��q	qS)�ctxN)r[r_rEr�r�rr )rN�stmtrf)r�extra_filter�typr%r&r�[s�����z.SentinelNodeFinder.__init__.<locals>.<setcomp>)3r1rnrWrjr�r��	is_pytest�	frozenset�assert_linenos�ignore_linenosr��get_actual_current_instructionrr��typer�r[�Call�	Subscript�Load�BinOp�dict�Pow�Multr��FloorDiv�Div�Mod�Add�Sub�LShift�RShift�BitAnd�BitXor�BitOr�UnaryOp�UAdd�USub�Not�Invert�	Attribute�Name�Compare�Store�RuntimeError�lockrKr~rG�matching_nodes�find_decorator)
 239  r=rnr�rWr�r�r�Zop_name�exprsZmatchingr%)rrrr�rr&r<s�
 240  
 241  
 242  
 243  
 244  ��
 245  ��
 246  
 247  
 248  �
 249  
 250  ��$�zSentinelNodeFinder.__init__c	s�t|�}tt|tjtf��|j}t|��fdd����j�D�}dd�t	|�D�d}t||dj
 251  �d��||t|�d|d�}tdd�|D�d	hk�|�
 252  �j�}|ddd�|}|�_|�_dS)
 253  Ncsg|]}|j�jjkr|�qSr%)r)rnr��rNr8r�r%r&rPxs
 254  �z5SentinelNodeFinder.find_decorator.<locals>.<listcomp>cSsg|]\}}|jdkr|�qS)�
 255  CALL_FUNCTION�r��rN�ir8r%r%r&rP}�
 256  
 257  ������rCZSTORE_cSsh|]}|j�qSr%r=r;r%r%r&r��sz4SentinelNodeFinder.find_decorator.<locals>.<setcomp>r<)rKr1rEr[r��function_node_typesr��clean_instructionsr��	enumerater�r�rF�indexrr�r~)	r=r�rZ
 258  decoratorsZline_instructionsZ last_decorator_instruction_indexZdecorator_instructionsZdecorator_indexr�r%r�r&r9rsB
 259  
 260  ����������
 261  z!SentinelNodeFinder.find_decoratorcs�fdd�t|�D�S)Ncs&g|]}|jdvr|j�jvr|�qS))�EXTENDED_ARG�NOP)r�r)rr;r�r%r&rP�s
 262  �z9SentinelNodeFinder.clean_instructions.<locals>.<listcomp>)r5r�r%r�r&rC�s
 263  �z%SentinelNodeFinder.clean_instructionscCs4|�|j�}tdd�|��D��sdd�|D�}|S)Ncss�|]}|jdkVqdS)�JUMP_IF_NOT_DEBUGNr=r;r%r%r&r��s
 264  ��
 265  �zESentinelNodeFinder.get_original_clean_instructions.<locals>.<genexpr>cSsg|]	}|jdkr|�qS)rHr=r;r%r%r&rP�s
 266  
 267  �zFSentinelNodeFinder.get_original_clean_instructions.<locals>.<listcomp>)rCr�r��compile_instructions)r=r~r%r%r&�get_original_clean_instructions�s��z2SentinelNodeFinder.get_original_clean_instructionsc	#s����}t�fdd�t|�D��}t|�D]�\}}t|�}|dus$J�tj|t��tjtd�d�}t�	|�||�z
 268  ��
 269  �}W||�n||�wtjdkrlzt
 270  ||�Wntyktsh|t|�dkrjYq�wdd�t|�D�}	t|	�D]\}
 271  }||
 272  d	8}t|�|�jd
 273  k�t|�|�jdk�qyt|	�D]l\}
 274  }||
 275  d	8}|d}||kr�q�||}
 276  ||}|
 277  j|jkr�dvr�nn#|
 278  j|jkr�||dj||djkr�d
 279  kr�nn|�|d�tjdk�rt||�D]\}}t|�o|�ot||��q�|Vq�qdS)Nc3s"�|]\}}|�jkr|VqdSr2rr>r�r%r&r����
 280  ��z4SentinelNodeFinder.matching_nodes.<locals>.<genexpr>)�s)�leftr��right)��
 281  rCcSsg|]\}}|jtkr|�qSr%)r��sentinel)rNr?rr%r%r&rP�r@z5SentinelNodeFinder.matching_nodes.<locals>.<listcomp>rDr�r��rr
 282  r�)rJrKrD�
 283  get_setterr[rr!�StrrQ�fix_missing_locationsrI�sys�version_info�handle_jumpsr�r�rFr1r�r��argr�
 284  opnames_match)r=r:�original_instructionsZoriginal_indexZ
 285  expr_index�expr�setter�replacement�instructions�indicesZ	index_numZsentinel_indexZ	new_index�
 286  original_inst�new_inst�inst1�inst2r%r�r&r8�sp��
 287  �
 288  
 289  
 290  ��	�
 291  ����z!SentinelNodeFinder.matching_nodescCs&t|j|j�}t|�|��}|�|�Sr2)r�rWr�rK�
 292  find_codesrC)r=Zmodule_coder�r%r%r&rIs
 293  z'SentinelNodeFinder.compile_instructionscsvtd�td�td�dd�g��js�td�td�g7���fdd	��g��|�r-��|����fd
 294  d���|��S)Nr3�co_freevars�co_cellvarscSst|j�p|jSr2)�is_ipython_cell_code_namer���cr%r%r&r�sz/SentinelNodeFinder.find_codes.<locals>.<lambda>�co_names�co_varnamescst��fdd��D��S)Nc3s"�|]}|��|�j�kVqdSr2r�)rN�f)rjr=r%r&r�s
 295  ��
 296  �zASentinelNodeFinder.find_codes.<locals>.matches.<locals>.<genexpr>)�allri)�checksr=rir&�matchess�z.SentinelNodeFinder.find_codes.<locals>.matchescs6|jD]}t�|�sq�|�r��|��|�qdSr2)�	co_consts�inspect�iscoderb)r��const)�code_options�finderrpr%r&rv!s
 297  
 298  
 299  
 300  �z-SentinelNodeFinder.find_codes.<locals>.finder)rrrb)r=Z	root_coder%)rorurvrpr=r&res"��
 301  
 302  zSentinelNodeFinder.find_codescsJtt|j��}t�fdd�t|�D��}	||}|jdkr |S|d7}q)z{
 303          Get the instruction corresponding to the current
 304          frame offset, skipping EXTENDED_ARG instructions
 305          c3s"�|]\}}|j�kr|VqdSr2)�offsetr>�r�r%r&r�8rKzDSentinelNodeFinder.get_actual_current_instruction.<locals>.<genexpr>TrFrC)rGr5r�rKrDr�)r=r�r_rErr%rxr&r.s	�
 306  �z1SentinelNodeFinder.get_actual_current_instruction)r!r"r#r~r<r9rCrJr8rIrerr%r%r%r&r�
 307  sb!	S&r�ccsd�d}tt|�|d�D]$\}}|jtkrt|jdk�d}q|r*t|jdk�d}q||fVqdS)zz
 308      Yields (index, instruction) pairs excluding the basic
 309      instructions introduced by the sentinel transformation
 310      FNr�Tr�)rrDr�rQr1r�)r_�startZ
 311  skip_powerr?r8r%r%r&�non_sentinel_instructionsFs�
 312  �rzccs��tt|�|d�}t||�}d}	zt|�\}}t|�\}	}
 313  Wn
 314  ty)YdSw|r@|j|
 315  jkr8dkr@nnt|�\}	}
 316  |j|
 317  jkoKdvnoS|j|
 318  jk}|||	|
 319  fVq)z�
 320      Yields matching indices and instructions from the new and original instructions,
 321      leaving out changes made by the sentinel transformation.
 322      NFTr�rR)rrDrz�next�
 323  StopIterationr�rY)r[�original_startr_ryZ
 324  original_iterZnew_iterZinverted_comparison�
 325  original_ira�new_irbr%r%r&�walk_both_instructionsYs(�
 326  ��
 327  ��r�c	s�	t|d|d�D]^\}}}�t|��rqd�jvrAd|jvrAt�fdd�t|�D��}t||||�}|dus8J�||||d�<n$g}||d�D]}|�|�|jdvrWnqIt�tt||��|||�<ndSq)	a�
 328      Transforms instructions in place until it looks more like original_instructions.
 329      This is only needed in 3.10+ where optimisations lead to more drastic changes
 330      after the sentinel transformation.
 331      Replaces JUMP instructions that aren't also present in original_instructions
 332      with the sections that they jump to until a raise or return.
 333      In some other cases duplication found in `original_instructions`
 334      is replicated in `instructions`.
 335      Tr�JUMPc3s0�|]\}}|j�jkrt|dd�s|VqdS)r+FN)rwr�r�r>�rbr%r&r��s�
 336  ���zhandle_jumps.<locals>.<genexpr>NrC��RETURN_VALUE�
 337  RAISE_VARARGS)	r�rZr�rKrD�handle_jumprbr-�find_new_matching)	r_r[r~rarryZnew_instructions�orig_sectionZsection_instr%r�r&rXts4�
 338  �	�
 339  
 340  ��rXccsr�tt|�t|��D]+}ttt||�t|���\}}t|�t|�kr&dSt||�r6|||dd�VqdS)z�
 341      Yields sections of `instructions` which match `orig_section`.
 342      The yielded sections include sentinel instructions, but these
 343      are ignored when checking for matches.
 344      NrArC)�rangerF�ziprrz�sections_match)r�r_ryr`�dup_sectionr%r%r&r��s���
 345  ��r�cCs�t||||�D]>\}}}}tt||��|jdvrEt|||d��}|D]}	d|	_q%|||d�}
 346  t||
 347  |�sAg|||d�<|SqdS)a`
 348      Returns the section of instructions starting at `start` and ending
 349      with a RETURN_VALUE or RAISE_VARARGS instruction.
 350      There should be a matching section in original_instructions starting at original_start.
 351      If that section doesn't appear elsewhere in original_instructions,
 352      then also delete the returned section of instructions.
 353      r�rCTN)r�r1rZr�rr+�check_duplicates)r[r}r_ryZ
 354  original_jraZnew_jrbZinlinedZinlr�r%r%r&r��s 	�
 355  ��r�cCsZtt|��D]$}||kr
 356  q|||t|��}t|�t|�kr"dSt||�r*dSqdS)z�
 357      Returns True if a section of original_instructions starting somewhere other
 358      than original_i and matching orig_section is found, i.e. orig_section is duplicated.
 359      FT)r�rFr�)r~r�r[Z	dup_startr�r%r%r&r��s
 360  �r�cCstdd�t||�D��S)z\
 361      Returns True if the given lists of instructions have matching linenos and opnames.
 362      cssF�|]\}}|j|jkpd|jko|jknot||�VqdS)�	POP_BLOCKN)r)r�rZ)rNZ	orig_instZdup_instr%r%r&r��s���
 363  �z!sections_match.<locals>.<genexpr>)rnr�)r�r�r%r%r&r��s�r�cCs\|j|jkp-d|jvod|jvp-|jdko|jdkp-|jdvo#|jdkp-|jdko-|jdkS)Nr��
 364  PRINT_EXPR�POP_TOP)r�r�r��CALL_METHODr<r=)rcrdr%r%r&rZ�s
 365  ��
 366  ��rZcsz|j�t���D]2\���|ur��fdd�}|St�t�r:t��D]\�}||ur9��fdd�}|Sq$qdS)Ncst��|�Sr2)r���new_node)r�r$r%r&r]�zget_setter.<locals>.settercs|��<dSr2r%r�)r�r?r%r&r]r�)r$r[r�rErGrD)rfr]�itemr%)r�r?r�r$r&rSs
 367  ��rScCs.t|tj�stt|�j}t|tj�rtt|�Sr2)rEr[rrr r$)rfr%r%r&r�!s�
 368  r�ccsD�t�|�D]}t|d�rtt|�tj�rt|�D]}|VqqdS)Nr$)r[r_r�rEr��Assertra)rWrfr)r%r%r&r)s�����rcCsLt|jtj�s|j}t|jtj�rt�d�}ttj|�g|_t�||�|S)Nr,)	rEr$r[�Moduler\rrr��
 369  copy_location)rrWr%r%r&�_extract_ipython_statement4s�
 370  r�cC�tt�d|��S)Nz(<module>|<cell line: \d+>)$)�bool�re�match)Z	code_namer%r%r&rhDr�rhcCr�)Nz'<ipython-input-|[/\\]ipykernel_\d+[/\\])r�r��searchrQr%r%r&�is_ipython_cell_filenameIr�r�cCst|j�o	t|j�Sr2)r�rkrhr�)Zcode_objr%r%r&r�Ns
 371  �r�c		Cspd}}|D]-}t|�}zt|||||�}|s|r#|js|jr#WdS|j}|j}Wqty3Yqw||fS)N)NN)r�r�r~r�r�)	rnr�r�r�rfr�rrWr�r%r%r&r�Vs
 372  �r�cCs.||krdS|�d�s
 373  dStt�d||��S)z�
 374      Checks that the user-visible attr (from ast) can correspond to
 375      the argval in the bytecode, i.e. the real attribute fetched internally,
 376      which may be mangled for private attributes.
 377      T�__Fz^_\w+%s$)r�r�r�r�)rr�r%r%r&ris
 378  
 379  rccsh�t|d�r0g}t|d�r$t|tj�r$|jdusJ�t|j|jd�}n|jg}|D]}|Vq*dSdS)Nr)�
 380  end_linenorC)r�rEr[r\r�r�r))rfZlinenosr)r%r%r&raws�
 381  �ra)rO�rC)�PositionNodeFinder)r,)cr�r�r[r4rrr�rqr�rVr��collectionsr�copyr�	functoolsr�	itertoolsrr�operatorr�pathlibr�	threadingr	�tokenizer
 382  �typingrrr
 383  rrrrrrrrrrrrrr�rrZasttokens.asttokensrr�r�rBrwr�r r(r*r1r5r�r�r9rBrK�objectrLr��NodeVisitorrc�sum�all_feature_namesr�r�rQr�r�rzr�rXr�r�r�r�rZrSr7r�rr�rhr�r�r�rrarWZ_position_node_finderr�r�r%r%r%r&�<module>s�H
 384  
 385  "?�;8
 386  
 387