a
    ÷£BeC  ã                   @   sZ  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	 d dl
mZ d dlmZmZmZ d dlmZmZ d d	lmZmZ d d
lmZ d dlmZ d dlmZ G dd„ dƒZG dd„ deƒZG dd„ dƒZG dd„ deeƒZG dd„ deeƒZG dd„ deeƒZ G dd„ deeƒZ!G dd„ deeƒZ"G dd„ deƒZ#G dd „ d e#ƒZ$d!d"„ Z%d#d$„ Z&d%S )&é    )Úabstractmethod)Úcontextmanager)ÚPath)ÚOptional)Úsearch_ancestor)ÚName)ÚParserTreeFilterÚMergedFilterÚGlobalNameFilter)ÚAnonymousParamNameÚTreeNameDefinition)Ú	NO_VALUESÚValueSet)Úget_parent_scope)Údebug)Úparser_utilsc                   @   s¸   e Zd Zdd„ Zed)dd„ƒZdd„ Zd*d	d
„Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZdd „ Zed!d"„ ƒZd#d$„ Zd%d&„ Zed'd(„ ƒZdS )+ÚAbstractContextc                 C   s   || _ i | _d S ©N)Úinference_stateÚpredefined_names)Úselfr   © r   úY/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jedi/inference/context.pyÚ__init__   s    zAbstractContext.__init__Nc                 C   s   t ‚d S r   ©ÚNotImplementedError©r   Úuntil_positionÚorigin_scoper   r   r   Úget_filters   s    zAbstractContext.get_filtersc                 C   sF   ddl m} t| t|tƒr|nd |ƒ}| ||¡}t d|| |¡ |S )Nr   )Úfinderzcontext.goto %s in (%s): %s)Újedi.inferencer    Ú_get_global_filters_for_nameÚ
isinstancer   Zfilter_namer   Údbg)r   Úname_or_strÚpositionr    ÚfiltersÚnamesr   r   r   Úgoto   s    ÿzAbstractContext.gotoTc                 C   sX  |du r| }|   ||¡}t|tƒr(|jn|}d}| jrœt|tƒrœ|}|durœt |¡sœ|j}|jdv rDz| j| }	|	| }
W n t	y’   Y qDY qD0 |
}qœqD|durÜ|rÜddl
m} |j| | j|d�}||ju rÖt}qð|}nt dd„ |D ƒ¡}|�s2|�s2|�r2t|tƒ�r2ddl
m} d	| }| |d
||¡ t d||¡ |�rJ|S |  |||¡S )zZ
        :param position: Position of the last statement -> tuple of line, column
        N)Zif_stmtZfor_stmtÚcomp_forÚsync_comp_forr   )Úflow_analysis)ÚcontextZvalue_scopeÚnodec                 s   s   | ]}|  ¡ V  qd S r   )Zinfer)Ú.0Únamer   r   r   Ú	<genexpr>M   ó    z5AbstractContext.py__getattribute__.<locals>.<genexpr>)Úanalysisz$NameError: name '%s' is not defined.z
name-errorz context.names_to_types: %s -> %s)r)   r#   r   Úvaluer   r   Úis_scopeÚparentÚtypeÚKeyErrorr!   r,   Zreachability_checkÚ	tree_nodeZUNREACHABLEr   r   Z	from_setsr3   Úaddr   r$   Ú_check_for_additional_knowledge)r   r%   Úname_contextr&   Zanalysis_errorsr(   Zstring_nameZfound_predefined_typesr.   Z	name_dictÚtypesr,   ÚcheckÚvaluesr3   Úmessager   r   r   Úpy__getattribute__&   sJ    


