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 fixer_base
10from ..fixer_util import ImportAndCall, touch_import
11
12
13class FixIntern(fixer_base.BaseFix):
14    BM_compatible = True
15    order = "pre"
16
17    PATTERN = """
18    power< 'intern'
19           trailer< lpar='('
20                    ( not(arglist | argument<any '=' any>) obj=any
21                      | obj=arglist<(not argument<any '=' any>) any ','> )
22                    rpar=')' >
23           after=any*
24    >
25    """
26
27    def transform(self, node, results):
28        if results:
29            # I feel like we should be able to express this logic in the
30            # PATTERN above but I don't know how to do it so...
31            obj = results['obj']
32            if obj:
33                if (obj.type == self.syms.argument and
34                    obj.children[0].value in {'**', '*'}):
35                    return  # Make no change.
36        names = ('sys', 'intern')
37        new = ImportAndCall(node, results, names)
38        touch_import(None, 'sys', node)
39        return new
40