ó
î%![c           @   sØ  d  Z  d d l m Z d d l Z d d l m Z d d l Z d d l m Z m	 Z	 m
 Z
 m Z m Z 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 Z d „  Z d „  Z e j d8 k r6e i e e d d ƒd 6e e d d ƒd 6ƒ n0 e i e e d d ƒd 6e e d d ƒd 6ƒ d „  Z d „  Z d „  Z d d d d „ Z e e d e j d e j  e j! e j" e j# e j$ e j% f d e& ƒZ' e e d e j  d e j e j! e j" e j# e j$ e j% f d e( ƒZ) e e d e j! d e j  e j e j" e j$ f d e* ƒZ+ e e d e j" d e j  e j e j! e j" e j$ f d e, ƒZ- d  „  Z. d d! „ Z/ d d" „ Z0 d# „  Z1 d d$ „ Z2 d d% „ Z3 d d& „ Z4 d d' „ Z5 d d( „ Z6 d d) „ Z7 d d* „ Z8 d+ „  Z9 e e5 d, ƒ e e0 d- ƒ e e4 d. ƒ e e2 d/ ƒ e e3 d0 ƒ e e' d1 ƒ e e+ d2 ƒ e e) d3 ƒ e e/ d4 ƒ e e- d5 ƒ e e6 d6 ƒ e e7 d7 ƒ e j: e j; e e8 ƒ e9 ƒ d S(9   s#   Astroid hooks for various builtins.iÿÿÿÿ(   t   partialN(   t   dedent(   t   MANAGERt   UseInferenceDefaultt   AttributeInferenceErrort   inference_tipt   InferenceErrort   NameInferenceError(   t	   arguments(   t   AstroidBuilder(   t   helpers(   t   nodes(   t   objects(   t   scoped_nodes(   t   utils   object.__new__c         C   sn   t  d ƒ } | j d | ƒ } t t ƒ j | ƒ d } x0 | j ƒ  D]" } | g |  j | j <|  | _ qD Wd S(   s,   function to extend builtin str/unicode classs$  
    class whatever(object):
        def join(self, iterable):
            return {rvalue}
        def replace(self, old, new, count=None):
            return {rvalue}
        def format(self, *args, **kwargs):
            return {rvalue}
        def encode(self, encoding='ascii', errors=None):
            return ''
        def decode(self, encoding='ascii', errors=None):
            return u''
        def capitalize(self):
            return {rvalue}
        def title(self):
            return {rvalue}
        def lower(self):
            return {rvalue}
        def upper(self):
            return {rvalue}
        def swapcase(self):
            return {rvalue}
        def index(self, sub, start=None, end=None):
            return 0
        def find(self, sub, start=None, end=None):
            return 0
        def count(self, sub, start=None, end=None):
            return 0
        def strip(self, chars=None):
            return {rvalue}
        def lstrip(self, chars=None):
            return {rvalue}
        def rstrip(self, chars=None):
            return {rvalue}
        def rjust(self, width, fillchar=None):
            return {rvalue}
        def center(self, width, fillchar=None):
            return {rvalue}
        def ljust(self, width, fillchar=None):
            return {rvalue}
    t   rvaluet   whateverN(	   R   t   formatR	   R   t   string_buildt	   mymethodst   localst   namet   parent(   t
   class_nodeR   t   codet   faket   method(    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt   _extend_str   s    (	c         C   sL   d d l  m } t j | } x( |  j ƒ  D] \ } } | | | ƒ q* Wd  S(   Niÿÿÿÿ(   t   BUILTINS(   t   astroid.basesR   R   t   astroid_cachet   items(   t   class_transformsR   t   builtin_astt
   class_namet	   transform(    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt   extend_builtinsQ   s    i   i    R   s   b''t   bytess   ''t   strs   u''t   unicodec            s;   d ‡ f d † } t j t j t | ƒ ‡  f d †  ƒ d S(   s    Register a new transform function for the given *builtin_name*.

    The transform function must accept two parameters, a node and
    an optional context.
    c            sU   ˆ  |  d | ƒ} | rH | j  s- |  | _  n  |  j | _ |  j | _ n  t | g ƒ S(   Nt   context(   R   t   linenot
   col_offsett   iter(   t   nodeR(   t   result(   R#   (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt   _transform_wrapperf   s    	c            s%   t  |  j t j ƒ o$ |  j j ˆ  k S(   N(   t
   isinstancet   funcR   t   NameR   (   t   n(   t   builtin_name(    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt   <lambda>u   s   N(   t   NoneR   t   register_transformR   t   CallR   (   R#   R3   R.   (    (   R3   R#   sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt   register_builtin_transform`   s    	c         C   sá   |  j  } | s | ƒ  St |  j  ƒ d k r7 t ƒ  ‚ n  | \ } | | ƒ } | s» y t | j d | ƒ ƒ } Wn  t t f k
 r t ƒ  ‚ n X| t j k r¬ t ƒ  ‚ n  | | ƒ } n  | sÑ | t j k rÝ t ƒ  ‚ n  | S(   Ni   R(   (	   t   argst   lenR   t   nextt   inferR   t   StopIterationR   t   Uninferable(   R,   R(   t	   node_typeR#   R9   t   argt   transformedt   inferred(    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt   _generic_inferencey   s$    		c         C   s  t  |  | ƒ r |  St  |  | ƒ ri t d „  |  j Dƒ ƒ sG t ƒ  ‚ n  g  |  j D] } | j ^ qQ } n t  |  t j ƒ rÆ t d „  |  j Dƒ ƒ s  t ƒ  ‚ n  g  |  j D] } | d j ^ qª } n@ t  |  t j ƒ rt  |  j t	 j
 t	 j f ƒ r|  j } n d  S| j d | | ƒ ƒ S(   Nc         s   s!   |  ] } t  | t j ƒ Vq d  S(   N(   R/   R   t   Const(   t   .0t   elt(    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pys	   <genexpr>“   s   c         s   s%   |  ] } t  | d  t j ƒ Vq d S(   i    N(   R/   R   RD   (   RE   RF   (    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pys	   <genexpr>š   s   i    t   elts(   R/   t   allRG   R   t   valueR   t   DictR   RD   t   sixt   string_typest   binary_typet   from_constants(   R@   t   klasst	   iterablest
   build_eltsRF   RG   t   item(    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt   _generic_transform   s"    	"	&c         C   s1   t  t d | d | d | ƒ} t |  | | | ƒ S(   NRO   RP   RQ   (   R    RS   RC   (   R,   R(   RO   RP   RQ   t   transform_func(    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt   _infer_builtin¦   s    	RO   RP   RQ   c         C   s  d „  } y t  |  j | ƒ ƒ } Wn  t t f k
 rD t ƒ  ‚ n Xt | t j ƒ rc | j } nµ | | ƒ rg  } x  | j	 D]‰ } | | ƒ s t ƒ  ‚ n  t
 | j	 ƒ d k r¾ t ƒ  ‚ n  t | j	 d t j t j t j f ƒ sò t ƒ  ‚ n  | j t | j	 ƒ ƒ q Wn	 t ƒ  ‚ | S(   Nc         S   s   t  |  t j t j t j f ƒ S(   N(   R/   R   t   Listt   Tuplet   Set(   R2   (    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyR4   Ò   s   i   i    (   R;   R<   R   R   R   R/   R   RJ   R   RG   R:   RW   RD   R1   t   appendt   tuple(   R@   R(   t   is_iterableRB   R   RF   (    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt	   _get_eltsÑ   s(    		c   
      C   sv  t  j j |  ƒ } | j ƒ  s* | j ƒ  r3 t ‚ n  | j } t | j j	 ƒ  ƒ } | ri | ri t
 j ƒ  S| r§ | r§ g  | D]! \ } } t
 j | ƒ | f ^ q} } n— t | ƒ d k r| rt | d | ƒ } g  | D]! \ } } t
 j | ƒ | f ^ qÙ }	 | |	 } n1 t | ƒ d k r5t | d | ƒ } n	 t ƒ  ‚ t
 j d |  j d |  j d |  j ƒ } | j | ƒ | S(   sC  Try to infer a dict call to a Dict node.

    The function treats the following cases:

        * dict()
        * dict(mapping)
        * dict(iterable)
        * dict(iterable, **kwargs)
        * dict(mapping, **kwargs)
        * dict(**kwargs)

    If a case can't be inferred, we'll fallback to default inference.
    i   i    R*   R)   R   (   R   t   CallSitet	   from_callt   has_invalid_argumentst   has_invalid_keywordsR   t   positional_argumentst   listt   keyword_argumentsR   R   RJ   RD   R:   R\   R*   R)   R   t   postinit(
   R,   R(   t   callR9   t   kwargst   keyRI   R   RG   t   keys(    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt
   infer_dictí   s*    		
1.		c      	   C   sz  t  |  j ƒ d k r t ‚ n  |  j ƒ  } t | t j ƒ sE t ‚ n  | j d
 k r] t ‚ n  t j	 | ƒ } t  |  j ƒ s¨ | } | j d k r™ | } q"| j
 ƒ  } nz y# t |  j d j d | ƒ ƒ } Wn t k
 rä t ‚ n Xy# t |  j d j d | ƒ ƒ } Wn t k
 r!t ‚ n X| t j k s@| t j k rIt ‚ n  t j d | d | d | d	 | ƒ } |  | _ | S(   s€  Understand super calls.

    There are some restrictions for what can be understood:

        * unbounded super (one argument form) is not understood.

        * if the super call is not inside a function (classmethod or method),
          then the default inference will be used.

        * if the super arguments can't be inferred, the default inference
          will be used.
    i   t   classmethodR   i    R(   t   mro_pointert   mro_typet
   self_classt   scope(   Rj   R   (   R:   R9   R   Rn   R/   R   t   FunctionDeft   typeR   t   get_wrapping_classt   instantiate_classR;   R<   R   R   R>   R   t   SuperR   (   R,   R(   Rn   t   clsRk   Rl   t	   super_obj(    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt   infer_super  s:    				#
#
			c         C   së   t  |  j ƒ d k r t ‚ n  yB t |  j d j d | ƒ ƒ } t |  j d j d | ƒ ƒ } Wn t k
 ry t ‚ n X| t j k s˜ | t j k r¨ t j t j f St | t	 j
 ƒ oÌ t | j t j ƒ } | sÞ t ‚ n  | | j f S(   Ni   i   i    R(   i   (   i   i   (   R:   R9   R   R;   R<   R   R   R>   R/   R   RD   RI   RK   RL   (   R,   R(   t   objt   attrt	   is_string(    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt   _infer_getattr_argsR  s    	#
	c         C   sá   t  |  | ƒ \ } } | t j k sC | t j k sC t | d ƒ rJ t j Sy t | j | d | ƒƒ SWnm t t t f k
 rÖ t	 |  j
 ƒ d k r× y! t |  j
 d j d | ƒ ƒ SWqÓ t k
 rÏ t ‚ qÓ Xq× n Xt ‚ d S(   sÀ   Understand getattr calls

    If one of the arguments is an Uninferable object, then the
    result will be an Uninferable object. Otherwise, the normal attribute
    lookup will be done.
    t   igetattrR(   i   i   N(   Rz   R   R>   t   hasattrR;   R{   R=   R   R   R:   R9   R<   R   (   R,   R(   Rw   Rx   (    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt   infer_getattrl  s    .!c         C   s    ya t  |  | ƒ \ } } | t j k sF | t j k sF t | d ƒ rM t j S| j | d | ƒWn/ t k
 rx t j St k
 r’ t j t	 ƒ SXt j t
 ƒ S(   sL  Understand hasattr calls

    This always guarantees three possible outcomes for calling
    hasattr: Const(False) when we are sure that the object
    doesn't have the intended attribute, Const(True) when
    we know that the object has the attribute and Uninferable
    when we are unsure of the outcome of the function call.
    t   getattrR(   (   Rz   R   R>   R|   R~   R   R   R   RD   t   Falset   True(   R,   R(   Rw   Rx   (    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt   infer_hasattr„  s    	.c         C   sˆ   t  |  j ƒ d k r t ‚ n  |  j d } y t | j d | ƒ ƒ } Wn t k
 r^ t j SX| t j k ru t j St j	 | j
 ƒ  ƒ S(   sÑ   Understand callable calls

    This follows Python's semantics, where an object
    is callable if it provides an attribute __call__,
    even though that attribute is something which can't be
    called.
    i   i    R(   (   R:   R9   R   R;   R<   R   R   R>   R   RD   t   callable(   R,   R(   t   argumentRB   (    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt   infer_callable›  s    	c         C   sº   t  |  j ƒ d k r t ‚ n  |  j s4 t j t ƒ S|  j d } y t | j d | ƒ ƒ } Wn t k
 rt t	 j
 SX| t	 j
 k r‹ t	 j
 S| j ƒ  } | t	 j
 k r­ t	 j
 St j | ƒ S(   s   Understand bool calls.i   i    R(   (   R:   R9   R   R   RD   R   R;   R<   R   R   R>   t
   bool_value(   R,   R(   Rƒ   RB   R…   (    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt
   infer_bool±  s    		c         C   s5   t  |  j ƒ d k r t ‚ n  t j |  j d | ƒ S(   s+   Understand the one-argument form of *type*.i   i    (   R:   R9   R   R
   t   object_type(   R,   R(   (    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt
   infer_typeÈ  s    	c         C   s)  |  j  } d t | ƒ k  o& d k n s4 t ‚ n  t t t j | ƒ ƒ } xo | D]g } | so | t j k rx t ‚ n  t	 | t
 j ƒ s“ t ‚ n  t	 | j t d ƒ t f ƒ sS t ‚ qS qS Wt | ƒ d k  rñ | j d g d t | ƒ ƒ n  t
 j d |  j d |  j d |  j ƒ } | j | Œ  | S(   s   Understand `slice` calls.i    i   R)   R*   R   N(   R9   R:   R   Rb   t   mapR
   t
   safe_inferR   R>   R/   R   RD   RI   Rp   R5   t   intt   extendt   SliceR)   R*   R   Rd   (   R,   R(   R9   R@   t
   slice_node(    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt   infer_sliceÐ  s$    	"			!	c         C   s   t  |  j ƒ  f ƒ S(   N(   R+   Rr   (   R,   R(   (    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt   _infer_object__new__decoratorê  s    c         C   sS   |  j  s t Sx? |  j  j D]1 } t | t j ƒ r | j ƒ  t k rK t Sq q Wt S(   sd   Predicate before inference_tip

    Check if the given ClassDef has a @object.__new__ decorator
    (   t
   decoratorsR   R   R/   t	   Attributet	   as_stringt   OBJECT_DUNDER_NEWR€   (   R,   t	   decorator(    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt#   _infer_object__new__decorator_checkð  s    	t   boolt   superR‚   R~   R|   RZ   t   setRb   t   dictt	   frozensetRp   t   slice(   i   i    (<   t   __doc__t	   functoolsR    t   syst   textwrapR   RK   t   astroidR   R   R   R   R   R   R   t   astroid.builderR	   R
   R   R   R   R   R”   R   R$   t   version_infoR8   RC   RS   R5   RU   RW   RV   RX   t	   FrozenSett	   DictItemst   DictKeyst
   DictValuesRZ   t   infer_tupleRb   t
   infer_listR™   t	   infer_setR›   t   infer_frozensetR\   Ri   Rv   Rz   R}   R   R„   R†   Rˆ   R   R   R–   R6   t   ClassDef(    (    (    sD   lib/python2.7/site-packages/astroid/brain/brain_builtin_inference.pyt   <module>   sž   .	5				
									,9			