Skip to content

Commit 331a0c7

Browse files
committed
fix code object lnotab, code and flags internal types
- changed exception message to match cpython on incompatible args (builtin constructor) - code builtins getters return empty vals for the unsupported types
1 parent 9979be0 commit 331a0c7

File tree

4 files changed

+54
-41
lines changed

4 files changed

+54
-41
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETITEM__;
5353
import static com.oracle.graal.python.runtime.exception.PythonErrorType.NotImplementedError;
5454
import static com.oracle.graal.python.runtime.exception.PythonErrorType.StopIteration;
55+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SystemError;
5556
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
5657
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
5758

@@ -1843,8 +1844,6 @@ Object call() {
18431844
@Builtin(name = "code", constructsClass = {PythonBuiltinClassType.PCode}, isPublic = false, minNumOfPositionalArgs = 14, maxNumOfPositionalArgs = 16)
18441845
@GenerateNodeFactory
18451846
public abstract static class CodeTypeNode extends PythonBuiltinNode {
1846-
@Child private SequenceStorageNodes.ToByteArrayNode toByteArrayNode;
1847-
18481847
@Specialization
18491848
Object call(PythonClass cls, int argcount, int kwonlyargcount,
18501849
int nlocals, int stacksize, int flags,
@@ -1854,26 +1853,29 @@ Object call(PythonClass cls, int argcount, int kwonlyargcount,
18541853
String lnotab) {
18551854
return factory().createCode(cls, argcount, kwonlyargcount,
18561855
nlocals, stacksize, flags,
1857-
codestring, constants, names,
1856+
toBytes(codestring), constants.getArray(), names.getArray(),
18581857
varnames.getArray(), freevars.getArray(), cellvars.getArray(),
18591858
filename, name, firstlineno,
1860-
lnotab);
1859+
toBytes(lnotab));
18611860
}
18621861

18631862
@Specialization
1864-
@TruffleBoundary
18651863
Object call(PythonClass cls, int argcount, int kwonlyargcount,
18661864
int nlocals, int stacksize, int flags,
18671865
PBytes codestring, PTuple constants, PTuple names,
18681866
PTuple varnames, PTuple freevars, PTuple cellvars,
18691867
PString filename, PString name, int firstlineno,
1870-
PBytes lnotab) {
1868+
PBytes lnotab,
1869+
@Cached("create()") SequenceStorageNodes.ToByteArrayNode toByteArrayNode) {
1870+
byte[] codeBytes = toByteArrayNode.execute(codestring.getSequenceStorage());
1871+
byte[] lnotabBytes = toByteArrayNode.execute(lnotab.getSequenceStorage());
1872+
18711873
return factory().createCode(cls, argcount, kwonlyargcount,
18721874
nlocals, stacksize, flags,
1873-
toString(getByteArray(codestring)), constants, names,
1875+
codeBytes, constants.getArray(), names.getArray(),
18741876
varnames.getArray(), freevars.getArray(), cellvars.getArray(),
18751877
filename.getValue(), name.getValue(), firstlineno,
1876-
lnotab);
1878+
lnotabBytes);
18771879
}
18781880

18791881
@SuppressWarnings("unused")
@@ -1884,20 +1886,12 @@ Object call(Object cls, Object argcount, Object kwonlyargcount,
18841886
Object varnames, Object freevars, Object cellvars,
18851887
Object filename, Object name, Object firstlineno,
18861888
Object lnotab) {
1887-
throw raise(PythonErrorType.NotImplementedError, "code object instance from generic arguments");
1889+
throw raise(SystemError, "bad argument to internal function");
18881890
}
18891891

18901892
@TruffleBoundary
1891-
private static String toString(byte[] data) {
1892-
return new String(data);
1893-
}
1894-
1895-
private byte[] getByteArray(PIBytesLike pByteArray) {
1896-
if (toByteArrayNode == null) {
1897-
CompilerDirectives.transferToInterpreterAndInvalidate();
1898-
toByteArrayNode = insert(SequenceStorageNodes.ToByteArrayNode.create());
1899-
}
1900-
return toByteArrayNode.execute(pByteArray.getSequenceStorage());
1893+
private static byte[] toBytes(String data) {
1894+
return data.getBytes();
19011895
}
19021896
}
19031897

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
3434
import com.oracle.graal.python.builtins.PythonBuiltins;
3535
import com.oracle.graal.python.builtins.objects.PNone;
36-
import com.oracle.graal.python.builtins.objects.PNotImplemented;
3736
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
3837
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
3938
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -160,26 +159,41 @@ protected Object get(PCode self) {
160159
@GenerateNodeFactory
161160
public abstract static class GetCodeNode extends PythonBuiltinNode {
162161
@Specialization
163-
protected Object get(@SuppressWarnings("unused") PCode self) {
164-
return PNotImplemented.NOT_IMPLEMENTED;
162+
protected Object get(PCode self) {
163+
byte[] codestring = self.getCodestring();
164+
if (codestring == null) {
165+
// TODO: this is for the moment undefined
166+
codestring = new byte[0];
167+
}
168+
return factory().createBytes(codestring);
165169
}
166170
}
167171

168172
@Builtin(name = "co_consts", fixedNumOfPositionalArgs = 1, isGetter = true)
169173
@GenerateNodeFactory
170174
public abstract static class GetConstsNode extends PythonBuiltinNode {
171175
@Specialization
172-
protected Object get(@SuppressWarnings("unused") PCode self) {
173-
return PNotImplemented.NOT_IMPLEMENTED;
176+
protected Object get(PCode self) {
177+
Object[] constants = self.getConstants();
178+
if (constants == null) {
179+
// TODO: this is for the moment undefined (see co_code)
180+
constants = new Object[0];
181+
}
182+
return factory().createTuple(constants);
174183
}
175184
}
176185

177186
@Builtin(name = "co_names", fixedNumOfPositionalArgs = 1, isGetter = true)
178187
@GenerateNodeFactory
179188
public abstract static class GetNamesNode extends PythonBuiltinNode {
180189
@Specialization
181-
protected Object get(@SuppressWarnings("unused") PCode self) {
182-
return PNotImplemented.NOT_IMPLEMENTED;
190+
protected Object get(PCode self) {
191+
Object[] names = self.getNames();
192+
if (names == null) {
193+
// TODO: this is for the moment undefined (see co_code)
194+
names = new Object[0];
195+
}
196+
return factory().createTuple(names);
183197
}
184198
}
185199

@@ -200,8 +214,13 @@ protected Object get(PCode self) {
200214
@GenerateNodeFactory
201215
public abstract static class GetLNoTabNode extends PythonBuiltinNode {
202216
@Specialization
203-
protected Object get(@SuppressWarnings("unused") PCode self) {
204-
return PNotImplemented.NOT_IMPLEMENTED;
217+
protected Object get(PCode self) {
218+
byte[] lnotab = self.getLnotab();
219+
if (lnotab == null) {
220+
// TODO: this is for the moment undefined (see co_code)
221+
lnotab = new byte[0];
222+
}
223+
return factory().createBytes(lnotab);
205224
}
206225
}
207226
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ public class PCode extends PythonBuiltinObject {
8989
// set if the function is a generator.
9090
private int flags = -1;
9191
// is a string representing the sequence of bytecode instructions
92-
private String codestring;
92+
private byte[] codestring;
9393
// tuple of constants used in the bytecode
94-
private Object constants;
94+
private Object[] constants;
9595
// tuple containing the literals used by the bytecode
96-
private Object names;
96+
private Object[] names;
9797
// is a tuple containing the names of the local variables (starting with the argument names)
9898
private Object[] varnames;
9999
// name of file in which this code object was created
@@ -103,7 +103,7 @@ public class PCode extends PythonBuiltinObject {
103103
// number of first line in Python source code
104104
private int firstlineno = -1;
105105
// is a string encoding the mapping from bytecode offsets to line numbers
106-
private Object lnotab;
106+
private byte[] lnotab;
107107
// tuple of names of free variables (referenced via a function’s closure)
108108
private Object[] freevars;
109109
// tuple of names of cell variables (referenced by containing scopes)
@@ -122,10 +122,10 @@ public PCode(PythonClass cls, RootNode rootNode, PythonCore core) {
122122

123123
public PCode(PythonClass cls, int argcount, int kwonlyargcount,
124124
int nlocals, int stacksize, int flags,
125-
String codestring, Object constants, Object names,
125+
byte[] codestring, Object[] constants, Object[] names,
126126
Object[] varnames, Object[] freevars, Object[] cellvars,
127127
String filename, String name, int firstlineno,
128-
Object lnotab) {
128+
byte[] lnotab) {
129129
super(cls);
130130
this.rootNode = null;
131131
this.core = null;
@@ -366,7 +366,7 @@ public int getStacksize() {
366366
return stacksize;
367367
}
368368

369-
public long getFlags() {
369+
public int getFlags() {
370370
if (flags == -1 && rootNode != null) {
371371
extractArgStats();
372372
}
@@ -380,19 +380,19 @@ public Object[] getVarnames() {
380380
return varnames;
381381
}
382382

383-
public String getCodestring() {
383+
public byte[] getCodestring() {
384384
return codestring;
385385
}
386386

387-
public Object getConstants() {
387+
public Object[] getConstants() {
388388
return constants;
389389
}
390390

391-
public Object getNames() {
391+
public Object[] getNames() {
392392
return names;
393393
}
394394

395-
public Object getLnotab() {
395+
public byte[] getLnotab() {
396396
return lnotab;
397397
}
398398

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,10 +723,10 @@ public PCode createCode(RootNode result) {
723723

724724
public PCode createCode(PythonClass cls, int argcount, int kwonlyargcount,
725725
int nlocals, int stacksize, int flags,
726-
String codestring, Object constants, Object names,
726+
byte[] codestring, Object[] constants, Object[] names,
727727
Object[] varnames, Object[] freevars, Object[] cellvars,
728728
String filename, String name, int firstlineno,
729-
Object lnotab) {
729+
byte[] lnotab) {
730730
return trace(new PCode(cls, argcount, kwonlyargcount,
731731
nlocals, stacksize, flags,
732732
codestring, constants, names,

0 commit comments

Comments
 (0)