Files
PowerToys/PythonHome/Lib/lib2to3/fixes/fix_metaclass.pyc

72 lines
6.3 KiB
Plaintext
Raw Normal View History

2014-07-09 18:15:23 +08:00
<03>
<EFBFBD>W`Sc@s<>dZddlmZddlmZddlmZmZmZm Z d<00>Z
d<00>Z d<00>Z d<00>Z d <00>Zd
<00>Zd ejfd <00><00>YZd S(s<>Fixer for __metaclass__ = X -> (metaclass=X) methods.
The various forms of classef (inherits nothing, inherits once, inherints
many) don't parse the same in the CST so we look at ALL classes for
a __metaclass__ and if we find one normalize the inherits to all be
an arglist.
For one-liner classes ('class X: pass') there is no indent/dedent so
we normalize those into having a suite.
Moving the __metaclass__ into the classdef can also cause the class
body to be empty so there is some special casing for that as well.
This fixer also tries very hard to keep original indenting and spacing
in all those corner cases.
i(t
fixer_base(ttoken(tNametsymstNodetLeafcCs<>x<>|jD]<5D>}|jtjkr,t|<00>S|jtjkr
|jr
|jd}|jtjkr<>|jr<>|jd}t|t<00>r<>|j dkr<>t
Sq<53>q
q
Wt S(s<> we have to check the cls_node without changing it.
There are two possiblities:
1) clsdef => suite => simple_stmt => expr_stmt => Leaf('__meta')
2) clsdef => simple_stmt => expr_stmt => Leaf('__meta')
it __metaclass__( tchildrenttypeRtsuitet has_metaclasst simple_stmtt expr_stmtt
isinstanceRtvaluetTruetFalse(tparenttnodet expr_nodet left_side((s$.\Lib\lib2to3\fixes\fix_metaclass.pyR
s
  cCs<>x'|jD]}|jtjkr
dSq
Wx?t|j<00>D]"\}}|jtjkr:Pq:q:Wtd<00><00>ttjg<00>}xC|j|dr<>|j|d}|j |j
<00><00>|j <00>q<>W|j |<00>|}dS(sf one-line classes don't get a suite in the parse tree so we add
one to normalize the tree
NsNo class suite and no ':'!i( RRRR t enumerateRtCOLONt
ValueErrorRt append_childtclonetremove(tcls_nodeRtiR t move_node((s$.\Lib\lib2to3\fixes\fix_metaclass.pytfixup_parse_tree-s  c
Cs<>x7t|j<00>D]"\}}|jtjkrPqqWdS|j<00>ttjg<00>}ttj |g<00>}x;|j|r<>|j|}|j
|j <00><00>|j<00>qnW|j ||<00>|jdjd}|jdjd} | j |_ dS(s<> if there is a semi-colon all the parts count as part of the same
simple_stmt. We just want the __metaclass__ part so we move
everything after the semi-colon into its own simple_stmt node
Ni(RRRRtSEMIRRRR R RRt insert_childtprefix(
RRt stmt_nodetsemi_indRtnew_exprtnew_stmtRt new_leaf1t old_leaf1((s$.\Lib\lib2to3\fixes\fix_metaclass.pytfixup_simple_stmtGs
 cCs:|jr6|jdjtjkr6|jdj<00>ndS(Ni<4E><69><EFBFBD><EFBFBD>(RRRtNEWLINER(R((s$.\Lib\lib2to3\fixes\fix_metaclass.pytremove_trailing_newline_s"ccs<>x3|jD]}|jtjkr
Pq
q
Wtd<00><00>x<>tt|j<00><00>D]<5D>\}}|jtjkrL|jrL|jd}|jtjkr<>|jr<>|jd}t |t
<00>r<>|j dkr<>t |||<00>t |<00>|||fVq<>q<>qLqLWdS(NsNo class suite!iu __metaclass__(RRRR RtlistRR R R RRR(R*(RRRt simple_nodeRt left_node((s$.\Lib\lib2to3\fixes\fix_metaclass.pyt
find_metasds "  
cCs<>|jddd<00>}x,|rD|j<00>}|jtjkrPqqWxm|r<>|j<00>}t|t<00>r<>|jtjkr<>|jr<>d|_ndS|j |jddd<00><19>qHWdS(s<> If an INDENT is followed by a thing with a prefix then nuke the prefix
Otherwise we get in trouble when removing __metaclass__ at suite start
Ni<4E><69><EFBFBD><EFBFBD>u(
RtpopRRtINDENTR RtDEDENTR!textend(R tkidsR((s$.\Lib\lib2to3\fixes\fix_metaclass.pyt fixup_indent{s    !  t FixMetaclasscBseZeZdZd<00>ZRS(s
classdef<any*>
cCs't|<00>sdSt|<00>d}x-t|<00>D]\}}}|}|j<00>q-W|jdj}t|j<00>dkr<>|jdjtj kr<>|jd}q<>|jdj
<00>} t tj | g<00>}|j d|<00>n<>t|j<00>dkrt tj g<00>}|j d|<00>n~t|j<00>dkr<>t tj g<00>}|j dttjd<00><00>|j d|<00>|j dttjd<00><00>n td <00><00>|jdjd}
d
|
_|
j} |jr<>|jttjd <00><00>d |
_n d |
_|jd} | jtjkst<00>d | jd_d | jd_|j|<00>t|<00>|js<>|j<00>t|d<00>} | | _|j| <00>|jttjd<00><00>n<>t|j<00>dkr#|jdjtjkr#|jdjtjkr#t|d<00>} |j d| <00>|j dttjd<00><00>ndS(Niiiiiiu)u(sUnexpected class definitiont metaclassu,u uiupassu
i<EFBFBD><EFBFBD><EFBFBD><EFBFBD>i<EFBFBD><EFBFBD><EFBFBD><EFBFBD>(R
RtNoneR.RRRtlenRtarglistRRt set_childR RRtRPARtLPARRRR!RtCOMMAR tAssertionErrorR4R)R0R1(tselfRtresultstlast_metaclassR Rtstmtt text_typeR9Rtmeta_txttorig_meta_prefixR t pass_leaf((s$.\Lib\lib2to3\fixes\fix_metaclass.pyt transform<72>s` 
        
 
  (t__name__t
__module__Rt BM_compatibletPATTERNRG(((s$.\Lib\lib2to3\fixes\fix_metaclass.pyR5<00>sN(t__doc__tRtpygramRt
fixer_utilRRRRR
RR(R*R.R4tBaseFixR5(((s$.\Lib\lib2to3\fixes\fix_metaclass.pyt<module>s"