-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathPyBaseCode.java
More file actions
372 lines (335 loc) · 14.6 KB
/
Copy pathPyBaseCode.java
File metadata and controls
372 lines (335 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* Copyright (c) Corporation for National Research Initiatives
* Copyright (c) Jython Developers
*/
package org.python.core;
import com.google.common.base.Joiner;
import org.python.modules._systemrestart;
import com.google.common.base.CharMatcher;
import java.util.ArrayList;
public abstract class PyBaseCode extends PyCode {
public int co_argcount;
public int co_kwonlyargcount;
int nargs;
public int co_firstlineno = -1;
public String[] co_varnames;
public String[] co_names;
public String[] co_cellvars;
public String[] co_freevars;
public PyObject[] co_consts;
public String co_filename;
public int jy_npurecell; // internal: jython specific
public CompilerFlags co_flags = new CompilerFlags();
public int co_nlocals;
public boolean varargs, varkwargs;
public boolean hasFreevars() {
return co_freevars != null && co_freevars.length > 0;
}
@Override
public PyObject call(ThreadState ts, PyFrame frame, PyObject closure) {
if (ts.systemState == null) {
ts.systemState = Py.defaultSystemState;
}
// Push frame
frame.f_back = ts.frame;
if (frame.f_builtins == null) {
if (frame.f_back != null) {
frame.f_builtins = frame.f_back.f_builtins;
} else {
frame.f_builtins = ts.systemState.builtins;
}
}
// nested scopes: setup env with closure
// this should only be done once, so let the frame take care of it
frame.setupEnv((PyTuple) closure);
ts.frame = frame;
// Handle trace function for debugging
if (ts.tracefunc != null) {
frame.f_lineno = co_firstlineno;
frame.tracefunc = ts.tracefunc.traceCall(frame);
}
// Handle trace function for profiling
if (ts.profilefunc != null) {
ts.profilefunc.traceCall(frame);
}
PyObject ret;
ThreadStateMapping.enterCall(ts);
try {
ret = interpret(frame, ts);
} catch (Throwable t) {
// Convert Exceptions that occurred in Java code to PyExceptions
PyException pye = Py.JavaError(t);
pye.normalize();
pye.tracebackHere(frame);
frame.f_lasti = -1;
if (frame.tracefunc != null) {
frame.tracefunc.traceException(frame, pye);
}
if (ts.profilefunc != null) {
ts.profilefunc.traceException(frame, pye);
}
ts.frame = ts.frame.f_back;
throw pye;
} finally {
ThreadStateMapping.exitCall(ts);
}
if (frame.tracefunc != null) {
frame.tracefunc.traceReturn(frame, ret);
}
// Handle trace function for profiling
if (ts.profilefunc != null) {
ts.profilefunc.traceReturn(frame, ret);
}
ts.frame = ts.frame.f_back;
// Check for interruption, which is used for restarting the interpreter
// on Jython
if (ts.systemState._systemRestart && Thread.currentThread().isInterrupted()) {
throw new PyException(_systemrestart.SystemRestart);
}
return ret;
}
@Override
public PyObject call(ThreadState state, PyObject globals, PyObject[] defaults,
PyDictionary kw_defaults, PyObject closure)
{
if (co_argcount != 0 || co_kwonlyargcount != 0 || varargs || varkwargs || !kw_defaults.isEmpty())
return call(state, Py.EmptyObjects, Py.NoKeywords, globals, defaults,
kw_defaults, closure);
PyFrame frame = new PyFrame(this, globals);
if (co_flags.isFlagSet(CodeFlag.CO_COROUTINE)) {
return new PyCoroutine(frame, closure);
} else if (co_flags.isFlagSet(CodeFlag.CO_GENERATOR)) {
return new PyGenerator(frame, closure);
}
return call(state, frame, closure);
}
@Override
public PyObject call(ThreadState state, PyObject arg1, PyObject globals, PyObject[] defaults,
PyDictionary kw_defaults, PyObject closure)
{
if (co_argcount != 1 || varargs || varkwargs || !kw_defaults.isEmpty())
return call(state, new PyObject[] {arg1},
Py.NoKeywords, globals, defaults, kw_defaults, closure);
PyFrame frame = new PyFrame(this, globals);
frame.f_fastlocals[0] = arg1;
if (co_flags.isFlagSet(CodeFlag.CO_COROUTINE)) {
return new PyCoroutine(frame, closure);
} else if (co_flags.isFlagSet(CodeFlag.CO_GENERATOR)) {
return new PyGenerator(frame, closure);
}
return call(state, frame, closure);
}
@Override
public PyObject call(ThreadState state, PyObject arg1, PyObject arg2, PyObject globals,
PyObject[] defaults, PyDictionary kw_defaults, PyObject closure)
{
if (co_argcount != 2 || varargs || varkwargs || !kw_defaults.isEmpty())
return call(state, new PyObject[] {arg1, arg2},
Py.NoKeywords, globals, defaults, kw_defaults, closure);
PyFrame frame = new PyFrame(this, globals);
frame.f_fastlocals[0] = arg1;
frame.f_fastlocals[1] = arg2;
if (co_flags.isFlagSet(CodeFlag.CO_GENERATOR)) {
return new PyGenerator(frame, closure);
} else if (co_flags.isFlagSet(CodeFlag.CO_COROUTINE)) {
return new PyCoroutine(frame, closure);
}
return call(state, frame, closure);
}
@Override
public PyObject call(ThreadState state, PyObject arg1, PyObject arg2, PyObject arg3,
PyObject globals, PyObject[] defaults, PyDictionary kw_defaults,
PyObject closure)
{
if (co_argcount != 3 || varargs || varkwargs || !kw_defaults.isEmpty())
return call(state, new PyObject[] {arg1, arg2, arg3},
Py.NoKeywords, globals, defaults, kw_defaults, closure);
PyFrame frame = new PyFrame(this, globals);
frame.f_fastlocals[0] = arg1;
frame.f_fastlocals[1] = arg2;
frame.f_fastlocals[2] = arg3;
if (co_flags.isFlagSet(CodeFlag.CO_GENERATOR)) {
return new PyGenerator(frame, closure);
} else if (co_flags.isFlagSet(CodeFlag.CO_COROUTINE)) {
return new PyCoroutine(frame, closure);
}
return call(state, frame, closure);
}
@Override
public PyObject call(ThreadState state, PyObject arg1, PyObject arg2,
PyObject arg3, PyObject arg4, PyObject globals,
PyObject[] defaults, PyDictionary kw_defaults, PyObject closure) {
if (co_argcount != 4 || varargs || varkwargs || !kw_defaults.isEmpty())
return call(state, new PyObject[]{arg1, arg2, arg3, arg4},
Py.NoKeywords, globals, defaults, kw_defaults, closure);
PyFrame frame = new PyFrame(this, globals);
frame.f_fastlocals[0] = arg1;
frame.f_fastlocals[1] = arg2;
frame.f_fastlocals[2] = arg3;
frame.f_fastlocals[3] = arg4;
if (co_flags.isFlagSet(CodeFlag.CO_GENERATOR)) {
return new PyGenerator(frame, closure);
} else if (co_flags.isFlagSet(CodeFlag.CO_COROUTINE)) {
return new PyCoroutine(frame, closure);
}
return call(state, frame, closure);
}
@Override
public PyObject call(ThreadState state, PyObject self, PyObject args[],
String keywords[], PyObject globals,
PyObject[] defaults, PyDictionary kw_defaults, PyObject closure)
{
PyObject[] os = new PyObject[args.length+1];
os[0] = self;
System.arraycopy(args, 0, os, 1, args.length);
return call(state, os, keywords, globals, defaults, kw_defaults, closure);
}
@Override
public PyObject call(ThreadState state, PyObject args[], String kws[], PyObject globals,
PyObject[] defs, PyDictionary kw_defaults, PyObject closure) {
final PyFrame frame = new PyFrame(this, globals);
int paramCount = co_argcount + co_kwonlyargcount;
if (varargs) paramCount++;
if (varkwargs) paramCount++;
final int argcount = args.length - kws.length;
if ((paramCount > 0) || varargs || varkwargs) {
int i;
int n = argcount;
PyObject kwdict = null;
final PyObject[] fastlocals = frame.f_fastlocals;
if (varkwargs) {
kwdict = new PyDictionary();
i = co_argcount + co_kwonlyargcount;
if (varargs) {
i++;
}
fastlocals[i] = kwdict;
}
if (argcount > co_argcount) {
if (!varargs) {
int defcount = defs != null ? defs.length : 0;
String msg;
if (defcount > 0) {
msg = positionalArgErrorMessage(defcount, args.length);
} else {
msg = String.format("%.200s() takes %d positional argument but %d were given",
co_name, co_argcount, args.length);
}
throw Py.TypeError(msg);
}
n = co_argcount;
}
if (args.length > 0) {
System.arraycopy(args, 0, fastlocals, 0, n);
}
if (varargs) {
PyObject[] u = new PyObject[argcount - n];
if (args.length > 0)
System.arraycopy(args, n, u, 0, u.length);
PyObject uTuple = new PyTuple(u);
fastlocals[co_argcount] = uTuple;
}
for (i = 0; i < kws.length; i++) {
String keyword = kws[i];
PyObject value = args[i + argcount];
int j;
for (j = 0; j < paramCount; j++) {
if (co_varnames[j].equals(keyword)) {
break;
}
}
if (j == paramCount) { // not in varnames
if (kwdict == null) {
throw Py.TypeError(String.format(
"%.200s() got an unexpected keyword argument '%.400s'",
co_name,
Py.newUnicode(keyword).encode("ascii", "replace")));
}
if (CharMatcher.ASCII.matchesAllOf(keyword)) {
kwdict.__setitem__(keyword, value);
} else {
kwdict.__setitem__(Py.newUnicode(keyword), value);
}
} else {
if (fastlocals[j] != null) {
throw Py.TypeError(String.format("%.200s() got multiple values for "
+ "keyword argument '%.400s'",
co_name, keyword));
}
fastlocals[j] = value;
}
}
java.util.List<String> missingKwArg = new ArrayList<>();
int kwonlyargZeroIndex = co_argcount;
if (varargs) kwonlyargZeroIndex++;
for (int j = 0; j < co_kwonlyargcount; j++) {
int kwonlyargIdx = kwonlyargZeroIndex + j;
String name = co_varnames[kwonlyargIdx];
PyUnicode key = Py.newUnicode(name);
if (fastlocals[kwonlyargIdx] == null) {
if (kw_defaults.__contains__(key)) {
fastlocals[kwonlyargIdx] = kw_defaults.__getitem__(key);
} else {
missingKwArg.add(name);
}
}
}
if (!missingKwArg.isEmpty()) {
throw Py.TypeError(String.format("%.200s() missing %d keyword-only %s: '%s'", co_name, missingKwArg.size(),
missingKwArg.size() > 1 ? "arguments" : "argument", Joiner.on(',').join(missingKwArg)));
}
if (argcount < co_argcount) {
final int defcount = defs != null ? defs.length : 0;
final int m = co_argcount - defcount;
for (i = argcount; i < m; i++) {
if (fastlocals[i] == null) {
String msg =
String.format("%.200s() takes %s %d %sargument%s (%d given)",
co_name,
(varargs || defcount > 0) ? "at least" : "exactly",
m,
kws.length > 0 ? "" : "",
m == 1 ? "" : "s",
args.length);
throw Py.TypeError(msg);
}
}
if (n > m) {
i = n - m;
} else {
i = 0;
}
for (; i < defcount; i++) {
if (fastlocals[m + i] == null) {
fastlocals[m + i] = defs[i];
}
}
}
} else if ((argcount > 0) || (args.length > 0 && (paramCount == 0 && !varargs && !varkwargs))) {
throw Py.TypeError(String.format("%.200s() takes no arguments (%d given)",
co_name, args.length));
}
if (co_flags.isFlagSet(CodeFlag.CO_GENERATOR)) {
return new PyGenerator(frame, closure);
} else if (co_flags.isFlagSet(CodeFlag.CO_COROUTINE)) {
return new PyCoroutine(frame, closure);
}
return call(state, frame, closure);
}
public String toString() {
return String.format("<code object %.100s at %s, file \"%.300s\", line %d>",
co_name, Py.idstr(this), co_filename, co_firstlineno);
}
protected abstract PyObject interpret(PyFrame f, ThreadState ts);
protected int getline(PyFrame f) {
return f.f_lineno;
}
// returns the augmented version of CompilerFlags (instead of just as a bit vector int)
public CompilerFlags getCompilerFlags() {
return co_flags;
}
private String positionalArgErrorMessage(int defcount, int argLength) {
return String.format("%.200s() takes from %d to %d positional arguments but %d were given",
co_name, co_argcount - defcount, co_argcount, argLength);
}
}