Skip to content

Commit ed72c0c

Browse files
committed
fix set flags for *args and **kwargs when dealing with generators
1 parent e9cc687 commit ed72c0c

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/PCode.java

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,10 @@ private static int extractStackSize(RootNode rootNode) {
214214
}
215215

216216
private void extractArgStats() {
217-
String[] freevars = getFreeVars(rootNode);
218-
String[] cellvars = getCellVars(rootNode);
219-
Set<String> freeVarsSet = asSet(freevars);
220-
Set<String> cellVarsSet = asSet(cellvars);
217+
this.freevars = getFreeVars(rootNode);
218+
this.cellvars = getCellVars(rootNode);
219+
Set<String> freeVarsSet = asSet((String[])freevars);
220+
Set<String> cellVarsSet = asSet((String[])cellvars);
221221

222222
List<ReadKeywordNode> readKeywordNodes = NodeUtil.findAllNodeInstances(rootNode, ReadKeywordNode.class);
223223
List<ReadIndexedArgumentNode> readIndexedArgumentNodes = NodeUtil.findAllNodeInstances(rootNode, ReadIndexedArgumentNode.class);
@@ -229,52 +229,49 @@ private void extractArgStats() {
229229
allArgNames.addAll(kwNames);
230230
allArgNames.addAll(argNames);
231231

232-
int argcount = readIndexedArgumentNodes.size();
233-
int kwonlyargcount = 0;
234-
int flags = 0;
232+
this.argcount = readIndexedArgumentNodes.size();
233+
this.kwonlyargcount = 0;
234+
this.flags = 0;
235235

236236
for (ReadKeywordNode kwNode : readKeywordNodes) {
237237
if (!kwNode.canBePositional()) {
238238
kwonlyargcount++;
239239
}
240240
}
241241

242-
Set<String> varnames = new HashSet<>();
242+
Set<String> varnamesSet = new HashSet<>();
243243
for (Object identifier : rootNode.getFrameDescriptor().getIdentifiers()) {
244244
if (identifier instanceof String) {
245245
String varName = (String) identifier;
246246

247247
if (core.getParser().isIdentifier(core, varName)) {
248248
if (allArgNames.contains(varName)) {
249-
varnames.add(varName);
249+
varnamesSet.add(varName);
250250
} else if (!freeVarsSet.contains(varName) && !cellVarsSet.contains(varName)) {
251-
varnames.add(varName);
251+
varnamesSet.add(varName);
252252
}
253253
}
254254
}
255255
}
256256

257-
// set the flags
257+
// 0x20 - generator
258+
RootNode funcRootNode = rootNode;
259+
if (funcRootNode instanceof GeneratorFunctionRootNode) {
260+
flags |= (1 << 5);
261+
funcRootNode = ((GeneratorFunctionRootNode) funcRootNode).getFunctionRootNode();
262+
}
263+
258264
// 0x04 - *arguments
259-
if (NodeUtil.findAllNodeInstances(rootNode, ReadVarArgsNode.class).size() == 1) {
265+
if (NodeUtil.findAllNodeInstances(funcRootNode, ReadVarArgsNode.class).size() == 1) {
260266
flags |= (1 << 2);
261267
}
262268
// 0x08 - **keywords
263-
if (NodeUtil.findAllNodeInstances(rootNode, ReadVarKeywordsNode.class).size() == 1) {
269+
if (NodeUtil.findAllNodeInstances(funcRootNode, ReadVarKeywordsNode.class).size() == 1) {
264270
flags |= (1 << 3);
265271
}
266-
// 0x20 - generator
267-
if (rootNode instanceof GeneratorFunctionRootNode) {
268-
flags |= (1 << 5);
269-
}
270272

271-
this.argcount = argcount;
272-
this.kwonlyargcount = kwonlyargcount;
273-
this.freevars = freevars;
274-
this.cellvars = cellvars;
275-
this.varnames = varnames.toArray();
276-
this.nlocals = varnames.size();
277-
this.flags = flags;
273+
this.varnames = varnamesSet.toArray();
274+
this.nlocals = varnamesSet.size();
278275
}
279276

280277
public RootNode getRootNode() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/generator/GeneratorFunctionRootNode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import com.oracle.truffle.api.RootCallTarget;
4949
import com.oracle.truffle.api.frame.FrameDescriptor;
5050
import com.oracle.truffle.api.frame.VirtualFrame;
51+
import com.oracle.truffle.api.nodes.RootNode;
5152

5253
public class GeneratorFunctionRootNode extends PClosureFunctionRootNode {
5354
private final RootCallTarget callTarget;
@@ -76,4 +77,8 @@ public GeneratorFunctionRootNode(PythonLanguage language, RootCallTarget callTar
7677
public Object execute(VirtualFrame frame) {
7778
return factory.createGenerator(getName(), callTarget, frameDescriptor, frame.getArguments(), closure, cellSlots, numOfActiveFlags, numOfGeneratorBlockNode, numOfGeneratorForNode);
7879
}
80+
81+
public RootNode getFunctionRootNode() {
82+
return callTarget.getRootNode();
83+
}
7984
}

0 commit comments

Comments
 (0)