3
‡ŒðiV  ã               @   sn   d 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ZG dd„ dejƒZd	d
„ Zdd„ Zdd„ ZdS )a  
Based on fix_next.py by Collin Winter.

Replaces it.next() -> next(it), per PEP 3114.

Unlike fix_next.py, this fixer doesn't replace the name of a next method with __next__,
which would break Python 2 compatibility without further help from fixers in
stage 2.
é    )Útoken)Úpython_symbols)Ú
fixer_base)ÚNameÚCallÚfind_bindingz;Calls to builtin next() possibly shadowed by global bindingc                   s0   e Zd ZdZdZdZ‡ fdd„Zdd„ Z‡  ZS )ÚFixNextCallTzÓ
    power< base=any+ trailer< '.' attr='next' > trailer< '(' ')' > >
    |
    power< head=any+ trailer< '.' attr='next' > not trailer< '(' ')' > >
    |
    global=global_stmt< 'global' any* 'next' any* >
    Úprec                s>   t t| ƒj||ƒ td|ƒ}|r4| j|tƒ d| _nd| _d S )NÚnextTF)Úsuperr   Ú
start_treer   ÚwarningÚbind_warningÚshadowed_next)ÚselfÚtreeÚfilenameÚn)Ú	__class__© úA/tmp/pip-build-6m2fasei/future/libfuturize/fixes/fix_next_call.pyr       s    
zFixNextCall.start_treec             C   sÌ   |st ‚|jdƒ}|jdƒ}|jdƒ}|rf| jr2qÈdd„ |D ƒ}d|d _|jttd|jd	�|ƒƒ nb|rln\|r®t|ƒrÈ|d
 }djdd„ |D ƒƒj	ƒ dkr¨| j
|tƒ d S nd|krÈ| j
|tƒ d| _d S )NÚbaseÚattrÚnamec             S   s   g | ]}|j ƒ ‘qS r   )Úclone)Ú.0r   r   r   r   ú
<listcomp>7   s    z)FixNextCall.transform.<locals>.<listcomp>Ú r   r
   )ÚprefixÚheadc             S   s   g | ]}t |ƒ‘qS r   )Ústr)r   r   r   r   r   r   E   s    Ú__builtin__ÚglobalT)ÚAssertionErrorÚgetr   r   Úreplacer   r   Úis_assign_targetÚjoinÚstripr   r   )r   ÚnodeÚresultsr   r   r   r   r   r   r   Ú	transform*   s*    



zFixNextCall.transform)	Ú__name__Ú
__module__Ú__qualname__ZBM_compatibleZPATTERNÚorderr   r+   Ú__classcell__r   r   )r   r   r      s
   
r   c             C   sF   t | ƒ}|d krdS x,|jD ]"}|jtjkr0dS t|| ƒrdS qW dS )NFT)Úfind_assignÚchildrenÚtyper   ÚEQUALÚ
is_subtree)r)   ZassignÚchildr   r   r   r&   R   s    
r&   c             C   s4   | j tjkr| S | j tjks&| jd kr*d S t| jƒS )N)r3   ÚsymsZ	expr_stmtZsimple_stmtÚparentr1   )r)   r   r   r   r1   ^   s
    r1   c                s$   | ˆ krdS t ‡ fdd„| jD ƒƒS )NTc             3   s   | ]}t |ˆ ƒV  qd S )N)r5   )r   Úc)r)   r   r   ú	<genexpr>h   s    zis_subtree.<locals>.<genexpr>)Úanyr2   )Úrootr)   r   )r)   r   r5   e   s    r5   N)Ú__doc__Zlib2to3.pgen2r   Zlib2to3.pygramr   r7   Zlib2to3r   Zlib2to3.fixer_utilr   r   r   r   ZBaseFixr   r&   r1   r5   r   r   r   r   Ú<module>	   s   >