Skip to content

Commit 304b574

Browse files
committed
Code object add support for co_flags
1 parent 0650ac4 commit 304b574

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def test_code_attributes():
8181
assert code.co_kwonlyargcount == 0
8282
assert code.co_nlocals == 6
8383
assert code.co_stacksize >= code.co_nlocals
84-
# assert code.co_flags
84+
assert code.co_flags == 0
8585
# assert code.co_code
8686
# assert code.co_consts
8787
# assert set(code.co_names) == {'set', 'TypeError', 'print'}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ protected Object get(PCode self) {
150150
@GenerateNodeFactory
151151
public abstract static class GetFlagsNode extends PythonBuiltinNode {
152152
@Specialization
153-
protected Object get(@SuppressWarnings("unused") PCode self) {
154-
return PNotImplemented.NOT_IMPLEMENTED;
153+
protected Object get(PCode self) {
154+
return self.getFlags();
155155
}
156156
}
157157

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

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import com.oracle.graal.python.nodes.ModuleRootNode;
5151
import com.oracle.graal.python.nodes.argument.ReadIndexedArgumentNode;
5252
import com.oracle.graal.python.nodes.argument.ReadKeywordNode;
53+
import com.oracle.graal.python.nodes.argument.ReadVarArgsNode;
54+
import com.oracle.graal.python.nodes.argument.ReadVarKeywordsNode;
5355
import com.oracle.graal.python.nodes.frame.WriteIdentifierNode;
5456
import com.oracle.graal.python.nodes.function.FunctionRootNode;
5557
import com.oracle.graal.python.nodes.generator.GeneratorFunctionRootNode;
@@ -70,8 +72,12 @@ public class PCode extends PythonBuiltinObject {
7072
private final int nlocals;
7173
// is the required stack size (including local variables)
7274
private final int stacksize;
73-
// bitmap of CO_* flags, read more
74-
// (https://docs.python.org/3/library/inspect.html#inspect-module-co-flags)
75+
// is an integer encoding a number of flags for the interpreter.
76+
//
77+
// The following flag bits are defined for co_flags: bit 0x04 is set if the function uses the
78+
// *arguments syntax to accept an arbitrary number of positional arguments; bit 0x08 is set if
79+
// the function uses the **keywords syntax to accept arbitrary keyword arguments; bit 0x20 is
80+
// set if the function is a generator.
7581
private final int flags;
7682
// is a string representing the sequence of bytecode instructions
7783
private final String codestring;
@@ -110,9 +116,9 @@ public PCode(PythonClass cls, RootNode rootNode, PythonCore core) {
110116
this.cellvars = argStats.cellVars;
111117
this.varnames = argStats.varNames;
112118
this.nlocals = argStats.nLocals;
119+
this.flags = argStats.flags;
113120

114121
this.stacksize = getStackSize(rootNode);
115-
this.flags = -1;
116122
this.codestring = null;
117123
this.constants = null;
118124
this.names = null;
@@ -217,20 +223,22 @@ private static Set<String> getArgumentNames(List<ReadIndexedArgumentNode> readIn
217223
}
218224

219225
private final static class ArgStats {
220-
public final int argCnt;
221-
private final int kwOnlyArgCnt;
222-
private final Object[] varNames;
223-
private final Object[] freeVars;
224-
private final Object[] cellVars;
225-
private final int nLocals;
226-
227-
private ArgStats(int argCnt, int kwOnlyArgCnt, Object[] varNames, Object[] freeVars, Object[] cellVars) {
226+
final int argCnt;
227+
final int kwOnlyArgCnt;
228+
final Object[] varNames;
229+
final Object[] freeVars;
230+
final Object[] cellVars;
231+
final int flags;
232+
final int nLocals;
233+
234+
private ArgStats(int argCnt, int kwOnlyArgCnt, Object[] varNames, Object[] freeVars, Object[] cellVars, int flags) {
228235
this.argCnt = argCnt;
229236
this.kwOnlyArgCnt = kwOnlyArgCnt;
230237
this.varNames = varNames;
231238
this.freeVars = freeVars;
232239
this.cellVars = cellVars;
233240
this.nLocals = varNames.length;
241+
this.flags = flags;
234242
}
235243
}
236244

@@ -252,6 +260,7 @@ private static ArgStats getArgStats(RootNode rootNode, PythonCore core) {
252260

253261
int argC = readIndexedArgumentNodes.size();
254262
int kwOnlyArgC = 0;
263+
int flags = 0;
255264

256265
for (ReadKeywordNode kwNode : readKeywordNodes) {
257266
if (!kwNode.canBePositional()) {
@@ -273,7 +282,22 @@ private static ArgStats getArgStats(RootNode rootNode, PythonCore core) {
273282
}
274283
}
275284
}
276-
return new ArgStats(argC, kwOnlyArgC, varNames.toArray(), freeVars, cellVars);
285+
286+
// set the flags
287+
// 0x04 - *arguments
288+
if (NodeUtil.findAllNodeInstances(rootNode, ReadVarArgsNode.class).size() == 1) {
289+
flags |= (1 << 2);
290+
}
291+
// 0x08 - **keywords
292+
if (NodeUtil.findAllNodeInstances(rootNode, ReadVarKeywordsNode.class).size() == 1) {
293+
flags |= (1 << 3);
294+
}
295+
// 0x20 - generator
296+
if (rootNode instanceof GeneratorFunctionRootNode) {
297+
flags |= (1 << 5);
298+
}
299+
300+
return new ArgStats(argC, kwOnlyArgC, varNames.toArray(), freeVars, cellVars, flags);
277301
}
278302

279303
private static int getStackSize(RootNode rootNode) {
@@ -320,7 +344,7 @@ public int getStacksize() {
320344
return stacksize;
321345
}
322346

323-
public int getFlags() {
347+
public long getFlags() {
324348
return flags;
325349
}
326350

0 commit comments

Comments
 (0)