Skip to content

Commit ab382ca

Browse files
committed
[GR-23215] Make test_dictcomps pass.
PullRequest: graalpython/991
2 parents 2bd2aaa + 5f7f853 commit ab382ca

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_assign.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939

40+
import unittest
4041

4142
def assert_raises(err, fn, *args, **kwargs):
4243
raised = False
@@ -117,3 +118,30 @@ def __init__(self):
117118
id(a) # id is stored in a HiddenKey
118119

119120
return
121+
122+
class IllegaAssigmentTest(unittest.TestCase):
123+
def test_illegal_assignment(self):
124+
with self.assertRaisesRegex(SyntaxError, "assign to function call"):
125+
compile("a() = 1", "<test>", "exec")
126+
127+
with self.assertRaisesRegex(SyntaxError, "assign to function call"):
128+
compile("a() += 1", "<test>", "exec")
129+
130+
with self.assertRaisesRegex(SyntaxError, "assign to function call"):
131+
str = "def set() :\n\tprint(42)\n\nset() = 5"
132+
compile(str, "<test>", "exec")
133+
134+
with self.assertRaisesRegex(SyntaxError, "assign to function call"):
135+
compile("a(), b, c = (1, 2, 3)", "<test>", "exec")
136+
137+
with self.assertRaisesRegex(SyntaxError, "assign to function call"):
138+
compile("a, b(), c = (1, 2, 3)", "<test>", "exec")
139+
140+
with self.assertRaisesRegex(SyntaxError, "assign to dict comprehension"):
141+
compile("{s:s for s in [1]}, b, c = (1, 2, 3)", "<test>", "exec")
142+
143+
with self.assertRaisesRegex(SyntaxError, "assign to set comprehension"):
144+
compile("{s for s in [1]}, b, c = (1, 2, 3)", "<test>", "exec")
145+
146+
with self.assertRaisesRegex(SyntaxError, "assign to list comprehension"):
147+
compile("[s for s in [1]], b, c = (1, 2, 3)", "<test>", "exec")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*DictComprehensionTest.test_basics
22
*DictComprehensionTest.test_evaluation_order
33
*DictComprehensionTest.test_global_visibility
4+
*DictComprehensionTest.test_illegal_assignment
45
*DictComprehensionTest.test_local_visibility
56
*DictComprehensionTest.test_scope_isolation
67
*DictComprehensionTest.test_scope_isolation_from_global

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/sst/FactorySSTVisitor.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import com.oracle.graal.python.builtins.objects.PNone;
6161
import com.oracle.graal.python.builtins.objects.complex.PComplex;
6262
import com.oracle.graal.python.builtins.objects.function.Signature;
63+
import com.oracle.graal.python.nodes.BuiltinNames;
6364
import com.oracle.graal.python.nodes.EmptyNode;
6465
import com.oracle.graal.python.nodes.NoValueNode;
6566
import com.oracle.graal.python.nodes.NodeFactory;
@@ -244,6 +245,7 @@ public PNode visit(AssignmentSSTNode node) {
244245
ExpressionNode[] lhs = new ExpressionNode[node.lhs.length];
245246
for (int i = 0; i < node.lhs.length; i++) {
246247
SSTNode sstLhs = node.lhs[i];
248+
checkCannotAssignTo(sstLhs);
247249
lhs[i] = (ExpressionNode) sstLhs.accept(this);
248250
}
249251
ExpressionNode rhs = (ExpressionNode) node.rhs.accept(this);
@@ -268,6 +270,7 @@ public PNode visit(AssignmentSSTNode node) {
268270

269271
@Override
270272
public PNode visit(AugAssignmentSSTNode node) {
273+
checkCannotAssignTo(node.lhs);
271274
ExpressionNode lhs = (ExpressionNode) node.lhs.accept(this);
272275
if (!(lhs instanceof ReadNode)) {
273276
throw errors.raiseInvalidSyntax(source, createSourceSection(node.startOffset, node.endOffset), "illegal expression for augmented assignment");
@@ -280,6 +283,36 @@ public PNode visit(AugAssignmentSSTNode node) {
280283
return result;
281284
}
282285

286+
private void checkCannotAssignTo(SSTNode lhs) throws RuntimeException {
287+
if (lhs instanceof ForComprehensionSSTNode) {
288+
String calleeName;
289+
switch (((ForComprehensionSSTNode) lhs).resultType) {
290+
case PList:
291+
calleeName = BuiltinNames.LIST;
292+
break;
293+
case PSet:
294+
calleeName = BuiltinNames.SET;
295+
break;
296+
case PDict:
297+
calleeName = BuiltinNames.DICT;
298+
break;
299+
default:
300+
calleeName = null;
301+
}
302+
if (calleeName == null) {
303+
throw errors.raiseInvalidSyntax(source, createSourceSection(lhs.startOffset, lhs.endOffset), "cannot assign to comprehension");
304+
} else {
305+
throw errors.raiseInvalidSyntax(source, createSourceSection(lhs.startOffset, lhs.endOffset), "cannot assign to %s comprehension", calleeName);
306+
}
307+
} else if (lhs instanceof CallSSTNode) {
308+
throw errors.raiseInvalidSyntax(source, createSourceSection(lhs.startOffset, lhs.endOffset), "cannot assign to function call");
309+
} else if (lhs instanceof CollectionSSTNode) {
310+
for (SSTNode n : ((CollectionSSTNode) lhs).getValues()) {
311+
checkCannotAssignTo(n);
312+
}
313+
}
314+
}
315+
283316
@Override
284317
public PNode visit(BinaryArithmeticSSTNode node) {
285318
ExpressionNode left = (ExpressionNode) node.left.accept(this);

0 commit comments

Comments
 (0)