PageRenderTime 28ms CodeModel.GetById 18ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/lib2to3/fixes/fix_intern.py

http://unladen-swallow.googlecode.com/
Python | 44 lines | 33 code | 5 blank | 6 comment | 0 complexity | 00e20c0723e807004c3fd0ae88d26b09 MD5 | raw file
 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
16    PATTERN = """
17    power< 'intern'
18           trailer< lpar='('
19                    ( not(arglist | argument<any '=' any>) obj=any
20                      | obj=arglist<(not argument<any '=' any>) any ','> )
21                    rpar=')' >
22           after=any*
23    >
24    """
25
26    def transform(self, node, results):
27        syms = self.syms
28        obj = results["obj"].clone()
29        if obj.type == syms.arglist:
30            newarglist = obj.clone()
31        else:
32            newarglist = pytree.Node(syms.arglist, [obj.clone()])
33        after = results["after"]
34        if after:
35            after = [n.clone() for n in after]
36        new = pytree.Node(syms.power,
37                          Attr(Name("sys"), Name("intern")) +
38                          [pytree.Node(syms.trailer,
39                                       [results["lpar"].clone(),
40                                        newarglist,
41                                        results["rpar"].clone()])] + after)
42        new.set_prefix(node.get_prefix())
43        touch_import(None, 'sys', node)
44        return new