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        syms = self.syms
30        obj = results["obj"].clone()
31        if obj.type == syms.arglist:
32            newarglist = obj.clone()
33        else:
34            newarglist = pytree.Node(syms.arglist, [obj.clone()])
35        after = results["after"]
36        if after:
37            after = [n.clone() for n in after]
38        new = pytree.Node(syms.power,
39                          Attr(Name(u"sys"), Name(u"intern")) +
40                          [pytree.Node(syms.trailer,
41                                       [results["lpar"].clone(),
42                                        newarglist,
43                                        results["rpar"].clone()])] + after)
44        new.prefix = node.prefix
45        touch_import(None, u'sys', node)
46        return new
47