ó
ĊZc           @   s[   d  Z  d d l m Z m Z m Z d   Z d e f d     YZ d e f d     YZ d S(	   s  Finding bad name and attribute accesses

`find_errors` function can be used to find possible bad name and
attribute accesses.  As an example::

  errors = find_errors(project, project.get_resource('mod.py'))
  for error in errors:
      print('%s: %s' % (error.lineno, error.error))

prints possible errors for ``mod.py`` file.

TODO:

* use task handles
* reporting names at most once
* attributes of extension modules that don't appear in
  extension_modules project config can be ignored
* not calling `PyScope.get_inner_scope_for_line()` if it is a
  bottleneck; needs profiling
* not reporting occurrences where rope cannot infer the object
* rope saves multiple objects for some of the names in its objectdb
  use all of them not to give false positives
* ... ;-)

i˙˙˙˙(   t   astt   evaluatet	   pyobjectsc         C   s8   |  j  |  } t |  } t j | j   |  | j S(   sW   Find possible bad name and attribute accesses

    It returns a list of `Error`\s.
    (   t   get_pymodulet   _BadAccessFinderR    t   walkt   get_astt   errors(   t   projectt   resourcet   pymodulet   finder(    (    s6   lib/python2.7/site-packages/rope/contrib/finderrors.pyt   find_errors   s    R   c           B   s5   e  Z d    Z d   Z d   Z d   Z d   Z RS(   c         C   s%   | |  _  | j   |  _ g  |  _ d  S(   N(   R
   t	   get_scopet   scopeR   (   t   selfR
   (    (    s6   lib/python2.7/site-packages/rope/contrib/finderrors.pyt   __init__*   s    	c         C   s   t  | j t j t j f  r" d  S|  j j | j  } | j | j	  } | d  k rh |  j | d  n+ |  j | | | j  r |  j | d  n  d  S(   Ns   Unresolved variables   Defined later(   t
   isinstancet   ctxR    t   Storet   ParamR   t   get_inner_scope_for_linet   linenot   lookupt   idt   Nonet
   _add_errort   _is_defined_after(   R   t   nodeR   t   pyname(    (    s6   lib/python2.7/site-packages/rope/contrib/finderrors.pyt   _Name/   s    c         C   s¨   t  | j t j  s |  j j | j  } t j | | j	  } | d  k	 r | j   t j   k r | j | j   k r |  j | d  q q n  t j | j	 |   d  S(   Ns   Unresolved attribute(   R   R   R    R   R   R   R   R   t	   eval_nodet   valueR   t
   get_objectR   t   get_unknownt   attrR   R   (   R   R   R   R   (    (    s6   lib/python2.7/site-packages/rope/contrib/finderrors.pyt
   _Attribute9   s    c         C   sd   t  | t j  r | j } n	 | j } | d k r` t | j | d |  } |  j j |  n  d  S(   NR   t    (	   R   R    t	   AttributeR#   R   t   ErrorR   R   t   append(   R   R   t   msgt   namet   error(    (    s6   lib/python2.7/site-packages/rope/contrib/finderrors.pyR   C   s    	c         C   sl   | j    } | d  k	 rh | d d  k	 rh | d |  j k rh | | d k o\ | j   k n rh t Sn  d  S(   Ni   i    (   t   get_definition_locationR   R
   t   get_endt   True(   R   R   R   R   t   location(    (    s6   lib/python2.7/site-packages/rope/contrib/finderrors.pyR   L   s
    &(   t   __name__t
   __module__R   R   R$   R   R   (    (    (    s6   lib/python2.7/site-packages/rope/contrib/finderrors.pyR   (   s
   		
	
		R'   c           B   s   e  Z d    Z d   Z RS(   c         C   s   | |  _  | |  _ d  S(   N(   R   R+   (   R   R   R+   (    (    s6   lib/python2.7/site-packages/rope/contrib/finderrors.pyR   V   s    	c         C   s   d |  j  |  j f S(   Ns   %s: %s(   R   R+   (   R   (    (    s6   lib/python2.7/site-packages/rope/contrib/finderrors.pyt   __str__Z   s    (   R0   R1   R   R2   (    (    (    s6   lib/python2.7/site-packages/rope/contrib/finderrors.pyR'   T   s   	N(	   t   __doc__t	   rope.baseR    R   R   R   t   objectR   R'   (    (    (    s6   lib/python2.7/site-packages/rope/contrib/finderrors.pyt   <module>   s   	,