Skip to content

Commit c7b4631

Browse files
committed
Merge branch 'master' into bugfix/GR-11899
2 parents 2d6c7b2 + 7b8a4cd commit c7b4631

File tree

16 files changed

+283
-87
lines changed

16 files changed

+283
-87
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/PythonTests.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,15 @@ public static void assertPrints(String expected, Path scriptName) {
192192
public static void assertPrints(String expected, String code) {
193193
final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
194194
final PrintStream printStream = new PrintStream(byteArray);
195-
String source = code;
196-
PythonTests.runScript(new String[0], source, printStream, System.err);
195+
PythonTests.runScript(new String[0], code, printStream, System.err);
196+
String result = byteArray.toString().replaceAll("\r\n", "\n");
197+
assertEquals(expected.replaceAll(" at 0x[0-9a-f]*>", " at 0xabcd>"), result.replaceAll(" at 0x[0-9a-f]*>", " at 0xabcd>"));
198+
}
199+
200+
public static void assertPrints(String expected, org.graalvm.polyglot.Source code) {
201+
final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
202+
final PrintStream printStream = new PrintStream(byteArray);
203+
PythonTests.runScript(new String[0], code, printStream, System.err);
197204
String result = byteArray.toString().replaceAll("\r\n", "\n");
198205
assertEquals(expected.replaceAll(" at 0x[0-9a-f]*>", " at 0xabcd>"), result.replaceAll(" at 0x[0-9a-f]*>", " at 0xabcd>"));
199206
}
@@ -311,6 +318,15 @@ public static void runScript(String[] args, String source, OutputStream out, Out
311318
}
312319
}
313320

321+
public static void runScript(String[] args, org.graalvm.polyglot.Source source, OutputStream out, OutputStream err) {
322+
try {
323+
enterContext(args);
324+
context.eval(source);
325+
} finally {
326+
flush(out, err);
327+
}
328+
}
329+
314330
public static void runScript(String[] args, String source, OutputStream out, OutputStream err, Runnable cb) {
315331
try {
316332
enterContext(args);

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/basic/HelloWorldTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,11 @@ public class HelloWorldTests {
3434
public void helloworld() {
3535
assertPrints("hello world\n", "print(\"hello world\")");
3636
}
37+
38+
@Test
39+
public void helloworldAgain() {
40+
org.graalvm.polyglot.Source source = org.graalvm.polyglot.Source.create("python", "try: print(value)\nexcept:print('hello')\nvalue='world'");
41+
assertPrints("hello\n", source);
42+
assertPrints("hello\n", source);
43+
}
3744
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ def test_bin(self):
2525
self.assertEqual(bin(MyIndexable(False)), '0b0')
2626
self.assertEqual(bin(MyIndexable(True)), '0b1')
2727
self.assertEqual(bin(MyIndexable(-(2**65))), '-0b1' + '0' * 65)
28+
29+
def test_GR11897(self):
30+
globs = {}
31+
code = 'a' + ' = ' + '8.01234567890123'
32+
exec(code, globs)
33+
self.assertEqual(globs['a'], 8.01234567890123)

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.test/src/tests/test_signal.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939

40-
41-
def test_alarm():
40+
def skip_test_alarm():
4241
# (tfel): this test is very brittle, because it is supposed to work with our
4342
# first, very primitive implementation of signal handlers, which does not
4443
# allow Python code to run in the handler. So we rely on a side-effect on an

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def test_rfind():
2929
assert "test string test".rfind("string") == 5
3030
assert "test string".rfind("test", 5) == -1
3131
assert "test string test".rfind("test", None, 12) == 0
32+
assert "test string test".rfind("test", 4) == 12
33+
assert "test string test".rfind("test", 4, 12) == -1
34+
assert "test string test".rfind("test", 4, 14) == -1
35+
assert "test string test".rfind("test", None, 14) == 0
3236

3337

3438
def test_format():
@@ -471,7 +475,8 @@ class subtype(self.__class__.type2test):
471475
else:
472476
obj = subtype(obj)
473477
realresult = getattr(obj, methodname)(*args)
474-
self.assertIsNot(obj, realresult)
478+
self.assertTrue(obj is not realresult)
479+
#self.assertIsNot(obj, realresult)
475480

476481
# check that obj.method(*args) raises exc
477482
def checkraises(self, exc, obj, methodname, *args):
@@ -684,6 +689,32 @@ def test_isprintable(self):
684689
self.assertTrue('\U0001F46F'.isprintable())
685690
# self.assertFalse('\U000E0020'.isprintable())
686691

692+
def test_zfill(self):
693+
self.checkequal('123', '123', 'zfill', 2)
694+
self.checkequal('123', '123', 'zfill', 3)
695+
self.checkequal('0123', '123', 'zfill', 4)
696+
self.checkequal('+123', '+123', 'zfill', 3)
697+
self.checkequal('+123', '+123', 'zfill', 4)
698+
self.checkequal('+0123', '+123', 'zfill', 5)
699+
self.checkequal('-123', '-123', 'zfill', 3)
700+
self.checkequal('-123', '-123', 'zfill', 4)
701+
self.checkequal('-0123', '-123', 'zfill', 5)
702+
self.checkequal('000', '', 'zfill', 3)
703+
self.checkequal('34', '34', 'zfill', 1)
704+
self.checkequal('0034', '34', 'zfill', 4)
705+
706+
self.checkraises(TypeError, '123', 'zfill')
707+
708+
def test_zfill_specialization(self):
709+
self.checkequal('123', '123', 'zfill', True)
710+
711+
class MyIndexable(object):
712+
def __init__(self, value):
713+
self.value = value
714+
def __index__(self):
715+
return self.value
716+
717+
self.checkequal('0123', '123', 'zfill', MyIndexable(4))
687718

688719
def test_title(self):
689720
self.checkequal(' Hello ', ' hello ', 'title')

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/BuiltinFunctions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ private static Object evalNode(RootNode root, PythonObject globals, PythonObject
553553
// compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)
554554
@Builtin(name = COMPILE, fixedNumOfPositionalArgs = 3, keywordArguments = {"flags", "dont_inherit", "optimize"})
555555
@GenerateNodeFactory
556+
@TypeSystemReference(PythonArithmeticTypes.class)
556557
public abstract static class CompileNode extends PythonBuiltinNode {
557558
@Specialization
558559
@TruffleBoundary

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
}

0 commit comments

Comments
 (0)