Skip to content

Commit ba8c598

Browse files
committed
Add nodes for getting/setting exception args
1 parent 1bbb388 commit ba8c598

File tree

12 files changed

+247
-190
lines changed

12 files changed

+247
-190
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,6 @@ static PNone mutate(PythonModule self,
11081108
* Our replacement for PyErr_WarnFormat, warn_unicode and related functions.
11091109
*/
11101110
public abstract static class WarnNode extends Node {
1111-
private static final ErrorMessageFormatter formatter = new ErrorMessageFormatter();
11121111
private static final WarnNode UNCACHED = new WarnNodeUncached();
11131112

11141113
@NeverDefault
@@ -1184,7 +1183,7 @@ protected void execute(Frame frame, Object source, Object category, TruffleStrin
11841183
private static String formatMessage(TruffleString format, Object... formatArgs) {
11851184
String message;
11861185
try {
1187-
message = formatter.format(format, formatArgs);
1186+
message = ErrorMessageFormatter.format(format, formatArgs);
11881187
} catch (IllegalFormatException e) {
11891188
throw CompilerDirectives.shouldNotReachHere("error while formatting \"" + format + "\"", e);
11901189
}
@@ -1210,7 +1209,7 @@ private void executeImpl(Object source, Object category, TruffleString format, i
12101209
Object warn = DynamicObjectLibrary.getUncached().getOrDefault(_warnings, T_WARN, PNone.NONE);
12111210
TruffleString message;
12121211
try {
1213-
message = TruffleString.fromJavaStringUncached(formatter.format(format, formatArgs), TS_ENCODING);
1212+
message = TruffleString.fromJavaStringUncached(ErrorMessageFormatter.format(format, formatArgs), TS_ENCODING);
12141213
} catch (IllegalFormatException e) {
12151214
throw CompilerDirectives.shouldNotReachHere("error while formatting \"" + format + "\"", e);
12161215
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/BufferedWriterNodes.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
import com.oracle.graal.python.builtins.objects.PNone;
5555
import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAccessLibrary;
5656
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
57+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
58+
import com.oracle.graal.python.builtins.objects.exception.ExceptionNodes;
59+
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
5760
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
5861
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
5962
import com.oracle.graal.python.nodes.PNodeWithContext;
@@ -96,7 +99,9 @@ static int bufferedWriterWrite(VirtualFrame frame, @SuppressWarnings("unused") N
9699
@Cached IsBuiltinObjectProfile isBuiltinClassProfile,
97100
@Cached BufferedIONodes.RawSeekNode rawSeekNode,
98101
@Cached RawWriteNode rawWriteNode,
99-
@Cached FlushUnlockedNode flushUnlockedNode) {
102+
@Cached FlushUnlockedNode flushUnlockedNode,
103+
@Cached ExceptionNodes.GetArgsNode getArgsNode,
104+
@Cached SequenceStorageNodes.GetItemScalarNode getItemScalarNode) {
100105
final int bufLen = bufferLib.getBufferLength(buffer);
101106

102107
// TODO: check ENTER_BUFFERED(self)
@@ -148,12 +153,10 @@ static int bufferedWriterWrite(VirtualFrame frame, @SuppressWarnings("unused") N
148153
bufferLib.readIntoByteArray(buffer, 0, self.getBuffer(), self.getWriteEnd(), avail);
149154
self.incWriteEnd(avail);
150155
self.incPos(avail);
151-
/*
152-
* XXX Modifying the existing exception e using the pointer w will change
153-
* e.characters_written but not e.args[2]. Therefore we just replace with a new
154-
* error.
155-
*/
156-
Object errno = e.getUnreifiedException().getArgs().getSequenceStorage().getItemNormalized(0);
156+
157+
Object pythonException = e.getUnreifiedException();
158+
PTuple args = getArgsNode.execute(inliningTarget, pythonException);
159+
Object errno = getItemScalarNode.execute(args.getSequenceStorage(), 0);
157160
throw raiseBlockingIOError.get(inliningTarget).raise(errno, WRITE_COULD_NOT_COMPLETE_WITHOUT_BLOCKING, avail);
158161
}
159162
/*

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

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@
4242

4343
import com.oracle.graal.python.builtins.objects.PNone;
4444
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
45+
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
4546
import com.oracle.graal.python.nodes.PGuards;
4647
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
47-
import com.oracle.graal.python.util.PythonUtils;
48+
import com.oracle.truffle.api.dsl.Bind;
4849
import com.oracle.truffle.api.dsl.Cached;
50+
import com.oracle.truffle.api.dsl.Cached.Shared;
51+
import com.oracle.truffle.api.dsl.GenerateCached;
52+
import com.oracle.truffle.api.dsl.GenerateInline;
4953
import com.oracle.truffle.api.dsl.GenerateUncached;
5054
import com.oracle.truffle.api.dsl.ImportStatic;
5155
import com.oracle.truffle.api.dsl.Specialization;
@@ -86,18 +90,26 @@ protected static boolean withAttributes(PBaseException self) {
8690
return self.getExceptionAttributes() != null;
8791
}
8892

89-
private static Object[] ensureAttrStorage(PBaseException self, StorageFactory factory, SequenceStorageNodes.GetInternalObjectArrayNode getArrayNode, PythonObjectFactory objectFactory) {
90-
Object[] attributes = self.getExceptionAttributes();
91-
if (attributes == null) {
92-
// TODO: cbasca should we raise in case getArgs() is null (due to lazy init of args)?
93-
Object[] args = PythonUtils.EMPTY_OBJECT_ARRAY;
94-
if (self.getArgs() != null) {
95-
args = getArrayNode.execute(self.getArgs().getSequenceStorage());
93+
@GenerateInline
94+
@GenerateCached(false)
95+
@GenerateUncached
96+
abstract static class EnsureAttrStorageNode extends Node {
97+
abstract Object[] execute(Node inliningTarget, PBaseException self, StorageFactory storageFactory);
98+
99+
@Specialization
100+
static Object[] ensure(Node inliningTarget, PBaseException self, StorageFactory storageFactory,
101+
@Cached ExceptionNodes.GetArgsNode getArgsNode,
102+
@Cached SequenceStorageNodes.GetInternalObjectArrayNode getInternalObjectArrayNode,
103+
@Cached PythonObjectFactory factory) {
104+
Object[] attributes = self.getExceptionAttributes();
105+
if (attributes == null) {
106+
PTuple argsTuple = getArgsNode.execute(inliningTarget, self);
107+
Object[] args = getInternalObjectArrayNode.execute(argsTuple.getSequenceStorage());
108+
attributes = storageFactory.create(args, factory);
109+
self.setExceptionAttributes(attributes);
96110
}
97-
attributes = factory.create(args, objectFactory);
98-
self.setExceptionAttributes(attributes);
111+
return attributes;
99112
}
100-
return attributes;
101113
}
102114

103115
// GET
@@ -111,9 +123,9 @@ public Object getAttrWithStorage(PBaseException self, @SuppressWarnings("unused"
111123

112124
@Specialization(guards = {"isNoValue(none)", "!withAttributes(self)"})
113125
public Object getAttrNoStorage(PBaseException self, @SuppressWarnings("unused") PNone none, int index, StorageFactory factory,
114-
@Cached SequenceStorageNodes.GetInternalObjectArrayNode getArrayNode,
115-
@Cached PythonObjectFactory objectFactory) {
116-
Object[] attributes = ensureAttrStorage(self, factory, getArrayNode, objectFactory);
126+
@Bind("this") Node inliningTarget,
127+
@Shared @Cached EnsureAttrStorageNode ensureAttrStorageNode) {
128+
Object[] attributes = ensureAttrStorageNode.execute(inliningTarget, self, factory);
117129
assert attributes != null : "PBaseException attributes field is null";
118130
return getAttrWithStorage(self, none, index, factory);
119131
}
@@ -129,9 +141,9 @@ public Object setAttrWithStorage(PBaseException self, Object value, int index, @
129141

130142
@Specialization(guards = {"!isNoValue(value)", "!isDeleteMarker(value)", "!withAttributes(self)"})
131143
public Object setAttrNoStorage(PBaseException self, Object value, int index, StorageFactory factory,
132-
@Cached SequenceStorageNodes.GetInternalObjectArrayNode getArrayNode,
133-
@Cached PythonObjectFactory objectFactory) {
134-
Object[] attributes = ensureAttrStorage(self, factory, getArrayNode, objectFactory);
144+
@Bind("this") Node inliningTarget,
145+
@Shared @Cached EnsureAttrStorageNode ensureAttrStorageNode) {
146+
Object[] attributes = ensureAttrStorageNode.execute(inliningTarget, self, factory);
135147
assert attributes != null : "PBaseException attributes field is null";
136148
return setAttrWithStorage(self, value, index, factory);
137149
}
@@ -147,9 +159,9 @@ public Object delAttrWithStorage(PBaseException self, @SuppressWarnings("unused"
147159

148160
@Specialization(guards = {"!isNoValue(value)", "isDeleteMarker(value)", "!withAttributes(self)"})
149161
public Object delAttrNoStorage(PBaseException self, Object value, int index, StorageFactory factory,
150-
@Cached SequenceStorageNodes.GetInternalObjectArrayNode getArrayNode,
151-
@Cached PythonObjectFactory objectFactory) {
152-
Object[] attributes = ensureAttrStorage(self, factory, getArrayNode, objectFactory);
162+
@Bind("this") Node inliningTarget,
163+
@Shared @Cached EnsureAttrStorageNode ensureAttrStorageNode) {
164+
Object[] attributes = ensureAttrStorageNode.execute(inliningTarget, self, factory);
153165
assert attributes != null : "PBaseException attributes field is null";
154166
return delAttrWithStorage(self, value, index, factory);
155167
}

0 commit comments

Comments
 (0)