U if @svdZddlmZddlmZddlmZmZmZddZ ddZ d d Z d d Z d dZ ddZGdddejZdS)aFixer for __metaclass__ = X -> (metaclass=X) methods. The various forms of classef (inherits nothing, inherits once, inherits 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. ) fixer_base)token)symsNodeLeafcCsz|jD]n}|jtjkr"t|S|jtjkr|jr|jd}|jtjkr|jr|jd}t|tr|j dkrdSqdS)z we have to check the cls_node without changing it. There are two possibilities: 1) clsdef => suite => simple_stmt => expr_stmt => Leaf('__meta') 2) clsdef => simple_stmt => expr_stmt => Leaf('__meta') __metaclass__TF) childrentypersuite has_metaclass simple_stmt expr_stmt isinstancervalue)parentnode expr_nodeZ left_sider@/opt/alt/python38/lib64/python3.8/lib2to3/fixes/fix_metaclass.pyr s      r cCs|jD]}|jtjkrdSqt|jD]\}}|jtjkr(qJq(tdttjg}|j|ddr|j|d}| | | qV| ||}dS)zf one-line classes don't get a suite in the parse tree so we add one to normalize the tree NzNo class suite and no ':'!) r r rr enumeraterCOLON ValueErrorr append_childcloneremove)cls_noderir move_noderrrfixup_parse_tree-s      r c Cst|jD]\}}|jtjkr q(q dS|ttjg}ttj |g}|j|drz|j|}| | |qJ| |||jdjd}|jdjd} | j |_ dS)z 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 Nr)rr r rSEMIrrrrr rr insert_childprefix) rrZ stmt_nodeZsemi_indrZnew_exprZnew_stmtrZ new_leaf1Z old_leaf1rrrfixup_simple_stmtGs     r$cCs*|jr&|jdjtjkr&|jddS)N)r r rNEWLINEr)rrrrremove_trailing_newline_sr'ccs|jD]}|jtjkrq$qtdtt|jD]t\}}|jtjkr2|jr2|jd}|jtjkr2|jr2|jd}t |t r2|j dkr2t |||t ||||fVq2dS)NzNo class suite!rr)r r rr rlistrr rrrrr$r')rrrZ simple_noderZ left_noderrr find_metasds      r)cCsz|jddd}|r,|}|jtjkrq,q|rv|}t|tr^|jtjkr^|jrZd|_dS| |jdddq,dS)z 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 Nr%) r popr rINDENTrrDEDENTr#extend)r Zkidsrrrr fixup_indent{s r/c@seZdZdZdZddZdS) FixMetaclassTz classdef cCsJt|s dSt|d}t|D]\}}}|}|q |jdj}t|jdkr|jdjtjkrp|jd}n(|jd } t tj| g}| d|nt|jdkrt tjg}| d|nZt|jdkrt tjg}| dt tjd| d|| dt tjdntd |jdjd} d | _| j} |jrZ|t tjd d | _nd | _|jd} | jtjks|td | jd_d | jd_||t||js|t |d} | | _|| |t tjdnbt|jdkrF|jdjtjkrF|jdjtjkrFt |d} | d| | dt tjddS)Nrr)(zUnexpected class definition metaclass, r*rpass r%)r r r)rr r lenrarglistrrZ set_childr"rrRPARLPARrrr#rCOMMArAssertionErrorr/r&r,r-)selfrZresultsZlast_metaclassr rZstmtZ text_typer>rZmeta_txtZorig_meta_prefixrZ pass_leafrrr transformsd              zFixMetaclass.transformN)__name__ __module__ __qualname__Z BM_compatibleZPATTERNrDrrrrr0sr0N)__doc__r*rZpygramrZ fixer_utilrrrr r r$r'r)r/ZBaseFixr0rrrrs