Format: application/x-python-code
⬇ Download File (21.7 KB)
   1  o
   2  
   3  ��h�r�@s6UddlmZmZmZddlmZmZmZmZm	Z	m
   4  Z
   5  mZmZm
   6  Z
   7  mZmZmZmZmZmZmZmZmZddlZddlZddlZddlZddlZddlmZddlmZm Z ddl!m"Z"m#Z#ddl$m%Z%ej&dkrqdd	l'm(Z(m)Z)ndd	lm(Z(m)Z)ej&d
   8  kr�ddl'm*Z*nddlm*Z*e%Gdd
   9  �d
  10  e
  11  ��Z+e%Gdd�de
  12  ��Z,e%Gdd�de
  13  ��Z-e%Gdd�de
  14  ��Z.ee-e.fZ/dedeedffdd�Z0e%eGdd�d���Z1de2dee2fdd�Z3de2dee2de2fdd �Z4d!d"�Z5e%eGd#d$�d$e1���Z6Gd%d&�d&e
  15  �Z7Gd'd(�d(e
  16  �Z8Gd)d*�d*�Z9e9�Z:d+Z;e�Z<e=�Z>Gd,d-�d-e?�Z@d.e2d/e8fd0d1�ZAejBd2ejCd3ejDd4ejEd5ejFd6ejGd7ejHd8ejId9ejJd:ejKd;ejLd<ejMd=ejNd>i
  17  ZOeeejee2fePd?<ejQd@ejRdAejSdBejTdCejUdDejVdEejWdFiZXeeejYee2dGffePdH<ejZdIej[dJej\dKej]dLiZ^eeej_ee2dGffePdM<GdNdO�dO�Z`GdPdQ�dQ�Zadeee2dGfdffdRdS�ZbdTeejcdfd/e8fdUdV�ZddedTejed/e8fdWdX�ZfdYededTejed/e8fdZd[�Zgd\e2d/e8fd]d^�ZhdTejed/e8fd_d`�Zidadb�Zjhdc�Zkele2emeneoejpejqejrejsejtejuejve7e9hZwee,ePdd<dvdedf�ZxdgZydhZze{e|e{��e{e|e}��@Z~ei����Z�eej�j�ePdi<e�e�e�hZ�hem�exem��el�exeley��e�j��en�exenez��e{�exe{e~��e}�exe}��e��e2�exe2��eo�exeo��e��djdk�e�D��ejq�exejqez��ejp�exejpey��ejr�exejrey��ejt�exejtey��eju�exejuez��ejv�exejve|e2���ej��exej�ey��ej�j��ej�j��Z�hew�e{�e}�e=�e�e��e��e"�e#�Z�ee/ePdl<he��Z�e1dmdndndndne{�dndndo�e6eweke�dpdqhe�dmdmdme�dr�	e1dmdmdmdmdmdmdmds�dt�Z�gdu�Z�dS)w�)�isclass�	signature�	Signature)�	Annotated�AnyStr�Callable�Dict�Literal�
  18  NamedTuple�NewType�Optional�Protocol�Set�Sequence�Tuple�Type�	TypeGuard�Union�get_args�
  19  get_origin�is_typeddictN)�cached_property)�	dataclass�field)�MethodDescriptorType�
  20  ModuleType)�undoc)��)�Self�
  21  LiteralString)r�)�
  22  TypeAliasTypec@�eZdZddd�ZdS)�
  23  HasGetItem�returnNcC�dS�N���self�keyr(r(�Oc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/core/guarded_eval.py�__getitem__/�zHasGetItem.__getitem__�r%N)�__name__�
  24  __module__�__qualname__r-r(r(r(r,r$-�r$c@seZdZdefdd�ZdS)�InstancesHaveGetItemr%cOr&r'r()r*�args�kwargsr(r(r,�__call__4r.zInstancesHaveGetItem.__call__N)r0r1r2r$r7r(r(r(r,r42sr4c@r#)�
  25  HasGetAttrr%NcCr&r'r(r)r(r(r,�__getattr__9r.zHasGetAttr.__getattr__r/)r0r1r2r9r(r(r(r,r87r3r8c@seZdZdS)�DoesNotHaveGetAttrN)r0r1r2r(r(r(r,r:<sr:�funcr%cCsVt|dd�}t|�}t|dd�}t|dd�}|dur)|r)|r$|r)||vr)t||�SdS)z}Get unbound method for given bound method.
  26  
  27      Returns None if cannot get unbound method, or method is already unbound.
  28      �__self__Nr0�__dict__)�getattr�type)r;�ownerZowner_class�nameZinstance_dict_overridesr(r(r,�_unbind_methodEs���
  29  rBc@s�eZdZUdZdZeed<dZeed<dZeed<dZ	eed<dZ
  30  eed<dZeed<dZeed	<e
  31  ed
  32  �Zeeed<dd
  33  �Zdd�Zddeedffdd�Zdd�ZdS)�EvaluationPolicyz Definition of evaluation policy.F�allow_locals_access�allow_globals_access�allow_item_access�allow_attr_access�allow_builtins_access�allow_all_operations�allow_any_calls��default_factory�
  34  allowed_callscC�|jSr')rF�r*�value�itemr(r(r,�can_get_itemh�zEvaluationPolicy.can_get_itemcCrNr')rG)r*rP�attrr(r(r,�can_get_attrkrSzEvaluationPolicy.can_get_attrN�dunders.cCs|jrdSdS�NT)rI�r*rV�a�br(r(r,�can_operatens�zEvaluationPolicy.can_operatecCs:|jrdS||jvrdSt|�}|r||jvrdSdSdSrW)rJrMrB)r*r;Zowner_methodr(r(r,�can_callrs
  35  �zEvaluationPolicy.can_callr')r0r1r2�__doc__rD�bool�__annotations__rErFrGrHrIrJr�setrMrrrRrUr�strr[r\r(r(r(r,rCZs
  36  rC�module_name�access_pathcCs"tj|}|D]}t||�}q|S)z�Get value from external module given a dotted access path.
  37  
  38      Raises:
  39      * `KeyError` if module is removed not found, and
  40      * `AttributeError` if access path does not match an exported object
  41      )�sys�modulesr>)rbrc�member_typerTr(r(r,�
  42  _get_externals
  43  rg�method_namec	Cs�|tjvrdSz7t||�}t|�}t|�|krWdS|dkr!WdSt||�r9t||d�}t||d�}||kr<WdSWdSWdSttfyJYdSw)NFT�__getattribute__)rdrergr?�
  44  isinstancer>�AttributeError�KeyError)rPrbrcrhrf�
  45  value_type�methodZ
  46  member_methodr(r(r,�_has_original_dunder_external�s&
  47  
  48  
  49  ��roc	Cs^t|�}||vr
  50  dSt||d�}|durdS||vrdS|D]^}}t||||�r,dSqdS)NTF)r?r>ro)	rP�
  51  allowed_types�allowed_methods�allowed_externalrhrmrnrbrcr(r(r,�_has_original_dunder�s�rsc@sXeZdZUeed�Zeeed<eed�Z	ee
  52  edfed<eed�Zee
  53  ed<eed�Zee
  54  edfed<eed�Zeed<eed�Zee
  55  edfed<eed	d
  56  �Zeeeefed<dd
  57  �Zdd�Zd de
  58  edffdd�Zdedeefdd�Zedeefdd��Zedeefdd��Zedeefdd��Zdeefdd�ZdS)!�SelectivePolicyrK�allowed_getitem.�allowed_getitem_external�allowed_getattr�allowed_getattr_external�allowed_operations�allowed_operations_externalF)rL�init�_operation_methods_cachec
  59  
  60  Cs�t||j|j|jdd�}t||j|j|jdd�}d}|dur#|r#d}n|o&|}|rrt|�}t||�s4dSt||�}t|t	�}|sBdSt|�|jvrKdS|jD]#^}	}
  61  zt
  62  |	|
  63  �}t||�}Wn
  64  ttfykYdSw||kSdS)Nri�rprqrrrhr9FT)
  65  rsrw�_getattribute_methodsrx�_getattr_methodsr?�hasattrr>rj�propertyrgrlrk)
  66  r*rPrTZhas_original_attributeZhas_original_attr�acceptZvalue_classZclass_attr_valZis_propertyrbrcZexternal_classZexternal_class_attr_valr(r(r,rU�sJ��
  67  
  68  
  69  
  70  �zSelectivePolicy.can_get_attrcCst||j|j|jdd�S)zTAllow accessing `__getiitem__` of allow-listed instances unless it was not modified.r-r})rsru�_getitem_methodsrvrOr(r(r,rRs�zSelectivePolicy.can_get_itemNrVcs0|g�|dur��|�t��fdd�|D��S)Nc
  71  s2g|]}�D]}t|�j��|��j|d��qqS)r})rsry�_operator_dunder_methodsrz)�.0�dunder�obj��objectsr*r(r,�
  72  <listcomp>s�	���z/SelectivePolicy.can_operate.<locals>.<listcomp>)�append�allrXr(r�r,r[s
  73  ��zSelectivePolicy.can_operater�r%cCs(||jvr|�|j|�|j|<|j|Sr')r|�_safe_get_methodsry)r*r�r(r(r,r�#s
  74  
  75  
  76  �
  77  z(SelectivePolicy._operator_dunder_methodscC�|�|jd�S)Nr-)r�ru�r*r(r(r,r�*�z SelectivePolicy._getitem_methodscCr�)Nr9�r�rwr�r(r(r,r.r�z SelectivePolicy._getattr_methodscCr�)Nrir�r�r(r(r,r~2r�z%SelectivePolicy._getattribute_methodscs�fdd�|D�S)Ncs h|]}t|�d�}|r|�qSr'�r>)r��class_rn�rAr(r,�	<setcomp>7s��z4SelectivePolicy._safe_get_methods.<locals>.<setcomp>r()r*�classesrAr(r�r,r�6s
  78  �z!SelectivePolicy._safe_get_methodsr')r0r1r2rr`rurr4r_rvrrarw�MayHaveGetattrrxryrz�dictr|rrrUrRr[r�rr�rr~r�r(r(r(r,rt�s*
  79  �6
  80  rtc@�eZdZdZdS)�_DummyNamedTuplez<Used internally to retrieve methods of named tuple instance.N�r0r1r2r]r(r(r(r,r�?�r�c@s:eZdZUeed<eed<dZeded<dZeed<dS)	�EvaluationContext�locals�globals�	forbidden)r��minimal�limited�unsafe�	dangerous�
  81  evaluationF�in_subscriptN)	r0r1r2r�r_r�r	r�r^r(r(r(r,r�Cs
  82  �r�c@seZdZdZdd�ZdS)�_IdentitySubscriptz<Returns the key itself when item is requested via subscript.cCs|Sr'r(r)r(r(r,r-Tsz_IdentitySubscript.__getitem__N)r0r1r2r]r-r(r(r(r,r�Qsr�Z__SUBSCRIPT_SENTINEL__c@r�)�GuardRejectionz7Exception raised when guard rejects evaluation attempt.Nr�r(r(r(r,r�^sr��code�contextcCs�|j}|jdkrtd��|jr3|st�S|��}t|t<td|d}td
  83  ii|�	��d|i���}|jdkr@t
  84  ||j|j�Stj
  85  |dd�}t||�S)a7Evaluate provided code in the evaluation context.
  86  
  87      If evaluation policy given by context is set to ``forbidden``
  88      no evaluation will be performed; if it is set to ``dangerous``
  89      standard :func:`eval` will be used; finally, for any other,
  90      policy :func:`eval_node` will be called on parsed AST.
  91      r�zForbidden mode�[�]r�r��eval)�modeNr()r�r�r�r��tuple�copy�IDENTITY_SUBSCRIPT�SUBSCRIPT_MARKERr��_asdictr�r��ast�parse�	eval_node)r�r�Zlocals_Z
  92  expressionr(r(r,�guarded_evalds
  93  
  94  
  95  r�)�__add__)�__sub__)�__mul__)�__truediv__)�__floordiv__)�__mod__)�__pow__)�
  96  __lshift__)�
  97  __rshift__)�__or__)�__xor__)�__and__)�
  98  __matmul__�BINARY_OP_DUNDERS)�__eq__)�__ne__r�)�__lt__�__gt__)�__le__�__ge__)r�r�)r�r�)�__contains__.�COMP_OP_DUNDERS)�__neg__)�__pos__)�
  99  __invert__�__inv__)�__not__�UNARY_OP_DUNDERSc@r�)�ImpersonatingDuckzXA dummy class used to create objects of other classes without calling their ``__init__``Nr�r(r(r(r,r��r�r�cs|eZdZdZddeedeefdd�Zdefdd	�Zdefd
 100  d�Z	�fdd
 101  �Z
 102  defdd�Zdefdd�Zdd�Z
 103  �ZS)�_DuckzHA dummy class used to create objects pretending to have given attributesN�
 104  attributes�itemscCs|pi|_|pi|_dSr'�r�r�)r*r�r�r(r(r,�__init__�s
 105  z_Duck.__init__rTcC�
 106  |j|Sr'�r��r*rTr(r(r,r9��
 107  z_Duck.__getattr__cCs
 108  ||jvSr'r�r�r(r(r,�__hasattr__�r�z_Duck.__hasattr__csgtt��|j�Sr')�dir�superr�r���	__class__r(r,�__dir__�sz
 109  _Duck.__dir__r+cCr�r'�r�r)r(r(r,r-�r�z_Duck.__getitem__cCr�r'r�r)r(r(r,�__hasitem__�r�z_Duck.__hasitem__cCs
 110  |j��Sr')r��keysr�r(r(r,�_ipython_key_completions_�r�z_Duck._ipython_key_completions_)NN)r0r1r2r]rr�r�rar9r�r�r-r�r��
 111  __classcell__r(r(r�r,r��sr�cCs(d}|��D]\}}t||�r|}q|Sr')r�rj)Znode_oprVr��opZcandidate_dunderr(r(r,�_find_dunder�s
 112  �r��nodecs�t�j}|durdSt|tj�rt|j��St|tj�rRt|j��}t|j	��}t
 113  |jt�}|rR|�
 114  |||�rAt||d�|�Std|�d�t|�d�j�d���t|tj�r�t|j��}d}d}t|j|j�D]�\}}t|��}d}	t
 115  |t�}|s�t|tj�r�ttj}d}t|tj�r�d	}	t|tj�r�d	}	d}|	s�|r�|d}	|	r�|	d
 116  kr�||fn||f\}
 117  }|	d	ks�|r�|�
 118  ||
 119  |�r�tt|	�|
 120  |�}|r�|}|s�d}|}qitd|	�d�t|�d�j�d���td|	�d
 121  ���|St|tj�r�|jSt|tj��r
 122  t�fdd�|j D��St|tj!��r�fdd�|j D�St|tj"��r,�fdd�|j D�St|tj#��rJt$t�fdd�|j%D��fdd�|j&D���St|tj'��rct(t|j)��t|j*��t|j+���St|tj,��r�t|j-��}
 123  t
 124  |jt.�}|�r�|�
 125  ||
 126  ��r�t|
 127  |d��Std|�d�t|
 128  �d�j�d���t|tj/��r�t|j��}
 129  t|j(��}|�0|
 130  |��r�|
 131  |Stdt|
 132  �d�j�d���t|tj1��r�t2|j3��St|tj4��r�t|j��}
 133  |�5|
 134  |j6��r�t|
 135  |j6�Stdt|
 136  �d�j�d���t|tj7��rt|j8��}|�rt|j��St|j9��St|tj:��rit|j;��}|�<|��r:|j=�s:�fdd�|j>D�}||�St?|��rQt@|jA|��}|tBu�rM|StC|�St@||��}|tBu�r^|Std|d�j�d���tdt�D|���)a�Evaluate AST node in provided context.
 137  
 138      Applies evaluation restrictions defined in the context. Currently does not support evaluation of functions with keyword arguments.
 139  
 140      Does not evaluate actions that always have side effects:
 141  
 142      - class definitions (``class sth: ...``)
 143      - function definitions (``def sth: ...``)
 144      - variable assignments (``x = 1``)
 145      - augmented assignments (``x += 1``)
 146      - deletions (``del x``)
 147  
 148      Does not evaluate operations which do not return values:
 149  
 150      - assertions (``assert x``)
 151      - pass (``pass``)
 152      - imports (``import x``)
 153      - control flow:
 154  
 155          - conditionals (``if x:``) except for ternary IfExp (``a if x else b``)
 156          - loops (``for`` and ``while``)
 157          - exception handling
 158  
 159      The purpose of this function is to guard against unwanted side-effects;
 160      it does not give guarantees on protection from malicious code execution.
 161      NrzOperation (`z`) forznot allowed in � modeTF�is_r�z
 162  Comparison (`zComparison `z` not supportedc3s�|]}t|��VqdSr'�r��r��e�r�r(r,�	<genexpr>+s�zeval_node.<locals>.<genexpr>c�g|]}t|���qSr(r�r�r�r(r,r�-�zeval_node.<locals>.<listcomp>csh|]}t|���qSr(r�r�r�r(r,r�/r�zeval_node.<locals>.<setcomp>cr�r(r��r��kr�r(r,r�3r�cr�r(r�)r��vr�r(r,r�4r�z$Subscript access (`__getitem__`) forz not allowed in z$Attribute access (`__getattr__`) forcr�r(r��r��argr�r(r,r�gr�zCall forzUnhandled node)E�EVALUATION_POLICIESr�rjr��
 163  Expressionr��body�BinOp�left�rightr�r�r�r[r>r�r?�Compare�zip�ops�comparatorsr��NotIn�In�Is�IsNot�operator�
 164  ValueError�ConstantrPrr��elts�Listrrr�r��values�Slice�slice�lower�upper�step�UnaryOp�operandr��	SubscriptrR�Name�_eval_node_name�id�	AttributerUrT�IfExp�test�orelse�Callr;r\�keywordsr5r�_eval_return_type�__new__�
 165  NOT_EVALUATED�_create_duck_for_heap_type�dump)r�r��policyr�r�rVZall_true�negater�r�rYrZ�resultrPZslice_rr;r5Zoverridden_return_type�return_typer(r�r,r��s
 166  
 167  �
 168  
 169  
 170  
 171  �
 172  ���
 173  
 174  
 175  �
 176  ���
 177  
 178  
 179  �r�cCsJzt|�}Wntyt}Ynw|jtju}|r#t|j||||�StS)zyEvaluate return type of a given callable function.
 180  
 181      Returns the built-in type, a duck or NOT_EVALUATED sentinel.
 182      )rr	�UNKNOWN_SIGNATURE�return_annotationr�empty�_resolve_annotationr!)r;r�r��sig�	not_emptyr(r(r,r~s�rr,c
 183  s�t|t�r
 184  t|��n|}t|�}|turt�d�r�jS|tur0t|�}t	|�dkr.|dSdS|t
 185  ur6dS|turjd}t�j
 186  ���D]\}\}	}
 187  |
 188  jturR|}nqC|durf|t	�j�krht�j|��SdSdS|turqt�S|tur�����fdd�t|�D�}tt�|�d�St|�r�tt�tt�������fd	d
 189  �|j��D�d�St|d�r�tt�t|��d�S|tur�t|�d}t|�����St|t�r�t|j���St|t �r�t|j!���St|���S)
 190  zKResolve annotation created by user with `typing` module and custom objects.r<�r�Nc	s*g|]}tt|������D]}|�qqSr()r�r+)r��type_argrT�r�r;r�r,r(r,r��s���z'_resolve_annotation.<locals>.<listcomp>r�c	s"i|]
 191  \}}|t|������qSr()r+)r�r�r�r1r(r,�
 192  <dictcomp>�s��z'_resolve_annotation.<locals>.<dictcomp>r��_is_protocol)"rjrarrrr�r<r	r�lenr r�	enumerate�
 193  parametersr��
 194  annotationr5r�rr^rr�r��fromkeysrr�r_rr+r�_eval_or_create_duck�
 195  __supertype__r"Z	__value__)
 196  r7r,r;r�r��originZ	type_args�index�ir+rPr�r0r(r1r,r+�s`
 197  ���
 198  �����
 199  
 200  
 201  r+�node_idcCs�t|j}|jr||jvr|j|S|jr||jvr|j|S|jr,tt|�r,t	t|�S|js;|js;t
 202  d|j�d���t|�d���)Nz Namespace access not allowed in r�z+ not found in locals, globals, nor builtins)r�r�rDr�rEr�rHr��builtinsr>r��	NameError)r>r�r$r(r(r,r�s
 203  
 204  
 205  
 206  �rcs>t�j}|�|�r|js�fdd�|jD�}||�St|�S)Ncr�r(r�r�r�r(r,r��r�z(_eval_or_create_duck.<locals>.<listcomp>)r�r�r\rr5r")�	duck_typer�r�r$r5r(r�r,r9�s
 207  
 208  r9cCs(t�}z||_|WStyYtSw)z�Create an imitation of an object of a given type (a duck).
 209  
 210      Returns the duck or NOT_EVALUATED sentinel if duck could not be created.
 211      )r�r��	TypeErrorr!)rA�duckr(r(r,r"�s�r">��pandasZ	DataFrame)�numpyZvoid)rE�core�indexingZ_iLocIndexer)rErGrHZ_LocIndexer)rFZndarray�rEZSeries�BUILTIN_GETITEMcs"�fdd�|r|D�St��D�S)z=For use on immutable objects or with methods returning a copycsg|]}t�|��qSr(r�r���clsr(r,r�r�z!_list_methods.<locals>.<listcomp>)r�)rL�sourcer(rKr,�
 212  _list_methodss"rN)r�r�r
 213  r�)r�r<�count�	dict_keyscCsg|]}t|�D]}|�qqSr()rN)r�Znumeric_clsrnr(r(r,r�1sr��BUILTIN_GETATTRTF)rHrDrErFrGrMrJrIrDrI)	rurvrwrxryrHrDrErM)rHrDrErGrFrJrI)r�r�r�)r�r�r�r�rBr')��inspectrrr�typingrrrrr	r
 214  rrr
 215  rrrrrrrrrr�r?�collectionsrrd�	functoolsr�dataclassesrr�typesrrZIPython.utils.decoratorsr�version_infoZtyping_extensionsrr r"r$r4r8r:r�rBrCrargrorsrtr�r�r�r�r�r(�objectr!�	Exceptionr�r��Add�Sub�Mult�Div�FloorDiv�Mod�Pow�LShift�RShift�BitOr�BitXor�BitAnd�MatMultr�r_�Eq�NotEq�Lt�LtE�Gt�GtErr��cmpop�USub�UAdd�Invert�Notr��unaryopr�r�r��ASTr�rrr+rr9r"ZSUPPORTED_EXTERNAL_GETITEMr��bytes�listr��defaultdict�deque�OrderedDict�ChainMap�UserDict�UserList�
 216  UserStringrJrNZdict_non_mutating_methodsZlist_non_mutating_methodsr`r��	frozensetZset_non_mutating_methodsr?r�rP�abc�KeysView�int�float�complexZNUMERICS�
 217  isdisjoint�range�Counter�elements�most_commonZ
 218  ALLOWED_CALLSrQZBUILTIN_OPERATIONSr��__all__r(r(r(r,�<module>s�P
 219  
 220  #
 221  ��
 222  �|%�"�"�	+���
 223  �<
 224  �
 225  
 226  ��������	�
 227  ���
 228  �������
 229  ��
 230  ��
 231  ��
 232  ��
 233  ����
 234   �!�"�%��������	�
 235  �
 236  ����&