Skip to content

Commit 7b8a4cd

Browse files
committed
[GR-11944] _imp builtin module: add missing _fix_co_filename builtin
PullRequest: graalpython/210
2 parents 426ba01 + 4e0b4df commit 7b8a4cd

File tree

6 files changed

+123
-49
lines changed

6 files changed

+123
-49
lines changed

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

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,42 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939

40-
from posix import *
4140
import sys
41+
from posix import *
42+
4243
sys.path.append(sys._getframe().f_code.co_filename.rpartition("/")[0])
4344

45+
4446
def test_relative_import():
45-
import package
47+
# this is to prevent ides from optimising out the unused import
48+
try:
49+
import package
50+
except Exception as e:
51+
raise e
4652

4753

4854
def test_dotted_import():
49-
import package.moduleY
55+
# this is to prevent ides from optimising out the unused import
56+
try:
57+
import package.moduleY
58+
except Exception as e:
59+
raise e
5060

5161

5262
def test_recursive_import():
53-
import package.moduleRecursive
63+
# this is to prevent ides from optimising out the unused import
64+
try:
65+
import package.moduleRecursive
66+
except Exception as e:
67+
raise e
5468

5569

5670
def test_recursive_import2():
57-
import package.moduleRecursive2
71+
# this is to prevent ides from optimising out the unused import
72+
try:
73+
import package.moduleRecursive2
74+
except Exception as e:
75+
raise e
5876

5977

6078
def test_import_star_has_to_be_module():
@@ -91,3 +109,15 @@ def test_import_error():
91109
def test_import_some_star():
92110
import posix
93111
assert stat == posix.stat
112+
113+
114+
def test_imp_fix_co_filename():
115+
import _imp
116+
117+
def func(x):
118+
return x+x
119+
120+
code = func.__code__
121+
old_name = code.co_filename
122+
_imp._fix_co_filename(code, old_name + '_more_path')
123+
assert code.co_filename == old_name + '_more_path'

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

Lines changed: 26 additions & 22 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,37 +1844,38 @@ 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,
18511850
String codestring, PTuple constants, PTuple names,
18521851
PTuple varnames, PTuple freevars, PTuple cellvars,
1853-
String filename, String name, int firstlineno,
1852+
Object filename, Object name, int firstlineno,
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(),
1859-
filename, name, firstlineno,
1860-
lnotab);
1858+
getStringArg(filename), getStringArg(name), firstlineno,
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,
1869-
PString filename, PString name, int firstlineno,
1870-
PBytes lnotab) {
1867+
Object filename, Object name, int firstlineno,
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(),
1875-
filename.getValue(), name.getValue(), firstlineno,
1876-
lnotab);
1877+
getStringArg(filename), getStringArg(name), firstlineno,
1878+
lnotabBytes);
18771879
}
18781880

18791881
@SuppressWarnings("unused")
@@ -1884,20 +1886,22 @@ 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

1890-
@TruffleBoundary
1891-
private static String toString(byte[] data) {
1892-
return new String(data);
1892+
private String getStringArg(Object arg) {
1893+
if (arg instanceof String) {
1894+
return (String) arg;
1895+
} else if (arg instanceof PString) {
1896+
return ((PString) arg).toString();
1897+
} else {
1898+
throw raise(SystemError, "bad argument to internal function");
1899+
}
18931900
}
18941901

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());
1902+
@TruffleBoundary
1903+
private static byte[] toBytes(String data) {
1904+
return data.getBytes();
19011905
}
19021906
}
19031907

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import com.oracle.graal.python.builtins.modules.TruffleCextBuiltins.CheckFunctionResultNode;
5858
import com.oracle.graal.python.builtins.objects.PNone;
5959
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.AsPythonObjectNode;
60+
import com.oracle.graal.python.builtins.objects.code.PCode;
6061
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes.SetItemNode;
6162
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
6263
import com.oracle.graal.python.builtins.objects.dict.PDict;
@@ -69,6 +70,7 @@
6970
import com.oracle.graal.python.nodes.call.special.CallUnaryMethodNode;
7071
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
7172
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
73+
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
7274
import com.oracle.graal.python.runtime.PythonContext;
7375
import com.oracle.graal.python.runtime.PythonCore;
7476
import com.oracle.graal.python.runtime.PythonOptions;
@@ -385,4 +387,19 @@ public Object run(String path, PythonModule mod) {
385387
}
386388
}
387389

390+
@Builtin(name = "_fix_co_filename", fixedNumOfPositionalArgs = 2)
391+
@GenerateNodeFactory
392+
public abstract static class FixCoFilename extends PythonBinaryBuiltinNode {
393+
@Specialization
394+
public Object run(PCode code, PString path) {
395+
code.setFilename(path.getValue());
396+
return PNone.NONE;
397+
}
398+
399+
@Specialization
400+
public Object run(PCode code, String path) {
401+
code.setFilename(path);
402+
return PNone.NONE;
403+
}
404+
}
388405
}

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: 15 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;
@@ -313,6 +313,10 @@ public Object[] getCellVars() {
313313
return cellvars;
314314
}
315315

316+
public void setFilename(String filename) {
317+
this.filename = filename;
318+
}
319+
316320
public String getFilename() {
317321
if (filename == null && rootNode != null) {
318322
filename = extractFileName(rootNode);
@@ -362,7 +366,7 @@ public int getStacksize() {
362366
return stacksize;
363367
}
364368

365-
public long getFlags() {
369+
public int getFlags() {
366370
if (flags == -1 && rootNode != null) {
367371
extractArgStats();
368372
}
@@ -376,19 +380,19 @@ public Object[] getVarnames() {
376380
return varnames;
377381
}
378382

379-
public String getCodestring() {
383+
public byte[] getCodestring() {
380384
return codestring;
381385
}
382386

383-
public Object getConstants() {
387+
public Object[] getConstants() {
384388
return constants;
385389
}
386390

387-
public Object getNames() {
391+
public Object[] getNames() {
388392
return names;
389393
}
390394

391-
public Object getLnotab() {
395+
public byte[] getLnotab() {
392396
return lnotab;
393397
}
394398

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)