[Python-checkins] r54105 - in sandbox/trunk/2to3: fixes/fix_arg_tuples.py fixes/fix_tuple_params.py tests/test_fixers.py
collin.winter
python-checkins at python.org
Sat Mar 3 05:19:14 CET 2007
Author: collin.winter
Date: Sat Mar 3 05:19:11 2007
New Revision: 54105
Added:
sandbox/trunk/2to3/fixes/fix_tuple_params.py
- copied, changed from r54104, sandbox/trunk/2to3/fixes/fix_arg_tuples.py
Removed:
sandbox/trunk/2to3/fixes/fix_arg_tuples.py
Modified:
sandbox/trunk/2to3/tests/test_fixers.py
Log:
Rename tests/fix_arg_tuples.py; they're called tuple parameters, not tuple arguments.
Deleted: /sandbox/trunk/2to3/fixes/fix_arg_tuples.py
==============================================================================
--- /sandbox/trunk/2to3/fixes/fix_arg_tuples.py Sat Mar 3 05:19:11 2007
+++ (empty file)
@@ -1,86 +0,0 @@
-"""Fixer for function definitions with tuple parameters.
-
-def func(((a, b), c), d):
- ...
-
- ->
-
-def func(x, d):
- ((a, b), c) = x
- ...
-"""
-# Author: Collin Winter
-
-# Local imports
-import pytree
-from pgen2 import token
-from fixes import basefix
-from fixes.macros import Assign, Name, Newline
-
-def is_docstring(stmt):
- return isinstance(stmt, pytree.Node) and \
- stmt.children[0].type == token.STRING
-
-class FixArgTuples(basefix.BaseFix):
- PATTERN = """funcdef< 'def' any parameters< '(' args=any ')' >
- ['->' any] ':' suite=any+ >"""
-
- def transform(self, node):
- syms = self.syms
- results = self.match(node)
- assert results
-
- new_lines = []
- suite = results["suite"]
- args = results["args"]
- # This crap is so "def foo(...): x = 5; y = 7" is handled correctly.
- if suite[0].children[1].type == token.INDENT:
- start = 2
- indent = suite[0].children[1].value
- end = Newline()
- else:
- start = 0
- indent = "; "
- end = pytree.Leaf(token.INDENT, "")
-
- # We need access to self for new_name(), and making this a method
- # doesn't feel right. Closing over self and new_lines makes the
- # code below cleaner.
- def handle_tuple(tuple_arg, add_prefix=False):
- n = Name(self.new_name())
- arg = tuple_arg.clone()
- arg.set_prefix("")
- stmt = Assign(arg, n.clone())
- if add_prefix:
- n.set_prefix(" ")
- tuple_arg.replace(n)
- new_lines.append(pytree.Node(syms.simple_stmt, [stmt, end.clone()]))
-
- if args.type == syms.tfpdef:
- handle_tuple(args)
- elif args.type == syms.typedargslist:
- for i, arg in enumerate(args.children):
- if arg.type == syms.tfpdef:
- # Without add_prefix, the emitted code is correct,
- # just ugly.
- handle_tuple(arg, add_prefix=(i > 0))
-
- if not new_lines:
- return node
-
- # This isn't strictly necessary, but it plays nicely with other fixers.
- for line in new_lines:
- line.parent = suite[0]
-
- after = start
- if start == 0:
- new_lines[0].set_prefix(" ")
- elif is_docstring(suite[0].children[start]):
- new_lines[0].set_prefix(indent)
- after = start + 1
-
- children = list(suite[0].children)
- children[after:after] = new_lines
- for i in range(after+1, after+len(new_lines)+1):
- children[i].set_prefix(indent)
- suite[0].children = tuple(children)
Copied: sandbox/trunk/2to3/fixes/fix_tuple_params.py (from r54104, sandbox/trunk/2to3/fixes/fix_arg_tuples.py)
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_arg_tuples.py (original)
+++ sandbox/trunk/2to3/fixes/fix_tuple_params.py Sat Mar 3 05:19:11 2007
@@ -21,7 +21,7 @@
return isinstance(stmt, pytree.Node) and \
stmt.children[0].type == token.STRING
-class FixArgTuples(basefix.BaseFix):
+class FixTupleParams(basefix.BaseFix):
PATTERN = """funcdef< 'def' any parameters< '(' args=any ')' >
['->' any] ':' suite=any+ >"""
Modified: sandbox/trunk/2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_fixers.py (original)
+++ sandbox/trunk/2to3/tests/test_fixers.py Sat Mar 3 05:19:11 2007
@@ -496,8 +496,8 @@
def foo():
try:
pass
- except Exception as xxx_todo_changeme12:
- (f, e) = xxx_todo_changeme12.message
+ except Exception as xxx_todo_changeme:
+ (f, e) = xxx_todo_changeme.message
pass
except ImportError as e:
pass"""
@@ -527,8 +527,8 @@
a = """
try:
pass
- except Exception as xxx_todo_changeme13:
- (a, b) = xxx_todo_changeme13.message
+ except Exception as xxx_todo_changeme1:
+ (a, b) = xxx_todo_changeme1.message
pass"""
self.check(b, a)
@@ -542,8 +542,8 @@
a = """
try:
pass
- except Exception as xxx_todo_changeme14:
- d[5] = xxx_todo_changeme14
+ except Exception as xxx_todo_changeme2:
+ d[5] = xxx_todo_changeme2
pass"""
self.check(b, a)
@@ -557,8 +557,8 @@
a = """
try:
pass
- except Exception as xxx_todo_changeme15:
- a.foo = xxx_todo_changeme15
+ except Exception as xxx_todo_changeme3:
+ a.foo = xxx_todo_changeme3
pass"""
self.check(b, a)
@@ -572,8 +572,8 @@
a = """
try:
pass
- except Exception as xxx_todo_changeme16:
- a().foo = xxx_todo_changeme16
+ except Exception as xxx_todo_changeme4:
+ a().foo = xxx_todo_changeme4
pass"""
self.check(b, a)
@@ -1113,8 +1113,8 @@
self.check(b, a)
-class Test_arg_tuples(FixerTestCase):
- fixer = "arg_tuples"
+class Test_tuple_params(FixerTestCase):
+ fixer = "tuple_params"
def test_unchanged_1(self):
s = """def foo(): pass"""
@@ -1134,8 +1134,8 @@
x = 5"""
a = """
- def foo(xxx_todo_changeme):
- ((a, b), c) = xxx_todo_changeme
+ def foo(xxx_todo_changeme5):
+ ((a, b), c) = xxx_todo_changeme5
x = 5"""
self.check(b, a)
@@ -1145,8 +1145,8 @@
x = 5"""
a = """
- def foo(xxx_todo_changeme1, d):
- ((a, b), c) = xxx_todo_changeme1
+ def foo(xxx_todo_changeme6, d):
+ ((a, b), c) = xxx_todo_changeme6
x = 5"""
self.check(b, a)
@@ -1156,8 +1156,8 @@
x = 5"""
a = """
- def foo(xxx_todo_changeme2, d) -> e:
- ((a, b), c) = xxx_todo_changeme2
+ def foo(xxx_todo_changeme7, d) -> e:
+ ((a, b), c) = xxx_todo_changeme7
x = 5"""
self.check(b, a)
@@ -1166,7 +1166,7 @@
def foo(((a, b), c)): x = 5; y = 7"""
a = """
- def foo(xxx_todo_changeme10): ((a, b), c) = xxx_todo_changeme10; x = 5; y = 7"""
+ def foo(xxx_todo_changeme15): ((a, b), c) = xxx_todo_changeme15; x = 5; y = 7"""
self.check(b, a)
def test_keywords(self):
@@ -1175,8 +1175,8 @@
x = 5"""
a = """
- def foo(xxx_todo_changeme5, d, e=5) -> z:
- ((a, b), c) = xxx_todo_changeme5
+ def foo(xxx_todo_changeme10, d, e=5) -> z:
+ ((a, b), c) = xxx_todo_changeme10
x = 5"""
self.check(b, a)
@@ -1186,8 +1186,8 @@
x = 5"""
a = """
- def foo(xxx_todo_changeme11, d, *vargs, **kwargs) -> z:
- ((a, b), c) = xxx_todo_changeme11
+ def foo(xxx_todo_changeme16, d, *vargs, **kwargs) -> z:
+ ((a, b), c) = xxx_todo_changeme16
x = 5"""
self.check(b, a)
@@ -1197,9 +1197,9 @@
x = 5"""
a = """
- def foo(xxx_todo_changeme6, xxx_todo_changeme7) -> z:
- ((a, b), c) = xxx_todo_changeme6
- (d, e, f) = xxx_todo_changeme7
+ def foo(xxx_todo_changeme11, xxx_todo_changeme12) -> z:
+ ((a, b), c) = xxx_todo_changeme11
+ (d, e, f) = xxx_todo_changeme12
x = 5"""
self.check(b, a)
@@ -1209,9 +1209,9 @@
x = 5"""
a = """
- def foo(x, xxx_todo_changeme8, d, xxx_todo_changeme9, y) -> z:
- ((a, b), c) = xxx_todo_changeme8
- (e, f, g) = xxx_todo_changeme9
+ def foo(x, xxx_todo_changeme13, d, xxx_todo_changeme14, y) -> z:
+ ((a, b), c) = xxx_todo_changeme13
+ (e, f, g) = xxx_todo_changeme14
x = 5"""
self.check(b, a)
@@ -1222,10 +1222,10 @@
x = 5"""
a = """
- def foo(xxx_todo_changeme3, xxx_todo_changeme4) -> z:
+ def foo(xxx_todo_changeme8, xxx_todo_changeme9) -> z:
"foo foo foo foo"
- ((a, b), c) = xxx_todo_changeme3
- (d, e, f) = xxx_todo_changeme4
+ ((a, b), c) = xxx_todo_changeme8
+ (d, e, f) = xxx_todo_changeme9
x = 5"""
self.check(b, a)
More information about the Python-checkins
mailing list