annotate Lib/lib2to3/fixes/fix_isinstance.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents b36cb4602e21
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
58737
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
1 # Copyright 2008 Armin Ronacher.
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
2 # Licensed to PSF under a Contributor Agreement.
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
3
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
4 """Fixer that cleans up a tuple argument to isinstance after the tokens
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
5 in it were fixed. This is mainly used to remove double occurrences of
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
6 tokens as a leftover of the long -> int / unicode -> str conversion.
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
7
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
8 eg. isinstance(x, (int, long)) -> isinstance(x, (int, int))
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
9 -> isinstance(x, int)
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
10 """
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
11
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
12 from .. import fixer_base
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
13 from ..fixer_util import token
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
14
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
15
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
16 class FixIsinstance(fixer_base.BaseFix):
65340
7d1e453b120b Merged revisions 83852-83853,83857,84042,84216,84274-84276,84375,85388,85478,85506-85508 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 54696
diff changeset
17 BM_compatible = True
58737
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
18 PATTERN = """
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
19 power<
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
20 'isinstance'
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
21 trailer< '(' arglist< any ',' atom< '('
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
22 args=testlist_gexp< any+ >
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
23 ')' > > ')' >
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
24 >
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
25 """
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
26
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
27 run_order = 6
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
28
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
29 def transform(self, node, results):
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
30 names_inserted = set()
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
31 testlist = results["args"]
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
32 args = testlist.children
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
33 new_args = []
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
34 iterator = enumerate(args)
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
35 for idx, arg in iterator:
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
36 if arg.type == token.NAME and arg.value in names_inserted:
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
37 if idx < len(args) - 1 and args[idx + 1].type == token.COMMA:
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
38 iterator.next()
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
39 continue
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
40 else:
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
41 new_args.append(arg)
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
42 if arg.type == token.NAME:
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
43 names_inserted.add(arg.value)
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
44 if new_args and new_args[-1].type == token.COMMA:
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
45 del new_args[-1]
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
46 if len(new_args) == 1:
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
47 atom = testlist.parent
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
48 new_args[0].prefix = atom.prefix
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
49 atom.replace(new_args[0])
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
50 else:
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
51 args[:] = new_args
fab380674959 copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
52 node.changed()