Skip to content

Commit 42f998a

Browse files
committed
move most paths that raise exceptions (and thus allocate) into the nodes, they use a factory that is in the AST
1 parent b1aac6d commit 42f998a

File tree

18 files changed

+52
-92
lines changed

18 files changed

+52
-92
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@
142142
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
143143
import com.oracle.truffle.api.TruffleFile;
144144
import com.oracle.truffle.api.TruffleLanguage.Env;
145-
import com.oracle.truffle.api.nodes.Node;
146145
import com.oracle.truffle.api.source.Source;
147146

148147
/**
@@ -434,41 +433,16 @@ public PythonClass getErrorClass(PythonErrorType type) {
434433
}
435434

436435
@Override
437-
public PException raise(PBaseException exception, Node node) {
438-
PException pException = new PException(exception, node);
439-
exception.setException(pException);
440-
throw pException;
441-
}
442-
443-
@Override
444-
public PException raise(PythonErrorType type, Node node, String format, Object... args) {
436+
@TruffleBoundary
437+
public PException raise(PythonErrorType type, String format, Object... args) {
445438
PBaseException instance;
446439
PythonClass exceptionType = getErrorClass(type);
447440
if (format != null) {
448441
instance = factory.createBaseException(exceptionType, format, args);
449442
} else {
450-
instance = factory.createBaseException(exceptionType, factory.createEmptyTuple());
443+
instance = factory.createBaseException(exceptionType);
451444
}
452-
throw raise(instance, node);
453-
}
454-
455-
@Override
456-
public PException raise(PythonErrorType type, String format, Object... args) {
457-
return raise(type, null, format, args);
458-
}
459-
460-
@Override
461-
public PException raise(PythonErrorType type) {
462-
throw raise(factory.createBaseException(getErrorClass(type)), null);
463-
}
464-
465-
@Override
466-
public PException raise(PythonClass exceptionType, Node node) {
467-
throw raise(factory.createBaseException(exceptionType), node);
468-
}
469-
470-
public PException raise(PythonErrorType type, Node node) {
471-
throw raise(factory.createBaseException(getErrorClass(type)), node);
445+
throw PException.fromObject(instance, null);
472446
}
473447

474448
private void publishBuiltinModules() {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ private PComplex convertStringToComplex(String str, PythonClass cls) {
313313
}
314314

315315
if (s == n) {
316-
throw getCore().raise(ValueError, "empty string for complex()");
316+
throw raise(ValueError, "empty string for complex()");
317317
}
318318

319319
double z = -1.0;
@@ -378,7 +378,7 @@ private PComplex convertStringToComplex(String str, PythonClass cls) {
378378
int end = endDouble(str, s);
379379
z = Double.valueOf(str.substring(s, end)).doubleValue();
380380
if (z == Double.POSITIVE_INFINITY) {
381-
throw getCore().raise(ValueError, String.format("float() out of range: %.150s", str));
381+
throw raise(ValueError, String.format("float() out of range: %.150s", str));
382382
}
383383

384384
s = end;
@@ -406,7 +406,7 @@ private PComplex convertStringToComplex(String str, PythonClass cls) {
406406
} while (s < n && !swError);
407407

408408
if (swError) {
409-
throw getCore().raise(ValueError, "malformed string for complex() %s", str.substring(s));
409+
throw raise(ValueError, "malformed string for complex() %s", str.substring(s));
410410
}
411411

412412
return factory().createComplex(cls, x, y);
@@ -627,7 +627,7 @@ private double convertStringToDouble(String str) {
627627
for (int i = 0; i < n; i++) {
628628
char ch = str.charAt(i);
629629
if (ch == '\u0000') {
630-
throw getCore().raise(ValueError, "empty string for complex()");
630+
throw raise(ValueError, "empty string for complex()");
631631
}
632632
if (Character.isDigit(ch)) {
633633
if (s == null) {
@@ -654,7 +654,7 @@ private double convertStringToDouble(String str) {
654654
return Double.valueOf(sval).doubleValue();
655655
} catch (NumberFormatException exc) {
656656
// throw Py.ValueError("invalid literal for __float__: " + str);
657-
throw getCore().raise(ValueError, "could not convert string to float: %s", str);
657+
throw raise(ValueError, "could not convert string to float: %s", str);
658658
}
659659
}
660660

@@ -793,7 +793,7 @@ private Object stringToInt(String num, int base) {
793793
return bi.intValue();
794794
}
795795
} else {
796-
throw getCore().raise(ValueError, "base is out of range for int()");
796+
throw raise(ValueError, "base is out of range for int()");
797797
}
798798
}
799799

@@ -815,7 +815,7 @@ private Object toInt(Object arg1, Object arg2) {
815815
if (arg1 instanceof String && arg2 instanceof Integer) {
816816
return stringToInt((String) arg1, (Integer) arg2);
817817
} else {
818-
throw getCore().raise(ValueError, "invalid base or val for int()");
818+
throw raise(ValueError, "invalid base or val for int()");
819819
}
820820
}
821821

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ public Object repr(Object obj,
12111211
if (isString.profile(result instanceof String) || isPString.profile(result instanceof PString)) {
12121212
return result;
12131213
}
1214-
throw getCore().raise(TypeError, "__repr__ returned non-string (type %p)", obj);
1214+
throw raise(TypeError, "__repr__ returned non-string (type %p)", obj);
12151215
}
12161216
}
12171217

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ String decodeBytes(ByteBuffer bytes, String encoding, String errors) {
398398
CharBuffer decoded = charset.newDecoder().onMalformedInput(errorAction).onUnmappableCharacter(errorAction).decode(bytes);
399399
return String.valueOf(decoded);
400400
} catch (IllegalArgumentException e) {
401-
throw this.getCore().raise(LookupError, "unknown encoding: %s", encoding);
401+
throw this.raise(LookupError, "unknown encoding: %s", encoding);
402402
} catch (CharacterCodingException e) {
403403
throw raise(UnicodeDecodeError, "%s", e.getMessage());
404404
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ protected GetStackTraceRootNode(PythonLanguage language) {
226226
@Override
227227
public Object execute(VirtualFrame frame) {
228228
CompilerDirectives.transferToInterpreter();
229-
throw contextRef.get().getCore().raise(ValueError);
229+
throw contextRef.get().getCore().raise(ValueError, null);
230230
}
231231

232232
@Override

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,7 @@ Object run(@SuppressWarnings("unused") PythonClass typ, PBaseException val, @Sup
325325
if (val.getException() != null) {
326326
getContext().setCurrentException(val.getException());
327327
} else {
328-
PException pException = new PException(val, this);
329-
val.setException(pException);
328+
PException pException = PException.fromObject(val, this);
330329
getContext().setCurrentException(pException);
331330
}
332331
return PNone.NONE;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/dict/DictBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ Object run(PDict self, Object key,
319319
if (delItemNode.execute(self, self.getDictStorage(), key)) {
320320
return PNone.NONE;
321321
}
322-
throw getCore().raise(KeyError, this, "%s", key);
322+
throw raise(KeyError, "%s", key);
323323
}
324324
}
325325

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/generator/GeneratorBuiltins.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ Object sendThrow(VirtualFrame frame, PGenerator self, PythonClass typ, @Suppress
155155
@Cached("create(__CALL__)") LookupAndCallVarargsNode callTyp) {
156156
Object instance = callTyp.execute(frame, typ, new Object[0]);
157157
if (instance instanceof PBaseException) {
158-
PException pException = new PException((PBaseException) instance, this);
159-
((PBaseException) instance).setException(pException);
158+
PException pException = PException.fromObject((PBaseException) instance, this);
160159
PArguments.setSpecialArgument(self.getArguments(), pException);
161160
} else {
162161
throw raise(TypeError, "exceptions must derive from BaseException");
@@ -169,8 +168,7 @@ Object sendThrow(VirtualFrame frame, PGenerator self, PythonClass typ, PTuple va
169168
@Cached("create(__CALL__)") LookupAndCallVarargsNode callTyp) {
170169
Object instance = callTyp.execute(frame, typ, val.getArray());
171170
if (instance instanceof PBaseException) {
172-
PException pException = new PException((PBaseException) instance, this);
173-
((PBaseException) instance).setException(pException);
171+
PException pException = PException.fromObject((PBaseException) instance, this);
174172
PArguments.setSpecialArgument(self.getArguments(), pException);
175173
} else {
176174
throw raise(TypeError, "exceptions must derive from BaseException");
@@ -183,8 +181,7 @@ Object sendThrow(VirtualFrame frame, PGenerator self, PythonClass typ, Object va
183181
@Cached("create(__CALL__)") LookupAndCallVarargsNode callTyp) {
184182
Object instance = callTyp.execute(frame, typ, new Object[]{val});
185183
if (instance instanceof PBaseException) {
186-
PException pException = new PException((PBaseException) instance, this);
187-
((PBaseException) instance).setException(pException);
184+
PException pException = PException.fromObject((PBaseException) instance, this);
188185
PArguments.setSpecialArgument(self.getArguments(), pException);
189186
} else {
190187
throw raise(TypeError, "exceptions must derive from BaseException");
@@ -194,16 +191,14 @@ Object sendThrow(VirtualFrame frame, PGenerator self, PythonClass typ, Object va
194191

195192
@Specialization
196193
Object sendThrow(PGenerator self, PBaseException instance, @SuppressWarnings("unused") PNone val, @SuppressWarnings("unused") PNone tb) {
197-
PException pException = new PException(instance, this);
198-
instance.setException(pException);
194+
PException pException = PException.fromObject(instance, this);
199195
PArguments.setSpecialArgument(self.getArguments(), pException);
200196
return resumeGenerator(self);
201197
}
202198

203199
@Specialization
204200
Object sendThrow(PGenerator self, @SuppressWarnings("unused") PythonClass typ, PBaseException instance, PTraceback tb) {
205-
PException pException = new PException(instance, this);
206-
instance.setException(pException);
201+
PException pException = PException.fromObject(instance, this);
207202
instance.setTraceback(tb);
208203
PArguments.setSpecialArgument(self.getArguments(), pException);
209204
return resumeGenerator(self);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/getsetdescriptor/GetSetDescriptorTypeBuiltins.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@
5555
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
5656
import com.oracle.graal.python.builtins.objects.type.PythonClass;
5757
import com.oracle.graal.python.nodes.PGuards;
58+
import com.oracle.graal.python.nodes.PNodeWithContext;
5859
import com.oracle.graal.python.nodes.call.special.CallBinaryMethodNode;
5960
import com.oracle.graal.python.nodes.call.special.CallUnaryMethodNode;
6061
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6162
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
6263
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
6364
import com.oracle.graal.python.nodes.object.GetClassNode;
64-
import com.oracle.graal.python.runtime.PythonCore;
6565
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6666
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
6767
import com.oracle.truffle.api.dsl.NodeFactory;
@@ -94,7 +94,7 @@ abstract static class GetSetGetNode extends PythonTernaryBuiltinNode {
9494
// https://github.com/python/cpython/blob/e8b19656396381407ad91473af5da8b0d4346e88/Objects/descrobject.c#L149
9595
@Specialization
9696
Object get(GetSetDescriptor descr, Object obj, PythonClass type) {
97-
if (descr_check(getCore(), descr, obj, type)) {
97+
if (descr_check(this, descr, obj, type)) {
9898
return descr;
9999
}
100100
if (descr.getGet() != null) {
@@ -116,7 +116,7 @@ abstract static class GetSetSetNode extends PythonTernaryBuiltinNode {
116116
@Specialization
117117
Object set(GetSetDescriptor descr, Object obj, Object value) {
118118
// the noneType is not important here - there are no setters on None
119-
if (descr_check(getCore(), descr, obj, getClassNode.execute(obj))) {
119+
if (descr_check(this, descr, obj, getClassNode.execute(obj))) {
120120
return descr;
121121
}
122122
if (descr.getSet() != null) {
@@ -129,7 +129,7 @@ Object set(GetSetDescriptor descr, Object obj, Object value) {
129129
}
130130

131131
// https://github.com/python/cpython/blob/e8b19656396381407ad91473af5da8b0d4346e88/Objects/descrobject.c#L70
132-
private static boolean descr_check(PythonCore core, GetSetDescriptor descr, Object obj, PythonClass type) {
132+
private static boolean descr_check(PNodeWithContext node, GetSetDescriptor descr, Object obj, PythonClass type) {
133133
if (PGuards.isNone(obj)) {
134134
if (!(type instanceof PythonBuiltinClass) || ((PythonBuiltinClass) type).getType() != PythonBuiltinClassType.PNone) {
135135
return true;
@@ -141,6 +141,6 @@ private static boolean descr_check(PythonCore core, GetSetDescriptor descr, Obje
141141
}
142142
}
143143

144-
throw core.raise(TypeError, "descriptor '%s' for '%s' objects doesn't apply to '%s' object", descr.getName(), descr.getType().getName(), type.getName());
144+
throw node.raise(TypeError, "descriptor '%s' for '%s' objects doesn't apply to '%s' object", descr.getName(), descr.getType().getName(), type.getName());
145145
}
146146
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ private void shiftError(long shiftCount) {
980980
if (shiftCount >= Integer.SIZE) {
981981
throw new ArithmeticException("integer overflow");
982982
} else if (shiftCount < 0) {
983-
throw getCore().raise(ValueError, "negative shift count");
983+
throw raise(ValueError, "negative shift count");
984984
}
985985
}
986986

0 commit comments

Comments
 (0)