ý
z"AbstractContext.py__getattribute__c                 C   s~   |p| }t |tƒrz| ¡ sz|}|jg}tdd„ |D ƒƒr<tS ddlm} t|dd�}|||||ƒ}|d urn|S ||v rHqzqHtS )Nc                 s   s   | ]}|j d v V  qdS )©r*   r+   N)r7   )r/   Úbr   r   r   r1   a   r2   zBAbstractContext._check_for_additional_knowledge.<locals>.<genexpr>r   )Úcheck_flow_informationT)Zinclude_flows)	r#   r   Úis_instancer9   Úanyr   Zjedi.inference.finderrD   r   )r   r%   r<   r&   Ú
flow_scopeZ
base_nodesrD   Únr   r   r   r;   Z   s     ÿz/AbstractContext._check_for_additional_knowledgec                 C   s   | j }|d u r| S | ¡ S r   )Úparent_contextÚget_root_context)r   rI   r   r   r   rJ   n   s    z AbstractContext.get_root_contextc                 C   s   dS ©NFr   ©r   r   r   r   Ú	is_modulet   s    zAbstractContext.is_modulec                 C   s   dS rK   r   rL   r   r   r   Úis_builtins_modulew   s    z"AbstractContext.is_builtins_modulec                 C   s   dS rK   r   rL   r   r   r   Úis_classz   s    zAbstractContext.is_classc                 C   s   dS rK   r   rL   r   r   r   Úis_stub}   s    zAbstractContext.is_stubc                 C   s   dS rK   r   rL   r   r   r   rE   €   s    zAbstractContext.is_instancec                 C   s   dS rK   r   rL   r   r   r   Úis_compiledƒ   s    zAbstractContext.is_compiledc                 C   s   dS rK   r   rL   r   r   r   Úis_bound_method†   s    zAbstractContext.is_bound_methodc                 C   s   t ‚d S r   r   rL   r   r   r   Ú
py__name__‰   s    zAbstractContext.py__name__c                 C   s   t ‚d S r   r   rL   r   r   r   Ú	get_value�   s    zAbstractContext.get_valuec                 C   s   d S r   r   rL   r   r   r   r0   �   s    zAbstractContext.namec                 C   s   dS )Nr   r   rL   r   r   r   Úget_qualified_names”   s    z#AbstractContext.get_qualified_namesc                 C   s   dS )NÚ r   rL   r   r   r   Ú	py__doc__—   s    zAbstractContext.py__doc__c                 c   s,   | j }|||< zd V  W ||= n||= 0 d S r   )r   )r   rG   ÚdctZ
predefinedr   r   r   Úpredefine_namesš   s
    zAbstractContext.predefine_names)NN)NNT)Ú__name__Ú
__module__Ú__qualname__r   r   r   r)   rA   r;   rJ   rM   rN   rO   rP   rE   rQ   rR   rS   rT   Úpropertyr0   rU   rW   r   rY   r   r   r   r   r      s2   	  ÿ
4

