1# Copyright 2006 Georg Brandl. 2# Licensed to PSF under a Contributor Agreement. 3 4"""Fixer for intern(). 5 6intern(s) -> sys.intern(s)""" 7 8# Local imports 9from .. import pytree 10from .. import fixer_base 11from ..fixer_util import Name, Attr, touch_import 12 13 14class FixIntern(fixer_base.BaseFix): 15 BM_compatible = True 16 order = "pre" 17 18 PATTERN = """ 19 power< 'intern' 20 trailer< lpar='(' 21 ( not(arglist | argument<any '=' any>) obj=any 22 | obj=arglist<(not argument<any '=' any>) any ','> ) 23 rpar=')' > 24 after=any* 25 > 26 """ 27 28 def transform(self, node, results): 29 if results: 30 # I feel like we should be able to express this logic in the 31 # PATTERN above but I don't know how to do it so... 32 obj = results['obj'] 33 if obj: 34 if obj.type == self.syms.star_expr: 35 return # Make no change. 36 if (obj.type == self.syms.argument and 37 obj.children[0].value == '**'): 38 return # Make no change. 39 syms = self.syms 40 obj = results["obj"].clone() 41 if obj.type == syms.arglist: 42 newarglist = obj.clone() 43 else: 44 newarglist = pytree.Node(syms.arglist, [obj.clone()]) 45 after = results["after"] 46 if after: 47 after = [n.clone() for n in after] 48 new = pytree.Node(syms.power, 49 Attr(Name(u"sys"), Name(u"intern")) + 50 [pytree.Node(syms.trailer, 51 [results["lpar"].clone(), 52 newarglist, 53 results["rpar"].clone()])] + after) 54 new.prefix = node.prefix 55 touch_import(None, u'sys', node) 56 return new 57