3
‡Œði/  ã               @   s|   d Z ddlZddlmZmZmZ ddlmZ ddlm	Z	m
Z
mZmZ dd„ ZejdƒZd	d
„ Zdd„ ZG dd„ dejƒZdS )aL  
For the ``future`` package.

Adds this import line:

    from __future__ import division

at the top and changes any old-style divisions to be calls to
past.utils.old_div so the code runs as before on Py2.6/2.7 and has the same
behaviour on Py3.

If "from __future__ import division" is already in effect, this fixer does
nothing.
é    N)ÚLeafÚNodeÚComma)Ú
fixer_base)ÚtokenÚfuture_importÚtouch_import_topÚwrap_in_fn_callc             C   s,   t j}| j|ko*| jj|k o*| jj|k S )zw
    __future__.division redefines the meaning of a single slash for division,
    so we match that and only that.
    )r   ÚSLASHÚtypeÚnext_siblingÚprev_sibling)ÚnodeZslash© r   úE/tmp/pip-build-6m2fasei/future/libfuturize/fixes/fix_division_safe.pyÚmatch_division   s    r   z^[0-9]*[.][0-9]*$c             C   s   t | jƒpt | jƒS )N)Ú
_is_floatyr   r   )r   r   r   r   Ú	is_floaty"   s    r   c             C   sV   t | tƒr| d } t | tƒr(tj| jƒS t | tƒrRt | jd tƒrR| jd jdkS dS )Nr   ÚfloatF)Ú
isinstanceÚlistr   Úconst_reÚmatchÚvaluer   Úchildren)Úexprr   r   r   r   &   s    


r   c                   s:   e Zd ZdZejZdZ‡ fdd„Zdd„ Z	dd„ Z
‡  ZS )	ÚFixDivisionSafeé   z4
    term<(not('/') any)+ '/' ((not('/') any))>
    c                s"   t t| ƒj||ƒ d|jk| _dS )zO
        Skip this fixer if "__future__.division" is already imported.
        ÚdivisionN)Úsuperr   Ú
start_treeZfuture_featuresÚskip)ÚselfÚtreeÚname)Ú	__class__r   r   r    >   s    zFixDivisionSafe.start_treec             C   sº   |j | jjkr¶d}d}g }xn|jD ]d}|r0d}q"t|ƒrxt|ƒ rxd}d|d _td|tƒ |j	j
ƒ g |jd�g}d}q"|j|j
ƒ ƒ q"W |r¶ttdƒrªt|j ||jd�S t|j |ƒS dS )	z™
        Since the tree needs to be fixed once and only once if and only if it
        matches, we can start discarding matches after the first.
        FTÚ r   Úold_div)ÚprefixÚfixers_applied)r)   )r   ZsymsZtermr   r   r   r(   r	   r   r   ÚcloneÚappendÚhasattrr   r)   )r"   r   Úmatchedr!   r   Úchildr   r   r   r   E   s*    

zFixDivisionSafe.matchc             C   s$   | j r
d S td|ƒ tdd|ƒ |S )Nr   z
past.utilsr'   )r!   r   r   )r"   r   Úresultsr   r   r   Ú	transformh   s
    
zFixDivisionSafe.transform)Ú__name__Ú
__module__Ú__qualname__Z	run_orderr   r
   Z_accept_typeZPATTERNr    r   r0   Ú__classcell__r   r   )r%   r   r   4   s   #r   )Ú__doc__ÚreZlib2to3.fixer_utilr   r   r   Zlib2to3r   Zlibfuturize.fixer_utilr   r   r   r	   r   Úcompiler   r   r   ZBaseFixr   r   r   r   r   Ú<module>   s   	