r   c                       s¤   e Zd ZdZ‡ fdd„Zedd„ ƒZedd„ ƒZdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZdd„ Zdd„ Zdd„ Zd d!„ Z‡  ZS )"ÚValueContextzC
    Should be defined, otherwise the API returns empty types.
    c                    s   t ƒ  |j¡ || _d S r   )Úsuperr   r   Ú_value)r   r4   ©Ú	__class__r   r   r   ¨   s    zValueContext.__init__c                 C   s   | j jS r   )r`   r9   rL   r   r   r   r9   ¬   s    zValueContext.tree_nodec                 C   s   | j jS r   )r`   rI   rL   r   r   r   rI   °   s    zValueContext.parent_contextc                 C   s
   | j  ¡ S r   )r`   rM   rL   r   r   r   rM   ´   s    zValueContext.is_modulec                 C   s   | j | jjkS r   )r`   r   Úbuiltins_modulerL   r   r   r   rN   ·   s    zValueContext.is_builtins_modulec                 C   s
   | j  ¡ S r   )r`   rO   rL   r   r   r   rO   º   s    zValueContext.is_classc                 C   s
   | j  ¡ S r   )r`   rP   rL   r   r   r   rP   ½   s    zValueContext.is_stubc                 C   s
   | j  ¡ S r   )r`   rE   rL   r   r   r   rE   À   s    zValueContext.is_instancec                 C   s
   | j  ¡ S r   )r`   rQ   rL   r   r   r   rQ   Ã   s    zValueContext.is_compiledc                 C   s
   | j  ¡ S r   )r`   rR   rL   r   r   r   rR   Æ   s    zValueContext.is_bound_methodc                 C   s
   | j  ¡ S r   )r`   rS   rL   r   r   r   rS   É   s    zValueContext.py__name__c                 C   s   | j jS r   )r`   r0   rL   r   r   r   r0   Ì   s    zValueContext.namec                 C   s
   | j  ¡ S r   )r`   rU   rL   r   r   r   rU   Ð   s    z ValueContext.get_qualified_namesc                 C   s
   | j  ¡ S r   )r`   rW   rL   r   r   r   rW   Ó   s    zValueContext.py__doc__c                 C   s   | j S r   ©r`   rL   r   r   r   rT   Ö   s    zValueContext.get_valuec                 C   s   d| j j| jf S ©Nz%s(%s))rb   rZ   r`   rL   r   r   r   Ú__repr__Ù   s    zValueContext.__repr__)rZ   r[   r\   Ú__doc__r   r]   r9   rI   rM   rN   rO   rP   rE   rQ   rR   rS   r0   rU   rW   rT   rf   Ú__classcell__r   r   ra   r   r^   ¤   s(   


r^   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚTreeContextMixinc                 C   s   ddl m} || |ƒS )Nr   )Ú
infer_node)Zjedi.inference.syntax_treerj   )r   r.   rj   r   r   r   rj   Þ   s    zTreeContextMixin.infer_nodec                 C   s¶   ddl m} || jkr*|  ¡ s"J ‚|  ¡ S |  |¡}|jdv rŒ|j ||¡}| 	¡ rˆ|j
 |j¡}| | j|j
|¡}|j|| ¡ |d�}|S |jdkr¦| | j||¡S td| ƒ‚d S )Nr   )r4   )ÚfuncdefÚlambdef)ÚinstanceZclass_contextÚfunctionÚclassdefzProbably shouldn't happen: %s)r!   r4   r9   rM   rT   Úcreate_contextr7   ZFunctionValueZfrom_contextrO   rI   Úcreate_valueZAnonymousInstancer   ZBoundMethodÚ
as_contextZ
ClassValuer   )r   r.   r4   rI   ÚfuncZclass_valuerm   r   r   r   rq   â   s*    



ÿý
zTreeContextMixin.create_valuec                    sz   d
‡ ‡‡‡fdd„	‰ dd„ ‰ˆˆƒ}|j dv rn|j|j d¡ }ˆj|jk rnˆj}|j dkrf|jˆksnˆ|ƒ}ˆ |dd	�S )NTc                    sn   | ˆj krˆS | jdv r&ˆ | ¡ ¡ S | jdv r^ˆ ˆ| jƒƒ}ˆj| jd jkrT|S t|| ƒS td|  ƒ‚d S )N)rk   rl   ro   rB   éÿÿÿÿz(There's a scope that was not managed: %s)	r9   r7   rq   rr   r6   Ú	start_posÚchildrenÚCompForContextÚ	Exception)Ú
scope_nodeÚ	is_nestedrI   ©Úfrom_scope_noder.   Úparent_scoper   r   r   r|   ý   s    



z8TreeContextMixin.create_context.<locals>.from_scope_nodec                 S   sp   | j } t | ¡r| S | jdv r:| jd jdv rj| jd S q | jdkr | jdd… D ]}|jdv rR|  S qRq d S )N)ÚargumentZtestlist_compé   rB   Zdictorsetmakeré   )r6   r   r5   r7   rv   )r.   rH   r   r   r   r}   
  s    



z5TreeContextMixin.create_context.<locals>.parent_scope)rk   ro   ú:Úparam)rz   )T)r7   rv   Úindexru   r6   r0   )r   r.   ry   Úcolonr6   r   r{   r   rp   ü   s    
zTreeContextMixin.create_contextc                 C   sX   |  ¡ }|r@|jdkr@|j|kr@t|ddƒ}|  |¡}t||ƒS |  |¡}t||ƒS d S )Nr‚   rk   rl   )Zget_definitionr7   r0   r   rq   r   rp   r   )r   Z	tree_nameZ
definitionrk   rs   r-   r   r   r   Úcreate_name"  s    


zTreeContextMixin.create_nameN)rZ   r[   r\   rj   rq   rp   r…   r   r   r   r   ri   Ý   s   &ri   c                   @   s   e Zd Zddd„ZdS )ÚFunctionContextNc                 c   s   t | j| ||d�V  d S ©N)rI   r   r   )r   r   r   r   r   r   r   .  s    üzFunctionContext.get_filters)NN©rZ   r[   r\   r   r   r   r   r   r†   -  s   r†   c                   @   sP   e Zd Zee dœdd„Zddd„Zdd„ Zed	d
„ ƒZ	edd„ ƒZ
dd„ ZdS )ÚModuleContext©Úreturnc                 C   s
   | j  ¡ S r   ©r`   Ú
