Skip to content

Commit 65bea11

Browse files
committed
PBaseException: take data argument (can hold extra exception information)
1 parent 6c6208b commit 65bea11

File tree

10 files changed

+127
-136
lines changed

10 files changed

+127
-136
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
DebuggerTags.AlwaysHalt.class
126126
})
127127
public final class PythonLanguage extends TruffleLanguage<PythonContext> {
128+
public static final String GRAALPYTHON_ID = "graalpython";
128129
public static final String ID = "python";
129130
public static final String NAME = "Python";
130131
public static final String IMPLEMENTATION_NAME = "GraalVM Python";

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@
9696
import com.oracle.graal.python.lib.PyObjectTypeCheck;
9797
import com.oracle.graal.python.nodes.ErrorMessages;
9898
import com.oracle.graal.python.nodes.PNodeWithContext;
99-
import com.oracle.graal.python.nodes.PNodeWithRaise;
10099
import com.oracle.graal.python.nodes.PNodeWithRaiseAndIndirectCall;
101100
import com.oracle.graal.python.nodes.PRaiseNode;
102101
import com.oracle.graal.python.nodes.call.CallNode;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules;
4242

43+
import static com.oracle.graal.python.PythonLanguage.GRAALPYTHON_ID;
4344
import static com.oracle.graal.python.nodes.BuiltinNames.__GRAALPYTHON__;
4445
import static com.oracle.graal.python.nodes.BuiltinNames.__MAIN__;
4546
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__;
@@ -802,7 +803,7 @@ String doit(VirtualFrame frame) {
802803
TruffleFile tempFile;
803804
try {
804805
PythonContext context = getContext();
805-
tempFile = context.getEnv().createTempFile(context.getEnv().getCurrentWorkingDirectory(), "graalpython", ".hprof");
806+
tempFile = context.getEnv().createTempFile(context.getEnv().getCurrentWorkingDirectory(), GRAALPYTHON_ID, ".hprof");
806807
tempFile.delete();
807808
} catch (IOException e) {
808809
throw raiseOSError(frame, e);

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules;
4242

43+
import static com.oracle.graal.python.PythonLanguage.GRAALPYTHON_ID;
4344
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.AttributeError;
4445
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.DeprecationWarning;
4546
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.ImportError;
@@ -105,9 +106,6 @@
105106
import java.util.Map;
106107
import java.util.Set;
107108

108-
import com.oracle.graal.python.lib.PyFloatAsDoubleNode;
109-
import com.oracle.graal.python.lib.PyFloatCheckExactNode;
110-
import com.oracle.graal.python.lib.PyLongAsIntNode;
111109
import org.graalvm.nativeimage.ImageInfo;
112110

113111
import com.oracle.graal.python.PythonLanguage;
@@ -147,6 +145,9 @@
147145
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
148146
import com.oracle.graal.python.builtins.objects.tuple.StructSequence;
149147
import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins;
148+
import com.oracle.graal.python.lib.PyFloatAsDoubleNode;
149+
import com.oracle.graal.python.lib.PyFloatCheckExactNode;
150+
import com.oracle.graal.python.lib.PyLongAsIntNode;
150151
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
151152
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
152153
import com.oracle.graal.python.lib.PyObjectGetAttr;
@@ -370,7 +371,7 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
370371

371372
protected static PSimpleNamespace makeImplementation(PythonObjectFactory factory, PTuple versionInfo, String gmultiarch) {
372373
final PSimpleNamespace ns = factory.createSimpleNamespace();
373-
ns.setAttribute("name", "graalpython");
374+
ns.setAttribute("name", GRAALPYTHON_ID);
374375
ns.setAttribute("cache_tag", "graalpython-" + PythonLanguage.MAJOR + PythonLanguage.MINOR);
375376
ns.setAttribute("version", versionInfo);
376377
ns.setAttribute("_multiarch", gmultiarch);
@@ -469,7 +470,7 @@ public void initialize(Python3Core core) {
469470
builtinConstants.put("ps2", "... ");
470471
// CPython builds for distros report empty strings too, because they are built from
471472
// tarballs, not git
472-
builtinConstants.put("_git", factory.createTuple(new Object[]{"graalpython", "", ""}));
473+
builtinConstants.put("_git", factory.createTuple(new Object[]{GRAALPYTHON_ID, "", ""}));
473474

474475
super.initialize(core);
475476

@@ -1562,14 +1563,14 @@ Object setCheckInterval(VirtualFrame frame, @SuppressWarnings("unused") PythonMo
15621563
}
15631564

15641565
@Builtin(name = EXIT, declaresExplicitSelf = true, minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, doc = "exit($module, status=None, /)\n" +
1565-
"--\n" +
1566-
"\n" +
1567-
"Exit the interpreter by raising SystemExit(status).\n" +
1568-
"\n" +
1569-
"If the status is omitted or None, it defaults to zero (i.e., success).\n" +
1570-
"If the status is an integer, it will be used as the system exit status.\n" +
1571-
"If it is another kind of object, it will be printed and the system\n" +
1572-
"exit status will be one (i.e., failure).")
1566+
"--\n" +
1567+
"\n" +
1568+
"Exit the interpreter by raising SystemExit(status).\n" +
1569+
"\n" +
1570+
"If the status is omitted or None, it defaults to zero (i.e., success).\n" +
1571+
"If the status is an integer, it will be used as the system exit status.\n" +
1572+
"If it is another kind of object, it will be printed and the system\n" +
1573+
"exit status will be one (i.e., failure).")
15731574
@GenerateNodeFactory
15741575
abstract static class ExitNode extends PythonBinaryBuiltinNode {
15751576
@Specialization(guards = "!isPNone(status)")

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/BaseExceptionData.java

Lines changed: 0 additions & 44 deletions
This file was deleted.

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/PBaseException.java

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,42 @@ public final class PBaseException extends PythonObject {
8989
private PBaseException context;
9090
private PBaseException cause;
9191
private boolean suppressContext = false;
92-
// the data instance is used to store additional information for some of the builtin exceptions not unlike subclassing
93-
private BaseExceptionData data;
92+
// the data instance is used to store additional information for some of the builtin exceptions
93+
// not unlike subclassing
94+
private Object data;
9495

95-
public BaseExceptionData getData() {
96+
public PBaseException(Object cls, Shape instanceShape, Object data, PTuple args) {
97+
super(cls, instanceShape);
98+
this.data = data;
99+
this.args = args;
100+
this.hasMessageFormat = false;
101+
this.messageFormat = null;
102+
this.messageArgs = null;
103+
}
104+
105+
public PBaseException(Object cls, Shape instanceShape, Object data) {
106+
super(cls, instanceShape);
107+
this.data = data;
108+
this.args = null;
109+
this.hasMessageFormat = false;
110+
this.messageFormat = null;
111+
this.messageArgs = null;
112+
}
113+
114+
public PBaseException(Object cls, Shape instanceShape, Object data, String format, Object[] args) {
115+
super(cls, instanceShape);
116+
this.data = data;
117+
this.args = null;
118+
this.hasMessageFormat = true;
119+
this.messageFormat = format;
120+
this.messageArgs = args;
121+
}
122+
123+
public Object getData() {
96124
return data;
97125
}
98126

99-
public void setData(BaseExceptionData data) {
127+
public void setData(Object data) {
100128
this.data = data;
101129
}
102130

@@ -125,30 +153,6 @@ public void setSuppressContext(boolean suppressContext) {
125153
this.suppressContext = suppressContext;
126154
}
127155

128-
public PBaseException(Object cls, Shape instanceShape, PTuple args) {
129-
super(cls, instanceShape);
130-
this.args = args;
131-
this.hasMessageFormat = false;
132-
this.messageFormat = null;
133-
this.messageArgs = null;
134-
}
135-
136-
public PBaseException(Object cls, Shape instanceShape) {
137-
super(cls, instanceShape);
138-
this.args = null;
139-
this.hasMessageFormat = false;
140-
this.messageFormat = null;
141-
this.messageArgs = null;
142-
}
143-
144-
public PBaseException(Object cls, Shape instanceShape, String format, Object[] args) {
145-
super(cls, instanceShape);
146-
this.args = null;
147-
this.hasMessageFormat = true;
148-
this.messageFormat = format;
149-
this.messageArgs = args;
150-
}
151-
152156
public PException getException() {
153157
return exception;
154158
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/SystemExitBuiltins.java

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,47 +51,61 @@
5151
import com.oracle.graal.python.builtins.objects.PNone;
5252
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5353
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
54+
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
5455
import com.oracle.truffle.api.CompilerDirectives;
5556
import com.oracle.truffle.api.dsl.Cached;
5657
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
5758
import com.oracle.truffle.api.dsl.NodeFactory;
5859
import com.oracle.truffle.api.dsl.Specialization;
59-
import com.oracle.truffle.api.frame.VirtualFrame;
6060

6161
@CoreFunctions(extendClasses = PythonBuiltinClassType.SystemExit)
62-
public class SystemExitBuiltins extends PythonBuiltins {
62+
public final class SystemExitBuiltins extends PythonBuiltins {
6363

6464
@Override
6565
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
6666
return SystemExitBuiltinsFactory.getFactories();
6767
}
6868

6969
@CompilerDirectives.ValueType
70-
public static class SystemExitData extends BaseExceptionData {
70+
public static final class SystemExitData {
7171
private Object code;
7272

73+
private SystemExitData() {
74+
75+
}
76+
7377
public Object getCode() {
7478
return code;
7579
}
7680

7781
public void setCode(Object code) {
7882
this.code = code;
7983
}
84+
85+
public static SystemExitData create(Object code) {
86+
final SystemExitData data = new SystemExitData();
87+
data.setCode(code);
88+
return data;
89+
}
90+
91+
public static SystemExitData create(PythonObjectFactory factory, Object[] args) {
92+
final SystemExitData data = new SystemExitData();
93+
if (args.length == 1) {
94+
data.setCode(args[0]);
95+
} else {
96+
data.setCode(factory.createTuple(args));
97+
}
98+
return data;
99+
}
80100
}
81101

82102
@Builtin(name = __INIT__, minNumOfPositionalArgs = 1, takesVarArgs = true)
83103
@GenerateNodeFactory
84104
public abstract static class InitNode extends PythonBuiltinNode {
85105
@Specialization
86106
Object initNoArgs(PBaseException self, Object[] args,
87-
@Cached BaseExceptionBuiltins.BaseExceptionInitNode baseExceptionInitNode) {
88-
final SystemExitData data = new SystemExitData();
89-
if (args.length == 1) {
90-
data.setCode(args[0]);
91-
} else {
92-
data.setCode(factory().createTuple(args));
93-
}
94-
self.setData(data);
107+
@Cached BaseExceptionBuiltins.BaseExceptionInitNode baseExceptionInitNode) {
108+
self.setData(SystemExitData.create(factory(), args));
95109
baseExceptionInitNode.execute(self, args);
96110
return PNone.NONE;
97111
}
@@ -102,17 +116,16 @@ Object initNoArgs(PBaseException self, Object[] args,
102116
public abstract static class CodeNode extends PythonBuiltinNode {
103117
@Specialization(guards = "isNoValue(none)")
104118
public Object code(PBaseException self, @SuppressWarnings("unused") PNone none) {
105-
return ((SystemExitData)self.getData()).getCode();
119+
final Object data = self.getData();
120+
assert data instanceof SystemExitData;
121+
return ((SystemExitData) data).getCode();
106122
}
107123

108124
@Specialization(guards = "!isNoValue(value)")
109125
public Object code(PBaseException self, Object value) {
110-
SystemExitData data = (SystemExitData) self.getData();
111-
if (data == null) {
112-
data = new SystemExitData();
113-
self.setData(data);
114-
}
115-
data.setCode(value);
126+
final Object data = self.getData();
127+
assert data instanceof SystemExitData;
128+
((SystemExitData) data).setCode(value);
116129
return PNone.NONE;
117130
}
118131
}

0 commit comments

Comments
 (0)