fixed PY-2648 Dictionary creation intention breaks code when a value is a tuple

This commit is contained in:
Ekaterina Tuzova
2011-01-10 15:21:55 +03:00
parent c3c54b065b
commit 686acdd886
3 changed files with 33 additions and 1 deletions
@@ -148,7 +148,14 @@ public class PyAssignmentStatementImpl extends PyElementImpl implements PyAssign
PySequenceExpression rhs_tuple = null;
PyExpression rhs_one = null;
if (rhs instanceof PySequenceExpression) rhs_tuple = (PySequenceExpression)rhs;
if (rhs instanceof PyParenthesizedExpression) {
PyExpression exp = ((PyParenthesizedExpression)rhs).getContainedExpression();
if (exp instanceof PyTupleExpression)
rhs_tuple = (PySequenceExpression)exp;
else
rhs_one = rhs;
}
else if (rhs instanceof PySequenceExpression) rhs_tuple = (PySequenceExpression)rhs;
else if (rhs != null) rhs_one = rhs;
//
if (lhs_one != null) { // single LHS, single RHS (direct mapping) or multiple RHS (packing)
@@ -0,0 +1 @@
<dst1>a, <dst2>b = (<src1>1, <src2>2)
@@ -112,6 +112,30 @@ public class PyAssignmentMappingTest extends LightMarkedTestCase {
}
}
public void testParenthesizedTuple() throws Exception { //PY-2648
Map<String, PsiElement> marks = loadTest();
final int PAIR_NUM = 2;
assertEquals(PAIR_NUM*2, marks.size());
PsiElement[] srcs = new PsiElement[PAIR_NUM];
PsiElement[] dsts = new PsiElement[PAIR_NUM];
for (int i=0; i<PAIR_NUM; i+=1) {
PsiElement dst = marks.get("<dst" + String.valueOf(i + 1) + ">").getParent(); // ident -> target expr
assertTrue(dst instanceof PyTargetExpression);
dsts[i] = dst;
PsiElement src = marks.get("<src" + String.valueOf(i + 1) +">").getParent(); // ident -> target expr
assertTrue(src instanceof PyExpression);
srcs[i] = src;
}
PyAssignmentStatement stmt = (PyAssignmentStatement)srcs[0].getParent().getParent().getParent(); // tuple expr -> assignment
List<Pair<PyExpression, PyExpression>> mapping = stmt.getTargetsToValuesMapping();
assertEquals(PAIR_NUM, mapping.size());
for (int i=0; i<PAIR_NUM; i+=1) {
Pair<PyExpression, PyExpression> pair = mapping.get(i);
assertEquals(dsts[i], pair.getFirst());
assertEquals(srcs[i], pair.getSecond());
}
}
public void testTuplePack() throws Exception {
Map<String, PsiElement> marks = loadTest();
final int SRC_NUM = 2;