Skip to content

Commit 2596273

Browse files
authored
Update AST for extended unpacking (#1172)
1 parent 7b15d10 commit 2596273

File tree

2 files changed

+67
-83
lines changed

2 files changed

+67
-83
lines changed

Src/IronPython/Modules/_ast.cs

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,17 +1022,14 @@ internal override AstExpression Revert() {
10221022
[PythonType]
10231023
public class Call : expr {
10241024
public Call() {
1025-
_fields = PythonTuple.MakeTuple(new[] { nameof(func), nameof(args), nameof(keywords), nameof(starargs), nameof(kwargs) });
1025+
_fields = PythonTuple.MakeTuple(new[] { nameof(func), nameof(args), nameof(keywords) });
10261026
}
10271027

1028-
public Call(expr func, PythonList args, PythonList keywords, expr starargs, expr kwargs,
1029-
[Optional]int? lineno, [Optional]int? col_offset)
1028+
public Call(expr func, PythonList args, PythonList keywords, [Optional] int? lineno, [Optional] int? col_offset)
10301029
: this() {
10311030
this.func = func;
10321031
this.args = args;
10331032
this.keywords = keywords;
1034-
this.starargs = starargs;
1035-
this.kwargs = kwargs;
10361033
_lineno = lineno;
10371034
_col_offset = col_offset;
10381035
}
@@ -1043,31 +1040,33 @@ internal Call(CallExpression call)
10431040
keywords = new PythonList();
10441041
func = Convert(call.Target);
10451042
foreach (Arg arg in call.Args) {
1046-
if (arg.Name == null)
1043+
if (arg.Name == "*") {
1044+
var loc = arg.Start;
1045+
args.Add(new Starred(Convert(arg.Expression), Load.Instance, loc.Line, loc.Column));
1046+
} else {
10471047
args.Add(Convert(arg.Expression));
1048-
else // name == "*"
1049-
starargs = Convert(arg.Expression);
1048+
}
10501049
}
10511050
foreach (Arg arg in call.Kwargs) {
1052-
if (arg.Name == "**")
1053-
kwargs = Convert(arg.Expression);
1054-
else // name is proper
1055-
keywords.Add(new keyword(arg));
1051+
keywords.Add(new keyword(arg.Name == "**" ? null : arg.Name, Convert(arg.Expression)));
10561052
}
10571053
}
10581054

10591055
internal override AstExpression Revert() {
10601056
AstExpression target = expr.Revert(func);
10611057
List<Arg> newArgs = new List<Arg>();
10621058
List<Arg> newKwargs = new List<Arg>();
1063-
foreach (expr ex in args)
1064-
newArgs.Add(new Arg(expr.Revert(ex)));
1065-
if (null != starargs)
1066-
newArgs.Add(new Arg("*", expr.Revert(starargs)));
1067-
foreach (keyword kw in keywords)
1068-
newKwargs.Add(new Arg(kw.arg, expr.Revert(kw.value)));
1069-
if (null != kwargs)
1070-
newKwargs.Add(new Arg("**", expr.Revert(kwargs)));
1059+
foreach (expr ex in args) {
1060+
if (ex is Starred starred) {
1061+
newArgs.Add(new Arg("*", starred.value?.Revert()));
1062+
} else {
1063+
newArgs.Add(new Arg(ex?.Revert()));
1064+
}
1065+
}
1066+
foreach (keyword kw in keywords) {
1067+
newKwargs.Add(new Arg(kw.arg is null ? "**" : kw.arg, kw.value?.Revert()));
1068+
}
1069+
10711070
return new CallExpression(target, newArgs, newKwargs);
10721071
}
10731072

@@ -1076,26 +1075,19 @@ internal override AstExpression Revert() {
10761075
public PythonList args { get; set; }
10771076

10781077
public PythonList keywords { get; set; }
1079-
1080-
public expr starargs { get; set; } // TODO: remove in 3.5
1081-
1082-
public expr kwargs { get; set; } // TODO: remove in 3.5
10831078
}
10841079

10851080
[PythonType]
10861081
public class ClassDef : stmt {
10871082
public ClassDef() {
1088-
_fields = PythonTuple.MakeTuple(new[] { nameof(name), nameof(bases), nameof(keywords), nameof(starargs), nameof(kwargs), nameof(body), nameof(decorator_list) });
1083+
_fields = PythonTuple.MakeTuple(new[] { nameof(name), nameof(bases), nameof(keywords), nameof(body), nameof(decorator_list) });
10891084
}
10901085

1091-
public ClassDef(string name, PythonList bases, PythonList keywords, object starargs, object kwargs, PythonList body, PythonList decorator_list,
1092-
[Optional]int? lineno, [Optional]int? col_offset)
1086+
public ClassDef(string name, PythonList bases, PythonList keywords, PythonList body, PythonList decorator_list, [Optional] int? lineno, [Optional] int? col_offset)
10931087
: this() {
10941088
this.name = name;
10951089
this.bases = bases;
10961090
this.keywords = keywords;
1097-
this.starargs = starargs;
1098-
this.kwargs = kwargs;
10991091
this.body = body;
11001092
this.decorator_list = decorator_list;
11011093
_lineno = lineno;
@@ -1107,17 +1099,16 @@ internal ClassDef(ClassDefinition def)
11071099
name = def.Name;
11081100
bases = new PythonList(def.Bases.Count);
11091101
foreach (Arg arg in def.Bases) {
1110-
if (arg.Name == null)
1102+
if (arg.Name == "*") {
1103+
var loc = arg.Start;
1104+
bases.Add(new Starred(Convert(arg.Expression), Load.Instance, loc.Line, loc.Column));
1105+
} else {
11111106
bases.Add(Convert(arg.Expression));
1112-
else // name == "*"
1113-
starargs = Convert(arg.Expression);
1107+
}
11141108
}
11151109
keywords = new PythonList(def.Keywords.Count);
11161110
foreach (Arg arg in def.Keywords) {
1117-
if (arg.Name == "**")
1118-
kwargs = Convert(arg.Expression);
1119-
else // name is proper
1120-
keywords.Add(new keyword(arg));
1111+
keywords.Add(new keyword(arg.Name == "**" ? null : arg.Name, Convert(arg.Expression)));
11211112
}
11221113
body = ConvertStatements(def.Body);
11231114
if (def.Decorators != null) {
@@ -1132,14 +1123,16 @@ internal ClassDef(ClassDefinition def)
11321123
internal override Statement Revert() {
11331124
List<Arg> newBases = new List<Arg>();
11341125
List<Arg> newKeywords = new List<Arg>();
1135-
foreach (expr ex in bases)
1136-
newBases.Add(new Arg(expr.Revert(ex)));
1137-
if (null != starargs)
1138-
newBases.Add(new Arg("*", expr.Revert(starargs)));
1139-
foreach (keyword kw in keywords)
1140-
newKeywords.Add(new Arg(kw.arg, expr.Revert(kw.value)));
1141-
if (null != kwargs)
1142-
newKeywords.Add(new Arg("**", expr.Revert(kwargs)));
1126+
foreach (expr ex in bases) {
1127+
if (ex is Starred starred) {
1128+
newBases.Add(new Arg("*", starred.value?.Revert()));
1129+
} else {
1130+
newBases.Add(new Arg(ex?.Revert()));
1131+
}
1132+
}
1133+
foreach (keyword kw in keywords) {
1134+
newKeywords.Add(new Arg(kw.arg is null ? "**" : kw.arg, kw.value?.Revert()));
1135+
}
11431136

11441137
ClassDefinition cd = new ClassDefinition(name, newBases, newKeywords, RevertStmts(body));
11451138
if (decorator_list.Count != 0)
@@ -1153,10 +1146,6 @@ internal override Statement Revert() {
11531146

11541147
public PythonList keywords { get; set; }
11551148

1156-
public object starargs { get; set; } // TODO: remove in 3.5
1157-
1158-
public object kwargs { get; set; } // TODO: remove in 3.5
1159-
11601149
public PythonList body { get; set; }
11611150

11621151
public PythonList decorator_list { get; set; }

0 commit comments

Comments
 (0)