py__file__rL   r   r   r   r�   8  s    zModuleContext.py__file__Nc                 c   s>   | j  |¡}t|d ƒ tt| ||d�|  ¡ ƒV  |E d H  d S r‡   )r`   r   Únextr	   r   Úget_global_filter)r   r   r   r'   r   r   r   r   ;  s    
ýúzModuleContext.get_filtersc                 C   s   t | ƒS r   )r
   rL   r   r   r   r�   I  s    zModuleContext.get_global_filterc                 C   s   | j jS r   ©r`   Ústring_namesrL   r   r   r   r‘   L  s    zModuleContext.string_namesc                 C   s   | j jS r   )r`   Ú
code_linesrL   r   r   r   r’   P  s    zModuleContext.code_linesc                 C   s   | j S )zß
        This is the only function that converts a context back to a value.
        This is necessary for stub -> python conversion and vice versa. However
        this method shouldn't be moved to AbstractContext.
        rd   rL   r   r   r   rT   T  s    zModuleContext.get_value)NN)rZ   r[   r\   r   r   r�   r   r�   r]   r‘   r’   rT   r   r   r   r   r‰   7  s   


r‰   c                   @   s<   e Zd Zddd„Zdd„ Zedd„ ƒZee dœd	d
„Z	dS )ÚNamespaceContextNc                 C   s
   | j  ¡ S r   ©r`   r   r   r   r   r   r   ^  s    zNamespaceContext.get_filtersc                 C   s   | j S r   rd   rL   r   r   r   rT   a  s    zNamespaceContext.get_valuec                 C   s   | j jS r   r�   rL   r   r   r   r‘   d  s    zNamespaceContext.string_namesrŠ   c                 C   s
   | j  ¡ S r   rŒ   rL   r   r   r   r�   h  s    zNamespaceContext.py__file__)NN)
rZ   r[   r\   r   rT   r]   r‘   r   r   r�   r   r   r   r   r“   ]  s
   

r“   c                   @   s    e Zd Zddd„Zddd„ZdS )ÚClassContextNc                 c   s   |   ||¡V  d S r   )r�   r   r   r   r   r   m  s    zClassContext.get_filtersc                 C   s   t | ||d�S r‡   ©r   r   r   r   r   r�   p  s
    ýzClassContext.get_global_filter)NN)NN)rZ   r[   r\   r   r�   r   r   r   r   r•   l  s   
r•   c                       s>   e Zd Z‡ fdd„Zddd„Zdd„ Zdd	„ Zd
d„ Z‡  ZS )rw   c                    s   t ƒ  |j¡ || _|| _d S r   )r_   r   r   r9   rI   )r   rI   r*   ra   r   r   r   y  s    zCompForContext.__init__Nc                 c   s   t | ƒV  d S r   r–   r   r   r   r   r   ~  s    zCompForContext.get_filtersc                 C   s   d S r   r   rL   r   r   r   rT   �  s    zCompForContext.get_valuec                 C   s   dS )Nz<comprehension context>r   rL   r   r   r   rS   „  s    zCompForContext.py__name__c                 C   s   d| j j| jf S re   )rb   rZ   r9   rL   r   r   r   rf   ‡  s    zCompForContext.__repr__)NN)	rZ   r[   r\   r   r   rT   rS   rf   rh   r   r   ra   r   rw   x  s
   
rw   c                   @   s   e Zd Zddd„ZdS )ÚCompiledContextNc                 C   s
   | j  ¡ S r   r”   r   r   r   r   r   Œ  s    zCompiledContext.get_filters)NNrˆ   r   r   r   r   r—   ‹  s   r—   c                   @   s6   e Zd ZdZdd„ Zedd„ ƒZee dœdd„Z	dS )	ÚCompiledModuleContextNc                 C   s   | j S r   rd   rL   r   r   r   rT   “  s    zCompiledModuleContext.get_valuec                 C   s   | j jS r   r�   rL   r   r   r   r‘   –  s    z"CompiledModuleContext.string_namesrŠ   c                 C   s
   | j  ¡ S r   rŒ   rL   r   r   r   r�   š  s    z CompiledModuleContext.py__file__)
