mirror of
https://github.com/python/cpython.git
synced 2025-10-23 18:03:48 +00:00
svn+ssh://pythondev@svn.python.org/python/trunk
................
r72368 | benjamin.peterson | 2009-05-05 18:13:58 -0500 (Tue, 05 May 2009) | 53 lines
Merged revisions 68503,68507,68694,69054,69673,69679-69681,70991,70999,71003,71695 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r68503 | benjamin.peterson | 2009-01-10 14:14:49 -0600 (Sat, 10 Jan 2009) | 1 line
use variable
........
r68507 | benjamin.peterson | 2009-01-10 15:13:16 -0600 (Sat, 10 Jan 2009) | 1 line
rewrap
........
r68694 | benjamin.peterson | 2009-01-17 17:55:59 -0600 (Sat, 17 Jan 2009) | 1 line
test for specific node type
........
r69054 | guilherme.polo | 2009-01-28 10:01:54 -0600 (Wed, 28 Jan 2009) | 2 lines
Added mapping for the ttk module.
........
r69673 | benjamin.peterson | 2009-02-16 09:38:22 -0600 (Mon, 16 Feb 2009) | 1 line
fix handling of as imports #5279
........
r69679 | benjamin.peterson | 2009-02-16 11:36:06 -0600 (Mon, 16 Feb 2009) | 1 line
make Base.get_next_sibling() and Base.get_prev_sibling() properties
........
r69680 | benjamin.peterson | 2009-02-16 11:41:48 -0600 (Mon, 16 Feb 2009) | 1 line
normalize docstrings in pytree according to PEP 11
........
r69681 | benjamin.peterson | 2009-02-16 11:43:09 -0600 (Mon, 16 Feb 2009) | 1 line
use a set
........
r70991 | benjamin.peterson | 2009-04-01 15:54:50 -0500 (Wed, 01 Apr 2009) | 1 line
map urllib.urlopen to urllib.request.open #5637
........
r70999 | benjamin.peterson | 2009-04-01 17:36:47 -0500 (Wed, 01 Apr 2009) | 1 line
add very alpha support to 2to3 for running concurrently with multiprocessing
........
r71003 | benjamin.peterson | 2009-04-01 18:10:43 -0500 (Wed, 01 Apr 2009) | 1 line
fix when multiprocessing is not available or used
........
r71695 | benjamin.peterson | 2009-04-17 22:21:29 -0500 (Fri, 17 Apr 2009) | 1 line
refactor multiprocessing support, so it's less hacky to employ and only loads mp when needed
........
................
92 lines
3.2 KiB
Python
92 lines
3.2 KiB
Python
"""Fixer for except statements with named exceptions.
|
|
|
|
The following cases will be converted:
|
|
|
|
- "except E, T:" where T is a name:
|
|
|
|
except E as T:
|
|
|
|
- "except E, T:" where T is not a name, tuple or list:
|
|
|
|
except E as t:
|
|
T = t
|
|
|
|
This is done because the target of an "except" clause must be a
|
|
name.
|
|
|
|
- "except E, T:" where T is a tuple or list literal:
|
|
|
|
except E as t:
|
|
T = t.args
|
|
"""
|
|
# Author: Collin Winter
|
|
|
|
# Local imports
|
|
from .. import pytree
|
|
from ..pgen2 import token
|
|
from .. import fixer_base
|
|
from ..fixer_util import Assign, Attr, Name, is_tuple, is_list, syms
|
|
|
|
def find_excepts(nodes):
|
|
for i, n in enumerate(nodes):
|
|
if n.type == syms.except_clause:
|
|
if n.children[0].value == 'except':
|
|
yield (n, nodes[i+2])
|
|
|
|
class FixExcept(fixer_base.BaseFix):
|
|
|
|
PATTERN = """
|
|
try_stmt< 'try' ':' suite
|
|
cleanup=(except_clause ':' suite)+
|
|
tail=(['except' ':' suite]
|
|
['else' ':' suite]
|
|
['finally' ':' suite]) >
|
|
"""
|
|
|
|
def transform(self, node, results):
|
|
syms = self.syms
|
|
|
|
tail = [n.clone() for n in results["tail"]]
|
|
|
|
try_cleanup = [ch.clone() for ch in results["cleanup"]]
|
|
for except_clause, e_suite in find_excepts(try_cleanup):
|
|
if len(except_clause.children) == 4:
|
|
(E, comma, N) = except_clause.children[1:4]
|
|
comma.replace(Name("as", prefix=" "))
|
|
|
|
if N.type != token.NAME:
|
|
# Generate a new N for the except clause
|
|
new_N = Name(self.new_name(), prefix=" ")
|
|
target = N.clone()
|
|
target.set_prefix("")
|
|
N.replace(new_N)
|
|
new_N = new_N.clone()
|
|
|
|
# Insert "old_N = new_N" as the first statement in
|
|
# the except body. This loop skips leading whitespace
|
|
# and indents
|
|
#TODO(cwinter) suite-cleanup
|
|
suite_stmts = e_suite.children
|
|
for i, stmt in enumerate(suite_stmts):
|
|
if isinstance(stmt, pytree.Node):
|
|
break
|
|
|
|
# The assignment is different if old_N is a tuple or list
|
|
# In that case, the assignment is old_N = new_N.args
|
|
if is_tuple(N) or is_list(N):
|
|
assign = Assign(target, Attr(new_N, Name('args')))
|
|
else:
|
|
assign = Assign(target, new_N)
|
|
|
|
#TODO(cwinter) stopgap until children becomes a smart list
|
|
for child in reversed(suite_stmts[:i]):
|
|
e_suite.insert_child(0, child)
|
|
e_suite.insert_child(i, assign)
|
|
elif N.get_prefix() == "":
|
|
# No space after a comma is legal; no space after "as",
|
|
# not so much.
|
|
N.set_prefix(" ")
|
|
|
|
#TODO(cwinter) fix this when children becomes a smart list
|
|
children = [c.clone() for c in node.children[:3]] + try_cleanup + tail
|
|
return pytree.Node(node.type, children)
|