ó
V]c           @   s.  d  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
 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 d d l m Z m Z d d l m Z d e f d     YZ d   Z d   Z  d S(   s°  
Searching for names with given scope and name. This is very central in Jedi and
Python. The name resolution is quite complicated with descripter,
``__getattribute__``, ``__getattr__``, ``global``, etc.

If you want to understand name resolution, please read the first few chapters
in http://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/.

Flow checks
+++++++++++

Flow checks are not really mature. There's only a check for ``isinstance``.  It
would check whether a flow has the form of ``if isinstance(a, type_or_tuple)``.
Unfortunately every other thing is being ignored (e.g. a == '' would be easy to
check for -> a is a string). There's big potential in these checks.
i’’’’(   t   tree(   t   search_ancestor(   t   debug(   t   settings(   t   compiled(   t   analysis(   t   flow_analysis(   t   TreeArguments(   t   helpers(   t   iterable(   t   get_global_filters(   t   TreeNameDefinition(   t
   ContextSett   NO_CONTEXTS(   t   is_scopet   get_parent_scope(   t   convert_contextst
   NameFinderc           B   sY   e  Z d e d   Z d   Z d   Z e d  Z d   Z	 d   Z
 d   Z d   Z RS(	   c         C   sm   | |  _  | |  _ | |  _ | |  _ t | t j  rE | j |  _ n	 | |  _ | |  _	 d  |  _ | |  _ d  S(   N(   t
   _evaluatort   _contextt   _name_contextt   _namet
   isinstanceR    t   Namet   valuet   _string_namet	   _positiont   Nonet   _found_predefined_typest   _analysis_errors(   t   selft	   evaluatort   contextt   name_contextt   name_or_strt   positiont   analysis_errors(    (    s3   lib/python2.7/site-packages/jedi/evaluate/finder.pyt   __init__$   s    							c         C   s/  |  j  |  } |  j d k	 rh | rh t j d |  j d |  j j d |  j  } | t j k ra t	 S|  j S|  j
 | |  } | r+|  j r+| r+t |  j t j  o¾ t |  j j j t j  r+t |  j t j  r+| rü t j |  j |  j |  j  q(d |  j } t j |  j d |  j |  q+n  | S(   s   
        :params bool attribute_lookup: Tell to logic if we're accessing the
            attribute or the contents of e.g. a function.
        R    t   context_scopet   nodes$   NameError: name '%s' is not defined.s
   name-errorN(   t   filter_nameR   R   R   t   reachability_checkR   t	   tree_nodeR   t   UNREACHABLER   t   _names_to_typesR   R   R    R   t   parentt   ParamR   t   add_attribute_errorR   R   t   add(   R   t   filterst   attribute_lookupt   namest   checkt   typest   message(    (    s3   lib/python2.7/site-packages/jedi/evaluate/finder.pyt   find3   s*    		
"c         C   s_   t  |  j t j  rW |  j } x2 | j d  k	 rR t  | t j  sF Pn  | j } q! W| Sd  Sd  S(   N(   R   R   R    R   R-   R   t   Scope(   R   t   scope(    (    s3   lib/python2.7/site-packages/jedi/evaluate/finder.pyt   _get_origin_scopeS   s    	c         C   sž   |  j    } | rķ |  j } | d  k	 rŌ t | d d d  } d  } | d k ri | } t | d d  } n  | d  k	 rŌ | j d } | d  k	 rŃ | | j k  rŃ | d  k sæ | | j d j k  rĪ | j } qĪ qŃ qŌ n  t |  j |  j | |  S|  j	 |  Sd  S(   Nt   funcdeft   classdeft   lambdefiž’’’(
   R:   R   R   R   t   childrent	   start_posR
   R   R   t   _get_context_filters(   R   t   search_globalt   origin_scopeR#   t   ancestorR=   t   colon(    (    s3   lib/python2.7/site-packages/jedi/evaluate/finder.pyt   get_filters_   s     	"c         c   s   x+ |  j  j t |  j d | D] } | Vq W|  j  j   r x? t t |  j  h   D]" } x | j   D] } | Vqi WqV Wn  d  S(   NRB   (   R   RE   t   FalseR   t   is_stubR   R   (   R   RB   t   ft   c(    (    s3   lib/python2.7/site-packages/jedi/evaluate/finder.pyR@   y   s    %	c   	      C   s~  g  } |  j  j r° t |  j t j  r° |  j } x} | d k	 r¬ t |  r¬ | j } | j	 d	 k r3 y! |  j  j | } | |  j
 } Wn t k
 r q3 q© X| |  _ Pq3 q3 Wn  x | D] } | j |  j
  } | r· t |  d k rJ| \ } t | t  rJ| j |  j k rG|  j j   } | d k	 rD| j	 d k rDq· qDqGqJn  Pq· q· Wt j d |  j
 |  j  | |  j  t |  S(
   st   
        Searches names that are defined in a scope (the different
        ``filters``), until a name fits.
        t   if_stmtt   for_stmtt   comp_fort   sync_comp_fori   t   import_froms$   finder.filter_name %s in (%s): %s@%sN(   RJ   RK   RL   RM   (   R   t   predefined_namesR   R   R    R   R   R   R-   t   typeR   t   KeyErrorR   t   gett   lenR   t	   tree_namet   get_definitionR   t   dbgR   t   list(	   R   R1   R3   R'   t	   name_dictR5   t   filtert   nt   def_(    (    s3   lib/python2.7/site-packages/jedi/evaluate/finder.pyR(      s6    !					c         C   sF   t  j |  j |  j  } | j d  p3 | j d  } | j | |  S(   s8   Checks for both __getattr__ and __getattribute__ methodsu   __getattr__u   __getattribute__(   R   t   create_simple_objectR   R   t   get_function_slot_namest   execute_function_slots(   R   t   instt   nameR3   (    (    s3   lib/python2.7/site-packages/jedi/evaluate/finder.pyt   _check_getattr­   s    	c         C   s4  t  j d   | D  } t j d | |  | rb |  j j   rb |  j j   rb |  j |  j  S| r0t |  j	 t
 j  r0|  j j   r0|  j j   r0|  j	 } |  j j g } t d   | D  rŠ | Sx] t r,t | d t } t |  j | |  j	 |  j  } | d  k	 r| S| | k rÓ PqÓ qÓ Wn  | S(   Nc         s   s   |  ] } | j    Vq d  S(   N(   t   infer(   t   .0R`   (    (    s3   lib/python2.7/site-packages/jedi/evaluate/finder.pys	   <genexpr>¾   s    s    finder._names_to_types: %s -> %sc         s   s   |  ] } | j  d k Vq d S(   RL   RM   N(   RL   RM   (   RP   (   Rc   t   b(    (    s3   lib/python2.7/site-packages/jedi/evaluate/finder.pys	   <genexpr>Ė   s    t   include_flows(   R   t	   from_setsR   RV   R   t   is_instancet   is_compiledRa   R   R   R    R   R   R*   t   anyt   TrueR   t   _check_flow_informationR   R   (   R   R3   R2   t   contextst
   flow_scopet
   base_nodesRZ   (    (    s3   lib/python2.7/site-packages/jedi/evaluate/finder.pyR,   ½   s&    & 		N(   t   __name__t
   __module__R   Rj   R%   R7   R:   RF   RE   R@   R(   Ra   R,   (    (    (    s3   lib/python2.7/site-packages/jedi/evaluate/finder.pyR   #   s   	 				+	c         C   s|  t  j s d Sd } t |  rų | j   } y | j   | j } Wn t k
 rV d SXt g  | D]7 } | j	 | j	 k o | p | j
 k  n ra | ^ qa  } xT | D]I } t | d  }	 |	 d k	 rØ t |  |	 j |  } | d k	 rń | SqØ qØ Wn  | j d k rxg  | j d d d  D] }
 |
 d k r|
 ^ q} x9 t |  D]( } | j	 | j
 k rIt |  | |  SqIWn  | S(	   s’    Try to find out the type of a variable just with the information that
    is given by the flows: e.g. It is also responsible for assert checks.::

        if isinstance(k, str):
            k.  # <- completion here

    ensures that `k` is a string.
    t   assert_stmtRJ   t
   while_stmti   Ni   t   :(   RJ   Rr   (   R   t   dynamic_flow_informationR   R   t   get_root_nodet   get_used_namesR   RQ   t   reversedR?   t   end_posR   t   _check_isinstance_typet	   assertionRP   R>   (   R    t   flowt   search_namet   post   resultt   module_nodeR3   RZ   R`   t   assRI   t   potential_ifst   if_test(    (    s3   lib/python2.7/site-packages/jedi/evaluate/finder.pyRk   Ų   s0    		:5c         C   s  y}| j  d k s t  t | j  d k s3 t  | j \ } } | j  d k r` | j d k sf t  | j  d k r | j d d k s t  t | j  d	 k s© t  | j d
 } t |  j |  | |  } t | j    } t |  d k sų t  | \ \ } }	 \ }
 } | d  k r(|
 d  k s.t  t
 j |  } t
 j |	 j  } |  j j j } | |  | |  k s|t  Wn t k
 rd  SXt } xu | j   D]g } t | t j  rü| j d k rüx= | j   D] } | | j   j   O} qŁWq„| | j   O} q„W| S(   Nt   powert	   atom_expri   R`   R   t   traileri    t   (i   i   t   tuple(   R   R   (   RP   t   AssertionErrorRS   R>   R   R   R   RW   t   unpackR   R   t   call_of_leaft   datat   grammart
   _normalizeR   Rb   R   R	   t   Sequencet
   array_typet
   py__iter__t   execute_evaluated(   R    t   elementR|   t   firstR   t   arglistt   argst
   param_listt   key1t   lazy_context_objectt   key2t   lazy_context_clst   callt   is_instance_callt	   normalizet   context_sett
   cls_or_tupt   lazy_context(    (    s3   lib/python2.7/site-packages/jedi/evaluate/finder.pyRy      s4    $("!N(!   t   __doc__t   parso.pythonR    t
   parso.treeR   t   jediR   R   t   jedi.evaluateR   R   R   t   jedi.evaluate.argumentsR   R   t   jedi.evaluate.contextR	   t   jedi.evaluate.filtersR
   t   jedi.evaluate.namesR   t   jedi.evaluate.base_contextR   R   t   jedi.parser_utilsR   R   t    jedi.evaluate.gradual.conversionR   t   objectR   Rk   Ry   (    (    (    s3   lib/python2.7/site-packages/jedi/evaluate/finder.pyt   <module>   s$   µ	(