3 jf/@s|dZddlZddlmZmZmZddlmZddlm Z m Z m Z m Z ddZ ejdZd d Zd d ZGd ddejZdS)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)LeafNodeComma) fixer_base)token future_importtouch_import_topwrap_in_fn_callcCs,tj}|j|ko*|jj|k o*|jj|k S)zw __future__.division redefines the meaning of a single slash for division, so we match that and only that. )rSLASHtype next_sibling prev_sibling)nodeZslashrE/tmp/pip-build-s1i5078x/future/libfuturize/fixes/fix_division_safe.pymatch_divisionsrz^[0-9]*[.][0-9]*$cCst|jpt|jS)N) _is_floatyr r )rrrr is_floaty"srcCsVt|tr|d}t|tr(tj|jSt|trRt|jdtrR|jdjdkSdS)NrfloatF) isinstancelistrconst_rematchvaluerchildren)exprrrrr&s    rcs:eZdZdZejZdZfddZddZ ddZ Z S) FixDivisionSafez4 term<(not('/') any)+ '/' ((not('/') any))> cs"tt|j||d|jk|_dS)zO Skip this fixer if "__future__.division" is already imported. divisionN)superr start_treeZfuture_featuresskip)selftreename) __class__rrr >szFixDivisionSafe.start_treecCs|j|jjkrd}d}g}xn|jD]d}|r0d}q"t|rxt| rxd}d|d_td|t|j j g|jdg}d}q"|j |j q"W|rt t drt |j||jdSt |j|SdS) 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. FTrold_div)prefixfixers_applied)r))r ZsymsZtermrrrr(r rr cloneappendhasattrrr))r"rmatchedr!rchildrrrrEs*     zFixDivisionSafe.matchcCs$|jr dStd|tdd||S)Nrz past.utilsr')r!rr)r"rresultsrrr transformhs   zFixDivisionSafe.transform) __name__ __module__ __qualname__Z run_orderrr Z _accept_typeZPATTERNr rr0 __classcell__rr)r%rr4s  #r)__doc__reZlib2to3.fixer_utilrrrZlib2to3rZlibfuturize.fixer_utilrrrr rcompilerrrZBaseFixrrrrrs