Skip to content

Commit 4a95680

Browse files
committed
[GR-11992] Use str() to convert values toString and print using repr internally for interactive sources
PullRequest: graalpython/218
2 parents ab2f452 + b3be4c3 commit 4a95680

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/interop/JavaInteropTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public void accessSuitePy() throws IOException {
238238
}
239239
}
240240
assertNotNull("Dacapo found", dacapo);
241-
assertEquals("'e39957904b7e79caf4fa54f30e8e4ee74d4e9e37'", dacapo.getMember("sha1").toString());
241+
assertEquals("e39957904b7e79caf4fa54f30e8e4ee74d4e9e37", dacapo.getMember("sha1").toString());
242242
}
243243

244244
public static class ForeignObjectWithOOInvoke implements TruffleObject {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,16 @@
3434
import org.graalvm.options.OptionDescriptors;
3535

3636
import com.oracle.graal.python.builtins.Python3Core;
37-
import com.oracle.graal.python.builtins.objects.PNone;
37+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
3838
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
3939
import com.oracle.graal.python.builtins.objects.function.PArguments;
4040
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
4141
import com.oracle.graal.python.builtins.objects.function.PKeyword;
42-
import com.oracle.graal.python.builtins.objects.method.PBuiltinMethod;
43-
import com.oracle.graal.python.builtins.objects.module.PythonModule;
4442
import com.oracle.graal.python.builtins.objects.object.PythonObject;
45-
import com.oracle.graal.python.nodes.BuiltinNames;
43+
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
4644
import com.oracle.graal.python.nodes.NodeFactory;
4745
import com.oracle.graal.python.nodes.PNode;
46+
import com.oracle.graal.python.nodes.SpecialMethodNames;
4847
import com.oracle.graal.python.nodes.call.InvokeNode;
4948
import com.oracle.graal.python.nodes.control.TopLevelExceptionHandler;
5049
import com.oracle.graal.python.nodes.expression.ExpressionNode;
@@ -329,7 +328,7 @@ public static PythonCore getCore() {
329328

330329
@Override
331330
protected boolean isVisible(PythonContext context, Object value) {
332-
return value != PNone.NONE && value != PNone.NO_VALUE;
331+
return false;
333332
}
334333

335334
@Override
@@ -363,12 +362,12 @@ protected Iterable<Scope> findTopScopes(PythonContext context) {
363362

364363
@Override
365364
protected String toString(PythonContext context, Object value) {
366-
final PythonModule builtins = context.getBuiltins();
367-
PBuiltinFunction reprMethod = ((PBuiltinMethod) builtins.getAttribute(BuiltinNames.REPR)).getFunction();
365+
PythonBuiltinClass strType = context.getCore().lookupType(PythonBuiltinClassType.PString);
366+
PBuiltinFunction strConstructor = (PBuiltinFunction) strType.getAttribute(SpecialMethodNames.__NEW__);
368367
Object[] userArgs = PArguments.create(2);
369-
PArguments.setArgument(userArgs, 0, PNone.NONE);
368+
PArguments.setArgument(userArgs, 0, strType);
370369
PArguments.setArgument(userArgs, 1, value);
371-
Object res = InvokeNode.create(reprMethod).execute(null, userArgs, PKeyword.EMPTY_KEYWORDS);
370+
Object res = InvokeNode.create(strConstructor).execute(null, userArgs, PKeyword.EMPTY_KEYWORDS);
372371
return res.toString();
373372
}
374373

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/control/TopLevelExceptionHandler.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@
4141
package com.oracle.graal.python.nodes.control;
4242

4343
import static com.oracle.graal.python.nodes.SpecialMethodNames.__STR__;
44+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REPR__;
4445
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SystemExit;
4546

4647
import java.io.IOException;
48+
import java.io.OutputStream;
49+
import java.nio.charset.StandardCharsets;
4750

4851
import com.oracle.graal.python.PythonLanguage;
4952
import com.oracle.graal.python.builtins.objects.PNone;
@@ -81,6 +84,7 @@ public class TopLevelExceptionHandler extends RootNode {
8184

8285
@Child private CreateArgumentsNode createArgs = CreateArgumentsNode.create();
8386
@Child private LookupAndCallUnaryNode callStrNode = LookupAndCallUnaryNode.create(__STR__);
87+
@Child private LookupAndCallUnaryNode callReprNode = LookupAndCallUnaryNode.create(__REPR__);
8488
@Child private CallNode callNode = CallNode.create();
8589

8690
public TopLevelExceptionHandler(PythonLanguage language, RootNode child) {
@@ -106,8 +110,9 @@ public Object execute(VirtualFrame frame) {
106110
return null;
107111
} else {
108112
assert context.get().getCurrentException() == null;
113+
Object result;
109114
try {
110-
return run(frame);
115+
result = run(frame);
111116
} catch (PException e) {
112117
printExc(e);
113118
return null;
@@ -120,6 +125,26 @@ public Object execute(VirtualFrame frame) {
120125
}
121126
throw e;
122127
}
128+
if (getSourceSection().getSource().isInteractive()) {
129+
printResult(result);
130+
}
131+
return result;
132+
}
133+
}
134+
135+
@TruffleBoundary
136+
private void printResult(Object result) {
137+
if (result instanceof PNone) {
138+
return;
139+
}
140+
OutputStream out = getCurrentContext(PythonLanguage.class).getStandardOut();
141+
String stringResult = callReprNode.executeObject(result).toString();
142+
try {
143+
out.write(stringResult.getBytes(StandardCharsets.UTF_8));
144+
out.write(System.getProperty("line.separator").getBytes(StandardCharsets.UTF_8));
145+
} catch (IOException ioex) {
146+
// out stream has problems.
147+
throw new IllegalStateException(ioex);
123148
}
124149
}
125150

0 commit comments

Comments
 (0)