rZ   r[   r\   r’   rT   r]   r‘   r   r   r�   r   r   r   r   r˜   �  s
   
r˜   c                 C   s€   |d urtt |dddƒ}d }|dkr2|}t |ddƒ}|d urt|jd }|d urt||jk rt|d u sn||jd jk rt|j}t| ||ƒS )Nrk   ro   rl   éþÿÿÿ)r   rv   ru   Úget_global_filters)r-   Zname_or_noner&   Zancestorrl   r„   r   r   r   r"   ž  s    
r"   c                 c   sn   | }ddl m} | durF| j||d�E dH  t| |tfƒr>d}| j} qt|jj ¡ dƒ}|dusdJ ‚|V  dS )a»  
    Returns all filters in order of priority for name resolution.

    For global name lookups. The filters will handle name resolution
    themselves, but here we gather possible filters downwards.

    >>> from jedi import Script
    >>> script = Script('''
    ... x = ['a', 'b', 'c']
    ... def func():
    ...     y = None
    ... ''')
    >>> module_node = script._module_node
    >>> scope = next(module_node.iter_funcdefs())
    >>> scope
    <Function: func@3-5>
    >>> context = script._get_module_context().create_context(scope)
    >>> filters = list(get_global_filters(context, (4, 0), None))

    First we get the names from the function scope.

    >>> print(filters[0])  # doctest: +ELLIPSIS
    MergedFilter(<ParserTreeFilter: ...>, <GlobalNameFilter: ...>)
    >>> sorted(str(n) for n in filters[0].values())  # doctest: +NORMALIZE_WHITESPACE
    ['<TreeNameDefinition: string_name=func start_pos=(3, 4)>',
     '<TreeNameDefinition: string_name=x start_pos=(2, 0)>']
    >>> filters[0]._filters[0]._until_position
    (4, 0)
    >>> filters[0]._filters[1]._until_position

    Then it yields the names from one level "lower". In this example, this is
    the module scope (including globals).
    As a side note, you can see, that the position in the filter is None on the
    globals filter, because there the whole module is searched.

    >>> list(filters[1].values())  # package modules -> Also empty.
    []
    >>> sorted(name.string_name for name in filters[2].values())  # Module attributes
    ['__doc__', '__name__', '__package__']

    Finally, it yields the builtin filter, if `include_builtin` is
    true (default).

    >>> list(filters[3].values())  # doctest: +ELLIPSIS
    [...]
    r   )ÚBaseFunctionExecutionContextN)r   r   )	Zjedi.inference.value.functionr›   r   r#   r‰   rI   rŽ   r   rc   )r-   r   r   Zbase_contextr›   rC   r   r   r   rš   ³  s    /þrš   N)'Úabcr   Ú
contextlibr   Úpathlibr   Útypingr   Z
parso.treer   Zparso.python.treer   Zjedi.inference.filtersr   r	   r
   Zjedi.inference.namesr   r   Zjedi.inference.base_valuer   r   Zjedi.parser_utilsr   Zjedir   r   r   r^   ri   r†   r‰   r“   r•   rw   r—   r˜   r"   rš   r   r   r   r   Ú<module>   s0    9P
&