50
50
import com .oracle .graal .python .nodes .ModuleRootNode ;
51
51
import com .oracle .graal .python .nodes .argument .ReadIndexedArgumentNode ;
52
52
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 ;
53
55
import com .oracle .graal .python .nodes .frame .WriteIdentifierNode ;
54
56
import com .oracle .graal .python .nodes .function .FunctionRootNode ;
55
57
import com .oracle .graal .python .nodes .generator .GeneratorFunctionRootNode ;
@@ -70,8 +72,12 @@ public class PCode extends PythonBuiltinObject {
70
72
private final int nlocals ;
71
73
// is the required stack size (including local variables)
72
74
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.
75
81
private final int flags ;
76
82
// is a string representing the sequence of bytecode instructions
77
83
private final String codestring ;
@@ -110,9 +116,9 @@ public PCode(PythonClass cls, RootNode rootNode, PythonCore core) {
110
116
this .cellvars = argStats .cellVars ;
111
117
this .varnames = argStats .varNames ;
112
118
this .nlocals = argStats .nLocals ;
119
+ this .flags = argStats .flags ;
113
120
114
121
this .stacksize = getStackSize (rootNode );
115
- this .flags = -1 ;
116
122
this .codestring = null ;
117
123
this .constants = null ;
118
124
this .names = null ;
@@ -217,20 +223,22 @@ private static Set<String> getArgumentNames(List<ReadIndexedArgumentNode> readIn
217
223
}
218
224
219
225
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 ) {
228
235
this .argCnt = argCnt ;
229
236
this .kwOnlyArgCnt = kwOnlyArgCnt ;
230
237
this .varNames = varNames ;
231
238
this .freeVars = freeVars ;
232
239
this .cellVars = cellVars ;
233
240
this .nLocals = varNames .length ;
241
+ this .flags = flags ;
234
242
}
235
243
}
236
244
@@ -252,6 +260,7 @@ private static ArgStats getArgStats(RootNode rootNode, PythonCore core) {
252
260
253
261
int argC = readIndexedArgumentNodes .size ();
254
262
int kwOnlyArgC = 0 ;
263
+ int flags = 0 ;
255
264
256
265
for (ReadKeywordNode kwNode : readKeywordNodes ) {
257
266
if (!kwNode .canBePositional ()) {
@@ -273,7 +282,22 @@ private static ArgStats getArgStats(RootNode rootNode, PythonCore core) {
273
282
}
274
283
}
275
284
}
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 );
277
301
}
278
302
279
303
private static int getStackSize (RootNode rootNode ) {
@@ -320,7 +344,7 @@ public int getStacksize() {
320
344
return stacksize ;
321
345
}
322
346
323
- public int getFlags () {
347
+ public long getFlags () {
324
348
return flags ;
325
349
}
326
350
0 commit comments