Skip to content

Commit d8b3a98

Browse files
bpo-36187: Remove NamedStore. (pythonGH-12167)
NamedStore has been replaced with Store. The difference between NamedStore and Store is handled when precess the NamedExpr node one level upper.
1 parent adfffc7 commit d8b3a98

File tree

7 files changed

+103
-143
lines changed

7 files changed

+103
-143
lines changed

Include/Python-ast.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_syntax.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
Traceback (most recent call last):
4444
SyntaxError: cannot assign to True
4545
46+
>>> (True := 1)
47+
Traceback (most recent call last):
48+
SyntaxError: cannot use named assignment with True
49+
4650
>>> obj.__debug__ = 1
4751
Traceback (most recent call last):
4852
SyntaxError: cannot assign to __debug__

Parser/Python.asdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ module Python
9191
-- col_offset is the byte offset in the utf8 string the parser uses
9292
attributes (int lineno, int col_offset, int? end_lineno, int? end_col_offset)
9393

94-
expr_context = Load | Store | Del | AugLoad | AugStore | Param | NamedStore
94+
expr_context = Load | Store | Del | AugLoad | AugStore | Param
9595

9696
slice = Slice(expr? lower, expr? upper, expr? step)
9797
| ExtSlice(slice* dims)

Python/Python-ast.c

Lines changed: 1 addition & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/ast.c

Lines changed: 90 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ expr_context_name(expr_context_ty ctx)
9494
return "Load";
9595
case Store:
9696
return "Store";
97-
case NamedStore:
98-
return "NamedStore";
9997
case Del:
10098
return "Del";
10199
case AugLoad:
@@ -1029,6 +1027,80 @@ copy_location(expr_ty e, const node *n)
10291027
return e;
10301028
}
10311029

1030+
static const char *
1031+
get_expr_name(expr_ty e)
1032+
{
1033+
switch (e->kind) {
1034+
case Attribute_kind:
1035+
return "attribute";
1036+
case Subscript_kind:
1037+
return "subscript";
1038+
case Starred_kind:
1039+
return "starred";
1040+
case Name_kind:
1041+
return "name";
1042+
case List_kind:
1043+
return "list";
1044+
case Tuple_kind:
1045+
return "tuple";
1046+
case Lambda_kind:
1047+
return "lambda";
1048+
case Call_kind:
1049+
return "function call";
1050+
case BoolOp_kind:
1051+
case BinOp_kind:
1052+
case UnaryOp_kind:
1053+
return "operator";
1054+
case GeneratorExp_kind:
1055+
return "generator expression";
1056+
case Yield_kind:
1057+
case YieldFrom_kind:
1058+
return "yield expression";
1059+
case Await_kind:
1060+
return "await expression";
1061+
case ListComp_kind:
1062+
return "list comprehension";
1063+
case SetComp_kind:
1064+
return "set comprehension";
1065+
case DictComp_kind:
1066+
return "dict comprehension";
1067+
case Dict_kind:
1068+
return "dict display";
1069+
case Set_kind:
1070+
return "set display";
1071+
case JoinedStr_kind:
1072+
case FormattedValue_kind:
1073+
return "f-string expression";
1074+
case Constant_kind: {
1075+
PyObject *value = e->v.Constant.value;
1076+
if (value == Py_None) {
1077+
return "None";
1078+
}
1079+
if (value == Py_False) {
1080+
return "False";
1081+
}
1082+
if (value == Py_True) {
1083+
return "True";
1084+
}
1085+
if (value == Py_Ellipsis) {
1086+
return "Ellipsis";
1087+
}
1088+
return "literal";
1089+
}
1090+
case Compare_kind:
1091+
return "comparison";
1092+
case IfExp_kind:
1093+
return "conditional expression";
1094+
case NamedExpr_kind:
1095+
return "named expression";
1096+
default:
1097+
PyErr_Format(PyExc_SystemError,
1098+
"unexpected expression in assignment %d (line %d)",
1099+
e->kind, e->lineno);
1100+
return NULL;
1101+
}
1102+
}
1103+
10321104
/* Set the context ctx for expr_ty e, recursively traversing e.
10331105
10341106
Only sets context for expr kinds that "can appear in assignment context"
@@ -1040,10 +1112,6 @@ static int
10401112
set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
10411113
{
10421114
asdl_seq *s = NULL;
1043-
/* If a particular expression type can't be used for assign / delete,
1044-
set expr_name to its name and an error message will be generated.
1045-
*/
1046-
const char* expr_name = NULL;
10471115

