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