bpo-40715: Reject dict unpacking on dict comprehensions (GH-20292)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
This commit is contained in:
Batuhan Taskaya 2020-05-22 01:39:56 +03:00 committed by GitHub
parent 72e0aa2fd2
commit b8a65ec1d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 308 additions and 208 deletions

View File

@ -499,14 +499,16 @@ setcomp[expr_ty]:
| '{' a=expression b=for_if_clauses '}' { _Py_SetComp(a, b, EXTRA) }
| invalid_comprehension
dict[expr_ty]:
| '{' a=[kvpairs] '}' { _Py_Dict(CHECK(_PyPegen_get_keys(p, a)),
CHECK(_PyPegen_get_values(p, a)), EXTRA) }
| '{' a=[double_starred_kvpairs] '}' {
_Py_Dict(CHECK(_PyPegen_get_keys(p, a)), CHECK(_PyPegen_get_values(p, a)), EXTRA) }
dictcomp[expr_ty]:
| '{' a=kvpair b=for_if_clauses '}' { _Py_DictComp(a->key, a->value, b, EXTRA) }
kvpairs[asdl_seq*]: a=','.kvpair+ [','] { a }
kvpair[KeyValuePair*]:
| invalid_dict_comprehension
double_starred_kvpairs[asdl_seq*]: a=','.double_starred_kvpair+ [','] { a }
double_starred_kvpair[KeyValuePair*]:
| '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
| a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
| kvpair
kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
for_if_clauses[asdl_seq*]:
| for_if_clause+
for_if_clause[comprehension_ty]:
@ -657,6 +659,9 @@ invalid_block:
invalid_comprehension:
| ('[' | '(' | '{') a=starred_expression for_if_clauses {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
invalid_dict_comprehension:
| '{' a='**' bitwise_or for_if_clauses '}' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") }
invalid_parameters:
| param_no_default* (slash_with_default | param_with_default+) param_no_default {
RAISE_SYNTAX_ERROR("non-default argument follows default argument") }

View File

@ -158,6 +158,11 @@ List comprehension element unpacking
...
SyntaxError: iterable unpacking cannot be used in comprehension
>>> {**{} for a in [1]}
Traceback (most recent call last):
...
SyntaxError: dict unpacking cannot be used in dict comprehension
# Pegen is better here.
# Generator expression in function arguments

View File

@ -184,192 +184,194 @@ static KeywordToken *reserved_keywords[] = {
#define setcomp_type 1113
#define dict_type 1114
#define dictcomp_type 1115
#define kvpairs_type 1116
#define kvpair_type 1117
#define for_if_clauses_type 1118
#define for_if_clause_type 1119
#define yield_expr_type 1120
#define arguments_type 1121
#define args_type 1122
#define kwargs_type 1123
#define starred_expression_type 1124
#define kwarg_or_starred_type 1125
#define kwarg_or_double_starred_type 1126
#define star_targets_type 1127
#define star_targets_seq_type 1128
#define star_target_type 1129
#define star_atom_type 1130
#define single_target_type 1131
#define single_subscript_attribute_target_type 1132
#define del_targets_type 1133
#define del_target_type 1134
#define del_t_atom_type 1135
#define del_target_end_type 1136
#define targets_type 1137
#define target_type 1138
#define t_primary_type 1139 // Left-recursive
#define t_lookahead_type 1140
#define t_atom_type 1141
#define incorrect_arguments_type 1142
#define invalid_kwarg_type 1143
#define invalid_named_expression_type 1144
#define invalid_assignment_type 1145
#define invalid_block_type 1146
#define invalid_comprehension_type 1147
#define invalid_parameters_type 1148
#define invalid_star_etc_type 1149
#define invalid_lambda_star_etc_type 1150
#define invalid_double_type_comments_type 1151
#define invalid_del_target_type 1152
#define invalid_import_from_targets_type 1153
#define _loop0_1_type 1154
#define _loop0_2_type 1155
#define _loop0_4_type 1156
#define _gather_3_type 1157
#define _loop0_6_type 1158
#define _gather_5_type 1159
#define _loop0_8_type 1160
#define _gather_7_type 1161
#define _loop0_10_type 1162
#define _gather_9_type 1163
#define _loop1_11_type 1164
#define _loop0_13_type 1165
#define _gather_12_type 1166
#define _tmp_14_type 1167
#define _tmp_15_type 1168
#define _tmp_16_type 1169
#define _tmp_17_type 1170
#define _tmp_18_type 1171
#define _tmp_19_type 1172
#define _tmp_20_type 1173
#define _tmp_21_type 1174
#define _loop1_22_type 1175
#define _tmp_23_type 1176
#define _tmp_24_type 1177
#define _loop0_26_type 1178
#define _gather_25_type 1179
#define _loop0_28_type 1180
#define _gather_27_type 1181
#define _tmp_29_type 1182
#define _loop0_30_type 1183
#define _loop1_31_type 1184
#define _loop0_33_type 1185
#define _gather_32_type 1186
#define _tmp_34_type 1187
#define _loop0_36_type 1188
#define _gather_35_type 1189
#define _tmp_37_type 1190
#define _loop0_39_type 1191
#define _gather_38_type 1192
#define _loop0_41_type 1193
#define _gather_40_type 1194
#define _loop0_43_type 1195
#define _gather_42_type 1196
#define _loop0_45_type 1197
#define _gather_44_type 1198
#define _tmp_46_type 1199
#define _loop1_47_type 1200
#define _tmp_48_type 1201
#define _tmp_49_type 1202
#define _tmp_50_type 1203
#define _tmp_51_type 1204
#define _tmp_52_type 1205
#define _loop0_53_type 1206
#define _loop0_54_type 1207
#define _loop0_55_type 1208
#define _loop1_56_type 1209
#define _loop0_57_type 1210
#define _loop1_58_type 1211
#define _loop1_59_type 1212
#define _loop1_60_type 1213
#define _loop0_61_type 1214
#define _loop1_62_type 1215
#define _loop0_63_type 1216
#define _loop1_64_type 1217
#define _loop0_65_type 1218
#define _loop1_66_type 1219
#define _loop1_67_type 1220
#define _tmp_68_type 1221
#define _loop0_70_type 1222
#define _gather_69_type 1223
#define _loop1_71_type 1224
#define _loop0_73_type 1225
#define _gather_72_type 1226
#define _loop1_74_type 1227
#define _loop0_75_type 1228
#define _loop0_76_type 1229
#define _loop0_77_type 1230
#define _loop1_78_type 1231
#define _loop0_79_type 1232
#define _loop1_80_type 1233
#define _loop1_81_type 1234
#define _loop1_82_type 1235
#define _loop0_83_type 1236
#define _loop1_84_type 1237
#define _loop0_85_type 1238
#define _loop1_86_type 1239
#define _loop0_87_type 1240
#define _loop1_88_type 1241
#define _loop1_89_type 1242
#define _loop1_90_type 1243
#define _loop1_91_type 1244
#define _tmp_92_type 1245
#define _loop0_94_type 1246
#define _gather_93_type 1247
#define _tmp_95_type 1248
#define _tmp_96_type 1249
#define _tmp_97_type 1250
#define _tmp_98_type 1251
#define _loop1_99_type 1252
#define _tmp_100_type 1253
#define _tmp_101_type 1254
#define _loop0_103_type 1255
#define _gather_102_type 1256
#define _loop1_104_type 1257
#define _loop0_105_type 1258
#define _loop0_106_type 1259
#define _tmp_107_type 1260
#define _tmp_108_type 1261
#define _loop0_110_type 1262
#define _gather_109_type 1263
#define _loop0_112_type 1264
#define _gather_111_type 1265
#define _loop0_114_type 1266
#define _gather_113_type 1267
#define _loop0_116_type 1268
#define _gather_115_type 1269
#define _loop0_117_type 1270
#define _loop0_119_type 1271
#define _gather_118_type 1272
#define _tmp_120_type 1273
#define _loop0_122_type 1274
#define _gather_121_type 1275
#define _loop0_124_type 1276
#define _gather_123_type 1277
#define _tmp_125_type 1278
#define _loop0_126_type 1279
#define _tmp_127_type 1280
#define _tmp_128_type 1281
#define _tmp_129_type 1282
#define _tmp_130_type 1283
#define _loop0_131_type 1284
#define _tmp_132_type 1285
#define _tmp_133_type 1286
#define _tmp_134_type 1287
#define _tmp_135_type 1288
#define _tmp_136_type 1289
#define _tmp_137_type 1290
#define _tmp_138_type 1291
#define _tmp_139_type 1292
#define _tmp_140_type 1293
#define _tmp_141_type 1294
#define _tmp_142_type 1295
#define _tmp_143_type 1296
#define _tmp_144_type 1297
#define _tmp_145_type 1298
#define _loop1_146_type 1299
#define _tmp_147_type 1300
#define _tmp_148_type 1301
#define double_starred_kvpairs_type 1116
#define double_starred_kvpair_type 1117
#define kvpair_type 1118
#define for_if_clauses_type 1119
#define for_if_clause_type 1120
#define yield_expr_type 1121
#define arguments_type 1122
#define args_type 1123
#define kwargs_type 1124
#define starred_expression_type 1125
#define kwarg_or_starred_type 1126
#define kwarg_or_double_starred_type 1127
#define star_targets_type 1128
#define star_targets_seq_type 1129
#define star_target_type 1130
#define star_atom_type 1131
#define single_target_type 1132
#define single_subscript_attribute_target_type 1133
#define del_targets_type 1134
#define del_target_type 1135
#define del_t_atom_type 1136
#define del_target_end_type 1137
#define targets_type 1138
#define target_type 1139
#define t_primary_type 1140 // Left-recursive
#define t_lookahead_type 1141
#define t_atom_type 1142
#define incorrect_arguments_type 1143
#define invalid_kwarg_type 1144
#define invalid_named_expression_type 1145
#define invalid_assignment_type 1146
#define invalid_block_type 1147
#define invalid_comprehension_type 1148
#define invalid_dict_comprehension_type 1149
#define invalid_parameters_type 1150
#define invalid_star_etc_type 1151
#define invalid_lambda_star_etc_type 1152
#define invalid_double_type_comments_type 1153
#define invalid_del_target_type 1154
#define invalid_import_from_targets_type 1155
#define _loop0_1_type 1156
#define _loop0_2_type 1157
#define _loop0_4_type 1158
#define _gather_3_type 1159
#define _loop0_6_type 1160
#define _gather_5_type 1161
#define _loop0_8_type 1162
#define _gather_7_type 1163
#define _loop0_10_type 1164
#define _gather_9_type 1165
#define _loop1_11_type 1166
#define _loop0_13_type 1167
#define _gather_12_type 1168
#define _tmp_14_type 1169
#define _tmp_15_type 1170
#define _tmp_16_type 1171
#define _tmp_17_type 1172
#define _tmp_18_type 1173
#define _tmp_19_type 1174
#define _tmp_20_type 1175
#define _tmp_21_type 1176
#define _loop1_22_type 1177
#define _tmp_23_type 1178
#define _tmp_24_type 1179
#define _loop0_26_type 1180
#define _gather_25_type 1181
#define _loop0_28_type 1182
#define _gather_27_type 1183
#define _tmp_29_type 1184
#define _loop0_30_type 1185
#define _loop1_31_type 1186
#define _loop0_33_type 1187
#define _gather_32_type 1188
#define _tmp_34_type 1189
#define _loop0_36_type 1190
#define _gather_35_type 1191
#define _tmp_37_type 1192
#define _loop0_39_type 1193
#define _gather_38_type 1194
#define _loop0_41_type 1195
#define _gather_40_type 1196
#define _loop0_43_type 1197
#define _gather_42_type 1198
#define _loop0_45_type 1199
#define _gather_44_type 1200
#define _tmp_46_type 1201
#define _loop1_47_type 1202
#define _tmp_48_type 1203
#define _tmp_49_type 1204
#define _tmp_50_type 1205
#define _tmp_51_type 1206
#define _tmp_52_type 1207
#define _loop0_53_type 1208
#define _loop0_54_type 1209
#define _loop0_55_type 1210
#define _loop1_56_type 1211
#define _loop0_57_type 1212
#define _loop1_58_type 1213
#define _loop1_59_type 1214
#define _loop1_60_type 1215
#define _loop0_61_type 1216
#define _loop1_62_type 1217
#define _loop0_63_type 1218
#define _loop1_64_type 1219
#define _loop0_65_type 1220
#define _loop1_66_type 1221
#define _loop1_67_type 1222
#define _tmp_68_type 1223
#define _loop0_70_type 1224
#define _gather_69_type 1225
#define _loop1_71_type 1226
#define _loop0_73_type 1227
#define _gather_72_type 1228
#define _loop1_74_type 1229
#define _loop0_75_type 1230
#define _loop0_76_type 1231
#define _loop0_77_type 1232
#define _loop1_78_type 1233
#define _loop0_79_type 1234
#define _loop1_80_type 1235
#define _loop1_81_type 1236
#define _loop1_82_type 1237
#define _loop0_83_type 1238
#define _loop1_84_type 1239
#define _loop0_85_type 1240
#define _loop1_86_type 1241
#define _loop0_87_type 1242
#define _loop1_88_type 1243
#define _loop1_89_type 1244
#define _loop1_90_type 1245
#define _loop1_91_type 1246
#define _tmp_92_type 1247
#define _loop0_94_type 1248
#define _gather_93_type 1249
#define _tmp_95_type 1250
#define _tmp_96_type 1251
#define _tmp_97_type 1252
#define _tmp_98_type 1253
#define _loop1_99_type 1254
#define _tmp_100_type 1255
#define _tmp_101_type 1256
#define _loop0_103_type 1257
#define _gather_102_type 1258
#define _loop1_104_type 1259
#define _loop0_105_type 1260
#define _loop0_106_type 1261
#define _tmp_107_type 1262
#define _tmp_108_type 1263
#define _loop0_110_type 1264
#define _gather_109_type 1265
#define _loop0_112_type 1266
#define _gather_111_type 1267
#define _loop0_114_type 1268
#define _gather_113_type 1269
#define _loop0_116_type 1270
#define _gather_115_type 1271
#define _loop0_117_type 1272
#define _loop0_119_type 1273
#define _gather_118_type 1274
#define _tmp_120_type 1275
#define _loop0_122_type 1276
#define _gather_121_type 1277
#define _loop0_124_type 1278
#define _gather_123_type 1279
#define _tmp_125_type 1280
#define _loop0_126_type 1281
#define _tmp_127_type 1282
#define _tmp_128_type 1283
#define _tmp_129_type 1284
#define _tmp_130_type 1285
#define _loop0_131_type 1286
#define _tmp_132_type 1287
#define _tmp_133_type 1288
#define _tmp_134_type 1289
#define _tmp_135_type 1290
#define _tmp_136_type 1291
#define _tmp_137_type 1292
#define _tmp_138_type 1293
#define _tmp_139_type 1294
#define _tmp_140_type 1295
#define _tmp_141_type 1296
#define _tmp_142_type 1297
#define _tmp_143_type 1298
#define _tmp_144_type 1299
#define _tmp_145_type 1300
#define _loop1_146_type 1301
#define _tmp_147_type 1302
#define _tmp_148_type 1303
static mod_ty file_rule(Parser *p);
static mod_ty interactive_rule(Parser *p);
@ -487,7 +489,8 @@ static expr_ty set_rule(Parser *p);
static expr_ty setcomp_rule(Parser *p);
static expr_ty dict_rule(Parser *p);
static expr_ty dictcomp_rule(Parser *p);
static asdl_seq* kvpairs_rule(Parser *p);
static asdl_seq* double_starred_kvpairs_rule(Parser *p);
static KeyValuePair* double_starred_kvpair_rule(Parser *p);
static KeyValuePair* kvpair_rule(Parser *p);
static asdl_seq* for_if_clauses_rule(Parser *p);
static comprehension_ty for_if_clause_rule(Parser *p);
@ -519,6 +522,7 @@ static void *invalid_named_expression_rule(Parser *p);
static void *invalid_assignment_rule(Parser *p);
static void *invalid_block_rule(Parser *p);
static void *invalid_comprehension_rule(Parser *p);
static void *invalid_dict_comprehension_rule(Parser *p);
static void *invalid_parameters_rule(Parser *p);
static void *invalid_star_etc_rule(Parser *p);
static void *invalid_lambda_star_etc_rule(Parser *p);
@ -9284,7 +9288,7 @@ setcomp_rule(Parser *p)
return _res;
}
// dict: '{' kvpairs? '}'
// dict: '{' double_starred_kvpairs? '}'
static expr_ty
dict_rule(Parser *p)
{
@ -9301,7 +9305,7 @@ dict_rule(Parser *p)
UNUSED(_start_lineno); // Only used by EXTRA macro
int _start_col_offset = p->tokens[_mark]->col_offset;
UNUSED(_start_col_offset); // Only used by EXTRA macro
{ // '{' kvpairs? '}'
{ // '{' double_starred_kvpairs? '}'
if (p->error_indicator) {
return NULL;
}
@ -9311,7 +9315,7 @@ dict_rule(Parser *p)
if (
(_literal = _PyPegen_expect_token(p, 25)) // token='{'
&&
(a = kvpairs_rule(p), 1) // kvpairs?
(a = double_starred_kvpairs_rule(p), 1) // double_starred_kvpairs?
&&
(_literal_1 = _PyPegen_expect_token(p, 26)) // token='}'
)
@ -9338,7 +9342,7 @@ dict_rule(Parser *p)
return _res;
}
// dictcomp: '{' kvpair for_if_clauses '}'
// dictcomp: '{' kvpair for_if_clauses '}' | invalid_dict_comprehension
static expr_ty
dictcomp_rule(Parser *p)
{
@ -9390,21 +9394,35 @@ dictcomp_rule(Parser *p)
}
p->mark = _mark;
}
{ // invalid_dict_comprehension
if (p->error_indicator) {
return NULL;
}
void *invalid_dict_comprehension_var;
if (
(invalid_dict_comprehension_var = invalid_dict_comprehension_rule(p)) // invalid_dict_comprehension
)
{
_res = invalid_dict_comprehension_var;
goto done;
}
p->mark = _mark;
}
_res = NULL;
done:
return _res;
}
// kvpairs: ','.kvpair+ ','?
// double_starred_kvpairs: ','.double_starred_kvpair+ ','?
static asdl_seq*
kvpairs_rule(Parser *p)
double_starred_kvpairs_rule(Parser *p)
{
if (p->error_indicator) {
return NULL;
}
asdl_seq* _res = NULL;
int _mark = p->mark;
{ // ','.kvpair+ ','?
{ // ','.double_starred_kvpair+ ','?
if (p->error_indicator) {
return NULL;
}
@ -9412,7 +9430,7 @@ kvpairs_rule(Parser *p)
UNUSED(_opt_var); // Silence compiler warnings
asdl_seq * a;
if (
(a = _gather_102_rule(p)) // ','.kvpair+
(a = _gather_102_rule(p)) // ','.double_starred_kvpair+
&&
(_opt_var = _PyPegen_expect_token(p, 12), 1) // ','?
)
@ -9431,9 +9449,9 @@ kvpairs_rule(Parser *p)
return _res;
}
// kvpair: '**' bitwise_or | expression ':' expression
// double_starred_kvpair: '**' bitwise_or | kvpair
static KeyValuePair*
kvpair_rule(Parser *p)
double_starred_kvpair_rule(Parser *p)
{
if (p->error_indicator) {
return NULL;
@ -9461,6 +9479,34 @@ kvpair_rule(Parser *p)
}
p->mark = _mark;
}
{ // kvpair
if (p->error_indicator) {
return NULL;
}
KeyValuePair* kvpair_var;
if (
(kvpair_var = kvpair_rule(p)) // kvpair
)
{
_res = kvpair_var;
goto done;
}
p->mark = _mark;
}
_res = NULL;
done:
return _res;
}
// kvpair: expression ':' expression
static KeyValuePair*
kvpair_rule(Parser *p)
{
if (p->error_indicator) {
return NULL;
}
KeyValuePair* _res = NULL;
int _mark = p->mark;
{ // expression ':' expression
if (p->error_indicator) {
return NULL;
@ -12024,6 +12070,50 @@ invalid_comprehension_rule(Parser *p)
return _res;
}
// invalid_dict_comprehension: '{' '**' bitwise_or for_if_clauses '}'
static void *
invalid_dict_comprehension_rule(Parser *p)
{
if (p->error_indicator) {
return NULL;
}
void * _res = NULL;
int _mark = p->mark;
{ // '{' '**' bitwise_or for_if_clauses '}'
if (p->error_indicator) {
return NULL;
}
Token * _literal;
Token * _literal_1;
Token * a;
expr_ty bitwise_or_var;
asdl_seq* for_if_clauses_var;
if (
(_literal = _PyPegen_expect_token(p, 25)) // token='{'
&&
(a = _PyPegen_expect_token(p, 35)) // token='**'
&&
(bitwise_or_var = bitwise_or_rule(p)) // bitwise_or
&&
(for_if_clauses_var = for_if_clauses_rule(p)) // for_if_clauses
&&
(_literal_1 = _PyPegen_expect_token(p, 26)) // token='}'
)
{
_res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "dict unpacking cannot be used in dict comprehension" );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
return NULL;
}
goto done;
}
p->mark = _mark;
}
_res = NULL;
done:
return _res;
}
// invalid_parameters:
// | param_no_default* (slash_with_default | param_with_default+) param_no_default
static void *
@ -17336,7 +17426,7 @@ _tmp_101_rule(Parser *p)
return _res;
}
// _loop0_103: ',' kvpair
// _loop0_103: ',' double_starred_kvpair
static asdl_seq *
_loop0_103_rule(Parser *p)
{
@ -17354,7 +17444,7 @@ _loop0_103_rule(Parser *p)
}
ssize_t _children_capacity = 1;
ssize_t _n = 0;
{ // ',' kvpair
{ // ',' double_starred_kvpair
if (p->error_indicator) {
return NULL;
}
@ -17363,7 +17453,7 @@ _loop0_103_rule(Parser *p)
while (
(_literal = _PyPegen_expect_token(p, 12)) // token=','
&&
(elem = kvpair_rule(p)) // kvpair
(elem = double_starred_kvpair_rule(p)) // double_starred_kvpair
)
{
_res = elem;
@ -17400,7 +17490,7 @@ _loop0_103_rule(Parser *p)
return _seq;
}
// _gather_102: kvpair _loop0_103
// _gather_102: double_starred_kvpair _loop0_103
static asdl_seq *
_gather_102_rule(Parser *p)
{
@ -17409,14 +17499,14 @@ _gather_102_rule(Parser *p)
}
asdl_seq * _res = NULL;
int _mark = p->mark;
{ // kvpair _loop0_103
{ // double_starred_kvpair _loop0_103
if (p->error_indicator) {
return NULL;
}
KeyValuePair* elem;
asdl_seq * seq;
if (
(elem = kvpair_rule(p)) // kvpair
(elem = double_starred_kvpair_rule(p)) // double_starred_kvpair
&&
(seq = _loop0_103_rule(p)) // _loop0_103
)