10481116
/* The ast defines augmented store and load contexts, but the
10491117
implementation here doesn't actually use them. The code may be
@@ -1056,136 +1124,41 @@ set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
10561124

10571125
switch (e->kind) {
10581126
case Attribute_kind:
1059-
if (ctx == NamedStore) {
1060-
expr_name = "attribute";
1061-
break;
1062-
}
1063-
10641127
e->v.Attribute.ctx = ctx;
10651128
if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
10661129
return 0;
10671130
break;
10681131
case Subscript_kind:
1069-
if (ctx == NamedStore) {
1070-
expr_name = "subscript";
1071-
break;
1072-
}
1073-
10741132
e->v.Subscript.ctx = ctx;
10751133
break;
10761134
case Starred_kind:
1077-
if (ctx == NamedStore) {
1078-
expr_name = "starred";
1079-
break;
1080-
}
1081-
10821135
e->v.Starred.ctx = ctx;
10831136
if (!set_context(c, e->v.Starred.value, ctx, n))
10841137
return 0;
10851138
break;
10861139
case Name_kind:
1087-
if (ctx == Store || ctx == NamedStore) {
1140+
if (ctx == Store) {
10881141
if (forbidden_name(c, e->v.Name.id, n, 0))
10891142
return 0; /* forbidden_name() calls ast_error() */
10901143
}
10911144
e->v.Name.ctx = ctx;
10921145
break;
10931146
case List_kind:
1094-
if (ctx == NamedStore) {
1095-
expr_name = "list";
1096-
break;
1097-
}
1098-
10991147
e->v.List.ctx = ctx;
11001148
s = e->v.List.elts;
11011149
break;
11021150
case Tuple_kind:
1103-
if (ctx == NamedStore) {
1104-
expr_name = "tuple";
1105-
break;
1106-
}
1107-
11081151
e->v.Tuple.ctx = ctx;
11091152
s = e->v.Tuple.elts;
11101153
break;
1111-
case Lambda_kind:
1112-
expr_name = "lambda";
1113-
break;
1114-
case Call_kind:
1115-
expr_name = "function call";
1116-
break;
1117-
case BoolOp_kind:
1118-
case BinOp_kind:
1119-
case UnaryOp_kind:
1120-
expr_name = "operator";
1121-
break;
1122-
case GeneratorExp_kind:
1123-
expr_name = "generator expression";
1124-
break;
1125-
case Yield_kind:
1126-
case YieldFrom_kind:
1127-
expr_name = "yield expression";
1128-
break;
1129-
case Await_kind:
1130-
expr_name = "await expression";
1131-
break;
1132-
case ListComp_kind:
1133-
expr_name = "list comprehension";
1134-
break;
1135-
case SetComp_kind:
1136-
expr_name = "set comprehension";
1137-
break;
1138-
case DictComp_kind:
1139-
expr_name = "dict comprehension";
1140-
break;
1141-
case Dict_kind:
1142-
expr_name = "dict display";
1143-
break;
1144-
case Set_kind:
1145-
expr_name = "set display";
1146-
break;
1147-
case JoinedStr_kind:
1148-
case FormattedValue_kind:
1149-
expr_name = "f-string expression";
1150-
break;
1151-
case Constant_kind: {
1152-
PyObject *value = e->v.Constant.value;
1153-
if (value == Py_None || value == Py_False || value == Py_True
1154-
|| value == Py_Ellipsis)
1155-
{
1156-
return ast_error(c, n, "cannot %s %R",
1157-
ctx == Store ? "assign to" : "delete",
1158-
value);
1154+
default: {
1155+
const char *expr_name = get_expr_name(e);
1156+
if (expr_name != NULL) {
1157+
ast_error(c, n, "cannot %s %s",
1158+
ctx == Store ? "assign to" : "delete",
1159+
expr_name);
11591160
}
1160-
expr_name = "literal";
1161-
break;
1162-
}
1163-
case Compare_kind:
1164-
expr_name = "comparison";
1165-
break;
1166-
case IfExp_kind:
1167-
expr_name = "conditional expression";
1168-
break;
1169-
case NamedExpr_kind:
1170-
expr_name = "named expression";
1171-
break;
1172-
default:
1173-
PyErr_Format(PyExc_SystemError,
1174-
"unexpected expression in %sassignment %d (line %d)",
1175-
ctx == NamedStore ? "named ": "",
1176-
e->kind, e->lineno);
11771161
return 0;
1178-
}
1179-
/* Check for error string set by switch */
1180-
if (expr_name) {
1181-
if (ctx == NamedStore) {
1182-
return ast_error(c, n, "cannot use named assignment with %s",
1183-
expr_name);
1184-
}
1185-
else {
1186-
return ast_error(c, n, "cannot %s %s",
1187-
ctx == Store ? "assign to" : "delete",
1188-
expr_name);
11891162
}
11901163
}
11911164

@@ -1895,7 +1868,15 @@ ast_for_namedexpr(struct compiling *c, const node *n)
18951868
if (!value)
18961869
return NULL;
18971870

1898-
if (!set_context(c, target, NamedStore, n))
1871+
if (target->kind != Name_kind) {
1872+
const char *expr_name = get_expr_name(target);
1873+
if (expr_name != NULL) {
1874+
ast_error(c, n, "cannot use named assignment with %s", expr_name);
1875+
}
1876+
return NULL;
1877+
}
1878+
1879+
if (!set_context(c, target, Store, n))
18991880
return NULL;
19001881

19011882
return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,

0 commit comments

Comments
 (0)